Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 19 | import com.android.internal.content.PackageMonitor; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame^] | 20 | import com.android.internal.util.FastXmlSerializer; |
| 21 | import com.android.internal.util.XmlUtils; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 22 | import com.android.internal.widget.LockPatternUtils; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 23 | |
| 24 | import org.xmlpull.v1.XmlPullParser; |
| 25 | import org.xmlpull.v1.XmlPullParserException; |
| 26 | import org.xmlpull.v1.XmlSerializer; |
| 27 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 28 | import android.app.Activity; |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 29 | import android.app.DeviceAdminReceiver; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 30 | import android.app.DeviceAdminInfo; |
| 31 | import android.app.DevicePolicyManager; |
| 32 | import android.app.IDevicePolicyManager; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 33 | import android.content.BroadcastReceiver; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 34 | import android.content.ComponentName; |
| 35 | import android.content.Context; |
| 36 | import android.content.Intent; |
| 37 | import android.content.pm.PackageManager; |
| 38 | import android.content.pm.ResolveInfo; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 39 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 40 | import android.os.Binder; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 41 | import android.os.IBinder; |
| 42 | import android.os.IPowerManager; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 43 | import android.os.RecoverySystem; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 44 | import android.os.RemoteCallback; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 45 | import android.os.RemoteException; |
| 46 | import android.os.ServiceManager; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 47 | import android.os.SystemClock; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 48 | import android.util.Log; |
| 49 | import android.util.Xml; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 50 | import android.view.WindowManagerPolicy; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 51 | |
| 52 | import java.io.File; |
| 53 | import java.io.FileInputStream; |
| 54 | import java.io.FileOutputStream; |
| 55 | import java.io.IOException; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 56 | import java.util.ArrayList; |
| 57 | import java.util.HashMap; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 58 | import java.util.List; |
| 59 | |
| 60 | /** |
| 61 | * Implementation of the device policy APIs. |
| 62 | */ |
| 63 | public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 64 | static final String TAG = "DevicePolicyManagerService"; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 65 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 66 | final Context mContext; |
| 67 | final MyPackageMonitor mMonitor; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 68 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 69 | IPowerManager mIPowerManager; |
| 70 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 71 | int mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 72 | int mActivePasswordLength = 0; |
| 73 | int mFailedPasswordAttempts = 0; |
| 74 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 75 | final HashMap<ComponentName, ActiveAdmin> mAdminMap |
| 76 | = new HashMap<ComponentName, ActiveAdmin>(); |
| 77 | final ArrayList<ActiveAdmin> mAdminList |
| 78 | = new ArrayList<ActiveAdmin>(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 79 | |
| 80 | static class ActiveAdmin { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 81 | final DeviceAdminInfo info; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 82 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 83 | int passwordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 84 | int minimumPasswordLength = 0; |
| 85 | long maximumTimeToUnlock = 0; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 86 | int maximumFailedPasswordsForWipe = 0; |
| 87 | |
| 88 | ActiveAdmin(DeviceAdminInfo _info) { |
| 89 | info = _info; |
| 90 | } |
| 91 | |
| 92 | int getUid() { return info.getActivityInfo().applicationInfo.uid; } |
| 93 | |
| 94 | void writeToXml(XmlSerializer out) |
| 95 | throws IllegalArgumentException, IllegalStateException, IOException { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 96 | out.startTag(null, "policies"); |
| 97 | info.writePoliciesToXml(out); |
| 98 | out.endTag(null, "policies"); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 99 | if (passwordQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 100 | out.startTag(null, "password-quality"); |
| 101 | out.attribute(null, "value", Integer.toString(passwordQuality)); |
| 102 | out.endTag(null, "password-quality"); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 103 | if (minimumPasswordLength > 0) { |
| 104 | out.startTag(null, "min-password-length"); |
| 105 | out.attribute(null, "value", Integer.toString(minimumPasswordLength)); |
| 106 | out.endTag(null, "mn-password-length"); |
| 107 | } |
| 108 | } |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 109 | if (maximumTimeToUnlock != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 110 | out.startTag(null, "max-time-to-unlock"); |
| 111 | out.attribute(null, "value", Long.toString(maximumTimeToUnlock)); |
| 112 | out.endTag(null, "max-time-to-unlock"); |
| 113 | } |
| 114 | if (maximumFailedPasswordsForWipe != 0) { |
| 115 | out.startTag(null, "max-failed-password-wipe"); |
| 116 | out.attribute(null, "value", Integer.toString(maximumFailedPasswordsForWipe)); |
| 117 | out.endTag(null, "max-failed-password-wipe"); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void readFromXml(XmlPullParser parser) |
| 122 | throws XmlPullParserException, IOException { |
| 123 | int outerDepth = parser.getDepth(); |
| 124 | int type; |
| 125 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 126 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 127 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 128 | continue; |
| 129 | } |
| 130 | String tag = parser.getName(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 131 | if ("policies".equals(tag)) { |
| 132 | info.readPoliciesFromXml(parser); |
| 133 | } else if ("password-quality".equals(tag)) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 134 | passwordQuality = Integer.parseInt( |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 135 | parser.getAttributeValue(null, "value")); |
| 136 | } else if ("min-password-length".equals(tag)) { |
| 137 | minimumPasswordLength = Integer.parseInt( |
| 138 | parser.getAttributeValue(null, "value")); |
| 139 | } else if ("max-time-to-unlock".equals(tag)) { |
| 140 | maximumTimeToUnlock = Long.parseLong( |
| 141 | parser.getAttributeValue(null, "value")); |
| 142 | } else if ("max-failed-password-wipe".equals(tag)) { |
| 143 | maximumFailedPasswordsForWipe = Integer.parseInt( |
| 144 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 145 | } else { |
| 146 | Log.w(TAG, "Unknown admin tag: " + tag); |
| 147 | } |
| 148 | XmlUtils.skipCurrentTag(parser); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | class MyPackageMonitor extends PackageMonitor { |
| 154 | public void onSomePackagesChanged() { |
| 155 | synchronized (DevicePolicyManagerService.this) { |
| 156 | for (int i=mAdminList.size()-1; i>=0; i--) { |
| 157 | ActiveAdmin aa = mAdminList.get(i); |
| 158 | int change = isPackageDisappearing(aa.info.getPackageName()); |
| 159 | if (change == PACKAGE_PERMANENT_CHANGE |
| 160 | || change == PACKAGE_TEMPORARY_CHANGE) { |
| 161 | Log.w(TAG, "Admin unexpectedly uninstalled: " |
| 162 | + aa.info.getComponent()); |
| 163 | mAdminList.remove(i); |
| 164 | } else if (isPackageModified(aa.info.getPackageName())) { |
| 165 | try { |
| 166 | mContext.getPackageManager().getReceiverInfo( |
| 167 | aa.info.getComponent(), 0); |
| 168 | } catch (NameNotFoundException e) { |
| 169 | Log.w(TAG, "Admin package change removed component: " |
| 170 | + aa.info.getComponent()); |
| 171 | mAdminList.remove(i); |
| 172 | } |
| 173 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Instantiates the service. |
| 181 | */ |
| 182 | public DevicePolicyManagerService(Context context) { |
| 183 | mContext = context; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 184 | mMonitor = new MyPackageMonitor(); |
| 185 | mMonitor.register(context, true); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 188 | private IPowerManager getIPowerManager() { |
| 189 | if (mIPowerManager == null) { |
| 190 | IBinder b = ServiceManager.getService(Context.POWER_SERVICE); |
| 191 | mIPowerManager = IPowerManager.Stub.asInterface(b); |
| 192 | } |
| 193 | return mIPowerManager; |
| 194 | } |
| 195 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 196 | ActiveAdmin getActiveAdminUncheckedLocked(ComponentName who) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 197 | ActiveAdmin admin = mAdminMap.get(who); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 198 | if (admin != null |
| 199 | && who.getPackageName().equals(admin.info.getActivityInfo().packageName) |
| 200 | && who.getClassName().equals(admin.info.getActivityInfo().name)) { |
| 201 | return admin; |
| 202 | } |
| 203 | return null; |
| 204 | } |
| 205 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 206 | ActiveAdmin getActiveAdminForCallerLocked(ComponentName who, int reqPolicy) |
| 207 | throws SecurityException { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 208 | final int callingUid = Binder.getCallingUid(); |
| 209 | if (who != null) { |
| 210 | ActiveAdmin admin = mAdminMap.get(who); |
| 211 | if (admin == null) { |
| 212 | throw new SecurityException("No active admin " + who); |
| 213 | } |
| 214 | if (admin.getUid() != callingUid) { |
| 215 | throw new SecurityException("Admin " + who + " is not owned by uid " |
| 216 | + Binder.getCallingUid()); |
| 217 | } |
| 218 | if (!admin.info.usesPolicy(reqPolicy)) { |
| 219 | throw new SecurityException("Admin " + admin.info.getComponent() |
| 220 | + " did not specify uses-policy for: " |
| 221 | + admin.info.getTagForPolicy(reqPolicy)); |
| 222 | } |
| 223 | return admin; |
| 224 | } else { |
| 225 | final int N = mAdminList.size(); |
| 226 | for (int i=0; i<N; i++) { |
| 227 | ActiveAdmin admin = mAdminList.get(i); |
| 228 | if (admin.getUid() == callingUid && admin.info.usesPolicy(reqPolicy)) { |
| 229 | return admin; |
| 230 | } |
| 231 | } |
| 232 | throw new SecurityException("No active admin owned by uid " |
| 233 | + Binder.getCallingUid() + " for policy #" + reqPolicy); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 234 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 237 | void sendAdminCommandLocked(ActiveAdmin admin, String action) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 238 | Intent intent = new Intent(action); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 239 | intent.setComponent(admin.info.getComponent()); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 240 | mContext.sendBroadcast(intent); |
| 241 | } |
| 242 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 243 | void sendAdminCommandLocked(String action, int reqPolicy) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 244 | final int N = mAdminList.size(); |
| 245 | if (N > 0) { |
| 246 | for (int i=0; i<N; i++) { |
| 247 | ActiveAdmin admin = mAdminList.get(i); |
| 248 | if (admin.info.usesPolicy(reqPolicy)) { |
| 249 | sendAdminCommandLocked(admin, action); |
| 250 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 251 | } |
Dianne Hackborn | 4141d03 | 2010-01-21 16:29:00 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 255 | void removeActiveAdminLocked(ComponentName adminReceiver) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 256 | ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver); |
| 257 | if (admin != null) { |
| 258 | sendAdminCommandLocked(admin, |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 259 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 260 | // XXX need to wait for it to complete. |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 261 | mAdminList.remove(admin); |
| 262 | mAdminMap.remove(adminReceiver); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | public DeviceAdminInfo findAdmin(ComponentName adminName) { |
| 267 | Intent resolveIntent = new Intent(); |
| 268 | resolveIntent.setComponent(adminName); |
| 269 | List<ResolveInfo> infos = mContext.getPackageManager().queryBroadcastReceivers( |
| 270 | resolveIntent, PackageManager.GET_META_DATA); |
| 271 | if (infos == null || infos.size() <= 0) { |
| 272 | throw new IllegalArgumentException("Unknown admin: " + adminName); |
| 273 | } |
| 274 | |
| 275 | try { |
| 276 | return new DeviceAdminInfo(mContext, infos.get(0)); |
| 277 | } catch (XmlPullParserException e) { |
| 278 | Log.w(TAG, "Bad device admin requested: " + adminName, e); |
| 279 | return null; |
| 280 | } catch (IOException e) { |
| 281 | Log.w(TAG, "Bad device admin requested: " + adminName, e); |
| 282 | return null; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | private static JournaledFile makeJournaledFile() { |
| 287 | final String base = "/data/system/device_policies.xml"; |
| 288 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 289 | } |
| 290 | |
| 291 | private void saveSettingsLocked() { |
| 292 | JournaledFile journal = makeJournaledFile(); |
| 293 | FileOutputStream stream = null; |
| 294 | try { |
| 295 | stream = new FileOutputStream(journal.chooseForWrite(), false); |
| 296 | XmlSerializer out = new FastXmlSerializer(); |
| 297 | out.setOutput(stream, "utf-8"); |
| 298 | out.startDocument(null, true); |
| 299 | |
| 300 | out.startTag(null, "policies"); |
| 301 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 302 | final int N = mAdminList.size(); |
| 303 | for (int i=0; i<N; i++) { |
| 304 | ActiveAdmin ap = mAdminList.get(i); |
| 305 | if (ap != null) { |
| 306 | out.startTag(null, "admin"); |
| 307 | out.attribute(null, "name", ap.info.getComponent().flattenToString()); |
| 308 | ap.writeToXml(out); |
| 309 | out.endTag(null, "admin"); |
| 310 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 311 | } |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 312 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 313 | out.endTag(null, "policies"); |
| 314 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 315 | if (mFailedPasswordAttempts != 0) { |
| 316 | out.startTag(null, "failed-password-attempts"); |
| 317 | out.attribute(null, "value", Integer.toString(mFailedPasswordAttempts)); |
| 318 | out.endTag(null, "failed-password-attempts"); |
| 319 | } |
| 320 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 321 | out.endDocument(); |
| 322 | stream.close(); |
| 323 | journal.commit(); |
| 324 | } catch (IOException e) { |
| 325 | try { |
| 326 | if (stream != null) { |
| 327 | stream.close(); |
| 328 | } |
| 329 | } catch (IOException ex) { |
| 330 | // Ignore |
| 331 | } |
| 332 | journal.rollback(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | private void loadSettingsLocked() { |
| 337 | JournaledFile journal = makeJournaledFile(); |
| 338 | FileInputStream stream = null; |
| 339 | File file = journal.chooseForRead(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 340 | try { |
| 341 | stream = new FileInputStream(file); |
| 342 | XmlPullParser parser = Xml.newPullParser(); |
| 343 | parser.setInput(stream, null); |
| 344 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 345 | int type; |
| 346 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 347 | && type != XmlPullParser.START_TAG) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 348 | } |
| 349 | String tag = parser.getName(); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 350 | if (!"policies".equals(tag)) { |
| 351 | throw new XmlPullParserException( |
| 352 | "Settings do not start with policies tag: found " + tag); |
| 353 | } |
| 354 | type = parser.next(); |
| 355 | int outerDepth = parser.getDepth(); |
| 356 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 357 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 358 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 359 | continue; |
| 360 | } |
| 361 | tag = parser.getName(); |
| 362 | if ("admin".equals(tag)) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 363 | String name = parser.getAttributeValue(null, "name"); |
| 364 | try { |
| 365 | DeviceAdminInfo dai = findAdmin( |
| 366 | ComponentName.unflattenFromString(name)); |
| 367 | if (dai != null) { |
| 368 | ActiveAdmin ap = new ActiveAdmin(dai); |
| 369 | ap.readFromXml(parser); |
| 370 | mAdminMap.put(ap.info.getComponent(), ap); |
| 371 | mAdminList.add(ap); |
| 372 | } |
| 373 | } catch (RuntimeException e) { |
| 374 | Log.w(TAG, "Failed loading admin " + name, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 375 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 376 | } else if ("failed-password-attempts".equals(tag)) { |
| 377 | mFailedPasswordAttempts = Integer.parseInt( |
| 378 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 379 | XmlUtils.skipCurrentTag(parser); |
| 380 | } else { |
| 381 | Log.w(TAG, "Unknown tag: " + tag); |
| 382 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 383 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 384 | } |
| 385 | } catch (NullPointerException e) { |
| 386 | Log.w(TAG, "failed parsing " + file + " " + e); |
| 387 | } catch (NumberFormatException e) { |
| 388 | Log.w(TAG, "failed parsing " + file + " " + e); |
| 389 | } catch (XmlPullParserException e) { |
| 390 | Log.w(TAG, "failed parsing " + file + " " + e); |
| 391 | } catch (IOException e) { |
| 392 | Log.w(TAG, "failed parsing " + file + " " + e); |
| 393 | } catch (IndexOutOfBoundsException e) { |
| 394 | Log.w(TAG, "failed parsing " + file + " " + e); |
| 395 | } |
| 396 | try { |
| 397 | if (stream != null) { |
| 398 | stream.close(); |
| 399 | } |
| 400 | } catch (IOException e) { |
| 401 | // Ignore |
| 402 | } |
| 403 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 404 | long timeMs = getMaximumTimeToLock(null); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 405 | if (timeMs <= 0) { |
| 406 | timeMs = Integer.MAX_VALUE; |
| 407 | } |
| 408 | try { |
| 409 | getIPowerManager().setMaximumScreenOffTimeount((int)timeMs); |
| 410 | } catch (RemoteException e) { |
| 411 | Log.w(TAG, "Failure talking with power manager", e); |
| 412 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | public void systemReady() { |
| 416 | synchronized (this) { |
| 417 | loadSettingsLocked(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | public void setActiveAdmin(ComponentName adminReceiver) { |
| 422 | mContext.enforceCallingOrSelfPermission( |
| 423 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 424 | |
| 425 | DeviceAdminInfo info = findAdmin(adminReceiver); |
| 426 | if (info == null) { |
| 427 | throw new IllegalArgumentException("Bad admin: " + adminReceiver); |
| 428 | } |
| 429 | synchronized (this) { |
| 430 | long ident = Binder.clearCallingIdentity(); |
| 431 | try { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 432 | if (getActiveAdminUncheckedLocked(adminReceiver) != null) { |
| 433 | throw new IllegalArgumentException("Admin is already added"); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 434 | } |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 435 | ActiveAdmin admin = new ActiveAdmin(info); |
| 436 | mAdminMap.put(adminReceiver, admin); |
| 437 | mAdminList.add(admin); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 438 | saveSettingsLocked(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 439 | sendAdminCommandLocked(admin, |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 440 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 441 | } finally { |
| 442 | Binder.restoreCallingIdentity(ident); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 447 | public boolean isAdminActive(ComponentName adminReceiver) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 448 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 449 | return getActiveAdminUncheckedLocked(adminReceiver) != null; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | public List<ComponentName> getActiveAdmins() { |
| 454 | synchronized (this) { |
| 455 | final int N = mAdminList.size(); |
| 456 | if (N <= 0) { |
| 457 | return null; |
| 458 | } |
| 459 | ArrayList<ComponentName> res = new ArrayList<ComponentName>(N); |
| 460 | for (int i=0; i<N; i++) { |
| 461 | res.add(mAdminList.get(i).info.getComponent()); |
| 462 | } |
| 463 | return res; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 467 | public boolean packageHasActiveAdmins(String packageName) { |
| 468 | synchronized (this) { |
| 469 | final int N = mAdminList.size(); |
| 470 | for (int i=0; i<N; i++) { |
| 471 | if (mAdminList.get(i).info.getPackageName().equals(packageName)) { |
| 472 | return true; |
| 473 | } |
| 474 | } |
| 475 | return false; |
| 476 | } |
| 477 | } |
| 478 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 479 | public void removeActiveAdmin(ComponentName adminReceiver) { |
| 480 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 481 | ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver); |
| 482 | if (admin == null) { |
| 483 | return; |
| 484 | } |
| 485 | if (admin.getUid() != Binder.getCallingUid()) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 486 | mContext.enforceCallingOrSelfPermission( |
| 487 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 488 | } |
| 489 | long ident = Binder.clearCallingIdentity(); |
| 490 | try { |
| 491 | removeActiveAdminLocked(adminReceiver); |
| 492 | } finally { |
| 493 | Binder.restoreCallingIdentity(ident); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 498 | public void setPasswordQuality(ComponentName who, int mode) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 499 | synchronized (this) { |
| 500 | if (who == null) { |
| 501 | throw new NullPointerException("ComponentName is null"); |
| 502 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 503 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 504 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 505 | if (ap.passwordQuality != mode) { |
| 506 | ap.passwordQuality = mode; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 507 | saveSettingsLocked(); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 512 | public int getPasswordQuality(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 513 | synchronized (this) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 514 | int mode = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 515 | |
| 516 | if (who != null) { |
| 517 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 518 | return admin != null ? admin.passwordQuality : mode; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 522 | for (int i=0; i<N; i++) { |
| 523 | ActiveAdmin admin = mAdminList.get(i); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 524 | if (mode < admin.passwordQuality) { |
| 525 | mode = admin.passwordQuality; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | return mode; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 532 | public void setPasswordMinimumLength(ComponentName who, int length) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 533 | synchronized (this) { |
| 534 | if (who == null) { |
| 535 | throw new NullPointerException("ComponentName is null"); |
| 536 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 537 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 538 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 539 | if (ap.minimumPasswordLength != length) { |
| 540 | ap.minimumPasswordLength = length; |
| 541 | saveSettingsLocked(); |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 546 | public int getPasswordMinimumLength(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 547 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 548 | int length = 0; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 549 | |
| 550 | if (who != null) { |
| 551 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 552 | return admin != null ? admin.minimumPasswordLength : length; |
| 553 | } |
| 554 | |
| 555 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 556 | for (int i=0; i<N; i++) { |
| 557 | ActiveAdmin admin = mAdminList.get(i); |
| 558 | if (length < admin.minimumPasswordLength) { |
| 559 | length = admin.minimumPasswordLength; |
| 560 | } |
| 561 | } |
| 562 | return length; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 566 | public boolean isActivePasswordSufficient() { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 567 | synchronized (this) { |
| 568 | // This API can only be called by an active device admin, |
| 569 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 570 | getActiveAdminForCallerLocked(null, |
| 571 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 572 | return mActivePasswordQuality >= getPasswordQuality(null) |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 573 | && mActivePasswordLength >= getPasswordMinimumLength(null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | public int getCurrentFailedPasswordAttempts() { |
| 578 | synchronized (this) { |
| 579 | // This API can only be called by an active device admin, |
| 580 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 581 | getActiveAdminForCallerLocked(null, |
| 582 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 583 | return mFailedPasswordAttempts; |
| 584 | } |
| 585 | } |
| 586 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 587 | public void setMaximumFailedPasswordsForWipe(ComponentName who, int num) { |
| 588 | synchronized (this) { |
| 589 | // This API can only be called by an active device admin, |
| 590 | // so try to retrieve it to check that the caller is one. |
| 591 | getActiveAdminForCallerLocked(who, |
| 592 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
| 593 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 594 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
| 595 | if (ap.maximumFailedPasswordsForWipe != num) { |
| 596 | ap.maximumFailedPasswordsForWipe = num; |
| 597 | saveSettingsLocked(); |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 602 | public int getMaximumFailedPasswordsForWipe(ComponentName who) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 603 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 604 | int count = 0; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 605 | |
| 606 | if (who != null) { |
| 607 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 608 | return admin != null ? admin.maximumFailedPasswordsForWipe : count; |
| 609 | } |
| 610 | |
| 611 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 612 | for (int i=0; i<N; i++) { |
| 613 | ActiveAdmin admin = mAdminList.get(i); |
| 614 | if (count == 0) { |
| 615 | count = admin.maximumFailedPasswordsForWipe; |
| 616 | } else if (admin.maximumFailedPasswordsForWipe != 0 |
| 617 | && count > admin.maximumFailedPasswordsForWipe) { |
| 618 | count = admin.maximumFailedPasswordsForWipe; |
| 619 | } |
| 620 | } |
| 621 | return count; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 625 | public boolean resetPassword(String password) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 626 | int quality; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 627 | synchronized (this) { |
| 628 | // This API can only be called by an active device admin, |
| 629 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 630 | getActiveAdminForCallerLocked(null, |
| 631 | DeviceAdminInfo.USES_POLICY_RESET_PASSWORD); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 632 | quality = getPasswordQuality(null); |
| 633 | if (quality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 634 | int adjQuality = LockPatternUtils.adjustPasswordMode(password, quality); |
| 635 | if (adjQuality == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 636 | Log.w(TAG, "resetPassword: password does not meet quality " + quality); |
| 637 | return false; |
| 638 | } |
| 639 | quality = adjQuality; |
| 640 | } |
| 641 | int length = getPasswordMinimumLength(null); |
| 642 | if (password.length() < length) { |
| 643 | Log.w(TAG, "resetPassword: password does not meet length " + length); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 644 | return false; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | // Don't do this with the lock held, because it is going to call |
| 649 | // back in to the service. |
| 650 | long ident = Binder.clearCallingIdentity(); |
| 651 | try { |
| 652 | LockPatternUtils utils = new LockPatternUtils(mContext); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 653 | utils.saveLockPassword(password, quality); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 654 | } finally { |
| 655 | Binder.restoreCallingIdentity(ident); |
| 656 | } |
| 657 | |
| 658 | return true; |
| 659 | } |
| 660 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 661 | public void setMaximumTimeToLock(ComponentName who, long timeMs) { |
| 662 | synchronized (this) { |
| 663 | if (who == null) { |
| 664 | throw new NullPointerException("ComponentName is null"); |
| 665 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 666 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
Dianne Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 667 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 668 | if (ap.maximumTimeToUnlock != timeMs) { |
| 669 | ap.maximumTimeToUnlock = timeMs; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 670 | |
| 671 | long ident = Binder.clearCallingIdentity(); |
| 672 | try { |
| 673 | saveSettingsLocked(); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 674 | |
| 675 | timeMs = getMaximumTimeToLock(null); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 676 | if (timeMs <= 0) { |
| 677 | timeMs = Integer.MAX_VALUE; |
| 678 | } |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 679 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 680 | try { |
| 681 | getIPowerManager().setMaximumScreenOffTimeount((int)timeMs); |
| 682 | } catch (RemoteException e) { |
| 683 | Log.w(TAG, "Failure talking with power manager", e); |
| 684 | } |
| 685 | } finally { |
| 686 | Binder.restoreCallingIdentity(ident); |
| 687 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 692 | public long getMaximumTimeToLock(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 693 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 694 | long time = 0; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 695 | |
| 696 | if (who != null) { |
| 697 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 698 | return admin != null ? admin.maximumTimeToUnlock : time; |
| 699 | } |
| 700 | |
| 701 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 702 | for (int i=0; i<N; i++) { |
| 703 | ActiveAdmin admin = mAdminList.get(i); |
| 704 | if (time == 0) { |
| 705 | time = admin.maximumTimeToUnlock; |
| 706 | } else if (admin.maximumTimeToUnlock != 0 |
| 707 | && time > admin.maximumTimeToUnlock) { |
| 708 | time = admin.maximumTimeToUnlock; |
| 709 | } |
| 710 | } |
| 711 | return time; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 715 | public void lockNow() { |
| 716 | synchronized (this) { |
| 717 | // This API can only be called by an active device admin, |
| 718 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 719 | getActiveAdminForCallerLocked(null, |
| 720 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 721 | long ident = Binder.clearCallingIdentity(); |
| 722 | try { |
| 723 | mIPowerManager.goToSleepWithReason(SystemClock.uptimeMillis(), |
| 724 | WindowManagerPolicy.OFF_BECAUSE_OF_ADMIN); |
| 725 | } catch (RemoteException e) { |
| 726 | } finally { |
| 727 | Binder.restoreCallingIdentity(ident); |
| 728 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 732 | void wipeDataLocked(int flags) { |
| 733 | try { |
| 734 | RecoverySystem.rebootWipeUserData(mContext); |
| 735 | } catch (IOException e) { |
| 736 | Log.w(TAG, "Failed requesting data wipe", e); |
| 737 | } |
| 738 | } |
| 739 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 740 | public void wipeData(int flags) { |
| 741 | synchronized (this) { |
| 742 | // This API can only be called by an active device admin, |
| 743 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 744 | getActiveAdminForCallerLocked(null, |
| 745 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 746 | long ident = Binder.clearCallingIdentity(); |
| 747 | try { |
| 748 | wipeDataLocked(flags); |
| 749 | } finally { |
| 750 | Binder.restoreCallingIdentity(ident); |
| 751 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 752 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | public void getRemoveWarning(ComponentName comp, final RemoteCallback result) { |
| 756 | mContext.enforceCallingOrSelfPermission( |
| 757 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 758 | |
| 759 | synchronized (this) { |
| 760 | ActiveAdmin admin = getActiveAdminUncheckedLocked(comp); |
| 761 | if (admin == null) { |
| 762 | try { |
| 763 | result.sendResult(null); |
| 764 | } catch (RemoteException e) { |
| 765 | } |
| 766 | return; |
| 767 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 768 | Intent intent = new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 769 | intent.setComponent(admin.info.getComponent()); |
| 770 | mContext.sendOrderedBroadcast(intent, null, new BroadcastReceiver() { |
| 771 | @Override |
| 772 | public void onReceive(Context context, Intent intent) { |
| 773 | try { |
| 774 | result.sendResult(getResultExtras(false)); |
| 775 | } catch (RemoteException e) { |
| 776 | } |
| 777 | } |
| 778 | }, null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 782 | public void setActivePasswordState(int quality, int length) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 783 | mContext.enforceCallingOrSelfPermission( |
| 784 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 785 | |
| 786 | synchronized (this) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 787 | if (mActivePasswordQuality != quality || mActivePasswordLength != length |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 788 | || mFailedPasswordAttempts != 0) { |
| 789 | long ident = Binder.clearCallingIdentity(); |
| 790 | try { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 791 | mActivePasswordQuality = quality; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 792 | mActivePasswordLength = length; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 793 | if (mFailedPasswordAttempts != 0) { |
| 794 | mFailedPasswordAttempts = 0; |
| 795 | saveSettingsLocked(); |
| 796 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 797 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 798 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 799 | } finally { |
| 800 | Binder.restoreCallingIdentity(ident); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | public void reportFailedPasswordAttempt() { |
| 807 | mContext.enforceCallingOrSelfPermission( |
| 808 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 809 | |
| 810 | synchronized (this) { |
| 811 | long ident = Binder.clearCallingIdentity(); |
| 812 | try { |
| 813 | mFailedPasswordAttempts++; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 814 | saveSettingsLocked(); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 815 | int max = getMaximumFailedPasswordsForWipe(null); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 816 | if (max > 0 && mFailedPasswordAttempts >= max) { |
| 817 | wipeDataLocked(0); |
| 818 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 819 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_FAILED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 820 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 821 | } finally { |
| 822 | Binder.restoreCallingIdentity(ident); |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | public void reportSuccessfulPasswordAttempt() { |
| 828 | mContext.enforceCallingOrSelfPermission( |
| 829 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 830 | |
| 831 | synchronized (this) { |
| 832 | if (mFailedPasswordAttempts != 0) { |
| 833 | long ident = Binder.clearCallingIdentity(); |
| 834 | try { |
| 835 | mFailedPasswordAttempts = 0; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 836 | saveSettingsLocked(); |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 837 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_SUCCEEDED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 838 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 839 | } finally { |
| 840 | Binder.restoreCallingIdentity(ident); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | } |