OSDN Git Service

Disabling the activate button when paused
[android-x86/packages-apps-Settings.git] / src / com / android / settings / DeviceAdminAdd.java
index 63998e5..5e05a22 100644 (file)
 
 package com.android.settings;
 
+import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
+
 import org.xmlpull.v1.XmlPullParserException;
 
 import android.app.Activity;
+import android.app.ActivityManagerNative;
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.admin.DeviceAdminInfo;
@@ -35,6 +38,7 @@ import android.content.res.Resources;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.RemoteCallback;
+import android.os.RemoteException;
 import android.text.TextUtils.TruncateAt;
 import android.util.Log;
 import android.view.Display;
@@ -48,6 +52,8 @@ import android.widget.TextView;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
 
 public class DeviceAdminAdd extends Activity {
     static final String TAG = "DeviceAdminAdd";
@@ -84,7 +90,7 @@ public class DeviceAdminAdd extends Activity {
     @Override
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
-
+        getWindow().addPrivateFlags(PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
         mHandler = new Handler(getMainLooper());
         
         mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
@@ -102,7 +108,7 @@ public class DeviceAdminAdd extends Activity {
             finish();
             return;
         }
-        
+
         ActivityInfo ai;
         try {
             ai = getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);
@@ -111,7 +117,41 @@ public class DeviceAdminAdd extends Activity {
             finish();
             return;
         }
-        
+
+        // When activating, make sure the given component name is actually a valid device admin.
+        // No need to check this when deactivating, because it is safe to deactivate an active
+        // invalid device admin.
+        if (!mDPM.isAdminActive(cn)) {
+            List<ResolveInfo> avail = getPackageManager().queryBroadcastReceivers(
+                    new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED),
+                    PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS);
+            int count = avail == null ? 0 : avail.size();
+            boolean found = false;
+            for (int i=0; i<count; i++) {
+                ResolveInfo ri = avail.get(i);
+                if (ai.packageName.equals(ri.activityInfo.packageName)
+                        && ai.name.equals(ri.activityInfo.name)) {
+                    try {
+                        // We didn't retrieve the meta data for all possible matches, so
+                        // need to use the activity info of this specific one that was retrieved.
+                        ri.activityInfo = ai;
+                        DeviceAdminInfo dpi = new DeviceAdminInfo(this, ri);
+                        found = true;
+                    } catch (XmlPullParserException e) {
+                        Log.w(TAG, "Bad " + ri.activityInfo, e);
+                    } catch (IOException e) {
+                        Log.w(TAG, "Bad " + ri.activityInfo, e);
+                    }
+                    break;
+                }
+            }
+            if (!found) {
+                Log.w(TAG, "Request to add invalid device admin: " + cn);
+                finish();
+                return;
+            }
+        }
+
         ResolveInfo ri = new ResolveInfo();
         ri.activityInfo = ai;
         try {
@@ -193,6 +233,12 @@ public class DeviceAdminAdd extends Activity {
                     }
                     finish();
                 } else {
+                    try {
+                        // Don't allow the admin to put a dialog up in front
+                        // of us while we interact with the user.
+                        ActivityManagerNative.getDefault().stopAppSwitches();
+                    } catch (RemoteException e) {
+                    }
                     mDPM.getRemoveWarning(mDeviceAdmin.getComponent(),
                             new RemoteCallback(mHandler) {
                         @Override
@@ -202,6 +248,10 @@ public class DeviceAdminAdd extends Activity {
                                             DeviceAdminReceiver.EXTRA_DISABLE_WARNING)
                                     : null;
                             if (msg == null) {
+                                try {
+                                    ActivityManagerNative.getDefault().resumeAppSwitches();
+                                } catch (RemoteException e) {
+                                }
                                 mDPM.removeActiveAdmin(mDeviceAdmin.getComponent());
                                 finish();
                             } else {
@@ -216,13 +266,20 @@ public class DeviceAdminAdd extends Activity {
             }
         });
     }
-    
+
     @Override
     protected void onResume() {
         super.onResume();
+        mActionButton.setEnabled(true);
         updateInterface();
     }
-    
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+        mActionButton.setEnabled(false);
+    }
+
     @Override
     protected Dialog onCreateDialog(int id, Bundle args) {
         switch (id) {