begin implementing account authenticator

This commit is contained in:
Mariotaku Lee 2016-12-02 10:15:07 +08:00
parent 49b0af50d9
commit a9fff789f8
4 changed files with 115 additions and 0 deletions

View File

@ -461,6 +461,15 @@
<service
android:name=".service.BackgroundOperationService"
android:label="@string/label_background_operation_service"/>
<service android:name=".service.AccountAuthenticatorService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator"/>
</service>
<service
android:name=".nyan.NyanWallpaperService"
android:enabled="false"

View File

@ -0,0 +1,8 @@
package org.mariotaku.twidere.account
import android.accounts.AbstractAccountAuthenticator
import android.content.Context
/**
* Created by mariotaku on 2016/12/2.
*/

View File

@ -0,0 +1,92 @@
package org.mariotaku.twidere.service
import android.accounts.AbstractAccountAuthenticator
import android.accounts.Account
import android.accounts.AccountAuthenticatorResponse
import android.accounts.AccountManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.IBinder
import org.mariotaku.ktextension.set
import org.mariotaku.twidere.activity.SignInActivity
/**
* Created by mariotaku on 2016/12/2.
*/
class AccountAuthenticatorService : Service() {
private lateinit var authenticator: TwidereAccountAuthenticator
override fun onCreate() {
super.onCreate()
authenticator = TwidereAccountAuthenticator(this)
}
override fun onBind(intent: Intent): IBinder {
return authenticator.iBinder
}
internal class TwidereAccountAuthenticator(val context: Context) : AbstractAccountAuthenticator(context) {
// TODO: Make SignInActivity comply with AccountAuthenticatorActivity
override fun addAccount(response: AccountAuthenticatorResponse, accountType: String,
authTokenType: String?, requiredFeatures: Array<String>?,
options: Bundle?): Bundle {
val intent = Intent(context, SignInActivity::class.java)
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response)
val result = Bundle()
result[AccountManager.KEY_INTENT] = intent
return result
}
override fun getAuthToken(response: AccountAuthenticatorResponse, account: Account, authTokenType: String, options: Bundle?): Bundle {
val am = AccountManager.get(context)
val authToken = am.peekAuthToken(account, authTokenType)
if (authToken.isNullOrEmpty()) {
val intent = Intent(context, SignInActivity::class.java)
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response)
val result = Bundle()
result[AccountManager.KEY_INTENT] = intent
return result
}
val result = Bundle()
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name)
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type)
result.putString(AccountManager.KEY_AUTHTOKEN, authToken)
return result
}
override fun confirmCredentials(response: AccountAuthenticatorResponse, account: Account, options: Bundle?): Bundle {
val result = Bundle()
result[AccountManager.KEY_BOOLEAN_RESULT] = true
return result
}
override fun editProperties(response: AccountAuthenticatorResponse, accountType: String): Bundle {
val result = Bundle()
result[AccountManager.KEY_BOOLEAN_RESULT] = true
return result
}
override fun getAuthTokenLabel(authTokenType: String): String {
return authTokenType
}
override fun hasFeatures(response: AccountAuthenticatorResponse, account: Account, features: Array<String>): Bundle {
val result = Bundle()
result[AccountManager.KEY_BOOLEAN_RESULT] = true
return result
}
override fun updateCredentials(response: AccountAuthenticatorResponse, account: Account,
authTokenType: String, options: Bundle?): Bundle {
val result = Bundle()
result[AccountManager.KEY_BOOLEAN_RESULT] = true
return result
}
}
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="org.mariotaku.twidere.account"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"/>