Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(account): add custom account
- Loading branch information
Showing
7 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
app/src/main/java/com/yueban/customcontact/account/AuthenticatorService.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/yueban/customcontact/account/CustomAccountManger.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" /> |