OSDN Git Service

Make UsageAccess WarningDialog actually instatiatable.
authorAdam Lesinski <adamlesinski@google.com>
Tue, 9 Sep 2014 18:34:55 +0000 (11:34 -0700)
committerAdam Lesinski <adamlesinski@google.com>
Tue, 9 Sep 2014 18:34:55 +0000 (11:34 -0700)
The DialogFragment used for WarningDialog was not instatiatable
by the fragment manager.

Bug:17435234
Change-Id: I5f65dd1e0ec0b95d048934480bd5fafd8648ab7c

src/com/android/settings/UsageAccessSettings.java

index 8d0650b..89e184e 100644 (file)
@@ -24,6 +24,8 @@ import android.app.AlertDialog;
 import android.app.AppOpsManager;
 import android.app.Dialog;
 import android.app.DialogFragment;
+import android.app.Fragment;
+import android.app.FragmentTransaction;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.pm.IPackageManager;
@@ -316,9 +318,12 @@ public class UsageAccessSettings extends SettingsPreferenceFragment implements
             }
 
             // Turning on the setting has a Warning.
-            getFragmentManager().beginTransaction()
-                    .add(new WarningDialog(pe), "warning")
-                    .commit();
+            FragmentTransaction ft = getChildFragmentManager().beginTransaction();
+            Fragment prev = getChildFragmentManager().findFragmentByTag("warning");
+            if (prev != null) {
+                ft.remove(prev);
+            }
+            WarningDialogFragment.newInstance(pe.packageName).show(ft, "warning");
             return false;
         }
         return true;
@@ -330,6 +335,17 @@ public class UsageAccessSettings extends SettingsPreferenceFragment implements
         pe.appOpMode = newMode;
     }
 
+    void allowAccess(String packageName) {
+        final PackageEntry entry = mPackageEntryMap.get(packageName);
+        if (entry == null) {
+            Log.w(TAG, "Unable to give access to package " + packageName + ": it does not exist.");
+            return;
+        }
+
+        setNewMode(entry, AppOpsManager.MODE_ALLOWED);
+        entry.preference.setChecked(true);
+    }
+
     private final PackageMonitor mPackageMonitor = new PackageMonitor() {
         @Override
         public void onPackageAdded(String packageName, int uid) {
@@ -342,12 +358,16 @@ public class UsageAccessSettings extends SettingsPreferenceFragment implements
         }
     };
 
-    private class WarningDialog extends DialogFragment
+    public static class WarningDialogFragment extends DialogFragment
             implements DialogInterface.OnClickListener {
-        private final PackageEntry mEntry;
-
-        public WarningDialog(PackageEntry pe) {
-            mEntry = pe;
+        private static final String ARG_PACKAGE_NAME = "package";
+
+        public static WarningDialogFragment newInstance(String packageName) {
+            WarningDialogFragment dialog = new WarningDialogFragment();
+            Bundle args = new Bundle();
+            args.putString(ARG_PACKAGE_NAME, packageName);
+            dialog.setArguments(args);
+            return dialog;
         }
 
         @Override
@@ -364,8 +384,8 @@ public class UsageAccessSettings extends SettingsPreferenceFragment implements
         @Override
         public void onClick(DialogInterface dialog, int which) {
             if (which == DialogInterface.BUTTON_POSITIVE) {
-                setNewMode(mEntry, AppOpsManager.MODE_ALLOWED);
-                mEntry.preference.setChecked(true);
+                ((UsageAccessSettings) getParentFragment()).allowAccess(
+                        getArguments().getString(ARG_PACKAGE_NAME));
             } else {
                 dialog.cancel();
             }