Bug 21589105 Rescope SYSTEM_ALERT_WINDOW (UX and Settings app change)...

Overall, fixed the detection of the state of permission in the corresponding UX
to be more accurate. Also ensured that apps can actually launch the summary UX
through a custom intent.

AndroidManifest:
Adds the proper intent-filter so that apps can launch the Settings page using
intent.

strings:
Made changes to strings so that wordings are uniform everywhere and raised the
char limit due to requests from translators.

Change-Id: Ia03403641ad53bd1a33b84dae6db1739cfcf9d60
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index e80856d..3b99168 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2537,6 +2537,8 @@
                 <action android:name="android.settings.action.MANAGE_OVERLAY_PERMISSION" />
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
+            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
+                android:value="com.android.settings.applications.ManageApplications" />
         </activity>
 
         <activity android:name="Settings$WriteSettingsActivity"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 5c57f83..5e9fd61 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -6849,8 +6849,8 @@
     <string name="accessibility_lock_screen_progress"><xliff:g id="count" example="1">%1$d</xliff:g> of <xliff:g id="count" example="1">%2$d</xliff:g> characters used</string>
 
     <!-- System Alert Window settings -->
-    <!-- Title of Draw Overlay preference item [CHAR LIMIT=30] -->
-    <string name="draw_overlay_title">Apps that can draw overlay</string>
+    <!-- Title of Draw Overlay preference item [CHAR LIMIT=45] -->
+    <string name="draw_overlay_title">Apps that can draw over other apps</string>
     <!-- Title of draw overlay screen [CHAR LIMIT=30] -->
     <string name="draw_overlay">Draw over other apps</string>
     <!-- Settings title in main settings screen for SYSTEM_ALERT_WINDOW [CHAR LIMIT=45] -->
@@ -6858,9 +6858,9 @@
     <!-- Title for the apps with SYSTEM_ALERT_WINDOW permission/privilege [CHAR LIMIT=20] -->
     <string name="system_alert_window_apps_title">Apps</string>
     <!-- Title for the apps that are allowed to draw on top of other apps [CHAR LIMIT=60] -->
-    <string name="system_alert_window_access_title">Can draw overlays</string>
+    <string name="system_alert_window_access_title">Draw over other apps</string>
     <!-- Label for setting which controls whether app can draw overlays [CHAR LIMIT=45] -->
-    <string name="permit_draw_overlay">Permit drawing overlays</string>
+    <string name="permit_draw_overlay">Permit drawing over other apps</string>
     <!-- Link to the apps page for SYSTEM_ALERT_WINDOW settings [CHAR LIMIT=45] -->
     <string name="app_overlay_permission_preference">App draw on top permission</string>
     <!-- Description of allowing overlay setting [CHAR LIMIT=NONE] -->
@@ -6868,8 +6868,8 @@
 
     <!-- Keyword for SYSTEM_ALERT_WINDOW -->
     <string name="keywords_system_alert_window">system alert window dialog draw on top other apps</string>
-    <!-- Main settings screen item's title to go into the overlay settings screen [CHAR LIMIT=25] -->
-    <string name="overlay_settings">Overlay settings</string>
+    <!-- Main settings screen item's title to go into the overlay settings screen [CHAR LIMIT=30] -->
+    <string name="overlay_settings">Draw over other apps</string>
 
     <!-- Summary of number of apps currently can draw overlays [CHAR LIMIT=60] -->
     <string name="system_alert_window_summary"><xliff:g id="count" example="10">%d</xliff:g> of <xliff:g id="count" example="10">%d</xliff:g> apps allowed to draw on top of other apps</string>
@@ -6894,9 +6894,9 @@
     <string name="filter_write_settings_apps">Can write system settings</string>
     <!-- Title for the apps that are allowed to write system settings [CHAR LIMIT=60] -->
     <string name="write_settings_title">Can write system settings</string>
-    <!-- Main settings screen item's title to go into the write system settings screen [CHAR LIMIT=25] -->
+    <!-- Main settings screen item's title to go into the write system settings screen [CHAR LIMIT=30] -->
     <string name="write_system_settings">Write system settings</string>
-    <!-- Link to the apps page for WRITE_SETTINGS settings [CHAR LIMIT=45] -->
+    <!-- Link to the apps page for WRITE_SETTINGS settings [CHAR LIMIT=52] -->
     <string name="write_settings_preference">App write system settings permission</string>
     <!-- Label for setting which controls whether app can write system settings [CHAR LIMIT=45] -->
     <string name="permit_write_settings">Allow write system settings</string>
diff --git a/src/com/android/settings/applications/AppStateOverlayBridge.java b/src/com/android/settings/applications/AppStateOverlayBridge.java
index 21586bc..75900a4 100644
--- a/src/com/android/settings/applications/AppStateOverlayBridge.java
+++ b/src/com/android/settings/applications/AppStateOverlayBridge.java
@@ -59,15 +59,13 @@
         return super.getNumPackagesAllowedByAppOps();
     }
 
