accounts账号管理分析

accounts账号管理分析

源码路径frameworks\base\core\java\android\accounts

原生代码主要提供有

3个接口:

AccountManagerCallback<V>

AccountManagerFuture<V>

OnAccountsUpdateListener

6个类:

Account

AccountManager

AbstractAccountAuthenticator

AccountAuthenticatorActivity

AccountAuthenticatorResponse

AuthenticatorDescription

4个异常处理:

AccountsException

AuthenticatorException

NetworkErrorException

OperationCanceledException

一.  接口

OnAccountsUpdateListener

public interface OnAccountsUpdateListener {

    void onAccountsUpdated(Account[] accounts);

}

AccountManagerFuture<V>

package android.accounts;

import java.util.concurrent.TimeUnit;

import java.io.IOException;

public interface AccountManagerFuture<V> {

    boolean cancel(boolean mayInterruptIfRunning);

    boolean isCancelled();

    boolean isDone();

    V getResult() throws OperationCanceledException, IOException, AuthenticatorException;

    V getResult(long timeout, TimeUnit unit)

            throws OperationCanceledException, IOException, AuthenticatorException;

}

AccountManagerCallback<V> 

public interface AccountManagerCallback<V> {

    void run(AccountManagerFuture<V> future);

}

二。类

Account——表示AccountManager中的帐户的值类型。这个对象是Parcelable的,并且重写了equals(Object)和hashCode(),因此它适合作为Map的键使用。

  public final String name;

  public final String type;

    public boolean equals(Object o) {

        if (o == this) return true;

        if (!(o instanceof Account)) return false;

        final Account other = (Account)o;

        return name.equals(other.name) && type.equals(other.type);

    }

    public int hashCode() {

        int result = 17;

        result = 31 * result + name.hashCode();

        result = 31 * result + type.hashCode();

        return result;

    }

AccountManager——管理设备上的所有帐户,并运行该显示。应用程序可以从它请求auth- token,这是它的工作。无论它是否意味着它需要打开一个新的“登录”/“创建帐户”活动,或者检索先前请求的存储的auth- token,AccountManager知道要调用谁以及在每个场景中做什么来完成任务。

addAccount() :添加一个帐户。

addAccountExplicitly(): 直接添加一个帐户到AccountManager。

getAccounts():获取所有帐户。

getAccountsByType(String package):获取指定的账号

removeAccount():删除帐户

AccountAuthenticator——一个处理特定帐户类型的模块。AccountManager找到合适的AccountAuthenticator与它进行对话,以执行account类型上的所有操作。AccountAuthenticator知道向用户显示哪些活动来输入他的凭据,以及在哪里找到服务器已经返回的任何存储的auth- token。在单一帐户类型下,这可能是许多不同服务的共同之处。例如,谷歌在Android上的authenticator正在验证谷歌邮件服务(Gmail)以及谷歌日历和谷歌驱动器等其他谷歌服务。

AbstractAccountAuthenticator——创建AccountAuthenticator的抽象基类。为了成为身份验证者,必须扩展该类,为抽象方法提供实现,并编写一个服务,该服务将返回服务的Service.onBind(android.content.Intent)中getIBinder()的结果。此服务必须在其AndroidManifest.xml文件中指定以下意图筛选器和元数据标记

<intent-filter>

     <action android:name="android.accounts.AccountAuthenticator" />

   </intent-filter>

   <meta-data android:name="android.accounts.AccountAuthenticator"

             android:resource="@xml/authenticator" />

在实现类中需要复写他的抽象方法,一共7个。如下:

1.editProperties:返回一个Bundle,其中包含可用于编辑属性的活动的Intent,如果无返回,可以直接返回null,或者抛出常,例如new UnsupportedOperationException()。

2.addAccount:添加指定的accountType的帐户。(重要)当用户在添加账户页面选择账户进行添加或者调用accountManager.addAccount 的时候,AbstractAccountAuthenticator中的addAccount 方法会被默认调用,因此需要重写addAccount 方法。

3.confirmCredentials:检查用户是否知道帐户的凭据。连接服务器进行身份校验。如果无校验可以返回返回null,或者抛出常。

4.getAuthToken:获得一个账户authtoken。(重要)当执行AccountAuthenticatorActivity中的mAccountManager.blockingGetAuthToken(account,Constants.AUTHTOKEN_TYPE,NOTIFY_AUTH_FAILURE);时调用该方法。如果之前成功获取过AuthenToken会缓存,之后不会在调用getAuthenToken()方法,除非调用invalidateAuthenToken()。

5.getAuthTokenLabel:向认证者询问给定的authTokenType的本地化标签。如果无,可以直接返回null,或者抛出异常。

6.updateCredentials:更新帐户的本地存储的凭据。如果无更新,可以直接返回null,或者抛出异常。

7.hasFeatures:检查帐户是否支持所有指定的验证器特定功能。如果不需要可以直接返回null,或者抛出异常。

AccountAuthenticatorResponse--序列化的对象类, 用于将响应传达回 AccountManager 的对象

private IAccountAuthenticatorResponse mAccountAuthenticatorResponse;

/**

* @hide

*/

@UnsupportedAppUsage

public AccountAuthenticatorResponse(IAccountAuthenticatorResponse response) {

    mAccountAuthenticatorResponse = response;

}

public AccountAuthenticatorResponse(Parcel parcel) {

    mAccountAuthenticatorResponse =

            IAccountAuthenticatorResponse.Stub.asInterface(parcel.readStrongBinder());

}

AuthenticatorDescription-序列化的对象类,包含有关帐户验证器信息的可包裹值类型。

public AuthenticatorDescription(String type, String packageName, int labelId, int iconId,int smallIconId, int prefId, boolean customTokens)

完整 AuthenticatorDescription 的构造函数

AccountAuthenticatorActivity——基类被称为“登录/创建账户”活动的身份当用户需要确定自己。该活动负责对服务器的登录或帐户创建过程,并将auth- token返回给调用authenticator。 这个类在API级别30中被废弃。

public final void setAccountAuthenticatorResult(Bundle result) {

    mResultBundle = result;

}

protected void onCreate(Bundle icicle) {

    super.onCreate(icicle);

    mAccountAuthenticatorResponse =

            getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);

    if (mAccountAuthenticatorResponse != null) {

        mAccountAuthenticatorResponse.onRequestContinued();

    }

}

public void finish() {

    if (mAccountAuthenticatorResponse != null) {

        // send the result bundle back if set, otherwise send an error.

        if (mResultBundle != null) {

            mAccountAuthenticatorResponse.onResult(mResultBundle);

        } else {

            mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED,

                    "canceled");

        }

        mAccountAuthenticatorResponse = null;

    }

    super.finish();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代我西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值