-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from mntechnique/develop
RATT and AddAccountSnackbar
- Loading branch information
Showing
4 changed files
with
90 additions
and
2 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
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/mntechnique/otpmobileauth/AddAccountSnackbar.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,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() | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/mntechnique/otpmobileauth/auth/RetrieveAuthTokenTask.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,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 | ||
} | ||
} |
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