Bug 21589105 Rescope WRITE_SETTINGS (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 needing this capability can
launch the summary UX through a specific intent.

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

strings.xml:
Increased the CHAR limits for some strings due to requests from translators.

Change-Id: Ie64f86e034867ed582c1c583a5e8f84671fa4d63
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 3b99168..1fef233 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2548,6 +2548,8 @@
                 <action android:name="android.settings.action.MANAGE_WRITE_SETTINGS" />
                 <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>
 
     </application>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 72af8b9..b8a3401 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -6868,7 +6868,7 @@
     <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=45] -->
+    <!-- Title of Draw Overlay preference item [CHAR LIMIT=55] -->
     <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>
diff --git a/src/com/android/settings/applications/AppStateWriteSettingsBridge.java b/src/com/android/settings/applications/AppStateWriteSettingsBridge.java
index 4ab737f..e43b5f5 100644
--- a/src/com/android/settings/applications/AppStateWriteSettingsBridge.java
+++ b/src/com/android/settings/applications/AppStateWriteSettingsBridge.java
@@ -60,15 +60,12 @@
         return super.getNumPackagesAllowedByAppOps();
     }
 
-    public static class WriteSettingsState {
-        PermissionState mPermissionState;
-
+    public static class WriteSettingsState extends AppStateAppOpsBridge.PermissionState {
         public WriteSettingsState(PermissionState permissionState) {
-            mPermissionState = permissionState;
-        }
-
-        public boolean canWrite() {
-            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/WriteSettingsDetails.java b/src/com/android/settings/applications/WriteSettingsDetails.java
index eeee90c..30533c6 100644
--- a/src/com/android/settings/applications/WriteSettingsDetails.java
+++ b/src/com/android/settings/applications/WriteSettingsDetails.java
@@ -23,7 +23,6 @@
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.preference.Preference;
@@ -36,6 +35,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.AppStateWriteSettingsBridge.WriteSettingsState;
 import com.android.settingslib.applications.ApplicationsState;
 import com.android.settingslib.applications.ApplicationsState.AppEntry;
@@ -98,7 +98,7 @@
                 try {
                     getActivity().startActivityAsUser(mSettingsIntent, new UserHandle(mUserId));
                 } catch (ActivityNotFoundException e) {
-                    Log.w(TAG, "Unable to launch write system settings " + mSettingsIntent, e);
+                    Log.w(LOG_TAG, "Unable to launch write system settings " + mSettingsIntent, e);
                 }
             }
             return true;
@@ -109,8 +109,9 @@
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         if (preference == mSwitchPref) {
-            if (mWriteSettingsState != null && (Boolean) newValue != mWriteSettingsState.canWrite()) {
-                setCanWriteSettings(!mWriteSettingsState.canWrite());
+            if (mWriteSettingsState != null && (Boolean) newValue != mWriteSettingsState
+                    .isPermissible()) {
+                setCanWriteSettings(!mWriteSettingsState.isPermissible());
                 refreshUi();
             }
             return true;
@@ -122,7 +123,6 @@
         mAppOpsManager.setMode(AppOpsManager.OP_WRITE_SETTINGS,
                 mPackageInfo.applicationInfo.uid, mPackageName, newState
                 ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED);
-        canWriteSettings(mPackageName);
     }
 
     private boolean canWriteSettings(String pkgName) {
@@ -140,17 +140,10 @@
         mWriteSettingsState = mAppBridge.getWriteSettingsInfo(mPackageName,
                 mPackageInfo.applicationInfo.uid);
 
-        boolean canWrite = mWriteSettingsState.canWrite();
+        boolean canWrite = mWriteSettingsState.isPermissible();
         mSwitchPref.setChecked(canWrite);
         mWriteSettingsPrefs.setEnabled(canWrite);
-
-        ResolveInfo resolveInfo = mPm.resolveActivityAsUser(mSettingsIntent,
-                PackageManager.GET_META_DATA, mUserId);
-        if (resolveInfo == null) {
-            if (findPreference(KEY_APP_OPS_SETTINGS_PREFS) != null) {
-                getPreferenceScreen().removePreference(mWriteSettingsPrefs);
-            }
-        }
+        getPreferenceScreen().removePreference(mWriteSettingsPrefs);
 
         return true;
     }
@@ -166,9 +159,20 @@
     }
 
     public static CharSequence getSummary(Context context, AppEntry entry) {
+        if (entry.extraInfo != null) {
+            return getSummary(context, new WriteSettingsState((PermissionState)entry
+                    .extraInfo));
+        }
+
+        // fallback if entry.extrainfo is null - although this should not happen
         return getSummary(context, entry.info.packageName);
     }
 
+    public static CharSequence getSummary(Context context, WriteSettingsState writeSettingsState) {
+        return context.getString(writeSettingsState.isPermissible() ? R.string.write_settings_on :
+                R.string.write_settings_off);
+    }
+
     public static CharSequence getSummary(Context context, String pkg) {
         // first check if pkg is a system pkg
         boolean isSystem = false;
@@ -180,8 +184,8 @@
             }
         } catch (PackageManager.NameNotFoundException e) {
             // pkg doesn't even exist?
-            Log.w(TAG, "Package " + pkg + " not found", e);
-            return context.getString(R.string.system_alert_window_off);
+            Log.w(LOG_TAG, "Package " + pkg + " not found", e);
+            return context.getString(R.string.write_settings_off);
         }
 
         AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context
@@ -189,7 +193,7 @@
         List<AppOpsManager.PackageOps> packageOps = appOpsManager.getPackagesForOps(
                 APP_OPS_OP_CODE);
         if (packageOps == null) {
-            return context.getString(R.string.system_alert_window_off);
+            return context.getString(R.string.write_settings_off);
         }
 
         int uid = isSystem ? 0 : -1;
@@ -201,7 +205,7 @@
         }
 
         if (uid == -1) {
-            return context.getString(R.string.system_alert_window_off);
+            return context.getString(R.string.write_settings_off);
         }
 
         int mode = appOpsManager.noteOpNoThrow(AppOpsManager.OP_WRITE_SETTINGS, uid, pkg);