Просмотр исходного кода

1.修改Android Apex Drivers Token过期处理。
2.修改Android Apex Drivers首页重复显示。
3修改Android Apex Drivers收到通知后刷新首页。

Pen Li 7 лет назад
Родитель
Сommit
260f471dfd

+ 2 - 0
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/utils/Network.java

@@ -50,6 +50,7 @@ public class Network {
     public static int			AP_UPLOAD_FAIL					= 5;
 
 
+
     public static final int		RESULT_FALSE					= 0;
     public static final int		RESULT_TRUE						= 2;
     public static final int		RESULT_NET_ERROR				= -3;
@@ -60,6 +61,7 @@ public class Network {
     public static final int		RESULT_UPDATE_USERAUTH_ERROR	= -11;
     public static final int		RESULT_SESSION_EXPIRED			= -13;
     public static final int		RESULT_VER_LOW					= -15;
+    public static int			RESULT_AUTH_EXPIRED				= 99;
 
     public static String getJson(String url, Bundle parms,int timeout)
     {

+ 6 - 0
ApexDrivers/apexdriverscn/src/main/java/com/usai/apex/apexdriverscn/ApplicationCN.java

@@ -12,4 +12,10 @@ public class ApplicationCN extends ApexDriverApplication {
 
         super.onCreate();
     }
+
+    @Override
+    public Class getMainActivityClass() {
+
+        return MainActivityCN.class;
+    }
 }

+ 4 - 0
ApexDrivers/apexdriversi/src/main/java/com/usai/apex/apexdriversi/ApplicationI.java

@@ -12,4 +12,8 @@ public class ApplicationI extends ApexDriverApplication {
         CHANNEL_NAME = "Apex & Drivers I";
     }
 
+    @Override
+    public Class getMainActivityClass() {
+        return MainActivityI.class;
+    }
 }

+ 73 - 1
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/ApexDriverApplication.java

@@ -58,6 +58,7 @@ public class ApexDriverApplication extends Application {
     private String firstName;
 
     private volatile long notificationID;
+    private boolean authExpired = false;
 
     public boolean isbackground=true;
 
@@ -119,6 +120,10 @@ public class ApexDriverApplication extends Application {
                 bintent.setClass(ApexDriverApplication.this, ApexDriverAlarmReceiver.class);
 //        bintent.putExtra("msg", msg.toString());
                 sendBroadcast(bintent);
+
+                if (authExpired) {
+                    showAuthoExpiredAlert(activity);
+                }
             }
 //            isbackground = false;
         }
@@ -519,6 +524,7 @@ public class ApexDriverApplication extends Application {
             editor.commit();
         }
 
+        authExpired = false;
         setShouldAutoLogin(false);
     }
 
@@ -674,6 +680,68 @@ public class ApexDriverApplication extends Application {
         }
     }
 
+    /**
+     * 自动登出提示的情况
+     * 1. 当前在前台,存在Activity
+     *  1.1 如果Activity是MainActivity,直接登出
+     *  1.2 如果Activity不是MainActivity,返回到MainActivity,并且在MainActivity.OnResume时登出
+     *
+     * 2. 当前在后台,没有Activity,则在LifeCycleCallback的回掉中,OnResume通过Activity提示登出
+     *  2.1 如果Activity是MainActivity,直接登出
+     *  2.2 如果Activity不是MainActivity,返回到MainActivity,并且在MainActivity.OnResume时登出
+     *
+     * */
+    public void authorizationExpired() {
+        new Handler(Looper.getMainLooper()).post(new Runnable() {
+            @Override
+            public void run() {
+                authExpired = true;
+                if (mCurActivity != null && !isbackground) {
+                    showAuthoExpiredAlert(mCurActivity);
+                }
+            }
+        });
+    }
+
+    private void showAuthoExpiredAlert(final Activity activity) {
+        if (activity != null && authExpired) {
+
+            new AlertDialog.Builder(activity)
+                    .setTitle(getString(R.string.warning))
+                    .setTitle(getString(R.string.auth_expired_msg))
+                    .setPositiveButton(getString(R.string.btn_ok), new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            autoLogout(activity);
+                        }
+                    })
+                    .setCancelable(false)
+                    .show();
+
+        }
+    }
+
+    private void autoLogout(Activity activity) {
+        if (activity != null) {
+
+            if (activity instanceof MainActivity) {
+                ((MainActivity) activity).logout();
+            } else {
+
+                // 在MainActivity.onResume自动登出
+                Intent intent = new Intent(activity, getMainActivityClass());
+                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+                activity.startActivity(intent);
+
+            }
+
+        }
+    }
+
+    public boolean isAuthExpired() {
+        return authExpired;
+    }
+
     public void receiveNotificationOnMainThread(final JSONObject notification) {
         new Handler(Looper.getMainLooper()).post(new Runnable() {
             @Override
@@ -764,6 +832,10 @@ public class ApexDriverApplication extends Application {
 
     }
 
+    public Class getMainActivityClass() {
+        return MainActivity.class;
+    }
+
     public void popLocalNotification(JSONObject notification) {
 
         if (notification == null) {
@@ -794,7 +866,7 @@ public class ApexDriverApplication extends Application {
 //
 //        Intent intent = DetailActivity.build(getApplicationContext(),orderID,orderType,orderType2,statusNo);
 //        intent.putExtra("goHome",true);
-        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
+        Intent intent = new Intent(getApplicationContext(), getMainActivityClass());
         intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); // getIntent可能是null
         intent.putExtra("aps",aps.toString()); // 程序在后台的情况下,点击通知将程序唤醒到前台时,并不能取得extra
 

+ 18 - 2
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/MainActivity.java

@@ -181,7 +181,7 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
         checkPermissions();
 
         checkNotificationEnable();
-        checkNotification();
+//        checkNotification();
 
         IntentFilter intentFilter = new IntentFilter();
         intentFilter.addAction(APEX_DRIVER_UPLOAD_COUNT_CHANGE_NOTIFICATION);
@@ -208,6 +208,13 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
     protected void onResume() {
         super.onResume();
 
+        if (ApexDriverApplication.sharedApplication().isLogin() && ApexDriverApplication.sharedApplication().isAuthExpired()) {
+
+            logout();
+
+            return;
+        }
+
         checkNotification();
     }
 
@@ -219,6 +226,7 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
         if (ApexDriverApplication.sharedApplication().isLogin()) {
 
             Intent intent = getIntent();
+            setIntent(null);
             String apsStr = null;
             boolean is_alert = false;
             // 从Intent获取aps
@@ -232,6 +240,8 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
             // 若aps不为空,则程序是从点击通知进入的
             if (!TextUtils.isEmpty(apsStr)) {
 
+                sendBroadcast(new Intent(HomeFragment.HomeReloadBroadcastAction)); // 刷新Home
+
                 intent.removeExtra("aps");
 
                 try {
@@ -265,6 +275,7 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
 //
 //            }
             if (notifications.length > 0) {
+
                 StatusBarNotification statusBarNotification = notifications[0];
                 Notification notification = statusBarNotification.getNotification();
                 if (notification != null) {
@@ -273,6 +284,7 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
                         immutIntent.putExtra("is_alert",true);
                         PendingIntent contentIntent = notification.contentIntent;
                        if (contentIntent != null) {
+                           sendBroadcast(new Intent(HomeFragment.HomeReloadBroadcastAction)); // 刷新Home
                            contentIntent.send(this,statusBarNotification.getId(),immutIntent);
                        }
                     } catch (Exception e) {
@@ -470,7 +482,7 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
 
         finish();
 
-        startActivity(new Intent(this,MainActivity.class));
+        startActivity(new Intent(this,ApexDriverApplication.sharedApplication().getMainActivityClass()));
 
     }
 
@@ -686,6 +698,10 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
 
     public void logout() {
 
+        if (!ApexDriverApplication.sharedApplication().isLogin()) {
+            return;
+        }
+
         showProgressDialog();
         new Thread(new Runnable() {
             @Override

+ 1 - 1
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/detail/DetailActivity.java

@@ -302,7 +302,7 @@ public class DetailActivity extends BasicActivity implements DetailAdapter.Detai
     }
 
     private void goHome() {
-        Intent intent = new Intent(self, MainActivity.class);
+        Intent intent = new Intent(self, ApexDriverApplication.sharedApplication().getMainActivityClass());
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         self.startActivity(intent);
 

+ 5 - 3
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/home/HomeFragment.java

@@ -802,10 +802,12 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
             }
             ((HomeCellLayout) cell).setIsMore(false);
 
-            HomeSectionModel sectionModel = mSectionArray.get(groupPosition);
-            HomeOrderModel orderModel = sectionModel.orderModelForIndex(childPosition);
+            if (groupPosition < mSectionArray.size()) {
+                HomeSectionModel sectionModel = mSectionArray.get(groupPosition);
+                HomeOrderModel orderModel = sectionModel.orderModelForIndex(childPosition);
 
-            holder.bindOrderModel(orderModel);
+                holder.bindOrderModel(orderModel);
+            }
 
             return cell;
         }

+ 2 - 4
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/network/Network.java

@@ -42,7 +42,7 @@ public class Network extends com.usai.redant.rautils.utils.Network {
     public static final String URL_CHANGE_PASSWORD = URL_HOST + "/mobile/changePassword.mo/";
     public static final String URL_MESSAGE_LIST = URL_HOST + "/mobile/moreDriverMessages.mo/";
     public static final String URL_UPLOAD_TOKEN = URL_HOST + "/mobile/uploadToken.mo/";
-    public static final String URL_PULL_NOTIFICATION = URL_HOST + "/mobile/pullNotification.mo/";
+    public static final String URL_PULL_NOTIFICATION = URL_HOST + "/mobile/getAndroidDriverNotifications.mo/";
 
     public static final int PUSH_TIME_OUT_INTERVAL = 5 * 1000;
 
@@ -487,9 +487,7 @@ public class Network extends com.usai.redant.rautils.utils.Network {
 
         prepareParams(params);
 
-        String url = "http://192.168.0.130:8080/MyWeb/Test";
-//        String url = URL_UPLOAD_TOKEN;
-        String jsonStr = getJSON(url,params,PUSH_TIME_OUT_INTERVAL);
+        String jsonStr = getJSON(URL_PULL_NOTIFICATION,params,PUSH_TIME_OUT_INTERVAL);
 
         return handleJson(jsonStr);
     }

+ 8 - 1
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/receiver/ApexDriverAlarmReceiver.java

@@ -18,6 +18,7 @@ import com.usai.redant.rautils.utils.RAUtil;
 import org.json.JSONArray;
 import org.json.JSONObject;
 
+import static com.usai.redant.rautils.utils.Network.RESULT_AUTH_EXPIRED;
 import static com.usai.redant.rautils.utils.Network.RESULT_TRUE;
 
 public class ApexDriverAlarmReceiver extends AlarmReceiver {
@@ -159,7 +160,7 @@ public class ApexDriverAlarmReceiver extends AlarmReceiver {
             @Override
             public void run() {
 
-                if (ApexDriverApplication.sharedApplication().isLogin()) {
+                if (ApexDriverApplication.sharedApplication().isLogin() && !ApexDriverApplication.sharedApplication().isAuthExpired()) {
 
                     JSONObject json = com.usai.redant.apexdrivers.network.Network.pullNotification();
 
@@ -168,6 +169,9 @@ public class ApexDriverAlarmReceiver extends AlarmReceiver {
                         int result = json.optInt("result",0);
                         if (result == RESULT_TRUE) {
 
+                            long notificationId = json.optLong("notificationId");
+                            ApexDriverApplication.sharedApplication().setNotificationID(notificationId);
+
                             JSONArray notifications = json.optJSONArray("notifications");
                             if (notifications != null) {
 
@@ -180,6 +184,9 @@ public class ApexDriverAlarmReceiver extends AlarmReceiver {
 
                             }
 
+                        } else if (result == RESULT_AUTH_EXPIRED) {
+                            // Token 过期,提示重新登陆
+                            ApexDriverApplication.sharedApplication().authorizationExpired();
                         }
 
                     }

+ 1 - 1
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/update/UpdateActivity.java

@@ -980,7 +980,7 @@ public class UpdateActivity extends BasicActivity implements UpdateAdapter.Updat
     }
 
     private void goHome() {
-        Intent intent = new Intent(self, MainActivity.class);
+        Intent intent = new Intent(self, ApexDriverApplication.sharedApplication().getMainActivityClass());
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         self.startActivity(intent);