begin implementing account authenticator
This commit is contained in:
parent
49b0af50d9
commit
a9fff789f8
|
@ -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"
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package org.mariotaku.twidere.account
|
||||
|
||||
import android.accounts.AbstractAccountAuthenticator
|
||||
import android.content.Context
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 2016/12/2.
|
||||
*/
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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"/>
|
Loading…
Reference in New Issue