Skip to content

Commit

Permalink
feature(account): add custom account
Browse files Browse the repository at this point in the history
  • Loading branch information
yueban committed Nov 30, 2019
1 parent 3759127 commit 4ce5e36
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -2,7 +2,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yueban.customcontact">

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />

<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -16,6 +20,18 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".account.AuthenticatorService"
android:exported="true">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>

<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/auth" />
</service>
</application>

</manifest>
3 changes: 3 additions & 0 deletions app/src/main/java/com/yueban/customcontact/MainActivity.kt
Expand Up @@ -2,11 +2,14 @@ package com.yueban.customcontact

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.yueban.customcontact.account.CustomAccountManger

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

CustomAccountManger.addAccount()
}
}
23 changes: 23 additions & 0 deletions app/src/main/java/com/yueban/customcontact/MyApp.kt
@@ -0,0 +1,23 @@
package com.yueban.customcontact

import android.app.Application
import android.content.Context

/**
* @author yueban fbzhh007@gmail.com
* @date 2019-11-30
*/
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
context = this
}

companion object {
private lateinit var context: MyApp

fun getContext(): Context {
return context
}
}
}
@@ -0,0 +1,96 @@
package com.yueban.customcontact.account

import android.accounts.*
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.IBinder

/**
* @author yueban fbzhh007@gmail.com
* @date 2019-11-30
*/
class AuthenticatorService : Service() {
private var authenticator: Authenticator? = null

protected fun getAuthenticator(): Authenticator {
if (authenticator == null) {
authenticator = Authenticator(this)
}
return authenticator!!
}

override fun onBind(intent: Intent): IBinder? {
return if (AccountManager.ACTION_AUTHENTICATOR_INTENT == intent.action) {
getAuthenticator().iBinder
} else {
null
}
}

class Authenticator internal constructor(context: Context?) :
AbstractAccountAuthenticator(context) {
override fun addAccount(
response: AccountAuthenticatorResponse,
accountType: String,
authTokenType: String,
requiredFeatures: Array<String>,
options: Bundle
): Bundle? {
return null
}

@Throws(NetworkErrorException::class)
override fun getAccountRemovalAllowed(
response: AccountAuthenticatorResponse,
account: Account
): Bundle {
return super.getAccountRemovalAllowed(response, account)
}

override fun confirmCredentials(
response: AccountAuthenticatorResponse?,
account: Account?,
options: Bundle?
): Bundle? {
return null
}

override fun editProperties(
response: AccountAuthenticatorResponse,
accountType: String
): Bundle? {
return null
}

override fun getAuthToken(
response: AccountAuthenticatorResponse,
account: Account,
authTokenType: String,
options: Bundle
): Bundle? {
return null
}

override fun getAuthTokenLabel(authTokenType: String): String? {
return null
}

override fun hasFeatures(
response: AccountAuthenticatorResponse,
account: Account,
features: Array<String>
): Bundle? {
return null
}

override fun updateCredentials(
response: AccountAuthenticatorResponse, account: Account, authTokenType: String,
options: Bundle
): Bundle? {
return null
}
}

}
@@ -0,0 +1,32 @@
package com.yueban.customcontact.account

import android.accounts.Account
import android.accounts.AccountManager
import android.os.Build
import com.yueban.customcontact.MyApp
import com.yueban.customcontact.R

/**
* @author yueban fbzhh007@gmail.com
* @date 2019-11-30
*/
class CustomAccountManger {
companion object {
val ACCOUNT_NAME = MyApp.getContext().getString(R.string.account_name)
val ACCOUNT_TYPE = MyApp.getContext().getString(R.string.account_type)

fun addAccount() {
val account = Account(ACCOUNT_NAME, ACCOUNT_TYPE)
AccountManager.get(MyApp.getContext()).addAccountExplicitly(account, "", null)
}

fun deleteAccount() {
val account = Account(ACCOUNT_NAME, ACCOUNT_TYPE)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
AccountManager.get(MyApp.getContext()).removeAccountExplicitly(account)
} else {
AccountManager.get(MyApp.getContext()).removeAccount(account, null, null)
}
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
@@ -1,4 +1,7 @@
<resources>
<string name="app_name">CustomContact</string>

<!-- account -->
<string name="account_name">Custom Account</string>
<string name="account_type">com.yueban.customcontact</string>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/xml/auth.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="@string/account_type"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:smallIcon="@mipmap/ic_launcher" />

0 comments on commit 4ce5e36

Please sign in to comment.