package com.likesoft.mdedal; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; import android.preference.PreferenceManager; import android.widget.LinearLayout; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * Created by darek on 2016-01-25. */ abstract class AsyncCallChangeDate extends AsyncTask { private Activity calendarActivity; ProgressDialog progressDialog; String response; String id_zlecenia; String kod_pola; String wartosc; LinearLayout movingEl; public AsyncCallChangeDate(Activity calendarActivity, String id_zlecenia, String kod_pola, String wartosc) { this.calendarActivity = calendarActivity; //this.progressDialog = new ProgressDialog(); this.id_zlecenia = id_zlecenia; this.kod_pola = kod_pola; this.wartosc = wartosc; //this.progressDialog = new ProgressDialog(); } @Override protected Void doInBackground(String... params) { android.util.Log.d("my_debug", "doInBackground"); DedalHttpConnection con = new DedalHttpConnection(calendarActivity.getApplicationContext()); Map params_post = new HashMap(); params_post.put("id_zlecenia", id_zlecenia); params_post.put("kod_pola", kod_pola); params_post.put("wartosc", wartosc); response = con.sendPost("public/ajax/zmien_wartosc_zlecenie_pole_stale.php", params_post); return null; } @Override protected void onPostExecute(Void result) { android.util.Log.d("my_debug", "onPostExecute"); //this.progressDialog.dismiss(); Log.d("CalAct", "resp: "+response); if( response != null && "true".equals(response) ) { JSONObject jZlecenie = null; try { // read file Context context = calendarActivity.getApplicationContext(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); File dLista = new File(prefs.getString("dir", "") + "/baza/lista.profil"); String sJson = DedalHelper.getFileContent(dLista.getAbsolutePath()); JSONObject jFile = new JSONObject(sJson); JSONObject jProfiles = jFile.getJSONObject("profile"); Iterator keys = jProfiles.keys(); while (keys.hasNext()) { String id_profilu_curr = (String) keys.next(); try { JSONObject jProfil = jProfiles.getJSONObject(id_profilu_curr); // if( !jProfil.has("zlecenia") || !"[]".equals(jProfil.getString("zlecenia")) ) { // continue; // } JSONObject jZlecenia_local = jProfil.getJSONObject("zlecenia"); if (!jZlecenia_local.has(id_zlecenia)) continue; jZlecenie = jZlecenia_local.getJSONObject(id_zlecenia); //change value if (jZlecenie.has(kod_pola)) { jZlecenie.put(kod_pola, wartosc); Log.d("CalAct", "kod_pola: " + kod_pola + "wartosc: " + wartosc); } } catch (JSONException e) { Log.e("DEDAL", "onPostExecute", e); } } // save file DedalHelper.putFileContent(prefs.getString("dir", "") + "/baza/lista.profil", jFile.toString()); // Toast.makeText(CalendarActivity.this, "Uaktualniono poprawnie", // Toast.LENGTH_LONG).show(); loadInputs(); // movingEl.removeAllViews(); // movingEl.addView(getZlecenieElement(jZlecenie, jKolumny)); } catch (JSONException e) { Log.e("DEDAL", "JSON", e); } } else { reloadContent(); Toast.makeText(calendarActivity, "Wystąpił błąd podczas uaktulaniania, zmiany zostały cofnięte", Toast.LENGTH_LONG).show(); Log.d("CalAct", "Błąd przy połaćzeniu do zmien_wartosc_zlecenie_pole_stale.php"); } } protected abstract void reloadContent(); abstract public void loadInputs(); @Override protected void onPreExecute() { android.util.Log.d("my_debug", "onPreExecute"); // Toast.makeText(CalendarActivity.this, "Rozpoczynam uaktualnianie ...", // Toast.LENGTH_LONG).show(); } @Override protected void onProgressUpdate(Void... values) { android.util.Log.i("my_debug", "onProgressUpdate"); } }