Skip to content

Commit

Permalink
add easy account lib
Browse files Browse the repository at this point in the history
  • Loading branch information
KouroshMsv committed May 13, 2018
1 parent 29ecc00 commit 9902bd7
Show file tree
Hide file tree
Showing 15 changed files with 387 additions and 12 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,5 @@ fastlane/readme.md
.idea/dictionaries

build/
/easyaccount
local.properties

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation project(':easyaccount')
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".SS"/>
</application>

</manifest>
18 changes: 9 additions & 9 deletions app/src/main/java/com/mousavi/card/gmail/accountmanager/SS.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
//import com.mousavi.card.gmail.easyaccount.EasyAccountService;
import com.mousavi.card.gmail.easyaccount.EasyAccountService;

public class SS extends Service {

@Nullable
@Override
public IBinder onBind(Intent ibinderIntent) {
// EasyAccountService easyAccount= EasyAccountService.newBuilder(this)
// .accountType("com.mousavi.card.gmail.accountmanager")
// .destinationClass(MainActivity.class)
// .enableLogger(true)
// .build();
// return easyAccount.getIBinder();
return null;}

EasyAccountService easyAccount = EasyAccountService.newBuilder(this)
.accountType("com.mousavi.card.gmail.accountmanager")
.destinationClass(MainActivity.class)
.enableLogger(true)
.build();
return easyAccount.getIBinder();
}
}
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'

classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
17 changes: 17 additions & 0 deletions easyaccount/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.KouroshMsv'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
21 changes: 21 additions & 0 deletions easyaccount/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
9 changes: 9 additions & 0 deletions easyaccount/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mousavi.card.gmail.easyaccount">

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


</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mousavi.card.gmail.easyaccount;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

abstract class AbstractEasyAccountAuthenticator extends AbstractAccountAuthenticator {



AbstractEasyAccountAuthenticator(Context context) {
super(context);
}


@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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.mousavi.card.gmail.easyaccount;

import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import java.util.Arrays;

final class EasyAccountAuthenticator extends AbstractEasyAccountAuthenticator {

private String authTokenType;
private String accountType;
private Class loginClass;
private L l;
private Intent tokenIntent;
private Service service;

EasyAccountAuthenticator(Service service, Class loginClass, L l, Intent tokenIntent) {
super(service);
this.loginClass = loginClass;
this.l = l;
this.tokenIntent = tokenIntent;
this.service = service;
}

@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType,
String[] requiredFeatures, Bundle options) {
if (loginClass != null) {
final Intent intent = new Intent(service, loginClass);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
l.d("addAccount", intentToString(intent));
final Bundle bundle = new Bundle();
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
return bundle;
}
return null;
}

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) {
Bundle bundle = new Bundle();
if (this.authTokenType.equals(authTokenType)) {
AccountManager am = AccountManager.get(service);
String authToken = am.peekAuthToken(account, authTokenType);
if (authToken != null) {
l.d("authToken", authToken);
}
if (TextUtils.isEmpty(authToken)) {
l.d("auth token is empty");
final Intent intent = new Intent(service, loginClass);
am.setAuthToken(account, this.authTokenType, null);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
if (tokenIntent != null) {
intent.putExtras(tokenIntent);
}
l.d("startActivity(" + loginClass.getSimpleName() + ")", "intent : " + intentToString(intent));
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
return bundle;
}
bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
bundle.putString(AccountManager.KEY_AUTHTOKEN, authToken);

l.d("sent token", "intent : " + bundleToString(bundle));
return bundle;
}
l.d("invalid authTokenType\n"
+ "1: " + authTokenType + "\n"
+ "2: " + this.authTokenType);
bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
return bundle;
}

public void setAuthTokenType(String authTokenType) {
this.authTokenType = authTokenType;
}

public String getAuthTokenType() {
return authTokenType;
}

public void setAccountType(String accountType) {
this.accountType = accountType;
}

public String getAccountType() {
return accountType;
}

private String intentToString(Intent intent) {
if (intent == null) {
return null;
}

return intent.toString() + " " + bundleToString(intent.getExtras());
}

private String bundleToString(Bundle bundle) {
StringBuilder out = new StringBuilder("Bundle[");

if (bundle == null) {
out.append("null");
} else {
boolean first = true;
for (String key : bundle.keySet()) {
if (!first) {
out.append(", ");
}

out.append(key).append('=');

Object value = bundle.get(key);

if (value instanceof int[]) {
out.append(Arrays.toString((int[]) value));
} else if (value instanceof byte[]) {
out.append(Arrays.toString((byte[]) value));
} else if (value instanceof boolean[]) {
out.append(Arrays.toString((boolean[]) value));
} else if (value instanceof short[]) {
out.append(Arrays.toString((short[]) value));
} else if (value instanceof long[]) {
out.append(Arrays.toString((long[]) value));
} else if (value instanceof float[]) {
out.append(Arrays.toString((float[]) value));
} else if (value instanceof double[]) {
out.append(Arrays.toString((double[]) value));
} else if (value instanceof String[]) {
out.append(Arrays.toString((String[]) value));
} else if (value instanceof CharSequence[]) {
out.append(Arrays.toString((CharSequence[]) value));
} else if (value instanceof Parcelable[]) {
out.append(Arrays.toString((Parcelable[]) value));
} else if (value instanceof Bundle) {
out.append(bundleToString((Bundle) value));
} else {
out.append(value);
}

first = false;
}
}

out.append("]");
return out.toString();
}
}
Loading

0 comments on commit 9902bd7

Please sign in to comment.