OSDN Git Service

[VpnSettings]Crash when activity has been recycled
authorliurong <liurong@xiaomi.com>
Fri, 24 Mar 2017 12:11:48 +0000 (20:11 +0800)
committerRobin Lee <rgl@google.com>
Fri, 24 Mar 2017 15:21:23 +0000 (15:21 +0000)
This is a case we found in monkey test, the main thread handles message while the activity has been recycled. It will raise a NullPointerException when we use getActivity() in following lines.
Assumpt the situation is: settings run in background and activity been recycled quickly, but handler still in the process.

Test: NA

Change-Id: I19db4f13e13294618ec8e42e4d9c54ce23504344
Signed-off-by: liurong <liurong@xiaomi.com>
src/com/android/settings/vpn2/VpnSettings.java

index 0d968ab..29a67ab 100644 (file)
@@ -19,6 +19,7 @@ package com.android.settings.vpn2;
 import android.annotation.UiThread;
 import android.annotation.WorkerThread;
 import android.app.AppOpsManager;
+import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageInfo;
@@ -208,9 +209,16 @@ public class VpnSettings extends RestrictedSettingsFragment implements
     public boolean handleMessage(Message message) {
         mUpdater.removeMessages(RESCAN_MESSAGE);
 
+        //Return if activity has been recycled
+        final Activity activity = getActivity();
+        if (activity == null) {
+            return true;
+        }
+        final Context context = activity.getApplicationContext();
+
         // Run heavy RPCs before switching to UI thread
         final List<VpnProfile> vpnProfiles = loadVpnProfiles(mKeyStore);
-        final List<AppVpnInfo> vpnApps = getVpnApps(getActivity(), /* includeProfiles */ true);
+        final List<AppVpnInfo> vpnApps = getVpnApps(context, /* includeProfiles */ true);
 
         final Map<String, LegacyVpnInfo> connectedLegacyVpns = getConnectedLegacyVpns();
         final Set<AppVpnInfo> connectedAppVpns = getConnectedAppVpns();
@@ -219,7 +227,7 @@ public class VpnSettings extends RestrictedSettingsFragment implements
         final String lockdownVpnKey = VpnUtils.getLockdownVpn();
 
         // Refresh list of VPNs
-        getActivity().runOnUiThread(new Runnable() {
+        activity.runOnUiThread(new Runnable() {
             @Override
             public void run() {
                 // Can't do anything useful if the context has gone away
@@ -271,7 +279,6 @@ public class VpnSettings extends RestrictedSettingsFragment implements
                 }
             }
         });
-
         mUpdater.sendEmptyMessageDelayed(RESCAN_MESSAGE, RESCAN_INTERVAL_MS);
         return true;
     }