From 1a7431a852071db2a12a535ff78e54c74436366b Mon Sep 17 00:00:00 2001 From: Revant Nandgaonkar Date: Sat, 29 Jul 2017 00:14:07 +0530 Subject: [PATCH 1/2] RATT and AddAccountSnackbar --- app/build.gradle | 4 +- .../otpmobileauth/AddAccountSnackbar.kt | 42 ++++++++++++++ .../auth/RetrieveAuthTokenTask.java | 55 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/mntechnique/otpmobileauth/AddAccountSnackbar.kt create mode 100644 app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.java diff --git a/app/build.gradle b/app/build.gradle index 15cf768..7ccd974 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 15 targetSdkVersion 26 - versionCode 7 - versionName "1.0.1" + versionCode 8 + versionName "1.0.2" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/app/src/main/java/com/mntechnique/otpmobileauth/AddAccountSnackbar.kt b/app/src/main/java/com/mntechnique/otpmobileauth/AddAccountSnackbar.kt new file mode 100644 index 0000000..0a45177 --- /dev/null +++ b/app/src/main/java/com/mntechnique/otpmobileauth/AddAccountSnackbar.kt @@ -0,0 +1,42 @@ +package com.mntechnique.otpmobileauth + +import android.accounts.Account +import android.accounts.AccountManager +import android.content.Intent +import android.os.Bundle +import android.support.design.widget.Snackbar +import android.support.v7.app.AppCompatActivity +import android.view.View + +import com.mntechnique.otpmobileauth.auth.AuthenticatorActivity + +/** + * Created by revant on 28/7/17. + */ + +class AddAccountSnackbar : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + showAddAccountSnackBar() + } + + public override fun onResume() { + super.onResume() + showAddAccountSnackBar() + } + + fun showAddAccountSnackBar() { + val packageName = resources.getString(R.string.package_name) + val mAccountManager = AccountManager.get(this) + val accounts = mAccountManager.getAccountsByType(packageName) + if (accounts.size == 0) { + Snackbar.make(findViewById(android.R.id.content), R.string.please_add_account, Snackbar.LENGTH_INDEFINITE) + .setAction(android.R.string.ok) { + val intent = Intent(this@AddAccountSnackbar, AuthenticatorActivity::class.java) + intent.putExtra(AuthenticatorActivity.ARG_ACCOUNT_TYPE, packageName) + startActivity(intent) + }.show() + } + } +} diff --git a/app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.java b/app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.java new file mode 100644 index 0000000..fef8826 --- /dev/null +++ b/app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.java @@ -0,0 +1,55 @@ +package com.mntechnique.otpmobileauth.auth; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.accounts.AccountManagerCallback; +import android.accounts.AccountManagerFuture; +import android.content.Context; +import android.os.AsyncTask; +import android.os.Bundle; +import android.util.Log; + +import com.mntechnique.otpmobileauth.R; + +/** + * Created by revant on 28/7/17. + */ + +public class RetrieveAuthTokenTask extends AsyncTask { + + private Exception exception; + private AuthReqCallback callback; + private Context context; + private String authToken; + + public RetrieveAuthTokenTask(Context context, AuthReqCallback postExecuteCallback) { + this.callback = postExecuteCallback; + this.context = context; + } + + protected Void doInBackground(String... urls) { + Account[] accounts = null; + final AccountManager am = AccountManager.get(context); + accounts = am.getAccountsByType(context.getResources().getString(R.string.package_name)); + if (accounts.length == 1) { + final Account account = accounts[0]; + am.getAuthToken(account, "Full access", null, true, new AccountManagerCallback() { + @Override + public void run(AccountManagerFuture future) { + try { + Bundle bundle = future.getResult(); + authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN); + callback.onSuccessResponse(authToken); + am.invalidateAuthToken(account.type, authToken); + } catch (Exception e) { + Log.d("error", e.getMessage()); + callback.onErrorResponse(e.toString()); + } + } + }, null); + } else { + Log.d("Accounts", "NOT 1 account found!"); + } + return null; + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e6ae67c..a4086c4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,4 +16,5 @@ /api/method/mnt_mobile_otp.api.get_otp /api/method/mnt_mobile_otp.api.authenticate_otp 6 + Please add account From 24ea2c93d12a5d48497e857f9ca562f08b103a66 Mon Sep 17 00:00:00 2001 From: Revant Nandgaonkar Date: Sat, 29 Jul 2017 00:19:45 +0530 Subject: [PATCH 2/2] RATT added --- .../auth/RetrieveAuthTokenTask.java | 55 ------------------- .../auth/RetrieveAuthTokenTask.kt | 45 +++++++++++++++ 2 files changed, 45 insertions(+), 55 deletions(-) delete mode 100644 app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.java create mode 100644 app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.kt diff --git a/app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.java b/app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.java deleted file mode 100644 index fef8826..0000000 --- a/app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.mntechnique.otpmobileauth.auth; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.accounts.AccountManagerCallback; -import android.accounts.AccountManagerFuture; -import android.content.Context; -import android.os.AsyncTask; -import android.os.Bundle; -import android.util.Log; - -import com.mntechnique.otpmobileauth.R; - -/** - * Created by revant on 28/7/17. - */ - -public class RetrieveAuthTokenTask extends AsyncTask { - - private Exception exception; - private AuthReqCallback callback; - private Context context; - private String authToken; - - public RetrieveAuthTokenTask(Context context, AuthReqCallback postExecuteCallback) { - this.callback = postExecuteCallback; - this.context = context; - } - - protected Void doInBackground(String... urls) { - Account[] accounts = null; - final AccountManager am = AccountManager.get(context); - accounts = am.getAccountsByType(context.getResources().getString(R.string.package_name)); - if (accounts.length == 1) { - final Account account = accounts[0]; - am.getAuthToken(account, "Full access", null, true, new AccountManagerCallback() { - @Override - public void run(AccountManagerFuture future) { - try { - Bundle bundle = future.getResult(); - authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN); - callback.onSuccessResponse(authToken); - am.invalidateAuthToken(account.type, authToken); - } catch (Exception e) { - Log.d("error", e.getMessage()); - callback.onErrorResponse(e.toString()); - } - } - }, null); - } else { - Log.d("Accounts", "NOT 1 account found!"); - } - return null; - } -} diff --git a/app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.kt b/app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.kt new file mode 100644 index 0000000..9538d3a --- /dev/null +++ b/app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.kt @@ -0,0 +1,45 @@ +package com.mntechnique.otpmobileauth.auth + +import android.accounts.Account +import android.accounts.AccountManager +import android.accounts.AccountManagerCallback +import android.accounts.AccountManagerFuture +import android.content.Context +import android.os.AsyncTask +import android.os.Bundle +import android.util.Log + +import com.mntechnique.otpmobileauth.R + +/** + * Created by revant on 28/7/17. + */ + +class RetrieveAuthTokenTask(private val context: Context, private val callback: AuthReqCallback) : AsyncTask() { + + private val exception: Exception? = null + private var authToken: String? = null + + override fun doInBackground(vararg urls: String): Void? { + var accounts: Array? = null + val am = AccountManager.get(context) + accounts = am.getAccountsByType(context.resources.getString(R.string.package_name)) + if (accounts!!.size == 1) { + val account = accounts[0] + am.getAuthToken(account, AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS, null, true, { future -> + try { + val bundle = future.result + authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN) + callback.onSuccessResponse(authToken!!) + am.invalidateAuthToken(account.type, authToken) + } catch (e: Exception) { + Log.d("error", e.message) + callback.onErrorResponse(e.toString()) + } + }, null) + } else { + Log.d("Accounts", "NOT 1 account found!") + } + return null + } +}