-    public static class OverlayState {
-        PermissionState mPermissionState;
+    public static class OverlayState extends AppStateAppOpsBridge.PermissionState {
 
         public OverlayState(PermissionState permissionState) {
-            mPermissionState = permissionState;
-        }
-
-        public boolean isAllowed() {
-            return mPermissionState.isPermissible();
+            super(permissionState.packageName, permissionState.userHandle);
+            this.packageInfo = permissionState.packageInfo;
+            this.appOpMode = permissionState.appOpMode;
+            this.permissionDeclared = permissionState.permissionDeclared;
         }
     }
 
diff --git a/src/com/android/settings/applications/DrawOverlayDetails.java b/src/com/android/settings/applications/DrawOverlayDetails.java
index 078c2c5..4ea45b1 100644
--- a/src/com/android/settings/applications/DrawOverlayDetails.java
+++ b/src/com/android/settings/applications/DrawOverlayDetails.java
@@ -22,8 +22,8 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.preference.Preference;
@@ -36,6 +36,7 @@
 import com.android.internal.logging.MetricsLogger;
 import com.android.settings.InstrumentedFragment;
 import com.android.settings.R;
+import com.android.settings.applications.AppStateAppOpsBridge.PermissionState;
 import com.android.settings.applications.AppStateOverlayBridge.OverlayState;
 import com.android.settingslib.applications.ApplicationsState;
 import com.android.settingslib.applications.ApplicationsState.AppEntry;
@@ -99,7 +100,7 @@
                 try {
                     getActivity().startActivityAsUser(mSettingsIntent, new UserHandle(mUserId));
                 } catch (ActivityNotFoundException e) {
-                    Log.w(TAG, "Unable to launch app draw overlay settings " + mSettingsIntent, e);
+                    Log.w(LOG_TAG, "Unable to launch app draw overlay settings " + mSettingsIntent, e);
                 }
             }
             return true;
@@ -110,8 +111,8 @@
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         if (preference == mSwitchPref) {
-            if (mOverlayState != null && (Boolean) newValue != mOverlayState.isAllowed()) {
-                setCanDrawOverlay(!mOverlayState.isAllowed());
+            if (mOverlayState != null && (Boolean) newValue != mOverlayState.isPermissible()) {
+                setCanDrawOverlay(!mOverlayState.isPermissible());
                 refreshUi();
             }
             return true;
@@ -123,7 +124,6 @@
         mAppOpsManager.setMode(AppOpsManager.OP_SYSTEM_ALERT_WINDOW,
                 mPackageInfo.applicationInfo.uid, mPackageName, newState
                 ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED);
-        canDrawOverlay(mPackageName);
     }
 
     private boolean canDrawOverlay(String pkgName) {
@@ -141,17 +141,10 @@
         mOverlayState = mOverlayBridge.getOverlayInfo(mPackageName,
                 mPackageInfo.applicationInfo.uid);
 
-        boolean isAllowed = mOverlayState.isAllowed();
+        boolean isAllowed = mOverlayState.isPermissible();
         mSwitchPref.setChecked(isAllowed);
         mOverlayPrefs.setEnabled(isAllowed);
-
-        ResolveInfo resolveInfo = mPm.resolveActivityAsUser(mSettingsIntent,
-                PackageManager.GET_META_DATA, mUserId);
-        if (resolveInfo == null) {
-            if (findPreference(KEY_APP_OPS_SETTINGS_PREFS) != null) {
-                getPreferenceScreen().removePreference(mOverlayPrefs);
-            }
-        }
+        getPreferenceScreen().removePreference(mOverlayPrefs);
 
         return true;
     }
@@ -167,40 +160,38 @@
     }
 
     public static CharSequence getSummary(Context context, AppEntry entry) {
+        if (entry.extraInfo != null) {
+            return getSummary(context, new OverlayState((PermissionState)entry.extraInfo));
+        }
+
+        // fallback if for whatever reason entry.extrainfo is null - the result
+        // may be less accurate
         return getSummary(context, entry.info.packageName);
     }
 
+    public static CharSequence getSummary(Context context, OverlayState overlayState) {
+        return context.getString(overlayState.isPermissible() ?
+            R.string.system_alert_window_on : R.string.system_alert_window_off);
+    }
+
     public static CharSequence getSummary(Context context, String pkg) {
         // first check if pkg is a system pkg
-        boolean isSystem = false;
         PackageManager packageManager = context.getPackageManager();
+        int uid = -1;
         try {
             ApplicationInfo appInfo = packageManager.getApplicationInfo(pkg, 0);
+            uid = appInfo.uid;
             if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
-                isSystem = true;
+                return context.getString(R.string.system_alert_window_on);
             }
         } catch (PackageManager.NameNotFoundException e) {
             // pkg doesn't even exist?
-            Log.w(TAG, "Package " + pkg + " not found", e);
+            Log.w(LOG_TAG, "Package " + pkg + " not found", e);
             return context.getString(R.string.system_alert_window_off);
         }
 
         AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context
                 .APP_OPS_SERVICE);
-        List<AppOpsManager.PackageOps> packageOps = appOpsManager.getPackagesForOps(
-                APP_OPS_OP_CODE);
-        if (packageOps == null) {
-            return context.getString(R.string.system_alert_window_off);
-        }
-
-        int uid = isSystem ? 0 : -1;
-        for (AppOpsManager.PackageOps packageOp : packageOps) {
-            if (pkg.equals(packageOp.getPackageName())) {
-                uid = packageOp.getUid();
-                break;
-            }
-        }
-
         if (uid == -1) {
             return context.getString(R.string.system_alert_window_off);
         }
diff --git a/src/com/android/settings/applications/ManageApplications.java b/src/com/android/settings/applications/ManageApplications.java
index e4b404f..3820123 100644
--- a/src/com/android/settings/applications/ManageApplications.java
+++ b/src/com/android/settings/applications/ManageApplications.java
@@ -1086,8 +1086,7 @@
                     break;
 
                 case LIST_TYPE_OVERLAY:
-                    holder.summary.setText(DrawOverlayDetails.getSummary(mContext,
-                            holder.entry));
+                    holder.summary.setText(DrawOverlayDetails.getSummary(mContext, holder.entry));
                     break;
 
                 case LIST_TYPE_WRITE_SETTINGS: