Stop apps with Storage Managers from launching activity when disabled

An app with a storage manager would be able to launch Manage Storage
from application settings, but when the app was disabled, the
activity would be null.

Test: In order to properly unit test this function, a large redesign is 
needed in this class, and is outside the scope of this bug.

Change-Id: Ia6b78bc6761bac8b701c904a6e438bbd951b63e8
Fixes: 30457616
(cherry picked from commit 8f17fcaa1dd85c816916ffbed3f66ad80c2bbf0a)
diff --git a/src/com/android/settings/applications/AppStorageSettings.java b/src/com/android/settings/applications/AppStorageSettings.java
index 357ec1e..7c7d5a5 100644
--- a/src/com/android/settings/applications/AppStorageSettings.java
+++ b/src/com/android/settings/applications/AppStorageSettings.java
@@ -60,6 +60,9 @@
 import java.util.Objects;
 import java.util.TreeMap;
 
+import static android.content.pm.ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA;
+import static android.content.pm.ApplicationInfo.FLAG_SYSTEM;
+
 public class AppStorageSettings extends AppInfoWithHeader
         implements OnClickListener, Callbacks, DialogInterface.OnClickListener {
     private static final String TAG = AppStorageSettings.class.getSimpleName();
@@ -354,19 +357,27 @@
     }
 
     private void initDataButtons() {
-        // If the app doesn't have its own space management UI
-        // And it's a system app that doesn't allow clearing user data or is an active admin
-        // Then disable the Clear Data button.
-        if (mAppEntry.info.manageSpaceActivityName == null
-                && ((mAppEntry.info.flags&(ApplicationInfo.FLAG_SYSTEM
-                        | ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA))
-                        == ApplicationInfo.FLAG_SYSTEM
-                        || mDpm.packageHasActiveAdmins(mPackageName))) {
+        final boolean appHasSpaceManagementUI = mAppEntry.info.manageSpaceActivityName != null;
+        final boolean appHasActiveAdmins = mDpm.packageHasActiveAdmins(mPackageName);
+        // Check that SYSTEM_APP flag is set, and ALLOW_CLEAR_USER_DATA is not set.
+        final boolean isNonClearableSystemApp =
+                (mAppEntry.info.flags & (FLAG_SYSTEM | FLAG_ALLOW_CLEAR_USER_DATA)) == FLAG_SYSTEM;
+        final boolean appRestrictsClearingData = isNonClearableSystemApp || appHasActiveAdmins;
+
+        final Intent intent = new Intent(Intent.ACTION_DEFAULT);
+        if (appHasSpaceManagementUI) {
+            intent.setClassName(mAppEntry.info.packageName, mAppEntry.info.manageSpaceActivityName);
+        }
+        final boolean isManageSpaceActivityAvailable =
+                getPackageManager().resolveActivity(intent, 0) != null;
+
+        if ((!appHasSpaceManagementUI && appRestrictsClearingData)
+                || !isManageSpaceActivityAvailable) {
             mClearDataButton.setText(R.string.clear_user_data_text);
             mClearDataButton.setEnabled(false);
             mCanClearData = false;
         } else {
-            if (mAppEntry.info.manageSpaceActivityName != null) {
+            if (appHasSpaceManagementUI) {
                 mClearDataButton.setText(R.string.manage_space_text);
             } else {
                 mClearDataButton.setText(R.string.clear_user_data_text);