Explorar o código

重构 apn db

losion.liu@sikey.com.cn hai 11 meses
pai
achega
982ad06cca

+ 14 - 12
app/src/main/java/com/xplora/xpsettings/Activity/ApnListActivity.java

@@ -41,37 +41,39 @@ public class ApnListActivity extends BaseActivity {
     }
 
     private void moveApn() {
-        List<ApnModel> apnModelList = DataManager.getApnList();
+        List<ApnModel> apnModelList = DataManager.getApnListFromSP();
         Log.d(TAG, "moveApn: size: " + apnModelList.size());
         if (apnModelList.size() <= 0)
             return;
         for (ApnModel model: apnModelList) {
             DatabaseHelper.insertAPN(this, model);
         }
-        DataManager.setApnList(new ArrayList<>());
+        DataManager.setApnListToSP(new ArrayList<>());
     }
 
     private void initApn() {
         ApnModel currentApn = ApnManager.getCurrentAPN(this);
         if (currentApn != null) {
-            Log.d(TAG, "initApn currentApn: " + currentApn.name + " apn_id:" +currentApn.apn_id + " mcc:" + currentApn.mcc + " mnc:" + currentApn.mnc);
+            Log.d(TAG, "initApn currentApn: " + currentApn.name + " _id:" +currentApn._id + " mcc:" + currentApn.mcc + " mnc:" + currentApn.mnc);
             currentApn.isCurrentApn = true;
             mApnList.add(currentApn);
         }
+
         //排除当前正在使用的apn
-        List<ApnModel> apnModelList = DatabaseHelper.getApnList(this);
+        List<ApnModel> modelList = DatabaseHelper.getCustomApnList(this);
+        List<ApnModel> apnModelList = ApnManager.getAPNList(this, modelList);
+        Log.d(TAG, "initApn: customApn size: " + apnModelList.size());
         for (ApnModel model: apnModelList) {
-            Log.d(TAG, "initApn customApn: " + model.name + " apn_id:" + model.apn_id + " mcc:" + model.mcc + " mnc:" + model.mnc);
+            Log.d(TAG, "initApn customApn: " + model.name + " apn_id:" + model.apn_id + " _id:" + model._id + " mcc:" + model.mcc + " mnc:" + model.mnc);
             model.isCurrentApn = false;
-            if (currentApn == null || currentApn.apn_id.isEmpty()) {
+            if (currentApn == null || currentApn._id.isEmpty()) {
                 mApnList.add(model);
             } else {
-                if (!model.apn_id.equals(currentApn.apn_id)) {
+                if (!model._id.equals(currentApn._id)) {
                     mApnList.add(model);
                 }
             }
         }
-        Log.d(TAG, "initApn: size: " + mApnList.size());
     }
 
     private void initData() {
@@ -188,20 +190,20 @@ public class ApnListActivity extends BaseActivity {
         int _id = ApnManager.insertAPN(getApplicationContext(), apnModel);
         if (_id < 0)
             return;
-        apnModel.apn_id = String.valueOf(_id);
+        apnModel._id = String.valueOf(_id);
         DatabaseHelper.insertAPN(this, apnModel);
         reloadView();
     }
 
     void updateAPN(ApnModel apnModel) {
-        ApnManager.editAPN(getApplicationContext(), apnModel.apn_id, apnModel);
+        ApnManager.editAPN(getApplicationContext(), apnModel);
         DatabaseHelper.updateAPN(this, apnModel);
         reloadView();
     }
 
     void deleteApn(ApnModel model) {
-        ApnManager.deleteAPN(getApplicationContext(), model.apn_id);
-        DatabaseHelper.deleteAPN(this, model._id);
+        ApnManager.deleteAPN(getApplicationContext(), model);
+        DatabaseHelper.deleteAPN(this, model);
         reloadView();
     }
 

+ 31 - 18
app/src/main/java/com/xplora/xpsettings/Data/ApnManager.java

@@ -14,6 +14,7 @@ import com.xplora.xpsettings.Model.ApnModel;
 import com.xplora.xpsettings.Utils.Macros;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 public class ApnManager {
@@ -23,14 +24,20 @@ public class ApnManager {
     public static Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
 
     @SuppressLint("Range")
-    public static List<ApnModel> getAPNList(Context context){
+    public static List<ApnModel> getAPNList(Context context, List<ApnModel> modelList){
+        Log.d(TAG, "getAPNList: " + modelList.size());
         List<ApnModel> list = new ArrayList<ApnModel>();
         ContentResolver resolver = context.getContentResolver();
 
-        Cursor cr = resolver.query(APN_URI, null, null, null, null);
+        String[] dbList = new String[modelList.size()];
+        for (int i = 0; i < modelList.size(); i++) {
+            dbList[i] = modelList.get(i)._id;
+        }
+
+        Cursor cr = resolver.query(APN_URI, new String[]{"_id"},  "_id" + "=?", dbList, null);
         while(cr!=null && cr.moveToNext()){
             ApnModel a = new ApnModel();
-            a.apn_id = getCursorColumnValue(cr,"_id");
+            a._id = getCursorColumnValue(cr,"_id");
             a.name = getCursorColumnValue(cr,"name");
             a.apn_name = getCursorColumnValue(cr,"apn");
             a.user_name = getCursorColumnValue(cr,"user");
@@ -50,6 +57,12 @@ public class ApnManager {
             a.bearer = getCursorColumnValue(cr,"bearer");
             a.mvno_type = getCursorColumnValue(cr,"mvno_type");
             a.mvno_match_data = getCursorColumnValue(cr,"mvno_match_data");
+            for (ApnModel model: modelList) {
+                if (model._id.equals(a._id)) {
+                    a.apn_id = model.apn_id;
+                    break;
+                }
+            }
             list.add(a);
         }
         if(cr!=null)
@@ -69,40 +82,40 @@ public class ApnManager {
 
         long value = ContentUris.parseId(newRow);
         Log.d(TAG, "insertAPN: row: " + value);
-        int apn_id = Integer.parseInt(String.valueOf(value));
+        int _id = Integer.parseInt(String.valueOf(value));
 
-        setCurrentAPN(context, apn_id);
-        return apn_id;
+        setCurrentAPN(context, _id);
+        return _id;
     }
 
-    public static void editAPN(Context context, String apn_id, ApnModel apnModel) {
-        Log.d(TAG, "editAPN: " + apn_id);
-        if (apn_id == null || apn_id.isEmpty())
+    public static void editAPN(Context context, ApnModel apnModel) {
+        Log.d(TAG, "editAPN: " + apnModel._id);
+        if (apnModel._id == null || apnModel._id.isEmpty())
             return;
         String NUMERIC = getSIMInfo(context);
         if (NUMERIC == null)
             return;
         ContentValues values = getContentValues(apnModel, NUMERIC);
         ContentResolver resolver = context.getContentResolver();
-        int value = resolver.update(APN_URI, values, "_id" + "=?", new String[] { apn_id });
+        int value = resolver.update(APN_URI, values, "_id" + "=?", new String[] { apnModel._id });
         Log.d(TAG, "editAPN result:" + value);
 
-        setCurrentAPN(context, Integer.parseInt(apn_id));
+        setCurrentAPN(context, Integer.parseInt(apnModel._id));
     }
 
-    public static void deleteAPN(Context context, String apn_id) {
-        Log.d(TAG, "deleteAPN: " + apn_id);
-        if (apn_id == null || apn_id.isEmpty())
+    public static void deleteAPN(Context context, ApnModel apnModel) {
+        Log.d(TAG, "deleteAPN: " + apnModel._id);
+        if (apnModel._id == null || apnModel._id.isEmpty())
             return;
         ContentResolver resolver = context.getContentResolver();
-        int returnValue = resolver.delete(APN_URI,  "_id" + "=?", new String[] { apn_id });
+        int returnValue = resolver.delete(APN_URI,  "_id" + "=?", new String[] { apnModel._id });
     }
 
     // 设置接入点
-    public static void setCurrentAPN(Context context, int apn_id) {
+    public static void setCurrentAPN(Context context, int _id) {
         ContentResolver resolver = context.getContentResolver();
         ContentValues values = new ContentValues();
-        values.put("apn_id", apn_id);
+        values.put("apn_id", _id);
         int returnValue = resolver.update(CURRENT_APN_URI, values, null, null);
         Log.d(TAG, "setCurrentAPN: " + returnValue);
     }
@@ -129,7 +142,7 @@ public class ApnManager {
             }
             Log.d(TAG, "getCurrentAPN: ok");
             ApnModel a = new ApnModel();
-            a.apn_id = getCursorColumnValue(cr,"_id");
+            a._id = getCursorColumnValue(cr,"_id");
             a.name = getCursorColumnValue(cr,"name");
             a.apn_name = getCursorColumnValue(cr,"apn");
             a.user_name = getCursorColumnValue(cr,"user");

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

@@ -597,7 +597,7 @@ public class DataManager extends Application {
         return SystemProperties.getBoolean("persist.operator.disable.wifi", false);
     }
 
-    public static List<ApnModel> getApnList() {
+    public static List<ApnModel> getApnListFromSP() {
         List<ApnModel> apnModelList = new ArrayList<>();
         if (Macros.DEBUG) {
             for (int i = 0; i < 5; i++) {
@@ -619,7 +619,7 @@ public class DataManager extends Application {
         } else {
             SharedPreferences xpsp = sContext.getSharedPreferences("xpsp", 0);
             String apnlist = xpsp.getString("apnlist", "");
-            Log.d(TAG, "getApnList: " + apnlist);
+            Log.d(TAG, "getApnListFromSP: " + apnlist);
             if (apnlist.isEmpty())
                 return apnModelList;
 
@@ -631,7 +631,7 @@ public class DataManager extends Application {
         return apnModelList;
     }
 
-    public static void setApnList(List<ApnModel> apnModelList) {
+    public static void setApnListToSP(List<ApnModel> apnModelList) {
         Type listType = new TypeToken<List<ApnModel>>() {}.getType();
         String content = new Gson().toJson(apnModelList, listType);
 
@@ -642,10 +642,10 @@ public class DataManager extends Application {
     }
 
     public static boolean isCustomApn(ApnModel apnModel) {
-        List<ApnModel> apnModelList = DatabaseHelper.getApnList(sContext);
+        List<ApnModel> apnModelList = DatabaseHelper.getCustomApnList(sContext);
         for (int i = 0; i < apnModelList.size(); i++) {
             ApnModel model = apnModelList.get(i);
-            if (model.apn_id.equals(apnModel.apn_id)) {
+            if (model._id.equals(apnModel._id)) {
                 return true;
             }
         }

+ 12 - 79
app/src/main/java/com/xplora/xpsettings/Data/DatabaseHelper.java

@@ -104,7 +104,7 @@ public class DatabaseHelper {
         return url.substring(url.lastIndexOf("/") + 1);
     }
 
-    public static ArrayList<ApnModel> getApnList(Context context) {
+    public static ArrayList<ApnModel> getCustomApnList(Context context) {
         if (Macros.DEBUG) {
             return new ArrayList<>();
         } else {
@@ -113,73 +113,35 @@ public class DatabaseHelper {
             Uri uri = Uri.parse(DB_GOPLAY_APN);
             Cursor cursor = resolver.query(uri, null, null, null, null);
             if (cursor == null) {
-                Log.d(TAG, "getApnList: cursor == null");
+                Log.d(TAG, "getCustomApnList: cursor == null");
                 return arrayList;
             }
             if (!cursor.moveToFirst()) {
-                Log.d(TAG, "getApnList: cursor.moveToFirst() == false");
+                Log.d(TAG, "getCustomApnList: cursor.moveToFirst() == false");
                 cursor.close();
                 return arrayList;
             }
             do {
                 ApnModel bean = new ApnModel();
-                bean._id = cursor.getString(cursor.getColumnIndex("_id"));
                 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);
+                bean._id = cursor.getString(cursor.getColumnIndex("_id"));
+                Log.d(TAG, "getCustomApnList: _id: " + bean._id + " mcc: " + bean.mcc + " mnc:" + bean.mnc);
                 arrayList.add(bean);
             } while (cursor.moveToNext());
             cursor.close();
-            Log.d(TAG, "getApnList: " + arrayList.size());
+            Log.d(TAG, "getCustomApnList: " + arrayList.size());
             return arrayList;
         }
     }
 
     public static int insertAPN(Context context, ApnModel model) {
-        Log.d(TAG, "====== insertAPN ========: " + model.apn_id);
+        Log.d(TAG, "====== insertAPN ========: " + model._id);
         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);
+            values.put("_id", model._id);
             Uri returnUri = context.getContentResolver().insert(uri, values);
             Log.d(TAG, "insertAPN row: " + returnUri.toString());
             String s = getLastPart(returnUri.toString());
@@ -188,46 +150,17 @@ public class DatabaseHelper {
     }
 
     public static void updateAPN(Context context, ApnModel model) {
-        Log.d(TAG, "====== updateAPN =======: " + model.apn_id);
-        if (Macros.DEBUG) {
-
-        } else {
-            Uri uri = Uri.parse(DB_GOPLAY_APN + "/" + model._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: " + row);
-        }
+        Log.d(TAG, "====== updateAPN =======: " + model._id);
     }
 
-    public static void deleteAPN(Context context, String _id) {
-        Log.d(TAG, "====== deleteAPN =======: " + _id);
+    public static void deleteAPN(Context context, ApnModel model) {
+        Log.d(TAG, "====== deleteAPN =======: " + model.apn_id);
         if (Macros.DEBUG) {
 
         } else {
-            Uri uri = Uri.parse(DB_GOPLAY_APN + "/" + _id);
+            Uri uri = Uri.parse(DB_GOPLAY_APN + "/" + model.apn_id);
             int row = context.getContentResolver().delete(uri, null, null);
             Log.d(TAG, "deleteAPN row: " + row);
         }
     }
-
 }

+ 2 - 2
app/src/main/java/com/xplora/xpsettings/Model/ApnModel.java

@@ -3,8 +3,8 @@ package com.xplora.xpsettings.Model;
 import java.io.Serializable;
 
 public class ApnModel implements Serializable {
-    public String _id = "";
-    public String apn_id = "";
+    public String _id = ""; //原生数据库 id
+    public String apn_id = ""; //carlos 数据库 id
     public String name = "";
     public String apn_name = "";
     public String user_name = "";