Android 8(1),源码解析

在 getAccountsAsUserForPackage 方法里面,经过一系列的判断,最终又会调用到 getAccountsInternal 方法。

@NonNull

private Account[] getAccountsInternal(

UserAccounts userAccounts,

int callingUid,

String callingPackage,

List visibleAccountTypes,

boolean includeUserManagedNotVisible) {

ArrayList visibleAccounts = new ArrayList<>();

for (String visibleType : visibleAccountTypes) {

Account[] accountsForType = getAccountsFromCache(

userAccounts, visibleType, callingUid, callingPackage,

includeUserManagedNotVisible);

if (accountsForType != null) {

visibleAccounts.addAll(Arrays.asList(accountsForType));

}

}

Account[] result = new Account[visibleAccounts.size()];

for (int i = 0; i < visibleAccounts.size(); i++) {

result[i] = visibleAccounts.get(i);

}

return result;

}

在 getAccountsInternal 方法里面,又会调用 getAccountsFromCache 去获取结果

protected Account[] getAccountsFromCache(UserAccounts userAccounts, String accountType,

int callingUid, @Nullable String callingPackage, boolean includeManagedNotVisible) {

Preconditions.checkState(!Thread.holdsLock(userAccounts.cacheLock),

“Method should not be called with cacheLock”);

if (accountType != null) {

Account[] accounts;

synchronized (userAccounts.cacheLock) {

accounts = userAccounts.accountCache.get(accountType);

}

if (accounts == null) {

return EMPTY_ACCOUNT_ARRAY;

} else {

return filterAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length),

callingUid, callingPackage, includeManagedNotVisible);

}

} else {

int totalLength = 0;

Account[] accountsArray;

synchronized (userAccounts.cacheLock) {

for (Account[] accounts : userAccounts.accountCache.values()) {

totalLength += accounts.length;

}

if (totalLength == 0) {

return EMPTY_ACCOUNT_ARRAY;

}

accountsArray = new Account[totalLength];

totalLength = 0;

for (Account[] accountsOfType : userAccounts.accountCache.values()) {

System.arraycopy(accountsOfType, 0, accountsArray, totalLength,

accountsOfType.length);

totalLength += accountsOfType.length;

}

}

return filterAccounts(userAccounts, accountsArray, callingUid, callingPackage,

includeManagedNotVisible);

}

}

而在 getAccountsFromCache 里面,不管 accountType 是否为空,最终都会调用到 filterAccounts 方法。

@NonNull

private Account[] filterAccounts(UserAccounts accounts, Account[] unfiltered, int callingUid,

@Nullable String callingPackage, boolean includeManagedNotVisible) {

String visibilityFilterPackage = callingPackage;

if (visibilityFilterPackage == null) {

visibilityFilterPackage = getPackageNameForUid(callingUid);

}

Map<Account, Integer> firstPass = new LinkedHashMap<>();

for (Account account : unfiltered) {

int visibility = resolveAccountVisibility(account, visibilityFilterPackage, accounts);

if ((visibility == AccountManager.VISIBILITY_VISIBLE

|| visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE)

|| (includeManagedNotVisible

&& (visibility

== AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE))) {

firstPass.put(account, visibility);

}

}

Map<Account, Integer> secondPass =

filterSharedAccounts(accounts, firstPass, callingUid, callingPackage);

Account[] filtered = new Account[secondPass.size()];

filtered = secondPass.keySet().toArray(filtered);

return filtered;

}

在 filterAccounts 里面,又会调用 resolveAccountVisibility 去判断我们的 CallerApp 是否可以访问我们的 Account。

private Integer resolveAccountVisibility(Account account, @NonNull String packageName,

UserAccounts accounts) {

Preconditions.checkNotNull(packageName, “packageName cannot be null”);

int uid = -1;

try {

long identityToken = clearCallingIdentity();

try {

uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId);

} finally {

restoreCallingIdentity(identityToken);

}

} catch (NameNotFoundException e) {

Log.d(TAG, "Package not found " + e.getMessage());

return AccountManager.VISIBILITY_NOT_VISIBLE;

}

// System visibility can not be restricted.

if (UserHandle.isSameApp(uid, Process.SYSTEM_UID)) {

return AccountManager.VISIBILITY_VISIBLE;

}

int signatureCheckResult =

checkPackageSignature(account.type, uid, accounts.userId);

// Authenticator can not restrict visibility to itself.

