Last active
November 6, 2019 04:50
-
-
Save nein37/4952d950e7c18f9b0d0f to your computer and use it in GitHub Desktop.
AccountManagerでアカウントを管理する ref: https://qiita.com/nein37/items/9aef7e4e06e71990c6e1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.nein37.authenticatorsample"> | |
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" /> | |
<application | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name"> | |
<service | |
android:name=".AuthenticatoinService" | |
android:exported="false"> | |
<intent-filter> | |
<action android:name="android.accounts.AccountAuthenticator" /> | |
</intent-filter> | |
<meta-data | |
android:name="android.accounts.AccountAuthenticator" | |
android:resource="@xml/authenticator" /> | |
</service> | |
<activity | |
android:name=".LoginActivity" | |
android:exported="true" | |
android:label="@string/app_name" /> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AuthenticatoinService extends Service { | |
private MyAuthenticator mAuthenticator; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
mAuthenticator =new MyAuthenticator(this); | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return mAuthenticator.getIBinder(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, | |
String authTokenType, Bundle options) throws NetworkErrorException { | |
AccountManager manager = AccountManager.get(mContext); | |
// キャッシュからトークンを取得 | |
String authToken = manager.peekAuthToken(account,authTokenType); | |
if(TextUtils.isEmpty(authToken)){ | |
// キャッシュが無効なのでトークンを取得 | |
authToken = "AUTH_TOKEN"; | |
manager.setAuthToken(account,authTokenType,authToken); | |
} | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<account-authenticator | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:accountType="com.example.test" | |
android:icon="@drawable/ic_launcher" | |
android:smallIcon="@drawable/ic_launcher" | |
android:label="@string/account_label" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginActivity extends ActionBarActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
final EditText nameEdit = (EditText) findViewById(R.id.name); | |
final EditText passwordEdit = (EditText) findViewById(R.id.password); | |
Button button = (Button) findViewById(R.id.login); | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String name = nameEdit.getText().toString(); | |
String password = passwordEdit.getText().toString(); | |
login(name, password); | |
} | |
}); | |
} | |
// ログイン処理 | |
public void login(final String name, final String password) { | |
// TODO:このメソッドは非同期の通信処理でログインを試みます。 | |
// ログインに成功した場合、loginSuccess()を呼び出します。 | |
loginSuccess(name, password); | |
} | |
// ログイン処理のコールバック | |
public void loginSuccess(final String name, final String password) { | |
Account account = new Account(name, "com.example.test"); | |
AccountManager am = AccountManager.get(this); | |
// アカウント情報を保存 | |
// TODO:本来はパスワードを暗号化する必要があります | |
am.addAccountExplicitly(account, password, null); | |
// 認証画面終了 | |
setResult(RESULT_OK); | |
finish(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyAuthenticator extends AbstractAccountAuthenticator { | |
public static final String ACCOUNT_TYPE = "com.example.test"; | |
final Context mContext; | |
public MyAuthenticator(Context context) { | |
super(context); | |
mContext = context; | |
} | |
@Override | |
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, | |
String authTokenType, String[] requiredFeatures, Bundle options) | |
throws NetworkErrorException { | |
// アカウントの追加を行う画面を呼び出すIntentを生成 | |
final Intent intent = new Intent(mContext, LoginActivity.class); | |
// アカウント追加後、戻り先の画面を設定 | |
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); | |
// Intentを返却 | |
final Bundle bundle = new Bundle(); | |
bundle.putParcelable(AccountManager.KEY_INTENT, intent); | |
return bundle; | |
} | |
@Override | |
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, | |
String authTokenType, Bundle options) throws NetworkErrorException { | |
AccountManager manager = AccountManager.get(mContext); | |
String name = account.name; | |
// TODO:本来はパスワードを復号化する必要があります | |
String password = manager.getPassword(account); | |
// TODO:本来はここで通信を行い、ユーザ名とパスワードからトークンの取得を行う | |
String authToken = "AUTH_TOKEN"; | |
// トークンをキャッシュ | |
manager.setAuthToken(account,authTokenType,authToken); | |
// トークンを返却する | |
Bundle result = new Bundle(); | |
result.putString(AccountManager.KEY_ACCOUNT_NAME, name); | |
result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE); | |
result.putString(AccountManager.KEY_AUTHTOKEN, authToken); | |
return result; | |
} | |
@Override | |
public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { | |
return null; | |
} | |
@Override | |
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, | |
Bundle options) throws NetworkErrorException { | |
return null; | |
} | |
@Override | |
public String getAuthTokenLabel(String authTokenType) { | |
return null; | |
} | |
@Override | |
public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, | |
String authTokenType, Bundle options) throws NetworkErrorException { | |
return null; | |
} | |
@Override | |
public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, | |
String[] features) throws NetworkErrorException { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment