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