blob: 2237c821b234df5896228e11292704f00cc816e8 [file] [log] [blame]
Dianne Hackbornd6847842010-01-12 18:14:19 -08001/*
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
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080017package android.app.admin;
Dianne Hackbornd6847842010-01-12 18:14:19 -080018
19import org.xmlpull.v1.XmlPullParser;
20import org.xmlpull.v1.XmlPullParserException;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080021import org.xmlpull.v1.XmlSerializer;
Dianne Hackbornd6847842010-01-12 18:14:19 -080022
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.pm.ActivityInfo;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080026import android.content.pm.ApplicationInfo;
Dianne Hackbornd6847842010-01-12 18:14:19 -080027import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080029import android.content.pm.PackageManager.NameNotFoundException;
30import android.content.res.Resources;
Dianne Hackbornd6847842010-01-12 18:14:19 -080031import android.content.res.TypedArray;
32import android.content.res.XmlResourceParser;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080033import android.content.res.Resources.NotFoundException;
Dianne Hackbornd6847842010-01-12 18:14:19 -080034import android.graphics.drawable.Drawable;
35import android.os.Parcel;
36import android.os.Parcelable;
37import android.util.AttributeSet;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080038import android.util.Log;
Dianne Hackbornd6847842010-01-12 18:14:19 -080039import android.util.Printer;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080040import android.util.SparseArray;
Dianne Hackbornd6847842010-01-12 18:14:19 -080041import android.util.Xml;
42
43import java.io.IOException;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080044import java.util.ArrayList;
45import java.util.HashMap;
Dianne Hackbornd6847842010-01-12 18:14:19 -080046
47/**
48 * This class is used to specify meta information of a device administrator
49 * component.
50 */
51public final class DeviceAdminInfo implements Parcelable {
52 static final String TAG = "DeviceAdminInfo";
53
54 /**
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080055 * A type of policy that this device admin can use: limit the passwords
Dianne Hackborn9327f4f2010-01-29 10:38:29 -080056 * that the user can select, via {@link DevicePolicyManager#setPasswordQuality}
Dianne Hackborn254cb442010-01-27 19:23:59 -080057 * and {@link DevicePolicyManager#setPasswordMinimumLength}.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080058 *
59 * <p>To control this policy, the device admin must have a "limit-password"
60 * tag in the "uses-policies" section of its meta-data.
61 */
62 public static final int USES_POLICY_LIMIT_PASSWORD = 0;
63
64 /**
65 * A type of policy that this device admin can use: able to watch login
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080066 * attempts from the user, via {@link DeviceAdminReceiver#ACTION_PASSWORD_FAILED},
67 * {@link DeviceAdminReceiver#ACTION_PASSWORD_SUCCEEDED}, and
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080068 * {@link DevicePolicyManager#getCurrentFailedPasswordAttempts}.
69 *
70 * <p>To control this policy, the device admin must have a "watch-login"
71 * tag in the "uses-policies" section of its meta-data.
72 */
73 public static final int USES_POLICY_WATCH_LOGIN = 1;
74
75 /**
76 * A type of policy that this device admin can use: able to reset the
77 * user's password via
78 * {@link DevicePolicyManager#resetPassword}.
79 *
80 * <p>To control this policy, the device admin must have a "reset-password"
81 * tag in the "uses-policies" section of its meta-data.
82 */
83 public static final int USES_POLICY_RESET_PASSWORD = 2;
84
85 /**
Dianne Hackborn315ada72010-02-11 12:14:08 -080086 * A type of policy that this device admin can use: able to force the device
87 * to lock via{@link DevicePolicyManager#lockNow} or limit the
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080088 * maximum lock timeout for the device via
89 * {@link DevicePolicyManager#setMaximumTimeToLock}.
90 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080091 * <p>To control this policy, the device admin must have a "force-lock"
92 * tag in the "uses-policies" section of its meta-data.
93 */
Dianne Hackborn315ada72010-02-11 12:14:08 -080094 public static final int USES_POLICY_FORCE_LOCK = 3;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080095
96 /**
97 * A type of policy that this device admin can use: able to factory
98 * reset the device, erasing all of the user's data, via
99 * {@link DevicePolicyManager#wipeData}.
100 *
101 * <p>To control this policy, the device admin must have a "wipe-data"
102 * tag in the "uses-policies" section of its meta-data.
103 */
Dianne Hackborn315ada72010-02-11 12:14:08 -0800104 public static final int USES_POLICY_WIPE_DATA = 4;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800105
Oscar Montemayor69238c62010-08-03 10:51:06 -0700106 /**
107 * A type of policy that this device admin can use: able to specify the
108 * device Global Proxy, via {@link DevicePolicyManager#setGlobalProxy}.
109 *
110 * <p>To control this policy, the device admin must have a "set-global-proxy"
111 * tag in the "uses-policies" section of its meta-data.
Oscar Montemayor69238c62010-08-03 10:51:06 -0700112 */
113 public static final int USES_POLICY_SETS_GLOBAL_PROXY = 5;
114
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800115 /** @hide */
116 public static class PolicyInfo {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800117 public final int ident;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800118 final public String tag;
119 final public int label;
120 final public int description;
121
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800122 public PolicyInfo(int identIn, String tagIn, int labelIn, int descriptionIn) {
123 ident = identIn;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800124 tag = tagIn;
125 label = labelIn;
126 description = descriptionIn;
127 }
128 }
129
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800130 static ArrayList<PolicyInfo> sPoliciesDisplayOrder = new ArrayList<PolicyInfo>();
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800131 static HashMap<String, Integer> sKnownPolicies = new HashMap<String, Integer>();
132 static SparseArray<PolicyInfo> sRevKnownPolicies = new SparseArray<PolicyInfo>();
133
134 static {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800135 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_WIPE_DATA, "wipe-data",
136 com.android.internal.R.string.policylab_wipeData,
137 com.android.internal.R.string.policydesc_wipeData));
138 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_RESET_PASSWORD, "reset-password",
139 com.android.internal.R.string.policylab_resetPassword,
140 com.android.internal.R.string.policydesc_resetPassword));
141 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_LIMIT_PASSWORD, "limit-password",
142 com.android.internal.R.string.policylab_limitPassword,
143 com.android.internal.R.string.policydesc_limitPassword));
144 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_WATCH_LOGIN, "watch-login",
145 com.android.internal.R.string.policylab_watchLogin,
146 com.android.internal.R.string.policydesc_watchLogin));
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800147 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_FORCE_LOCK, "force-lock",
148 com.android.internal.R.string.policylab_forceLock,
149 com.android.internal.R.string.policydesc_forceLock));
Oscar Montemayor69238c62010-08-03 10:51:06 -0700150 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_SETS_GLOBAL_PROXY, "set-global-proxy",
151 com.android.internal.R.string.policylab_setGlobalProxy,
152 com.android.internal.R.string.policydesc_setGlobalProxy));
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800153
154 for (int i=0; i<sPoliciesDisplayOrder.size(); i++) {
155 PolicyInfo pi = sPoliciesDisplayOrder.get(i);
156 sRevKnownPolicies.put(pi.ident, pi);
157 sKnownPolicies.put(pi.tag, pi.ident);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800158 }
159 }
160
161 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800162 * The BroadcastReceiver that implements this device admin component.
163 */
164 final ResolveInfo mReceiver;
165
166 /**
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800167 * Whether this should be visible to the user.
168 */
169 boolean mVisible;
170
171 /**
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800172 * The policies this administrator needs access to.
173 */
174 int mUsesPolicies;
175
176 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800177 * Constructor.
178 *
179 * @param context The Context in which we are parsing the device admin.
180 * @param receiver The ResolveInfo returned from the package manager about
181 * this device admin's component.
182 */
183 public DeviceAdminInfo(Context context, ResolveInfo receiver)
184 throws XmlPullParserException, IOException {
185 mReceiver = receiver;
186 ActivityInfo ai = receiver.activityInfo;
187
188 PackageManager pm = context.getPackageManager();
189
190 XmlResourceParser parser = null;
191 try {
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800192 parser = ai.loadXmlMetaData(pm, DeviceAdminReceiver.DEVICE_ADMIN_META_DATA);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800193 if (parser == null) {
194 throw new XmlPullParserException("No "
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800195 + DeviceAdminReceiver.DEVICE_ADMIN_META_DATA + " meta-data");
Dianne Hackbornd6847842010-01-12 18:14:19 -0800196 }
197
Dianne Hackborn20cb56e2010-03-04 00:58:29 -0800198 Resources res = pm.getResourcesForApplication(ai.applicationInfo);
199
Dianne Hackbornd6847842010-01-12 18:14:19 -0800200 AttributeSet attrs = Xml.asAttributeSet(parser);
201
202 int type;
203 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
204 && type != XmlPullParser.START_TAG) {
205 }
206
207 String nodeName = parser.getName();
208 if (!"device-admin".equals(nodeName)) {
209 throw new XmlPullParserException(
210 "Meta-data does not start with device-admin tag");
211 }
212
Dianne Hackborn20cb56e2010-03-04 00:58:29 -0800213 TypedArray sa = res.obtainAttributes(attrs,
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800214 com.android.internal.R.styleable.DeviceAdmin);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800215
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800216 mVisible = sa.getBoolean(
217 com.android.internal.R.styleable.DeviceAdmin_visible, true);
218
Dianne Hackbornd6847842010-01-12 18:14:19 -0800219 sa.recycle();
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800220
221 int outerDepth = parser.getDepth();
222 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
223 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
224 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
225 continue;
226 }
227 String tagName = parser.getName();
228 if (tagName.equals("uses-policies")) {
229 int innerDepth = parser.getDepth();
230 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
231 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
232 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
233 continue;
234 }
235 String policyName = parser.getName();
236 Integer val = sKnownPolicies.get(policyName);
237 if (val != null) {
238 mUsesPolicies |= 1 << val.intValue();
239 } else {
240 Log.w(TAG, "Unknown tag under uses-policies of "
241 + getComponent() + ": " + policyName);
242 }
243 }
244 }
245 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -0800246 } catch (NameNotFoundException e) {
247 throw new XmlPullParserException(
248 "Unable to create context for: " + ai.packageName);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800249 } finally {
250 if (parser != null) parser.close();
251 }
252 }
253
254 DeviceAdminInfo(Parcel source) {
255 mReceiver = ResolveInfo.CREATOR.createFromParcel(source);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800256 mUsesPolicies = source.readInt();
Dianne Hackbornd6847842010-01-12 18:14:19 -0800257 }
258
259 /**
260 * Return the .apk package that implements this device admin.
261 */
262 public String getPackageName() {
263 return mReceiver.activityInfo.packageName;
264 }
265
266 /**
267 * Return the class name of the receiver component that implements
268 * this device admin.
269 */
270 public String getReceiverName() {
271 return mReceiver.activityInfo.name;
272 }
273
274 /**
275 * Return the raw information about the receiver implementing this
276 * device admin. Do not modify the returned object.
277 */
278 public ActivityInfo getActivityInfo() {
279 return mReceiver.activityInfo;
280 }
281
282 /**
283 * Return the component of the receiver that implements this device admin.
284 */
285 public ComponentName getComponent() {
286 return new ComponentName(mReceiver.activityInfo.packageName,
287 mReceiver.activityInfo.name);
288 }
289
290 /**
291 * Load the user-displayed label for this device admin.
292 *
293 * @param pm Supply a PackageManager used to load the device admin's
294 * resources.
295 */
296 public CharSequence loadLabel(PackageManager pm) {
297 return mReceiver.loadLabel(pm);
298 }
299
300 /**
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800301 * Load user-visible description associated with this device admin.
302 *
303 * @param pm Supply a PackageManager used to load the device admin's
304 * resources.
305 */
306 public CharSequence loadDescription(PackageManager pm) throws NotFoundException {
307 if (mReceiver.activityInfo.descriptionRes != 0) {
308 String packageName = mReceiver.resolvePackageName;
309 ApplicationInfo applicationInfo = null;
310 if (packageName == null) {
311 packageName = mReceiver.activityInfo.packageName;
312 applicationInfo = mReceiver.activityInfo.applicationInfo;
313 }
314 return pm.getText(packageName,
315 mReceiver.activityInfo.descriptionRes, applicationInfo);
316 }
317 throw new NotFoundException();
318 }
319
320 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800321 * Load the user-displayed icon for this device admin.
322 *
323 * @param pm Supply a PackageManager used to load the device admin's
324 * resources.
325 */
326 public Drawable loadIcon(PackageManager pm) {
327 return mReceiver.loadIcon(pm);
328 }
329
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800330 /**
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800331 * Returns whether this device admin would like to be visible to the
332 * user, even when it is not enabled.
333 */
334 public boolean isVisible() {
335 return mVisible;
336 }
337
338 /**
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800339 * Return true if the device admin has requested that it be able to use
340 * the given policy control. The possible policy identifier inputs are:
341 * {@link #USES_POLICY_LIMIT_PASSWORD}, {@link #USES_POLICY_WATCH_LOGIN},
Raphaeldc2df322010-02-11 17:01:16 -0800342 * {@link #USES_POLICY_RESET_PASSWORD}, {@link #USES_POLICY_FORCE_LOCK},
Oscar Montemayor69238c62010-08-03 10:51:06 -0700343 * {@link #USES_POLICY_WIPE_DATA}, {@link #USES_POLICY_SETS_GLOBAL_PROXY}.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800344 */
345 public boolean usesPolicy(int policyIdent) {
346 return (mUsesPolicies & (1<<policyIdent)) != 0;
347 }
348
349 /**
350 * Return the XML tag name for the given policy identifier. Valid identifiers
351 * are as per {@link #usesPolicy(int)}. If the given identifier is not
352 * known, null is returned.
353 */
354 public String getTagForPolicy(int policyIdent) {
355 return sRevKnownPolicies.get(policyIdent).tag;
356 }
357
358 /** @hide */
359 public ArrayList<PolicyInfo> getUsedPolicies() {
360 ArrayList<PolicyInfo> res = new ArrayList<PolicyInfo>();
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800361 for (int i=0; i<sPoliciesDisplayOrder.size(); i++) {
362 PolicyInfo pi = sPoliciesDisplayOrder.get(i);
363 if (usesPolicy(pi.ident)) {
364 res.add(pi);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800365 }
366 }
367 return res;
368 }
369
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800370 /** @hide */
371 public void writePoliciesToXml(XmlSerializer out)
372 throws IllegalArgumentException, IllegalStateException, IOException {
373 out.attribute(null, "flags", Integer.toString(mUsesPolicies));
374 }
375
376 /** @hide */
377 public void readPoliciesFromXml(XmlPullParser parser)
378 throws XmlPullParserException, IOException {
379 mUsesPolicies = Integer.parseInt(
380 parser.getAttributeValue(null, "flags"));
381 }
382
Dianne Hackbornd6847842010-01-12 18:14:19 -0800383 public void dump(Printer pw, String prefix) {
384 pw.println(prefix + "Receiver:");
385 mReceiver.dump(pw, prefix + " ");
386 }
387
388 @Override
389 public String toString() {
390 return "DeviceAdminInfo{" + mReceiver.activityInfo.name + "}";
391 }
392
393 /**
394 * Used to package this object into a {@link Parcel}.
395 *
396 * @param dest The {@link Parcel} to be written.
397 * @param flags The flags used for parceling.
398 */
399 public void writeToParcel(Parcel dest, int flags) {
400 mReceiver.writeToParcel(dest, flags);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800401 dest.writeInt(mUsesPolicies);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800402 }
403
404 /**
405 * Used to make this class parcelable.
406 */
407 public static final Parcelable.Creator<DeviceAdminInfo> CREATOR =
408 new Parcelable.Creator<DeviceAdminInfo>() {
409 public DeviceAdminInfo createFromParcel(Parcel source) {
410 return new DeviceAdminInfo(source);
411 }
412
413 public DeviceAdminInfo[] newArray(int size) {
414 return new DeviceAdminInfo[size];
415 }
416 };
417
418 public int describeContents() {
419 return 0;
420 }
421}