Jelajahi Sumber

1.修改Android Apex Drivers数据保存与恢复。

Pen Li 7 tahun lalu
induk
melakukan
97ed1d2c4f

+ 7 - 1
ApexDrivers/app/src/main/AndroidManifest.xml

@@ -36,7 +36,13 @@
         </activity>
         <activity android:name=".camera.PreferencesActivity">
         </activity>
-        <activity android:name=".CodeScanner.CaptureActivity">
+        <activity android:name=".CodeScanner.CaptureActivity"
+                  android:clearTaskOnLaunch="true"
+                  android:label="Scan"
+                  android:screenOrientation="sensorLandscape"
+                  android:stateNotNeeded="true"
+                  android:theme="@style/CaptureTheme"
+                  android:windowSoftInputMode="stateAlwaysHidden">
         </activity>
         <activity android:name=".Update.PhotoPreviewActivity">
         </activity>

+ 81 - 31
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/Detail/DetailActivity.java

@@ -57,6 +57,8 @@ public class DetailActivity extends AppCompatActivity implements DetailAdapter.D
 
 
     private final static String IntentOrderIDKey = "orderID";
+    private final static  String SaveDataKey = "DetailSaveJSON";
+
     public static Intent build(Context context, String orderID) {
 
         if (context == null) {
@@ -100,6 +102,12 @@ public class DetailActivity extends AppCompatActivity implements DetailAdapter.D
 
         mListView.setAdapter(mAdapter);
         mListView.setGroupIndicator(null);
+        mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
+            @Override
+            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
+                return true; // 不允许GroupView点击
+            }
+        });
 
         mRefresh = findViewById(R.id.detail_refresh);
         mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@@ -109,7 +117,36 @@ public class DetailActivity extends AppCompatActivity implements DetailAdapter.D
             }
         });
 
-        loadData();
+        if (savedInstanceState != null) {
+
+            String jsonStr = savedInstanceState.getString(SaveDataKey);
+            if (jsonStr != null) {
+
+                try {
+
+                    JSONObject json = new JSONObject(jsonStr);
+
+                    handleJson(json);
+                    changeData();
+
+                } catch (JSONException e) {
+                    e.printStackTrace();
+                }
+
+            }
+
+        } else {
+            loadData();
+        }
+    }
+
+    @Override
+    protected void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+
+        if (mJson != null) {
+            outState.putString(SaveDataKey,mJson.toString());
+        }
     }
 
     @Override
@@ -162,6 +199,47 @@ public class DetailActivity extends AppCompatActivity implements DetailAdapter.D
                 .show();
     }
 
+    private void handleJson(JSONObject json) {
+
+        try {
+            JSONArray sectionArr = json.optJSONArray("sections");
+            if (sectionArr != null) {
+
+                mJson = json;
+                mSectionArray.clear();
+
+                for (int i = 0; i < sectionArr.length(); i++) {
+
+                    JSONObject section = sectionArr.getJSONObject(i);
+                    DetailSectionModel model = new DetailSectionModel();
+
+                    model.title = section.optString("title");
+                    model.setValues(section.optJSONArray("values"));
+
+                    mSectionArray.add(model);
+                }
+            }
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+
+    }
+
+    private void changeData() {
+
+        /**
+         * 手动调用打开/关闭 Group,否则默认关闭
+         * */
+        if (mSectionArray.size() > 0) {
+            for (int i = 0; i < mSectionArray.size(); i++) {
+                mListView.expandGroup(i);
+            }
+        }
+
+        mAdapter.notifyDataSetChanged();
+
+    }
+
     private void loadData() {
 
         showProgressDialog();
@@ -281,26 +359,7 @@ public class DetailActivity extends AppCompatActivity implements DetailAdapter.D
                             int restul = json.getInt("result");
                             if (restul == RESULT_TRUE) {
 
-                                JSONArray sectionArr = json.optJSONArray("sections");
-                                if (sectionArr != null) {
-
-                                    activity.mJson = json;
-                                    activity.mSectionArray.clear();
-
-                                    for (int i = 0; i < sectionArr.length(); i++) {
-
-                                        JSONObject section = sectionArr.getJSONObject(i);
-                                        DetailSectionModel model = new DetailSectionModel();
-
-                                        model.title = section.optString("title");
-                                        model.setValues(section.optJSONArray("values"));
-
-                                        activity.mSectionArray.add(model);
-                                    }
-
-
-
-                                }
+                                activity.handleJson(json);
 
                             } else {
                                 // error
@@ -322,16 +381,7 @@ public class DetailActivity extends AppCompatActivity implements DetailAdapter.D
                         String errMsg = "Sorry,there is something wrong";
                         activity.showWarningMsg(errMsg);
                     }
-                    /**
-                     * 手动调用打开/关闭 Group,否则默认关闭
-                     * */
-                    if (activity.mSectionArray.size() > 0) {
-                        for (int i = 0; i < activity.mSectionArray.size(); i++) {
-                            activity.mListView.expandGroup(i);
-                        }
-                    }
-
-                    activity.mAdapter.notifyDataSetChanged();
+                    activity.changeData();
                 }
                 break;
                 case DetailActionRemote: {

+ 62 - 26
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/Home/HomeFragment.java

@@ -30,6 +30,7 @@ import com.usai.redant.rautils.Utils.Network;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
+import org.json.JSONStringer;
 
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
@@ -37,6 +38,7 @@ import java.util.ArrayList;
 public class HomeFragment extends Fragment {
 
     public final static String HomeReloadBroadcastAction = "com.usai.redant.apexdriver.home_refresh";
+    private final static String HomeSaveDataKey = "HomeSaveDataKey";
 
     private Context mCtx;
     private HomeFragment self = this;
@@ -46,6 +48,7 @@ public class HomeFragment extends Fragment {
     private ArrayList<HomeSectionModel> mSectionArray = new ArrayList<>();
     private HomeHandler mHandler = new HomeHandler(this);
     private HomeBroadCastReceiver mReceiver;
+    private JSONObject mJson;
 
     @Nullable
     @Override
@@ -95,9 +98,6 @@ public class HomeFragment extends Fragment {
                 loadData();
             }
         });
-
-
-        loadData();
     }
 
     @Override
@@ -106,6 +106,24 @@ public class HomeFragment extends Fragment {
         mCtx = getActivity();
 
         registBroadcastReceiver();
+
+        if (savedInstanceState != null) {
+
+            String jsonStr = savedInstanceState.getString(HomeSaveDataKey);
+            if (jsonStr != null) {
+                try {
+
+                    JSONObject json = new JSONObject(jsonStr);
+                    handleJson(json);
+
+                } catch (JSONException e) {
+                    e.printStackTrace();
+                }
+            }
+
+        } else {
+            loadData();
+        }
     }
 
     @Override
@@ -116,6 +134,10 @@ public class HomeFragment extends Fragment {
     @Override
     public void onSaveInstanceState(@NonNull Bundle outState) {
         super.onSaveInstanceState(outState);
+
+        if (mJson != null) {
+            outState.putString(HomeSaveDataKey,mJson.toString());
+        }
     }
 
     @Override
@@ -160,6 +182,42 @@ public class HomeFragment extends Fragment {
                 .show();
     }
 
+    private void handleJson(JSONObject json) {
+
+        if (json == null) {
+            return;
+        }
+
+        try {
+            JSONArray sectionArr = json.optJSONArray("sections");
+            if (sectionArr != null) {
+                mJson = json;
+                mSectionArray.clear();
+
+                for (int i = 0; i < sectionArr.length(); i++) {
+
+                    JSONObject section = sectionArr.getJSONObject(i);
+                    HomeSectionModel model = new HomeSectionModel();
+                    model.section = i;
+
+                    model.type = section.optInt("type");
+                    model.title = section.optString("title");
+                    model.setOrders(section.optJSONArray("orders"));
+
+                    mSectionArray.add(model);
+                }
+
+                Message msg = new Message();
+                msg.what = HomeHandler.HomeActionReloadData;
+                mHandler.sendMessage(msg);
+
+            }
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+
+    }
+
     private void loadData() {
 
         showProgressDialog();
@@ -174,29 +232,7 @@ public class HomeFragment extends Fragment {
                         int restul = json.getInt("result");
                         if (restul == Network.RESULT_TRUE) {
 
-                            JSONArray sectionArr = json.optJSONArray("sections");
-                            if (sectionArr != null) {
-
-                                mSectionArray.clear();
-
-                                for (int i = 0; i < sectionArr.length(); i++) {
-
-                                    JSONObject section = sectionArr.getJSONObject(i);
-                                    HomeSectionModel model = new HomeSectionModel();
-                                    model.section = i;
-
-                                    model.type = section.optInt("type");
-                                    model.title = section.optString("title");
-                                    model.setOrders(section.optJSONArray("orders"));
-
-                                    mSectionArray.add(model);
-                                }
-
-                                Message msg = new Message();
-                                msg.what = HomeHandler.HomeActionReloadData;
-                                mHandler.sendMessage(msg);
-
-                            }
+                            handleJson(json);
 
                         } else {
 

+ 22 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/Update/Model/UpdateBaseModel.java

@@ -1,5 +1,8 @@
 package com.usai.redant.apexdrivers.Update.Model;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+
 import java.lang.ref.WeakReference;
 
 public class UpdateBaseModel {
@@ -26,4 +29,23 @@ public class UpdateBaseModel {
         }
     }
 
+    public JSONObject model2Json() {
+
+        try {
+            JSONObject json = new JSONObject();
+            json.put("type",type);
+            if (title != null) {
+                json.put("title",title);
+            }
+            if (key != null) {
+                json.put("key",key);
+            }
+
+            return json;
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
 }

+ 22 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/Update/Model/UpdateInputModel.java

@@ -1,5 +1,8 @@
 package com.usai.redant.apexdrivers.Update.Model;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+
 public class UpdateInputModel extends UpdateBaseModel {
 
     public boolean scannable;
@@ -12,4 +15,23 @@ public class UpdateInputModel extends UpdateBaseModel {
         }
     }
 
+    @Override
+    public JSONObject model2Json() {
+        JSONObject json =  super.model2Json();
+
+        if (json != null) {
+            try {
+
+                json.put("scannable",scannable);
+                if (value != null) {
+                    json.put("value",value);
+                }
+
+            } catch (JSONException e) {
+                e.printStackTrace();
+            }
+        }
+
+        return json;
+    }
 }

+ 19 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/Update/Model/UpdateLabelModel.java

@@ -1,7 +1,26 @@
 package com.usai.redant.apexdrivers.Update.Model;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+
 public class UpdateLabelModel extends UpdateBaseModel {
 
     public String value;
 
+    @Override
+    public JSONObject model2Json() {
+        JSONObject json = super.model2Json();
+
+        try {
+
+            if (value != null) {
+                json.put("value",value);
+            }
+
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+
+        return json;
+    }
 }

+ 20 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/Update/Model/UpdateMultInputModel.java

@@ -1,5 +1,8 @@
 package com.usai.redant.apexdrivers.Update.Model;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+
 public class UpdateMultInputModel extends UpdateBaseModel {
 
     public String value;
@@ -10,4 +13,21 @@ public class UpdateMultInputModel extends UpdateBaseModel {
             weakDelegate.get().refresh();
         }
     }
+
+    @Override
+    public JSONObject model2Json() {
+        JSONObject json = super.model2Json();
+
+        try {
+
+            if (value != null) {
+                json.put("value",value);
+            }
+
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+
+        return json;
+    }
 }

+ 20 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/Update/Model/UpdatePhotoModel.java

@@ -3,6 +3,9 @@ package com.usai.redant.apexdrivers.Update.Model;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+
 import java.io.File;
 
 public class UpdatePhotoModel extends UpdateBaseModel {
@@ -45,4 +48,21 @@ public class UpdatePhotoModel extends UpdateBaseModel {
         }
     }
 
+    @Override
+    public JSONObject model2Json() {
+        JSONObject json = super.model2Json();
+
+        try {
+
+            if (photoPath != null && photoPath.length() > 0) {
+                json.put("photoPath",photoPath);
+            }
+
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+
+        return json;
+    }
+
 }

+ 98 - 28
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/Update/UpdateActivity.java

@@ -21,6 +21,7 @@ import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
+import android.view.View;
 import android.widget.ExpandableListView;
 
 import com.usai.redant.apexdrivers.ApexDriverApplication;
@@ -54,6 +55,7 @@ public class UpdateActivity extends AppCompatActivity implements UpdateAdapter.U
     private final static String OrderIDKey = "OrderID";
     private final static String ActionIDKey = "ActionID";
     private final static String ActionTitleKey = "ActionTitle";
+    private final static String SaveDataKey = "UpdateSavedJson";
 
     private final static int REQUEST_SCANNER_CODE = 0;
     private final static int REQUEST_CAMERA_CODE = 1;
@@ -80,7 +82,6 @@ public class UpdateActivity extends AppCompatActivity implements UpdateAdapter.U
     private Context mCtx = this;
     private UpdateActivity self = this;
     private UpdateHandler mHandler;
-    private JSONObject mJson;
     private ArrayList<UpdateSectionModel> mSectionArray = new ArrayList<>();
     private UpdateAdapter mAdapter;
 
@@ -118,6 +119,12 @@ public class UpdateActivity extends AppCompatActivity implements UpdateAdapter.U
 
         mListView.setGroupIndicator(null);
         mListView.setAdapter(mAdapter);
+        mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
+            @Override
+            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
+                return true; // 不允许GroupView点击
+            }
+        });
 
         mRefresh = findViewById(R.id.update_refresh);
         mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@@ -127,13 +134,42 @@ public class UpdateActivity extends AppCompatActivity implements UpdateAdapter.U
             }
         });
 
-        loadData();
+        if (savedInstanceState != null) {
+
+            String jsonStr = savedInstanceState.getString(SaveDataKey);
+            if (jsonStr != null) {
+                try {
+                    JSONObject json = new JSONObject(jsonStr);
+
+                    handleJSON(json);
+                    changeData();
+
+                } catch (JSONException e) {
+                    e.printStackTrace();
+                }
+            }
+
+        } else {
+            loadData();
+        }
     }
 
     @Override
     protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
 
