Skip to content

Commit

Permalink
Merge pull request #7 from mntechnique/develop
Browse files Browse the repository at this point in the history
RATT and AddAccountSnackbar
  • Loading branch information
revant authored Jul 28, 2017
2 parents 9a9514d + 24ea2c9 commit 4019b25
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Void, Void>() {

private val exception: Exception? = null
private var authToken: String? = null

override fun doInBackground(vararg urls: String): Void? {
var accounts: Array<Account>? = 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
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
<string name="getOTPEndpoint">/api/method/mnt_mobile_otp.api.get_otp</string>
<string name="authOTPEndpoint">/api/method/mnt_mobile_otp.api.authenticate_otp</string>
<string name="otpLen">6</string>
<string name="please_add_account">Please add account</string>
</resources>

0 comments on commit 4019b25

Please sign in to comment.