|
|
@@ -2,23 +2,36 @@ package com.usai.apex;
|
|
|
|
|
|
import android.app.Activity;
|
|
|
import android.app.AlarmManager;
|
|
|
+import android.app.AlertDialog;
|
|
|
import android.app.Application;
|
|
|
+import android.app.DownloadManager;
|
|
|
import android.app.PendingIntent;
|
|
|
+import android.app.ProgressDialog;
|
|
|
import android.content.Context;
|
|
|
+import android.content.DialogInterface;
|
|
|
import android.content.Intent;
|
|
|
import android.content.SharedPreferences;
|
|
|
import android.content.res.Configuration;
|
|
|
import android.content.res.Resources;
|
|
|
+import android.net.Uri;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
import android.os.SystemClock;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
|
|
|
import com.usai.util.AES;
|
|
|
+import com.usai.util.Network;
|
|
|
import com.usai.util.RAUtil;
|
|
|
import com.usai.util.dbUtil;
|
|
|
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
import java.io.File;
|
|
|
+import java.lang.ref.WeakReference;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
public class ApexTrackingApplication extends Application
|
|
|
{
|
|
|
@@ -70,6 +83,7 @@ public class ApexTrackingApplication extends Application
|
|
|
registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
|
|
|
@Override
|
|
|
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
|
|
|
+ mCurrentActivity = activity;
|
|
|
Resources res = ApexTrackingApplication.getInstance().getResources();
|
|
|
Configuration config = new Configuration();
|
|
|
config.setToDefaults();
|
|
|
@@ -84,6 +98,8 @@ public class ApexTrackingApplication extends Application
|
|
|
// Configuration config = new Configuration();
|
|
|
// config.setToDefaults();
|
|
|
// res.updateConfiguration(config, res.getDisplayMetrics());
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
// ...
|
|
|
@@ -161,6 +177,134 @@ public class ApexTrackingApplication extends Application
|
|
|
createAppStorageDir();
|
|
|
}
|
|
|
|
|
|
+ private static final int HandlerMsgTypeCheckUpdate = 0;
|
|
|
+ private Handler handler = new CheckUpdateHandler(this);
|
|
|
+ private Activity mCurrentActivity;
|
|
|
+
|
|
|
+ private static final class CheckUpdateHandler extends Handler {
|
|
|
+
|
|
|
+ WeakReference<ApexTrackingApplication> mWeakApp;
|
|
|
+
|
|
|
+ public CheckUpdateHandler(ApexTrackingApplication application) {
|
|
|
+ mWeakApp = new WeakReference<> (application);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleMessage(Message msg) {
|
|
|
+ super.handleMessage(msg);
|
|
|
+ switch (msg.what) {
|
|
|
+ case HandlerMsgTypeCheckUpdate: {
|
|
|
+
|
|
|
+ final ApexTrackingApplication app = mWeakApp.get();
|
|
|
+ if (app == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ app.dismissProgressDialog();
|
|
|
+
|
|
|
+ if (msg.obj == null) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = new JSONObject((String) msg.obj);
|
|
|
+
|
|
|
+ final String ver = jsonObject.optString("ver");
|
|
|
+ final String url = jsonObject.optString("url");
|
|
|
+
|
|
|
+ if (ver != null && url != null) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ new AlertDialog.Builder(app.mCurrentActivity)
|
|
|
+ .setMessage("There is a new version " + ver + " ,are you ready to update it?")
|
|
|
+ .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
+ app.downloadNewVersionApp(url,ver);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .setNegativeButton("No",null)
|
|
|
+ .show();
|
|
|
+
|
|
|
+ } else {
|
|
|
+ new AlertDialog.Builder(app.mCurrentActivity)
|
|
|
+ .setMessage("Your App is the latest version")
|
|
|
+ .setNegativeButton("Ok",null)
|
|
|
+ .show();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void downloadNewVersionApp(String url,String ver) {
|
|
|
+
|
|
|
+ String appName = RAUtil.getApplicationName(this);
|
|
|
+
|
|
|
+ if (url == null || appName == null || ver == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
|
|
|
+
|
|
|
+ request.setDestinationInExternalPublicDir("/download/", appName + " " + ver + ".apk");
|
|
|
+
|
|
|
+ DownloadManager downloadManager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
+
|
|
|
+ downloadManager.enqueue(request);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkUpdate(boolean showProgress,String alertMsg) {
|
|
|
+ if (showProgress) {
|
|
|
+ showProgressDialog(alertMsg);
|
|
|
+ }
|
|
|
+ new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+
|
|
|
+ String jsonStr = Network.checkUpdate(instance);
|
|
|
+ Message msg = new Message();
|
|
|
+ msg.what = HandlerMsgTypeCheckUpdate;
|
|
|
+ msg.obj = jsonStr;
|
|
|
+ handler.sendMessage(msg);
|
|
|
+
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private ProgressDialog mProgressDialog;
|
|
|
+
|
|
|
+ public void showProgressDialog(String msg) {
|
|
|
+
|
|
|
+ if (mProgressDialog == null) {
|
|
|
+ if (this == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ mProgressDialog = new ProgressDialog(mCurrentActivity);
|
|
|
+ mProgressDialog.setCancelable(false);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ mProgressDialog.setMessage(msg);
|
|
|
+ mProgressDialog.show();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void dismissProgressDialog() {
|
|
|
+ if (mProgressDialog != null && mProgressDialog.isShowing()) {
|
|
|
+ mProgressDialog.dismiss();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void createAppStorageDir() {
|
|
|
String documentDir = getDocumentDir();
|
|
|
File docDir = new File(documentDir);
|