Przeglądaj źródła

修改apn数据存储到 db

losion.liu@sikey.com.cn 11 miesięcy temu
rodzic
commit
a1f60ee0f8

+ 27 - 25
app/src/main/java/com/xplora/xpsettings/Activity/ApnListActivity.java

@@ -17,6 +17,7 @@ import androidx.annotation.NonNull;
 import com.xplora.xpsettings.Adapter.BaseListAdapter;
 import com.xplora.xpsettings.Data.ApnManager;
 import com.xplora.xpsettings.Data.DataManager;
+import com.xplora.xpsettings.Data.DatabaseHelper;
 import com.xplora.xpsettings.Model.ApnModel;
 import com.xplora.xpsettings.Model.BaseModel;
 import com.xplora.xpsettings.R;
@@ -33,11 +34,31 @@ public class ApnListActivity extends BaseActivity {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_list);
+        moveApn();
+    }
+
+    @Override
+    protected void onStart() {
+        super.onStart();
+        mApnList.clear();
+        mDataList.clear();
         initApn();
         initData();
         initView();
     }
 
+    private void moveApn() {
+        List<ApnModel> apnModelList = DataManager.getApnList();
+        if (apnModelList.size() <= 0)
+            return;
+        for (ApnModel model: apnModelList) {
+            new Thread(()-> {
+                DatabaseHelper.insertAPN(this, model);
+            }).start();
+        }
+        DataManager.setApnList(new ArrayList<>());
+    }
+
     private void initApn() {
         ApnModel currentApn = ApnManager.getCurrentAPN(this);
         if (currentApn != null) {
@@ -46,7 +67,7 @@ public class ApnListActivity extends BaseActivity {
             mApnList.add(currentApn);
         }
         //排除当前正在使用的apn
-        List<ApnModel> apnModelList = DataManager.getApnList();
+        List<ApnModel> apnModelList = DatabaseHelper.getApnList(this);
         for (ApnModel model: apnModelList) {
             Log.d(TAG, "initApn customApn: " + model.name + " apn_id:" + model.apn_id + " mcc:" + model.mcc + " mnc:" + model.mnc);
             model.isCurrentApn = false;
@@ -164,7 +185,7 @@ public class ApnListActivity extends BaseActivity {
             if (type == 1) {
                 insertApn(apnModel);
             } else {
-                editApn(apnModel);
+                updateAPN(apnModel);
             }
         } else if (data.getAction().equals(Constant.INTENT_RESULT_ACTION_REMOVE)) {
             String apn_id = data.getStringExtra(Constant.INTENT_RESULT_VALUE);
@@ -178,39 +199,20 @@ public class ApnListActivity extends BaseActivity {
         if (_id < 0)
             return;
         apnModel.apn_id = String.valueOf(_id);
-        List<ApnModel> apnModelList = DataManager.getApnList();
-        apnModelList.add(apnModel);
-        DataManager.setApnList(apnModelList);
+        DatabaseHelper.insertAPN(this, apnModel);
         reloadView();
     }
 
-    void editApn(ApnModel apnModel) {
+    void updateAPN(ApnModel apnModel) {
         ApnManager.editAPN(getApplicationContext(), apnModel.apn_id, apnModel);
-        List<ApnModel> apnModelList = DataManager.getApnList();
-        for (int i = 0; i < apnModelList.size(); i++) {
-            ApnModel model = apnModelList.get(i);
-            if (model.apn_id.equals(apnModel.apn_id)) {
-                apnModelList.remove(model);
-                apnModelList.add(0, apnModel);
-                break;
-            }
-        }
-        DataManager.setApnList(apnModelList);
+        DatabaseHelper.updateAPN(this, apnModel);
         reloadView();
     }
 
     void deleteApn(String _id) {
         Log.d(TAG, "deleteApn: " + _id);
         ApnManager.deleteAPN(getApplicationContext(), _id);
-        List<ApnModel> apnModelList = DataManager.getApnList();
-        for (int i = 0; i < apnModelList.size(); i++) {
-            ApnModel model = apnModelList.get(i);
-            if (model.apn_id.equals(_id)) {
-                apnModelList.remove(i);
-                break;
-            }
-        }
-        DataManager.setApnList(apnModelList);
+        DatabaseHelper.deleteAPN(this, _id);
         reloadView();
     }
 

+ 1 - 2
app/src/main/java/com/xplora/xpsettings/Data/DataManager.java

@@ -27,7 +27,6 @@ import com.google.gson.Gson;
 import com.google.gson.reflect.TypeToken;
 import com.xplora.xpsettings.Model.ApnModel;
 import com.xplora.xpsettings.Model.RingtoneModel;
-import com.xplora.xpsettings.R;
 import com.xplora.xpsettings.Utils.Constant;
 import com.xplora.xpsettings.Utils.Macros;
 import com.xplora.xpsettings.Utils.MetaData;
@@ -643,7 +642,7 @@ public class DataManager extends Application {
     }
 
     public static boolean isCustomApn(ApnModel apnModel) {
-        List<ApnModel> apnModelList = DataManager.getApnList();
+        List<ApnModel> apnModelList = DatabaseHelper.getApnList(sContext);
         for (int i = 0; i < apnModelList.size(); i++) {
             ApnModel model = apnModelList.get(i);
             if (model.apn_id.equals(apnModel.apn_id)) {

+ 133 - 4
app/src/main/java/com/xplora/xpsettings/Data/DatabaseHelper.java

@@ -1,5 +1,8 @@
 package com.xplora.xpsettings.Data;
 
+import static com.xplora.xpsettings.Utils.MetaData.DB_GOPLAY_APN;
+import static com.xplora.xpsettings.Utils.MetaData.DB_GOPLAY_RINGTONES;
+
 import android.annotation.SuppressLint;
 import android.content.ContentResolver;
 import android.content.ContentValues;
@@ -8,6 +11,7 @@ import android.database.Cursor;
 import android.net.Uri;
 import android.util.Log;
 
+import com.xplora.xpsettings.Model.ApnModel;
 import com.xplora.xpsettings.Model.RingtoneModel;
 import com.xplora.xpsettings.Utils.Macros;
 import com.xplora.xpsettings.Utils.MetaData;
@@ -58,7 +62,7 @@ public class DatabaseHelper {
                 continue;
             ContentValues values = new ContentValues();
             values.put("status", "1");
-            Uri uri = Uri.parse(MetaData.DB_GOPLAY_RINGTONES + "/" + model._id);
+            Uri uri = Uri.parse(DB_GOPLAY_RINGTONES + "/" + model._id);
             int row = context.getContentResolver().update(uri, values, null, null);
             Log.d(TAG, "modifyRingtoneStatusDownloading: " + row);
         }
@@ -78,9 +82,7 @@ public class DatabaseHelper {
 
     public static int addWatchFace(Context context, String name) {
         Log.d(TAG, "addWatchFaceConfig: ");
-        String AUTHORITIES_COMMON = "content://com.xplora.WatchCommonProvider";
-        String watchFaceDB = AUTHORITIES_COMMON + "/ringtones";
-        Uri insertUri = Uri.parse(watchFaceDB);
+        Uri insertUri = Uri.parse(DB_GOPLAY_RINGTONES);
         ContentValues values = new ContentValues();
         values.put("contentType", "Watchface");
         values.put("title", name);
@@ -102,4 +104,131 @@ public class DatabaseHelper {
         return url.substring(url.lastIndexOf("/") + 1);
     }
 
+    public static ArrayList<ApnModel> getApnList(Context context) {
+        if (Macros.DEBUG) {
+            return new ArrayList<>();
+        } else {
+            ArrayList<ApnModel> arrayList = new ArrayList<>();
+            ContentResolver resolver = context.getContentResolver();
+            Uri uri = Uri.parse(DB_GOPLAY_APN + "/" + 0);
+            Cursor cursor = resolver.query(uri, null, null, null, null);
+            if (cursor == null) {
+                Log.d(TAG, "getApnList: cursor == null");
+                return arrayList;
+            }
+            if (!cursor.moveToFirst()) {
+                Log.d(TAG, "getApnList: cursor.moveToFirst() == false");
+                cursor.close();
+                return arrayList;
+            }
+            Log.d(TAG, "getApnList: db ok");
+            do {
+                ApnModel bean = new ApnModel();
+                bean.apn_id = cursor.getString(cursor.getColumnIndex("apn_id"));
+                bean.name = cursor.getString(cursor.getColumnIndex("name"));
+                bean.apn_name = cursor.getString(cursor.getColumnIndex("apn_name"));
+                bean.user_name = cursor.getString(cursor.getColumnIndex("user_name"));
+                bean.password = cursor.getString(cursor.getColumnIndex("password"));
+                bean.mcc = cursor.getString(cursor.getColumnIndex("mcc"));
+                bean.mnc = cursor.getString(cursor.getColumnIndex("mnc"));
+                bean.proxy = cursor.getString(cursor.getColumnIndex("proxy"));
+                bean.port = cursor.getString(cursor.getColumnIndex("port"));
+                bean.server = cursor.getString(cursor.getColumnIndex("server"));
+                bean.mmsc = cursor.getString(cursor.getColumnIndex("mmsc"));
+                bean.mmsproxy = cursor.getString(cursor.getColumnIndex("mmsproxy"));
+                bean.mmsport = cursor.getString(cursor.getColumnIndex("mmsport"));
+                bean.auth_type = cursor.getString(cursor.getColumnIndex("auth_type"));
+                bean.apn_type = cursor.getString(cursor.getColumnIndex("apn_type"));
+                bean.apn_protocol = cursor.getString(cursor.getColumnIndex("apn_protocol"));
+                bean.apn_roaming_protocol = cursor.getString(cursor.getColumnIndex("apn_roaming_protocol"));
+                bean.bearer = cursor.getString(cursor.getColumnIndex("bearer"));
+                bean.mvno_type = cursor.getString(cursor.getColumnIndex("mvno_type"));
+                bean.mvno_match_data = cursor.getString(cursor.getColumnIndex("mvno_match_data"));
+                Log.d(TAG, "getApnList: apn_id: " + bean.apn_id + " mcc: " + bean.mcc + " mnc:" + bean.mnc);
+                arrayList.add(bean);
+            } while (cursor.moveToNext());
+            cursor.close();
+            Log.d(TAG, "getApnList: " + arrayList.size());
+            return arrayList;
+        }
+    }
+
+    public static int insertAPN(Context context, ApnModel model) {
+        if (Macros.DEBUG) {
+            return -1;
+        } else {
+            Uri uri = Uri.parse(DB_GOPLAY_APN);
+            ContentValues values = new ContentValues();
+            values.put("apn_id", model.apn_id);
+            values.put("name", model.name);
+            values.put("apn_name", model.apn_name);
+            values.put("user_name", model.user_name);
+            values.put("password", model.password);
+            values.put("mcc", model.mcc);
+            values.put("mnc", model.mnc);
+            values.put("proxy", model.proxy);
+            values.put("port", model.port);
+            values.put("server", model.server);
+            values.put("mmsc", model.mmsc);
+            values.put("mmsproxy", model.mmsproxy);
+            values.put("mmsport", model.mmsport);
+            values.put("auth_type", model.auth_type);
+            values.put("apn_type", model.apn_type);
+            values.put("apn_protocol", model.apn_protocol);
+            values.put("apn_roaming_protocol", model.apn_roaming_protocol);
+            values.put("bearer", model.bearer);
+            values.put("mvno_type", model.mvno_type);
+            values.put("mvno_match_data", model.mvno_match_data);
+            Uri returnUri = context.getContentResolver().insert(uri, values);
+            Log.d(TAG, "insertAPN: " + returnUri.toString());
+            String s = getLastPart(returnUri.toString());
+            return Integer.parseInt(s);
+        }
+    }
+
+    public static void updateAPN(Context context, ApnModel model) {
+        if (Macros.DEBUG) {
+
+        } else {
+            new Thread(()->{
+                Uri uri = Uri.parse(DB_GOPLAY_APN + "/" + model.apn_id);
+                ContentValues values = new ContentValues();
+                values.put("apn_id", model.apn_id);
+                values.put("name", model.name);
+                values.put("apn_name", model.apn_name);
+                values.put("user_name", model.user_name);
+                values.put("password", model.password);
+                values.put("mcc", model.mcc);
+                values.put("mnc", model.mnc);
+                values.put("proxy", model.proxy);
+                values.put("port", model.port);
+                values.put("server", model.server);
+                values.put("mmsc", model.mmsc);
+                values.put("mmsproxy", model.mmsproxy);
+                values.put("mmsport", model.mmsport);
+                values.put("auth_type", model.auth_type);
+                values.put("apn_type", model.apn_type);
+                values.put("apn_protocol", model.apn_protocol);
+                values.put("apn_roaming_protocol", model.apn_roaming_protocol);
+                values.put("bearer", model.bearer);
+                values.put("mvno_type", model.mvno_type);
+                values.put("mvno_match_data", model.mvno_match_data);
+                int row = context.getContentResolver().update(uri, values, null, null);
+                Log.d(TAG, "updateAPN: " + row);
+            }).start();
+        }
+    }
+
+    public static void deleteAPN(Context context, String apnId) {
+        if (Macros.DEBUG) {
+
+        } else {
+            new Thread(()->{
+                Uri uri = Uri.parse(DB_GOPLAY_APN + "/" + apnId);
+                int row = context.getContentResolver().delete(uri, null, null);
+                Log.d(TAG, "updateAPN: " + row);
+            }).start();
+        }
+    }
+
 }

+ 2 - 0
app/src/main/java/com/xplora/xpsettings/Utils/MetaData.java

@@ -8,8 +8,10 @@ public class MetaData {
 
     public final static String AUTHORITIES_COMMON = "content://com.xplora.WatchCommonProvider";
     public final static String DB_GOPLAY_RINGTONES = AUTHORITIES_COMMON + "/ringtones";
+    public final static String DB_GOPLAY_APN = AUTHORITIES_COMMON + "/apn";
     public final static String DB_GOPLAY_CONTENT_QUERY = AUTHORITIES_COMMON + "/content";
 
+
     private static final String TABLE_NAME_SETTING = "setting";
     public static final Uri TABLE_URI_SETTING = Uri.parse(CONTENT_URI + TABLE_NAME_SETTING);