OSDN Git Service

Handle rotation (or refresh) before callback is received
authorJason Monk <jmonk@google.com>
Mon, 29 Jun 2015 18:42:25 +0000 (14:42 -0400)
committerJason Monk <jmonk@google.com>
Mon, 29 Jun 2015 18:43:19 +0000 (14:43 -0400)
Make sure we unregister the old receiver

Bug: 22160603
Change-Id: I798ca07dd65711caa4d3295b7ad811ca2fc69c1d

src/com/android/settings/applications/AdvancedAppSettings.java
src/com/android/settings/applications/InstalledAppDetails.java
src/com/android/settings/applications/PermissionsSummaryHelper.java

index 47a0098..7df269e 100644 (file)
@@ -15,6 +15,7 @@
  */
 package com.android.settings.applications;
 
+import android.content.BroadcastReceiver;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.os.Bundle;
@@ -45,6 +46,8 @@ public class AdvancedAppSettings extends SettingsPreferenceFragment implements
     private Preference mAppDomainURLsPreference;
     private Preference mHighPowerPreference;
 
+    private BroadcastReceiver mPermissionReceiver;
+
     @Override
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -63,6 +66,15 @@ public class AdvancedAppSettings extends SettingsPreferenceFragment implements
         updateUI();
     }
 
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        if (mPermissionReceiver != null) {
+            getContext().unregisterReceiver(mPermissionReceiver);
+            mPermissionReceiver = null;
+        }
+    }
+
     private void updateUI() {
         ArrayList<AppEntry> allApps = mSession.getAllApps();
 
@@ -79,7 +91,12 @@ public class AdvancedAppSettings extends SettingsPreferenceFragment implements
         int highPowerCount = PowerWhitelistBackend.getInstance().getWhitelistSize();
         mHighPowerPreference.setSummary(getResources().getQuantityString(R.plurals.high_power_count,
                 highPowerCount, highPowerCount));
-        PermissionsSummaryHelper.getAppWithPermissionsCounts(getContext(), mPermissionCallback);
+
+        if (mPermissionReceiver != null) {
+            getContext().unregisterReceiver(mPermissionReceiver);
+        }
+        mPermissionReceiver = PermissionsSummaryHelper.getAppWithPermissionsCounts(getContext(),
+                mPermissionCallback);
     }
 
     @Override
@@ -133,6 +150,7 @@ public class AdvancedAppSettings extends SettingsPreferenceFragment implements
             if (getActivity() == null) {
                 return;
             }
+            mPermissionReceiver = null;
             if (counts != null) {
                 mAppPermsPreference.setSummary(getContext().getString(
                         R.string.app_permissions_summary, counts[0], counts[1]));
index a275743..842660b 100755 (executable)
@@ -157,6 +157,8 @@ public class InstalledAppDetails extends AppInfoBase
     protected ProcStatsData mStatsManager;
     protected ProcStatsPackageEntry mStats;
 
+    private BroadcastReceiver mPermissionReceiver;
+
     private boolean handleDisableable(Button button) {
         boolean disableable = false;
         // Try to prevent the user from bricking their phone
@@ -312,6 +314,10 @@ public class InstalledAppDetails extends AppInfoBase
     @Override
     public void onDestroy() {
         TrafficStats.closeQuietly(mStatsSession);
+        if (mPermissionReceiver != null) {
+            getContext().unregisterReceiver(mPermissionReceiver);
+            mPermissionReceiver = null;
+        }
 
         super.onDestroy();
     }
@@ -489,8 +495,11 @@ public class InstalledAppDetails extends AppInfoBase
         // Update the preference summaries.
         Activity context = getActivity();
         mStoragePreference.setSummary(AppStorageSettings.getSummary(mAppEntry, context));
-        PermissionsSummaryHelper.getPermissionSummary(getContext(), mPackageName,
-                mPermissionCallback);
+        if (mPermissionReceiver != null) {
+            getContext().unregisterReceiver(mPermissionReceiver);
+        }
+        mPermissionReceiver = PermissionsSummaryHelper.getPermissionSummary(getContext(),
+                mPackageName, mPermissionCallback);
         mLaunchPreference.setSummary(Utils.getLaunchByDeafaultSummary(mAppEntry, mUsbManager,
                 mPm, context));
         mNotificationPreference.setSummary(getNotificationSummary(mAppEntry, context,
@@ -941,6 +950,10 @@ public class InstalledAppDetails extends AppInfoBase
             = new PermissionsResultCallback() {
         @Override
         public void onPermissionSummaryResult(int[] counts, CharSequence[] groupLabels) {
+            if (getActivity() == null) {
+                return;
+            }
+            mPermissionReceiver = null;
             final Resources res = getResources();
             CharSequence summary = null;
             boolean enabled = false;
index 1b384ea..859e446 100644 (file)
@@ -27,21 +27,21 @@ public class PermissionsSummaryHelper {
     private static final String ACTION_APP_COUNT_RESPONSE
             = "com.android.settings.APP_COUNT_RESPONSE";
 
-    public static void getPermissionSummary(Context context, String pkg,
+    public static BroadcastReceiver getPermissionSummary(Context context, String pkg,
             PermissionsResultCallback callback) {
         Intent request = new Intent(Intent.ACTION_GET_PERMISSIONS_COUNT);
         request.putExtra(Intent.EXTRA_PACKAGE_NAME, pkg);
-        sendPermissionRequest(context, ACTION_PERM_COUNT_RESPONSE, request, callback);
+        return sendPermissionRequest(context, ACTION_PERM_COUNT_RESPONSE, request, callback);
     }
 
-    public static void getAppWithPermissionsCounts(Context context,
+    public static BroadcastReceiver getAppWithPermissionsCounts(Context context,
             PermissionsResultCallback callback) {
         Intent request = new Intent(Intent.ACTION_GET_PERMISSIONS_COUNT);
-        sendPermissionRequest(context, ACTION_APP_COUNT_RESPONSE, request, callback);
+        return sendPermissionRequest(context, ACTION_APP_COUNT_RESPONSE, request, callback);
     }
 
-    private static void sendPermissionRequest(Context context, String action, Intent request,
-            final PermissionsResultCallback callback) {
+    private static BroadcastReceiver sendPermissionRequest(Context context, String action,
+            Intent request, final PermissionsResultCallback callback) {
         BroadcastReceiver receiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
@@ -59,6 +59,7 @@ public class PermissionsSummaryHelper {
         request.putExtra(Intent.EXTRA_GET_PERMISSIONS_RESPONSE_INTENT, action);
         request.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
         context.sendBroadcast(request);
+        return receiver;
     }
 
     public interface PermissionsResultCallback {