Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.d_logicnfc;
- // TODO CHECK HOW UPDATE APP WORKS
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.ContextWrapper;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Environment;
- import android.os.Handler;
- import android.support.v7.app.AlertDialog;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import d_logic.ticketsale.service.Action;
- import d_logic.ticketsale.service.Property;
- import d_logic.ticketsale.service.SynchroService;
- import d_logic.translator.Translator;
- /*
- @Author: Aleksandar Arsic
- @Description: This class download all settings from server and allow update for new software version
- @Date created: 27/07/2017 (dd/mm/yyyy)
- */
- public class DSetupAsyncTask extends AsyncTask<byte[], Void, String[]> {
- private Context context;
- private final String URL_SETTINGS = "http://settings.ticketing.rs/d_setup.php";
- private static final boolean enable_litas = false;
- private static final boolean enable_subotica = true;
- private static final boolean enable_kolasin = false;
- private static final boolean enable_feniks = false;
- private static final boolean enable_netbus = false;
- private static final boolean enable_euroline = false;
- private static final boolean enable_sid = false;
- private static final boolean enable_novisad = false;
- private static final boolean enable_nis = false;
- private static final boolean enable_cacak = false;
- private static final boolean enable_vulovic = false;
- private static final boolean enable_vranje = false;
- private static final boolean enable_banattrans = false;
- private static final boolean enable_icommuter = false;
- private Translator t;
- public static String http_server = "";
- public static String http_get_version = "";
- public static String http_new_version = "";
- public static String http_station_uids = "";
- public static String http_line_uids = "";
- public static String http_template_zone_cityuid = "";
- public static String http_user_data = "";
- public static String http_check_blacklist = "";
- private static boolean monthly_cards_check_option = true;
- private static boolean qr_barcode_check_option = false;
- private static boolean cancelled_tickets_check_option = false;
- private IbfmFactory IbfmFactory;
- private Activity activity;
- public DSetupAsyncTask(Context context, Translator t) {
- this.context = context;
- this.t = t;
- }
- @Override
- protected String[] doInBackground(byte[]... params) {
- String msg = "";
- String res = "";
- String success = null;
- IbfmFactory = new IbfmFactory(context);
- try {
- URL a = new URL(URL_SETTINGS);
- HttpURLConnection conn = (HttpURLConnection) a.openConnection();
- conn.setDoInput(true);
- conn.setDoOutput(true);
- conn.setUseCaches(false);
- conn.setRequestProperty("Content-Type", "application/octet-stream");
- conn.setRequestMethod("POST");
- conn.setConnectTimeout(1000 * 10);
- OutputStream toServer = conn.getOutputStream();
- conn.connect();
- toServer.write(params[0]);
- toServer.flush();
- BufferedReader fromServer = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = fromServer.readLine()) != null) {
- res += line;
- }
- int responseCode = conn.getResponseCode();
- if (responseCode != HttpURLConnection.HTTP_OK) {
- throw new Exception(context.getString(R.string.connection_to_the_server_was_not_successful));
- }
- if (res.equals("")) {
- throw new Exception(context.getString(R.string.server_did_not_recognize_the_device));
- }
- if (res.codePointAt(0) != 65533) {
- throw new Exception(context.getString(R.string.server_did_not_recognize_the_device));
- }
- toServer.close();
- fromServer.close();
- conn.disconnect();
- msg = context.getString(R.string.connection_to_the_server_is_successful_the_settings_taken);
- success = "";
- } catch (MalformedURLException e) {
- msg = e.getLocalizedMessage();
- } catch (IOException e) {
- msg = e.getLocalizedMessage();
- } catch (Exception e) {
- msg = e.getLocalizedMessage();
- }
- String xres="";
- if (res.equals("")){
- xres = "";
- } else {
- xres=res.substring(1, res.length() - 1);
- }
- String[] data = { msg, xres, success };
- return data;
- }
- @Override
- protected void onPostExecute(String[] result) {
- Toast.makeText(context, t._(result[0]), Toast.LENGTH_LONG).show();
- if (result[2] != null) {
- Log.v("Osnovna podešavanja", result[0]);
- Log.v("Osnovna podešavanja", result[1]);
- Log.v("Osnovna podešavanja", result[2]);
- String imei=IbfmFactory.getImei();
- String ibfm=IbfmFactory.getIbfmBC();
- http_server=result[1];
- if ((enable_kolasin==true) || (enable_novisad==true) || (enable_nis==true) || (enable_netbus==true) || (enable_cacak==true) || (enable_subotica==true)){
- // SynchroService ss=new SynchroService();
- // ss.start();
- Intent in = new Intent(context, SynchroService.class);
- in.putExtra(Property.ACTION, Action.SEND_UNSENDED_QRS);
- in.putExtra(Property.TIME, Property.SEND_UNSENDED_QRS_TIME);
- ComponentName res = context.startService(in);
- if (res == null) {
- Log.w("Milos log", "service error");
- } else {
- Log.w("Milos log", "service started");
- }
- }
- http_get_version = result[1]+"cadmin/android.php?imei="+imei+"&ibfm="+ibfm+"&action=get_settings&version="+BaseActivity.program_version;
- http_new_version = result[1]+"cadmin/android.php?imei="+imei+"&ibfm="+ibfm+"&action=get_software";
- BaseActivity.http_new_version = result[1]+"cadmin/android.php?imei="+imei+"&ibfm="+ibfm+"&action=get_software";
- // http_get_version = "http://192.168.1.62/cadmin/cadmin/android.php?imei="+imei+"&ibfm="+ibfm+"&action=get_settings";
- // http_new_version = "http://192.168.1.62/cadmin/cadmin/android.php?imei="+imei+"&ibfm="+ibfm+"&action=get_software";
- http_station_uids = result[1]+"cadmin/test_station_uids.php?test=text_koncentrator";
- http_line_uids = result[1]+"cadmin/test_line_uids.php?test=original_home_bus_operator_txt";
- http_template_zone_cityuid = result[1]+"cadmin/data/get_tzc.php";
- http_user_data = result[1]+"cadmin/data/get_usd.php";
- http_check_blacklist = result[1]+"cadmin/data/check_bl.php";
- new Thread(new Runnable() {
- public void run(){
- InputStream is=null;
- try{
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost(http_get_version);
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- is = entity.getContent();
- }catch(Exception e){
- activity.runOnUiThread(new Runnable() {
- public void run() {
- CharSequence text = t._("Nema konekcije sa serverom, proverite internet konekciju!!!");
- int duration = Toast.LENGTH_SHORT;
- Toast toast = Toast.makeText(context, text, duration);
- toast.show();
- }
- });
- return;
- }
- String result="";
- try{
- BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null) {
- sb.append(line);
- Log.w("lines",line);
- }
- is.close();
- result=sb.toString(); // JSON
- // Log.w("rawString",String.valueOf(sb));
- JSONObject jsonResponse;
- jsonResponse = new JSONObject(result);
- BaseActivity.black_list_version = jsonResponse.optString("current_black_list").toString();
- BaseActivity.settings_version = jsonResponse.optString("firmware_version").toString();
- result = jsonResponse.optString("android_flag").toString();
- // Log.w("rawString", result);
- int test=Integer.parseInt(jsonResponse.optString("MONTHLY_CARDS_CHECK").toString());
- if (test==0){
- monthly_cards_check_option=false;
- } else {
- monthly_cards_check_option=true;
- }
- result = jsonResponse.optString("android_flag").toString();
- test=Integer.parseInt(jsonResponse.optString("QR_BARCODE_CHECK").toString());
- if (test==0){
- qr_barcode_check_option=false;
- } else {
- qr_barcode_check_option=true;
- }
- result = jsonResponse.optString("android_flag").toString();
- test=Integer.parseInt(jsonResponse.optString("CANCELLED_TICKETS_CHECK").toString());
- if (test==0){
- cancelled_tickets_check_option=false;
- } else {
- cancelled_tickets_check_option=true;
- }
- // save settings to file
- String xres=monthly_cards_check_option+"|"+qr_barcode_check_option+"|"+cancelled_tickets_check_option;
- String FILENAME = "settings_server";
- FileOutputStream fosx = context.openFileOutput(FILENAME,Context.MODE_PRIVATE);
- fosx.write(xres.getBytes());
- fosx.close();
- }catch(Exception e){
- Log.e("log_tag", "Error converting result "+e.toString());
- }
- if (result.equals("1")){
- BaseActivity.updateAvailable = true;
- if (BaseActivity.isInUpdateActivity == true) {
- Update(http_new_version);
- }
- }
- }
- public void Update(String apkurl){
- try {
- URL url = new URL(apkurl);
- HttpURLConnection c = (HttpURLConnection) url.openConnection();
- c.setRequestMethod("GET");
- c.setDoOutput(true);
- c.connect();
- File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
- File file = new File(path, "D-LogicNFC.apk");
- path.mkdirs();
- InputStream is = c.getInputStream();
- OutputStream os = new FileOutputStream(file);
- byte[] buffer = new byte[1024];
- int len1 = 0;
- while ((len1 = is.read(buffer)) != -1) {
- os.write(buffer, 0, len1);
- }
- os.close();
- is.close();
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/D-LogicNFC.apk")), "application/vnd.android.package-archive");
- context.startActivity(intent);
- } catch (IOException e) {
- activity.runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(context, t._("Greška pri updateu!"), Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- }).start();
- //Wait 6 seconds before it starts LoginActivity
- Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- public void run() {
- if(BaseActivity.updateAvailable == false && BaseActivity.isInUpdateActivity == false) {
- Intent intent = new Intent(context, LoginActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- context.startActivity(intent);
- } else {
- Intent updateApp = new Intent(context, UpdateActivity.class);
- context.startActivity(updateApp);
- }
- }
- }, 6000);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement