Advertisement
ProgrameruPokusaju

asynctask

Aug 10th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.46 KB | None | 0 0
  1. package com.example.d_logicnfc;
  2.  // TODO CHECK HOW UPDATE APP WORKS
  3. import android.app.Activity;
  4. import android.content.ComponentName;
  5. import android.content.Context;
  6. import android.content.ContextWrapper;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.net.Uri;
  10. import android.os.AsyncTask;
  11. import android.os.Environment;
  12. import android.os.Handler;
  13. import android.support.v7.app.AlertDialog;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.Toast;
  18.  
  19. import org.apache.http.HttpEntity;
  20. import org.apache.http.HttpResponse;
  21. import org.apache.http.client.HttpClient;
  22. import org.apache.http.client.methods.HttpPost;
  23. import org.apache.http.impl.client.DefaultHttpClient;
  24. import org.json.JSONObject;
  25.  
  26. import java.io.BufferedReader;
  27. import java.io.File;
  28. import java.io.FileOutputStream;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.io.InputStreamReader;
  32. import java.io.OutputStream;
  33. import java.net.HttpURLConnection;
  34. import java.net.MalformedURLException;
  35. import java.net.URL;
  36.  
  37. import d_logic.ticketsale.service.Action;
  38. import d_logic.ticketsale.service.Property;
  39. import d_logic.ticketsale.service.SynchroService;
  40. import d_logic.translator.Translator;
  41.  
  42. /*
  43. @Author: Aleksandar Arsic
  44. @Description: This class download all settings from server and allow update for new software version
  45. @Date created: 27/07/2017 (dd/mm/yyyy)
  46.  */
  47.  
  48. public class DSetupAsyncTask  extends AsyncTask<byte[], Void, String[]>  {
  49.  
  50.     private Context context;
  51.     private final String URL_SETTINGS = "http://settings.ticketing.rs/d_setup.php";
  52.  
  53.     private static final boolean enable_litas = false;
  54.     private static final boolean enable_subotica = true;
  55.     private static final boolean enable_kolasin = false;
  56.     private static final boolean enable_feniks = false;
  57.     private static final boolean enable_netbus = false;
  58.     private static final boolean enable_euroline = false;
  59.     private static final boolean enable_sid = false;
  60.     private static final boolean enable_novisad = false;
  61.     private static final boolean enable_nis = false;
  62.     private static final boolean enable_cacak = false;
  63.     private static final boolean enable_vulovic = false;
  64.     private static final boolean enable_vranje = false;
  65.     private static final boolean enable_banattrans = false;
  66.     private static final boolean enable_icommuter = false;
  67.  
  68.     private Translator t;
  69.     public static String http_server = "";
  70.     public static String http_get_version = "";
  71.     public static String http_new_version = "";
  72.  
  73.     public static String http_station_uids = "";
  74.     public static String http_line_uids = "";
  75.     public static String http_template_zone_cityuid = "";
  76.     public static String http_user_data = "";
  77.     public static String http_check_blacklist = "";
  78.  
  79.     private static boolean monthly_cards_check_option = true;
  80.     private static boolean qr_barcode_check_option = false;
  81.     private static boolean cancelled_tickets_check_option = false;
  82.     private IbfmFactory IbfmFactory;
  83.     private Activity activity;
  84.  
  85.     public DSetupAsyncTask(Context context, Translator t) {
  86.         this.context = context;
  87.         this.t = t;
  88.     }
  89.     @Override
  90.     protected String[] doInBackground(byte[]... params) {
  91.  
  92.         String msg = "";
  93.         String res = "";
  94.         String success = null;
  95.         IbfmFactory = new IbfmFactory(context);
  96.         try {
  97.             URL a = new URL(URL_SETTINGS);
  98.             HttpURLConnection conn = (HttpURLConnection) a.openConnection();
  99.             conn.setDoInput(true);
  100.             conn.setDoOutput(true);
  101.             conn.setUseCaches(false);
  102.             conn.setRequestProperty("Content-Type", "application/octet-stream");
  103.             conn.setRequestMethod("POST");
  104.             conn.setConnectTimeout(1000 * 10);
  105.  
  106.             OutputStream toServer = conn.getOutputStream();
  107.             conn.connect();
  108.             toServer.write(params[0]);
  109.             toServer.flush();
  110.  
  111.             BufferedReader fromServer = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  112.             String line;
  113.             while ((line = fromServer.readLine()) != null) {
  114.                 res += line;
  115.             }
  116.             int responseCode = conn.getResponseCode();
  117.             if (responseCode != HttpURLConnection.HTTP_OK) {
  118.                 throw new Exception(context.getString(R.string.connection_to_the_server_was_not_successful));
  119.             }
  120.  
  121.             if (res.equals("")) {
  122.                 throw new Exception(context.getString(R.string.server_did_not_recognize_the_device));
  123.             }
  124.  
  125.             if (res.codePointAt(0) != 65533) {
  126.                 throw new Exception(context.getString(R.string.server_did_not_recognize_the_device));
  127.             }
  128.  
  129.             toServer.close();
  130.             fromServer.close();
  131.             conn.disconnect();
  132.  
  133.             msg = context.getString(R.string.connection_to_the_server_is_successful_the_settings_taken);
  134.             success = "";
  135.         } catch (MalformedURLException e) {
  136.             msg = e.getLocalizedMessage();
  137.         } catch (IOException e) {
  138.             msg = e.getLocalizedMessage();
  139.         } catch (Exception e) {
  140.             msg = e.getLocalizedMessage();
  141.         }
  142.         String xres="";
  143.         if (res.equals("")){
  144.             xres = "";
  145.         } else {
  146.             xres=res.substring(1, res.length() - 1);
  147.         }
  148.         String[] data = { msg, xres, success };
  149.  
  150.         return data;
  151.  
  152.     }
  153.  
  154.     @Override
  155.     protected void onPostExecute(String[] result) {
  156.         Toast.makeText(context, t._(result[0]), Toast.LENGTH_LONG).show();
  157.         if (result[2] != null) {
  158.             Log.v("Osnovna podešavanja", result[0]);
  159.             Log.v("Osnovna podešavanja", result[1]);
  160.             Log.v("Osnovna podešavanja", result[2]);
  161.  
  162.             String imei=IbfmFactory.getImei();
  163.             String ibfm=IbfmFactory.getIbfmBC();
  164.  
  165.             http_server=result[1];
  166.  
  167.             if ((enable_kolasin==true) || (enable_novisad==true) || (enable_nis==true) || (enable_netbus==true) || (enable_cacak==true) || (enable_subotica==true)){
  168.                 //  SynchroService ss=new SynchroService();
  169.                 //  ss.start();
  170.                 Intent in = new Intent(context, SynchroService.class);
  171.                 in.putExtra(Property.ACTION, Action.SEND_UNSENDED_QRS);
  172.                 in.putExtra(Property.TIME, Property.SEND_UNSENDED_QRS_TIME);
  173.                 ComponentName res = context.startService(in);
  174.                 if (res == null) {
  175.                     Log.w("Milos log", "service error");
  176.                 } else {
  177.                     Log.w("Milos log", "service started");
  178.                 }
  179.             }
  180.  
  181.             http_get_version = result[1]+"cadmin/android.php?imei="+imei+"&ibfm="+ibfm+"&action=get_settings&version="+BaseActivity.program_version;
  182.             http_new_version = result[1]+"cadmin/android.php?imei="+imei+"&ibfm="+ibfm+"&action=get_software";
  183.  
  184.             BaseActivity.http_new_version = result[1]+"cadmin/android.php?imei="+imei+"&ibfm="+ibfm+"&action=get_software";
  185.  
  186.  
  187.             //    http_get_version = "http://192.168.1.62/cadmin/cadmin/android.php?imei="+imei+"&ibfm="+ibfm+"&action=get_settings";
  188.             //    http_new_version = "http://192.168.1.62/cadmin/cadmin/android.php?imei="+imei+"&ibfm="+ibfm+"&action=get_software";
  189.  
  190.             http_station_uids = result[1]+"cadmin/test_station_uids.php?test=text_koncentrator";
  191.             http_line_uids = result[1]+"cadmin/test_line_uids.php?test=original_home_bus_operator_txt";
  192.             http_template_zone_cityuid = result[1]+"cadmin/data/get_tzc.php";
  193.             http_user_data = result[1]+"cadmin/data/get_usd.php";
  194.             http_check_blacklist = result[1]+"cadmin/data/check_bl.php";
  195.            
  196.             new Thread(new Runnable() {
  197.                 public void run(){
  198.                     InputStream is=null;
  199.                     try{
  200.                         HttpClient httpclient = new DefaultHttpClient();
  201.                         HttpPost httppost = new HttpPost(http_get_version);
  202.                         HttpResponse response = httpclient.execute(httppost);
  203.                         HttpEntity entity = response.getEntity();
  204.                         is = entity.getContent();
  205.                     }catch(Exception e){
  206.                         activity.runOnUiThread(new Runnable() {
  207.                             public void run() {
  208.                                 CharSequence text = t._("Nema konekcije sa serverom, proverite internet konekciju!!!");
  209.                                 int duration = Toast.LENGTH_SHORT;
  210.  
  211.                                 Toast toast = Toast.makeText(context, text, duration);
  212.                                 toast.show();
  213.                             }
  214.                         });
  215.                         return;
  216.                     }
  217.                     String result="";
  218.                     try{
  219.                         BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
  220.                         StringBuilder sb = new StringBuilder();
  221.                         String line = null;
  222.                         while ((line = reader.readLine()) != null) {
  223.                             sb.append(line);
  224.                             Log.w("lines",line);
  225.                         }
  226.                         is.close();
  227.  
  228.                         result=sb.toString(); // JSON
  229. //                        Log.w("rawString",String.valueOf(sb));
  230.                         JSONObject jsonResponse;
  231.                         jsonResponse = new JSONObject(result);
  232.  
  233.                         BaseActivity.black_list_version = jsonResponse.optString("current_black_list").toString();
  234.                         BaseActivity.settings_version = jsonResponse.optString("firmware_version").toString();
  235.                         result = jsonResponse.optString("android_flag").toString();
  236. //                        Log.w("rawString", result);
  237.                         int test=Integer.parseInt(jsonResponse.optString("MONTHLY_CARDS_CHECK").toString());
  238.  
  239.                         if (test==0){
  240.                             monthly_cards_check_option=false;
  241.                         } else {
  242.                             monthly_cards_check_option=true;
  243.                         }
  244.  
  245.                         result = jsonResponse.optString("android_flag").toString();
  246.                         test=Integer.parseInt(jsonResponse.optString("QR_BARCODE_CHECK").toString());
  247.                         if (test==0){
  248.                             qr_barcode_check_option=false;
  249.                         } else {
  250.                             qr_barcode_check_option=true;
  251.                         }
  252.  
  253.                         result = jsonResponse.optString("android_flag").toString();
  254.  
  255.                         test=Integer.parseInt(jsonResponse.optString("CANCELLED_TICKETS_CHECK").toString());
  256.                         if (test==0){
  257.                             cancelled_tickets_check_option=false;
  258.                         } else {
  259.                             cancelled_tickets_check_option=true;
  260.                         }
  261.  
  262.  
  263.                         // save settings to file
  264.                         String xres=monthly_cards_check_option+"|"+qr_barcode_check_option+"|"+cancelled_tickets_check_option;
  265.                         String FILENAME = "settings_server";
  266.                         FileOutputStream fosx = context.openFileOutput(FILENAME,Context.MODE_PRIVATE);
  267.                         fosx.write(xres.getBytes());
  268.                         fosx.close();
  269.  
  270.                     }catch(Exception e){
  271.                         Log.e("log_tag", "Error converting result "+e.toString());
  272.                     }
  273.  
  274.                     if (result.equals("1")){
  275.                       BaseActivity.updateAvailable = true;
  276.                       if (BaseActivity.isInUpdateActivity == true) {
  277.                           Update(http_new_version);
  278.                       }
  279.                     }
  280.                 }
  281.  
  282.                 public void Update(String apkurl){
  283.                     try {
  284.                         URL url = new URL(apkurl);
  285.                         HttpURLConnection c = (HttpURLConnection) url.openConnection();
  286.                         c.setRequestMethod("GET");
  287.                         c.setDoOutput(true);
  288.                         c.connect();
  289.                         File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
  290.                         File file = new File(path, "D-LogicNFC.apk");
  291.                         path.mkdirs();
  292.                         InputStream is = c.getInputStream();
  293.                         OutputStream os = new FileOutputStream(file);
  294.                         byte[] buffer = new byte[1024];
  295.                         int len1 = 0;
  296.                         while ((len1 = is.read(buffer)) != -1) {
  297.                             os.write(buffer, 0, len1);
  298.                         }
  299.                         os.close();
  300.                         is.close();
  301.                         Intent intent = new Intent(Intent.ACTION_VIEW);
  302.                         intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/D-LogicNFC.apk")), "application/vnd.android.package-archive");
  303.                         context.startActivity(intent);
  304.                     } catch (IOException e) {
  305.  
  306.                         activity.runOnUiThread(new Runnable() {
  307.                             public void run() {
  308.                                 Toast.makeText(context, t._("Greška pri updateu!"), Toast.LENGTH_LONG).show();
  309.                             }
  310.                         });
  311.                     }
  312.                 }
  313.  
  314.             }).start();
  315.  
  316.             //Wait 6 seconds before it starts LoginActivity
  317.             Handler handler = new Handler();
  318.             handler.postDelayed(new Runnable() {
  319.                 public void run() {
  320.                     if(BaseActivity.updateAvailable == false && BaseActivity.isInUpdateActivity == false) {
  321.                         Intent intent = new Intent(context, LoginActivity.class);
  322.                         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  323.                         context.startActivity(intent);
  324.                     } else {
  325.                       Intent updateApp = new Intent(context, UpdateActivity.class);
  326.                         context.startActivity(updateApp);
  327.                     }
  328.                 }
  329.             }, 6000);
  330.  
  331. }
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement