Prechádzať zdrojové kódy

1.修改Android Apex Mobile上传用户缓存数据。

Pen Li 7 rokov pred
rodič
commit
403b46f4a1

+ 12 - 5
Apex Mobile/app/src/main/java/com/usai/apex/ApexTrackingApplication.java

@@ -46,6 +46,8 @@ public class ApexTrackingApplication extends Application
 	public static boolean					m_bauthorized	= false;
 	private boolean 							personMode 		= false;
 
+	public boolean shouldCheckCache = false;
+
 	public boolean homeactive=false;
 	public boolean historyactive=false;
 	public boolean recentactive=false;
@@ -213,24 +215,29 @@ public class ApexTrackingApplication extends Application
                             final String ver = jsonObject.optString("ver");
                             final String url = jsonObject.optString("url");
                             int result = jsonObject.optInt("result",0);
-
+                            String log = jsonObject.optString("change_log");
+                            if (log == null) {
+                            	log = "";
+							}
                             if (result == 2 && 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() {
+										.setTitle("New version " + ver +" is available")
+                                        .setMessage("Change log:\n\n" + log + "\n\nDo you want to update to the latest version?\n")
+                                        .setPositiveButton("Download", new DialogInterface.OnClickListener() {
                                             @Override
                                             public void onClick(DialogInterface dialog, int which) {
                                                 app.downloadNewVersionApp(url,ver);
                                             }
                                         })
-                                        .setNegativeButton("No",null)
+                                        .setNegativeButton("Later",null)
                                         .show();
 
                             } else {
                                 if (!app.mSilence) {
 									new AlertDialog.Builder(app.mCurrentActivity)
-											.setMessage("Your App is the latest version")
+											.setTitle("Check update")
+											.setMessage("you are up to date.")
 											.setNegativeButton("Ok",null)
 											.show();
 								}

+ 2 - 0
Apex Mobile/app/src/main/java/com/usai/apex/mainframe/LoginFragment.java

@@ -387,6 +387,8 @@ public class LoginFragment extends Fragment/* implements OnClickListener */
 				}
 				editor.commit();
 
+				ApexTrackingApplication.getInstance().shouldCheckCache = true;
+
 				if(mCallBack!=null)
 					mCallBack.onLogin();
 

+ 95 - 0
Apex Mobile/app/src/main/java/com/usai/apex/mainframe/RecentFragment.java

@@ -2,6 +2,8 @@ package com.usai.apex.mainframe;
 
 
 import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.os.AsyncTask;
 import android.os.Bundle;
@@ -16,6 +18,7 @@ import com.usai.apex.ApexTrackingApplication;
 import com.usai.apex.R;
 import com.usai.apex.ShipMap.ShipMap;
 import com.usai.util.Network;
+import com.usai.util.RAUtil;
 import com.usai.util.dbUtil;
 
 import org.json.JSONArray;
@@ -154,6 +157,96 @@ public class RecentFragment extends TrackingListFragment {
 //        requestdata();
 //    }
 
+    private void checkDatabase() {
+        if (ApexTrackingApplication.getInstance().shouldCheckCache) {
+            ApexTrackingApplication.getInstance().shouldCheckCache = false;
+
+            if (dbUtil.isCachedDataForUser(mContext, ApexTrackingApplication.get_user())) {
+
+                new AlertDialog.Builder(mContext)
+                        .setTitle("Warning")
+                        .setMessage("there is some cached data need to upload to service, do you allow us to upload it? if you reject we would clean it")
+                        .setNegativeButton("Reject", new DialogInterface.OnClickListener() {
+                            @Override
+                            public void onClick(DialogInterface dialog, int which) {
+                                new Thread(new Runnable() {
+                                    @Override
+                                    public void run() {
+                                        dbUtil.cleanCachedDataForUser(mContext, ApexTrackingApplication.get_user());
+                                    }
+                                }).start();
+                            }
+                        })
+                        .setPositiveButton("Allow", new DialogInterface.OnClickListener() {
+                            @Override
+                            public void onClick(DialogInterface dialog, int which) {
+
+                                uploadCache();
+                            }
+                        })
+                        .show();
+
+            } // is cached data
+
+        }
+    }
+
+    private void uploadCache() {
+
+        isLoading = true;
+        showProgressDialog();
+
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+
+                String sql = dbUtil.prepareUploadSQLForUser(mContext, ApexTrackingApplication.get_user(), RAUtil.getDeviceID(mContext));
+                JSONObject json = Network.uploadUserSql(sql);
+                int result = Network.RESULT_FALSE;
+                if (json != null) {
+                    result = json.optInt("result", Network.RESULT_FALSE);
+                }
+                final int fr = result;
+
+                getActivity().runOnUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+
+                        isLoading = false;
+                        dismissProgressDialog();
+
+                        if (fr == Network.RESULT_TRUE) {
+
+                            showAlert("upload success");
+
+                        } else {
+
+                            new AlertDialog.Builder(mContext)
+                                    .setTitle("Warning")
+                                    .setMessage("upload local cached data failed,do you want to rery or cancel? if you choose cancel, then we will clean the cache")
+                                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
+                                        @Override
+                                        public void onClick(DialogInterface dialog, int which) {
+                                            dbUtil.cleanCachedDataForUser(mContext, ApexTrackingApplication.get_user());
+                                        }
+                                    })
+                                    .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
+                                        @Override
+                                        public void onClick(DialogInterface dialog, int which) {
+                                            uploadCache();
+                                        }
+                                    })
+                                    .show();
+
+                        }
+
+                    }
+                });
+            }
+        }).start();
+
+    }
+
     private void handleJson(JSONObject jsobj) {
 
         mJson = jsobj;
@@ -177,6 +270,8 @@ public class RecentFragment extends TrackingListFragment {
                     showRefreshButton(false);
                 setSelectedPosition(0);
 
+                checkDatabase();
+
             } else {
                 String msg = jsobj.getString("err_msg");
                 showAlert(msg);

+ 19 - 0
Apex Mobile/app/src/main/java/com/usai/util/Network.java

@@ -124,6 +124,10 @@ public class Network
 	public static String		URL_SEND_COMM_EMAIL				= "https://ra.apexshipping.com/main_new.php";
 	public static String		URL_CHECK_UPDATE				= "https://ra.apexshipping.com/main_new.php";
 
+	// 2019.1.5
+	public static String		URL_UPLOAD_SQL					= "https://ra.apexshipping.com/main_new.php";
+
+
 
 //
 ////debug
@@ -1901,4 +1905,19 @@ public class Network
 		return jsonStr;
 	}
 
+	public static JSONObject uploadUserSql(String sql) {
+
+		Bundle params = new Bundle();
+
+		if (sql == null) {
+			sql = "";
+		}
+
+		params.putString("action","handset_search");
+		params.putString("sql",sql);
+
+		String jstr = getJson(URL_UPLOAD_SQL,params);
+
+		return handleResponse(jstr);
+	}
 }

