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 + } +}