| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.admin; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.ComponentName; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.pm.ActivityInfo; |
| 24 |
import android.content.pm.PackageManager; |
| 25 |
import android.content.pm.PackageManager.NameNotFoundException; |
| 26 |
import android.content.pm.ResolveInfo; |
| 27 |
import android.content.res.Resources; |
| 28 |
import android.content.res.Resources.NotFoundException; |
| 29 |
import android.content.res.TypedArray; |
| 30 |
import android.content.res.XmlResourceParser; |
| 31 |
import android.graphics.drawable.Drawable; |
| 32 |
import android.os.Build; |
| 33 |
import android.os.Parcel; |
| 34 |
import android.os.Parcelable; |
| 35 |
import android.os.PersistableBundle; |
| 36 |
import android.util.AttributeSet; |
| 37 |
import android.util.Log; |
| 38 |
import android.util.Printer; |
| 39 |
import android.util.SparseArray; |
| 40 |
import android.util.TypedXmlPullParser; |
| 41 |
import android.util.TypedXmlSerializer; |
| 42 |
import android.util.Xml; |
| 43 |
|
| 44 |
import org.xmlpull.v1.XmlPullParser; |
| 45 |
import org.xmlpull.v1.XmlPullParserException; |
| 46 |
|
| 47 |
import java.io.IOException; |
| 48 |
import java.util.ArrayList; |
| 49 |
import java.util.HashMap; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public final class DeviceAdminInfo implements Parcelable { |
| 56 |
static final String TAG = "DeviceAdminInfo"; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static final int USES_POLICY_LIMIT_PASSWORD = 0; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public static final int USES_POLICY_WATCH_LOGIN = 1; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public static final int USES_POLICY_RESET_PASSWORD = 2; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static final int USES_POLICY_FORCE_LOCK = 3; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public static final int USES_POLICY_WIPE_DATA = 4; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public static final int USES_POLICY_SETS_GLOBAL_PROXY = 5; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public static final int USES_POLICY_EXPIRE_PASSWORD = 6; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public static final int USES_ENCRYPTED_STORAGE = 7; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public static final int USES_POLICY_DISABLE_CAMERA = 8; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public static final int USES_POLICY_DISABLE_KEYGUARD_FEATURES = 9; |
| 159 |
|
| 160 |
|
| 161 |
public static class PolicyInfo { |
| 162 |
public final int ident; |
| 163 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 164 |
public final String tag; |
| 165 |
public final int label; |
| 166 |
public final int description; |
| 167 |
public final int labelForSecondaryUsers; |
| 168 |
public final int descriptionForSecondaryUsers; |
| 169 |
|
| 170 |
public PolicyInfo(int ident, String tag, int label, int description) { |
| 171 |
this(ident, tag, label, description, label, description); |
| 172 |
} |
| 173 |
|
| 174 |
public PolicyInfo(int ident, String tag, int label, int description, |
| 175 |
int labelForSecondaryUsers, int descriptionForSecondaryUsers) { |
| 176 |
this.ident = ident; |
| 177 |
this.tag = tag; |
| 178 |
this.label = label; |
| 179 |
this.description = description; |
| 180 |
this.labelForSecondaryUsers = labelForSecondaryUsers; |
| 181 |
this.descriptionForSecondaryUsers = descriptionForSecondaryUsers; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
static ArrayList<PolicyInfo> sPoliciesDisplayOrder = new ArrayList<PolicyInfo>(); |
| 186 |
static HashMap<String, Integer> sKnownPolicies = new HashMap<String, Integer>(); |
| 187 |
static SparseArray<PolicyInfo> sRevKnownPolicies = new SparseArray<PolicyInfo>(); |
| 188 |
|
| 189 |
static { |
| 190 |
sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_WIPE_DATA, "wipe-data", |
| 191 |
com.android.internal.R.string.policylab_wipeData, |
| 192 |
com.android.internal.R.string.policydesc_wipeData, |
| 193 |
com.android.internal.R.string.policylab_wipeData_secondaryUser, |
| 194 |
com.android.internal.R.string.policydesc_wipeData_secondaryUser |
| 195 |
)); |
| 196 |
sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_RESET_PASSWORD, "reset-password", |
| 197 |
com.android.internal.R.string.policylab_resetPassword, |
| 198 |
com.android.internal.R.string.policydesc_resetPassword)); |
| 199 |
sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_LIMIT_PASSWORD, "limit-password", |
| 200 |
com.android.internal.R.string.policylab_limitPassword, |
| 201 |
com.android.internal.R.string.policydesc_limitPassword)); |
| 202 |
sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_WATCH_LOGIN, "watch-login", |
| 203 |
com.android.internal.R.string.policylab_watchLogin, |
| 204 |
com.android.internal.R.string.policydesc_watchLogin, |
| 205 |
com.android.internal.R.string.policylab_watchLogin, |
| 206 |
com.android.internal.R.string.policydesc_watchLogin_secondaryUser |
| 207 |
)); |
| 208 |
sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_FORCE_LOCK, "force-lock", |
| 209 |
com.android.internal.R.string.policylab_forceLock, |
| 210 |
com.android.internal.R.string.policydesc_forceLock)); |
| 211 |
sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_SETS_GLOBAL_PROXY, "set-global-proxy", |
| 212 |
com.android.internal.R.string.policylab_setGlobalProxy, |
| 213 |
com.android.internal.R.string.policydesc_setGlobalProxy)); |
| 214 |
sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_EXPIRE_PASSWORD, "expire-password", |
| 215 |
com.android.internal.R.string.policylab_expirePassword, |
| 216 |
com.android.internal.R.string.policydesc_expirePassword)); |
| 217 |
sPoliciesDisplayOrder.add(new PolicyInfo(USES_ENCRYPTED_STORAGE, "encrypted-storage", |
| 218 |
com.android.internal.R.string.policylab_encryptedStorage, |
| 219 |
com.android.internal.R.string.policydesc_encryptedStorage)); |
| 220 |
sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_DISABLE_CAMERA, "disable-camera", |
| 221 |
com.android.internal.R.string.policylab_disableCamera, |
| 222 |
com.android.internal.R.string.policydesc_disableCamera)); |
| 223 |
sPoliciesDisplayOrder.add(new PolicyInfo( |
| 224 |
USES_POLICY_DISABLE_KEYGUARD_FEATURES, "disable-keyguard-features", |
| 225 |
com.android.internal.R.string.policylab_disableKeyguardFeatures, |
| 226 |
com.android.internal.R.string.policydesc_disableKeyguardFeatures)); |
| 227 |
|
| 228 |
for (int i=0; i<sPoliciesDisplayOrder.size(); i++) { |
| 229 |
PolicyInfo pi = sPoliciesDisplayOrder.get(i); |
| 230 |
sRevKnownPolicies.put(pi.ident, pi); |
| 231 |
sKnownPolicies.put(pi.tag, pi.ident); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
final ActivityInfo mActivityInfo; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
boolean mVisible; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
int mUsesPolicies; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
boolean mSupportsTransferOwnership; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public DeviceAdminInfo(Context context, ResolveInfo resolveInfo) |
| 265 |
throws XmlPullParserException, IOException { |
| 266 |
this(context, resolveInfo.activityInfo); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public DeviceAdminInfo(Context context, ActivityInfo activityInfo) |
| 278 |
throws XmlPullParserException, IOException { |
| 279 |
mActivityInfo = activityInfo; |
| 280 |
|
| 281 |
PackageManager pm = context.getPackageManager(); |
| 282 |
|
| 283 |
XmlResourceParser parser = null; |
| 284 |
try { |
| 285 |
parser = mActivityInfo.loadXmlMetaData(pm, DeviceAdminReceiver.DEVICE_ADMIN_META_DATA); |
| 286 |
if (parser == null) { |
| 287 |
throw new XmlPullParserException("No " |
| 288 |
+ DeviceAdminReceiver.DEVICE_ADMIN_META_DATA + " meta-data"); |
| 289 |
} |
| 290 |
|
| 291 |
Resources res = pm.getResourcesForApplication(mActivityInfo.applicationInfo); |
| 292 |
|
| 293 |
AttributeSet attrs = Xml.asAttributeSet(parser); |
| 294 |
|
| 295 |
int type; |
| 296 |
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 297 |
&& type != XmlPullParser.START_TAG) { |
| 298 |
} |
| 299 |
|
| 300 |
String nodeName = parser.getName(); |
| 301 |
if (!"device-admin".equals(nodeName)) { |
| 302 |
throw new XmlPullParserException( |
| 303 |
"Meta-data does not start with device-admin tag"); |
| 304 |
} |
| 305 |
|
| 306 |
TypedArray sa = res.obtainAttributes(attrs, |
| 307 |
com.android.internal.R.styleable.DeviceAdmin); |
| 308 |
|
| 309 |
mVisible = sa.getBoolean( |
| 310 |
com.android.internal.R.styleable.DeviceAdmin_visible, true); |
| 311 |
|
| 312 |
sa.recycle(); |
| 313 |
|
| 314 |
int outerDepth = parser.getDepth(); |
| 315 |
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 316 |
&& (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 317 |
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 318 |
continue; |
| 319 |
} |
| 320 |
String tagName = parser.getName(); |
| 321 |
if (tagName.equals("uses-policies")) { |
| 322 |
int innerDepth = parser.getDepth(); |
| 323 |
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 324 |
&& (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) { |
| 325 |
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 326 |
continue; |
| 327 |
} |
| 328 |
String policyName = parser.getName(); |
| 329 |
Integer val = sKnownPolicies.get(policyName); |
| 330 |
if (val != null) { |
| 331 |
mUsesPolicies |= 1 << val.intValue(); |
| 332 |
} else { |
| 333 |
Log.w(TAG, "Unknown tag under uses-policies of " |
| 334 |
+ getComponent() + ": " + policyName); |
| 335 |
} |
| 336 |
} |
| 337 |
} else if (tagName.equals("support-transfer-ownership")) { |
| 338 |
if (parser.next() != XmlPullParser.END_TAG) { |
| 339 |
throw new XmlPullParserException( |
| 340 |
"support-transfer-ownership tag must be empty."); |
| 341 |
} |
| 342 |
mSupportsTransferOwnership = true; |
| 343 |
} |
| 344 |
} |
| 345 |
} catch (NameNotFoundException e) { |
| 346 |
throw new XmlPullParserException( |
| 347 |
"Unable to create context for: " + mActivityInfo.packageName); |
| 348 |
} finally { |
| 349 |
if (parser != null) parser.close(); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
DeviceAdminInfo(Parcel source) { |
| 354 |
mActivityInfo = ActivityInfo.CREATOR.createFromParcel(source); |
| 355 |
mUsesPolicies = source.readInt(); |
| 356 |
mSupportsTransferOwnership = source.readBoolean(); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public String getPackageName() { |
| 363 |
return mActivityInfo.packageName; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public String getReceiverName() { |
| 371 |
return mActivityInfo.name; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public ActivityInfo getActivityInfo() { |
| 379 |
return mActivityInfo; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
@NonNull |
| 386 |
public ComponentName getComponent() { |
| 387 |
return new ComponentName(mActivityInfo.packageName, |
| 388 |
mActivityInfo.name); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public CharSequence loadLabel(PackageManager pm) { |
| 398 |
return mActivityInfo.loadLabel(pm); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public CharSequence loadDescription(PackageManager pm) throws NotFoundException { |
| 408 |
if (mActivityInfo.descriptionRes != 0) { |
| 409 |
return pm.getText(mActivityInfo.packageName, |
| 410 |
mActivityInfo.descriptionRes, mActivityInfo.applicationInfo); |
| 411 |
} |
| 412 |
throw new NotFoundException(); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public Drawable loadIcon(PackageManager pm) { |
| 422 |
return mActivityInfo.loadIcon(pm); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public boolean isVisible() { |
| 430 |
return mVisible; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public boolean usesPolicy(int policyIdent) { |
| 443 |
return (mUsesPolicies & (1<<policyIdent)) != 0; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public String getTagForPolicy(int policyIdent) { |
| 452 |
return sRevKnownPolicies.get(policyIdent).tag; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
public boolean supportsTransferOwnership() { |
| 459 |
return mSupportsTransferOwnership; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 464 |
public ArrayList<PolicyInfo> getUsedPolicies() { |
| 465 |
ArrayList<PolicyInfo> res = new ArrayList<PolicyInfo>(); |
| 466 |
for (int i=0; i<sPoliciesDisplayOrder.size(); i++) { |
| 467 |
PolicyInfo pi = sPoliciesDisplayOrder.get(i); |
| 468 |
if (usesPolicy(pi.ident)) { |
| 469 |
res.add(pi); |
| 470 |
} |
| 471 |
} |
| 472 |
return res; |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
public void writePoliciesToXml(TypedXmlSerializer out) |
| 477 |
throws IllegalArgumentException, IllegalStateException, IOException { |
| 478 |
out.attributeInt(null, "flags", mUsesPolicies); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
public void readPoliciesFromXml(TypedXmlPullParser parser) |
| 483 |
throws XmlPullParserException, IOException { |
| 484 |
mUsesPolicies = parser.getAttributeInt(null, "flags"); |
| 485 |
} |
| 486 |
|
| 487 |
public void dump(Printer pw, String prefix) { |
| 488 |
pw.println(prefix + "Receiver:"); |
| 489 |
mActivityInfo.dump(pw, prefix + " "); |
| 490 |
} |
| 491 |
|
| 492 |
@Override |
| 493 |
public String toString() { |
| 494 |
return "DeviceAdminInfo{" + mActivityInfo.name + "}"; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
public void writeToParcel(Parcel dest, int flags) { |
| 504 |
mActivityInfo.writeToParcel(dest, flags); |
| 505 |
dest.writeInt(mUsesPolicies); |
| 506 |
dest.writeBoolean(mSupportsTransferOwnership); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
public static final @android.annotation.NonNull Parcelable.Creator<DeviceAdminInfo> CREATOR = |
| 513 |
new Parcelable.Creator<DeviceAdminInfo>() { |
| 514 |
public DeviceAdminInfo createFromParcel(Parcel source) { |
| 515 |
return new DeviceAdminInfo(source); |
| 516 |
} |
| 517 |
|
| 518 |
public DeviceAdminInfo[] newArray(int size) { |
| 519 |
return new DeviceAdminInfo[size]; |
| 520 |
} |
| 521 |
}; |
| 522 |
|
| 523 |
public int describeContents() { |
| 524 |
return 0; |
| 525 |
} |
| 526 |
} |