+ 8 - 0
Apex Mobile/app/src/main/java/com/usai/util/RAUtil.java

@@ -14,6 +14,7 @@ import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Environment;
+import android.provider.Settings;
 import android.support.v4.app.ActivityCompat;
 import android.support.v4.content.FileProvider;
 import android.support.v4.content.PermissionChecker;
@@ -846,4 +847,11 @@ public class RAUtil {
         }
     }
 
+    public static String getAndroidId(Context context) {
+        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
+    }
+
+    public static String getDeviceID(Context context) {
+        return getAndroidId(context);
+    }
 }

+ 135 - 0
Apex Mobile/app/src/main/java/com/usai/util/dbUtil.java

@@ -8,6 +8,7 @@ import android.database.sqlite.SQLiteDatabase.CursorFactory;
 import android.database.sqlite.SQLiteException;
 import android.net.Uri;
 import android.text.TextUtils;
+import android.text.format.DateFormat;
 import android.util.Log;
 
 import com.usai.apex.ApexTrackingApplication;
@@ -380,5 +381,139 @@ public class dbUtil
 	}
 
 	// if(isTableEmpty(db,"block_records"))
+	/**
+	 * 2019.1.5 修改将本地用户缓存上传至服务器并删除本地缓存
+	 * */
+
+	private static SQLiteDatabase getReadOnlyDatabase(Context context) {
+		SqlOpenHelper dbHelper = new SqlOpenHelper(context, "apex.db", null, 2);
+		return dbHelper.OpenDB(false);
+	}
+
+	private static boolean hasDatabaseFile(Context context) {
+
+		SQLiteDatabase db = getReadOnlyDatabase(context);
+		if (db != null) {
+			CloseDB(db);
+			return true;
+		}
+		return false;
+	}
+
+	public static boolean isCachedDataForUser(Context context, String user) {
+		boolean hasFile = hasDatabaseFile(context);
+		if (!hasFile) {
+			return false;
+		}
+
+		Boolean result = false;
+
+		String sql = String.format("select count(0) from (select user from favorites union all select user from fields_info  union all select user from history union all select user from search_history) where user = '%s';",user);
+		SQLiteDatabase db = getReadOnlyDatabase(context);
+		Cursor cursor = db.rawQuery(sql, null);
+		while (cursor.moveToNext()) {
+			int c = cursor.getInt(0);
+			result = c > 0;
+		}
+		CloseCursor(cursor);
+		CloseDB(db);
+
+		return result;
+	}
+
+	public static void cleanCachedDataForUser(Context context, String user) {
+
+		SQLiteDatabase db = OpenDB(context, null, true);
+
+		removeRecords(db, "fields_info", String.format("user = '%s'", user));
+		removeRecords(db, "history", String.format("user = '%s'", user));
+		removeRecords(db, "favorites", String.format("user = '%s'", user));
+		removeRecords(db, "search_history", String.format("user = '%s'", user));
+
+		CloseDB(db);
+	}
+
+	public static String prepareUploadSQLForUser(Context context, String user, String deviceId) {
+
+		StringBuffer sql = new StringBuffer();
+
+		SQLiteDatabase db = OpenDB(context, null, true);
+
+		// favorites
+		String favorites_sql = String.format("select ifnull(name,''), ifnull(params, ''), ifnull(action, ''), ifnull(module_name, ''), ifnull(user, ''), ifnull(create_time, '')  from favorites where user = '%s';", user);
+		Cursor favorites_cur = db.rawQuery(favorites_sql, null);
+		while (favorites_cur.moveToNext()) {
+
+			String name = favorites_cur.getString(0);
+			String params = favorites_cur.getString(1);
+			String action = favorites_cur.getString(2);
+			String module_name = favorites_cur.getString(3);
+			String user_name = favorites_cur.getString(4);
+			long create_time_l = favorites_cur.getLong(5);
+			String create_time = DateFormat.format("yyyy-MM-dd HH:mm:ss", create_time_l).toString();
+
+			String insert_sql = String.format("insert into favorites (name, params, action, module_name, user_name, device_id, create_time) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s');", name, params, action, module_name, user_name, deviceId, create_time);
+			sql.append(insert_sql);
+			sql.append("\r\n");
+		}
+		CloseCursor(favorites_cur);
+
+		// fields_info
+		String fieldsSql = String.format("select ifnull(name, ''), ifnull(aname, ''), ifnull(field_type, 0), ifnull(function_name, ''), ifnull(behavior, 0), ifnull(priority, 0), ifnull(show, 0), ifnull(user, '') from fields_info where user = '%s';",user);
+		Cursor fields_cur = db.rawQuery(fieldsSql, null);
+		while (fields_cur.moveToNext()) {
+
+			String name = fields_cur.getString(0);
+			String aname = fields_cur.getColumnName(1);
+			int field_type = fields_cur.getInt(2);
+			String function_name = fields_cur.getString(3);
+			int behavior = fields_cur.getInt(4);
+			int priority = fields_cur.getInt(5);
+			int show = fields_cur.getInt(6);
+			String user_name = fields_cur.getString(7);
+
+			String insert_sql = String.format("insert into fields_info (name, aname, field_type, function_name, behavior, priority, show, user_name, device_id) values ('%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s');", name, aname, field_type, function_name, behavior, priority, show, user_name, deviceId);
+			sql.append(insert_sql);
+			sql.append("\r\n");
+		}
+		CloseCursor(fields_cur);
+
+		// history
+		String historySql = String.format("select ifnull(name, ''), ifnull(params, ''), ifnull(action, ''), ifnull(module_name, ''), ifnull(user, ''), ifnull(create_time, '') from history where user = '%s';",user);
+		Cursor history_cur = db.rawQuery(historySql, null);
+		while (history_cur.moveToNext()) {
+
+			String name = history_cur.getString(0);
+			String params = history_cur.getString(1);
+			String action = history_cur.getString(2);
+			String module_name = history_cur.getString(3);
+			String user_name = history_cur.getString(4);
+			long create_time_l = history_cur.getLong(5);
+			String create_time = DateFormat.format("yyyy-MM-dd HH:mm:ss", create_time_l).toString();
+
+			String insert_sql = String.format("insert into history (name, params, action, module_name, user_name, device_id, create_time) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s');", name, params, action, module_name, user_name, deviceId, create_time);
+			sql.append(insert_sql);
+			sql.append("\r\n");
+		}
+		CloseCursor(history_cur);
+
+		// search history
+		String search_history_sql = String.format("select ifnull(h_val, ''), ifnull(h_field, ''), ifnull(level, 0), ifnull(user, ''), ifnull(h_time, '') from search_history where user = '%s';",user);
+		Cursor search_history_cur = db.rawQuery(search_history_sql, null);
+		while (search_history_cur.moveToNext()) {
+			String h_val = search_history_cur.getString(0);
+			String h_field = search_history_cur.getString(1);
+			int level = search_history_cur.getInt(2);
+			String user_name = search_history_cur.getString(3);
+			String h_time = search_history_cur.getString(4);
+
+			String insert_sql = String.format("insert into search_history (h_val, h_field, level, user_name, device_id, h_time) values ('%s', '%s', %d, '%s', '%s', '%s');",h_val, h_field, level, user_name, deviceId, h_time);
+			sql.append(insert_sql);
+			sql.append("\r\n");
+		}
+		CloseCursor(search_history_cur);
+
+		return sql.toString();
+	}
 
 }