OSDN Git Service

Add an util method and update setMenuItemAsDisabledByAdmin.
authorSudheer Shanka <sudheersai@google.com>
Fri, 15 Jan 2016 11:39:04 +0000 (11:39 +0000)
committerSudheer Shanka <sudheersai@google.com>
Fri, 15 Jan 2016 15:47:19 +0000 (15:47 +0000)
Added an utility method checkIfUninstallBlocked in
RestrictedLockUtils. Also updated setMenuItemAsDisabledByAdmin
to be able to remove the added padlock and color spans.

Change-Id: I7874f7097d09bc9ab127307f2a8a03a20560347c

packages/SettingsLib/src/com/android/settingslib/RestrictedLockUtils.java

index 2706e25..1796786 100644 (file)
 
 package com.android.settingslib;
 
+import android.app.AppGlobals;
 import android.app.admin.DevicePolicyManager;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.IPackageManager;
 import android.graphics.Color;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
+import android.os.RemoteException;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.provider.Settings;
 import android.text.Spanned;
 import android.text.SpannableStringBuilder;
@@ -147,30 +151,65 @@ public class RestrictedLockUtils {
         return enforcedAdmin;
     }
 
+    public static EnforcedAdmin checkIfUninstallBlocked(Context context,
+            String packageName, int userId) {
+        EnforcedAdmin allAppsControlDisallowedAdmin = checkIfRestrictionEnforced(context,
+                UserManager.DISALLOW_APPS_CONTROL, userId);
+        if (allAppsControlDisallowedAdmin != null) {
+            return allAppsControlDisallowedAdmin;
+        }
+        EnforcedAdmin allAppsUninstallDisallowedAdmin = checkIfRestrictionEnforced(context,
+                UserManager.DISALLOW_UNINSTALL_APPS, userId);
+        if (allAppsUninstallDisallowedAdmin != null) {
+            return allAppsUninstallDisallowedAdmin;
+        }
+        IPackageManager ipm = AppGlobals.getPackageManager();
+        try {
+            if (ipm.getBlockUninstallForUser(packageName, userId)) {
+                DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
+                        Context.DEVICE_POLICY_SERVICE);
+                ComponentName admin = dpm.getProfileOwner();
+                if (admin == null) {
+                    admin = dpm.getDeviceOwnerComponentOnCallingUser();
+                }
+                return new EnforcedAdmin(admin, UserHandle.myUserId());
+            }
+        } catch (RemoteException e) {
+            // Nothing to do
+        }
+        return null;
+    }
+
     /**
      * Set the menu item as disabled by admin by adding a restricted padlock at the end of the
      * text and set the click listener which will send an intent to show the admin support details
-     * dialog.
+     * dialog. If the admin is null, remove the padlock and disabled color span. When the admin is
+     * null, we also set the OnMenuItemClickListener to null, so if you want to set a custom
+     * OnMenuItemClickListener, set it after calling this method.
      */
     public static void setMenuItemAsDisabledByAdmin(final Context context,
             final MenuItem item, final EnforcedAdmin admin) {
         SpannableStringBuilder sb = new SpannableStringBuilder(item.getTitle());
         removeExistingRestrictedSpans(sb);
 
-        final int disabledColor = context.getColor(R.color.disabled_text_color);
-        sb.setSpan(new ForegroundColorSpan(disabledColor), 0, sb.length(),
-                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
-        ImageSpan image = new RestrictedLockImageSpan(context);
-        sb.append(" ", image, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
-        item.setTitle(sb);
+        if (admin != null) {
+            final int disabledColor = context.getColor(R.color.disabled_text_color);
+            sb.setSpan(new ForegroundColorSpan(disabledColor), 0, sb.length(),
+                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+            ImageSpan image = new RestrictedLockImageSpan(context);
+            sb.append(" ", image, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 
-        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
-            @Override
-            public boolean onMenuItemClick(MenuItem item) {
-                sendShowAdminSupportDetailsIntent(context, admin);
-                return true;
-            }
-        });
+            item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+                @Override
+                public boolean onMenuItemClick(MenuItem item) {
+                    sendShowAdminSupportDetailsIntent(context, admin);
+                    return true;
+                }
+            });
+        } else {
+            item.setOnMenuItemClickListener(null);
+        }
+        item.setTitle(sb);
     }
 
     private static void removeExistingRestrictedSpans(SpannableStringBuilder sb) {