+        JSONObject json = prepareSavedJson();
+        if (json != null) {
+            outState.putString(SaveDataKey,json.toString());
+        }
+
+    }
+
+    @Override
+    protected void onStop() {
+        super.onStop();
+
+        dismissProgressDialog();
     }
 
     @Override
@@ -260,6 +296,42 @@ public class UpdateActivity extends AppCompatActivity implements UpdateAdapter.U
 
     }
 
+    private void handleJSON(JSONObject json) {
+
+        JSONArray sectionArr = json.optJSONArray("sections");
+        if (sectionArr != null) {
+
+            mSectionArray.clear();
+            try {
+                for (int i = 0; i < sectionArr.length(); i++) {
+
+                    JSONObject section = sectionArr.getJSONObject(i);
+                    UpdateSectionModel sectionModel = new UpdateSectionModel();
+                    sectionModel.title = section.optString("title");
+                    sectionModel.setItems(section.optJSONArray("items"));
+
+                    mSectionArray.add(sectionModel);
+                }
+            } catch (JSONException e) {
+                e.printStackTrace();
+            }
+
+        }
+    }
+
+    private void changeData() {
+
+        /**
+         * 手动调用打开/关闭 Group,否则默认关闭
+         * */
+        if (mSectionArray.size() > 0) {
+            for (int i = 0; i < mSectionArray.size(); i++) {
+                mListView.expandGroup(i);
+            }
+        }
+        mAdapter.notifyDataSetChanged();
+    }
+
     private void update() {
 
         showProgressDialog();
@@ -497,7 +569,7 @@ public class UpdateActivity extends AppCompatActivity implements UpdateAdapter.U
         }
 
         mPhotoModel = model;
-        if (model.photoPath == null) {
+        if (model.photoPath == null || model.photoPath.length() == 0) {
 
             startCamera();
         } else {
@@ -630,22 +702,8 @@ public class UpdateActivity extends AppCompatActivity implements UpdateAdapter.U
 
                             int restul = json.getInt("result");
                             if (restul == RESULT_TRUE) {
-                                activity.mJson = json;
-                                JSONArray sectionArr = json.optJSONArray("sections");
-                                if (sectionArr != null) {
-
-                                    activity.mSectionArray.clear();
-                                    for (int i = 0; i < sectionArr.length(); i++) {
-
-                                        JSONObject section = sectionArr.getJSONObject(i);
-                                        UpdateSectionModel sectionModel = new UpdateSectionModel();
-                                        sectionModel.title = section.optString("title");
-                                        sectionModel.setItems(section.optJSONArray("items"));
 
-                                        activity.mSectionArray.add(sectionModel);
-                                    }
-
-                                }
+                                activity.handleJSON(json);
 
                             } else {
                                 // error
@@ -667,16 +725,8 @@ public class UpdateActivity extends AppCompatActivity implements UpdateAdapter.U
                         String errMsg = "Sorry,there is something wrong";
                         activity.showWarningMsg(errMsg);
                     }
-                    /**
-                     * 手动调用打开/关闭 Group,否则默认关闭
-                     * */
-                    if (activity.mSectionArray.size() > 0) {
-                        for (int i = 0; i < activity.mSectionArray.size(); i++) {
-                            activity.mListView.expandGroup(i);
-                        }
-                    }
 
-                    activity.mAdapter.notifyDataSetChanged();
+                    activity.changeData();
 
                 }
                 break;
@@ -687,6 +737,26 @@ public class UpdateActivity extends AppCompatActivity implements UpdateAdapter.U
     }
 
     /**
-     *
+     * Save
      * */
+
+    private JSONObject prepareSavedJson() {
+
+        JSONObject json = new JSONObject();
+        JSONArray sections = new JSONArray();
+        try {
+            for (int i = 0; i < mSectionArray.size(); i++) {
+
+                UpdateSectionModel sectionModel = mSectionArray.get(i);
+                JSONObject sectionJson = sectionModel.sectionModel2Json();
+                if (sectionJson != null) {
+                    sections.put(sectionJson);
+                }
+            }
+            json.put("sections",sections);
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+        return json;
+    }
 }

+ 28 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/Update/UpdateSectionModel.java

@@ -7,6 +7,7 @@ import com.usai.redant.apexdrivers.Update.Model.UpdateMultInputModel;
 import com.usai.redant.apexdrivers.Update.Model.UpdatePhotoModel;
 
 import org.json.JSONArray;
+import org.json.JSONException;
 import org.json.JSONObject;
 
 import java.util.ArrayList;
@@ -63,6 +64,7 @@ public class UpdateSectionModel {
                             model.type = type;
                             model.title = item.optString("title");
                             model.key = item.optString("key");
+                            model.setPhotoPath(item.optString("photoPath")); // 本地使用的,保存、恢复
                             this.items.add(model);
                         }
                         break;
@@ -85,4 +87,30 @@ public class UpdateSectionModel {
         return items.get(index);
     }
 
+    public JSONObject sectionModel2Json() {
+
+        JSONObject json = new JSONObject();
+
+        try {
+
+            if (title != null) {
+                json.put("title",title);
+            }
+            JSONArray items = new JSONArray();
+            for (int i = 0; i < itemCount(); i++) {
+                UpdateBaseModel model = itemModelForIndex(i);
+                JSONObject itemJson = model.model2Json();
+                if (itemJson != null) {
+                    items.put(itemJson);
+                }
+            }
+            json.put("items",items);
+
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+
+        return json;
+    }
+
 }

+ 24 - 0
ApexDrivers/app/src/main/res/values/themes.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright (C) 2012 ZXing authors
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+<resources>
+  <style name="CaptureTheme" parent="android:Theme.Holo">
+    <item name="android:windowFullscreen">true</item>
+    <item name="android:windowContentOverlay">@null</item>
+    <item name="android:windowActionBarOverlay">true</item>
+    <item name="android:windowActionModeOverlay">true</item>
+  </style>
+</resources>