if (signatureCheckResult == SIGNATURE_CHECK_UID_MATCH) {

return AccountManager.VISIBILITY_VISIBLE; // Authenticator can always see the account

}

// Return stored value if it was set.

int visibility = getAccountVisibilityFromCache(account, packageName, accounts);

if (AccountManager.VISIBILITY_UNDEFINED != visibility) {

return visibility;

}

boolean isPrivileged = isPermittedForPackage(packageName, uid, accounts.userId,

Manifest.permission.GET_ACCOUNTS_PRIVILEGED);

// Device/Profile owner gets visibility by default.

if (isProfileOwner(uid)) {

return AccountManager.VISIBILITY_VISIBLE;

}

// target sdk < 26

boolean preO = isPreOApplication(packageName);

// 签名是否一致

// target sdk < 26 小于 26 ,并且拥有 Manifest.permission.GET_ACCOUNTS 权限

// CallerApp 拥有 Manifest.permission.READ_CONTACTS ,authenticator APP 拥有 Manifest.permission.WRITE_CONTACTS

// 是否拥有 Manifest.permission.GET_ACCOUNTS_PRIVILEGED 权限

if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH)

|| (preO && checkGetAccountsPermission(packageName, uid, accounts.userId))

|| (checkReadContactsPermission(packageName, uid, accounts.userId)

&& accountTypeManagesContacts(account.type, accounts.userId))

|| isPrivileged) {

// Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature

// match.

visibility = getAccountVisibilityFromCache(account,

AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE, accounts);

if (AccountManager.VISIBILITY_UNDEFINED == visibility) {

visibility = AccountManager.VISIBILITY_USER_MANAGED_VISIBLE;

}

} else {

visibility = getAccountVisibilityFromCache(account,

AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, accounts);

if (AccountManager.VISIBILITY_UNDEFINED == visibility) {

visibility = AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE;

}

}

return visibility;

}

我们主要看这一段就好,从这一段我们可以看到满足下列其中一个条件,即可获取得到相关的 Account 信息。

  • 签名一致

  • target sdk < 26 小于 26 ,并且拥有 Manifest.permission.GET_ACCOUNTS 权限

  • CallerApp 拥有 Manifest.permission.READ_CONTACTS ,authenticator APP 拥有 Manifest.permission.WRITE_CONTACTS

  • 拥有 Manifest.permission.GET_ACCOUNTS_PRIVILEGED 权限

// target sdk < 26

boolean preO = isPreOApplication(packageName);

// 签名是否一致

// target sdk < 26 小于 26 ,并且拥有 Manifest.permission.GET_ACCOUNTS 权限

// CallerApp 拥有 Manifest.permission.READ_CONTACTS ,authenticator APP 拥有 Manifest.permission.WRITE_CONTACTS

// 是否拥有 Manifest.permission.GET_ACCOUNTS_PRIVILEGED 权限

if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH)

|| (preO && checkGetAccountsPermission(packageName, uid, accounts.userId))

|| (checkReadContactsPermission(packageName, uid, accounts.userId)

&& accountTypeManagesContacts(account.type, accounts.userId))

|| isPrivileged) {

// Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature

// match.

visibility = getAccountVisibilityFromCache(account,

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

img
img

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V:vip204888 备注Android获取(资料价值较高,非无偿)
img

最后

最后这里放上我这段时间复习的资料,这个资料也是偶然一位朋友分享给我的,里面包含了腾讯、字节跳动、阿里、百度2020-2021面试真题解析,并且把每个技术点整理成了视频和PDF(知识脉络 + 诸多细节)。

还有 高级架构技术进阶脑图、高级进阶架构资料 帮助大家学习提升进阶,这里我也免费分享给大家也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

一起互勉~

面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**
如果你觉得这些内容对你有帮助,可以添加V:vip204888 备注Android获取(资料价值较高,非无偿)
[外链图片转存中…(img-gPvG4671-1711536203521)]

最后

最后这里放上我这段时间复习的资料,这个资料也是偶然一位朋友分享给我的,里面包含了腾讯、字节跳动、阿里、百度2020-2021面试真题解析,并且把每个技术点整理成了视频和PDF(知识脉络 + 诸多细节)。

还有 高级架构技术进阶脑图、高级进阶架构资料 帮助大家学习提升进阶,这里我也免费分享给大家也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

[外链图片转存中…(img-SSha55p8-1711536203521)]

[外链图片转存中…(img-K0NhwMeO-1711536203523)]

一起互勉~

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

  • 10
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值