Android 8,kotlin实战

@NonNull

private Account[] getAccountsAsUserForPackage(

String type,

int userId,

String callingPackage,

int packageUid,

String opPackageName,

boolean includeUserManagedNotVisible) {

---- // 省略若干方法

long identityToken = clearCallingIdentity();

try {

UserAccounts accounts = getUserAccounts(userId);

return getAccountsInternal(

accounts,

callingUid,

opPackageName,

visibleAccountTypes,

includeUserManagedNotVisible);

} finally {

restoreCallingIdentity(identityToken);

}

}

在 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;

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

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

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

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

img
img

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

总结

最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的14套腾讯、字节跳动、阿里、百度等2021面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。

2020面试真题解析
腾讯面试真题解析

阿里巴巴面试真题解析

字节跳动面试真题解析
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

]

总结

最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的14套腾讯、字节跳动、阿里、百度等2021面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。

[外链图片转存中…(img-D0AX1FYP-1711536238595)]
[外链图片转存中…(img-w0g08jcz-1711536238595)]

[外链图片转存中…(img-6iLcS8Oe-1711536238595)]

[外链图片转存中…(img-K2enqYSG-1711536238596)]
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

[外链图片转存中…(img-ceFA59eN-1711536238596)]

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

  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值