package com.flyingsoftgames.googleplaytoken;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;

import com.google.android.gms.common.AccountPicker;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.content.Intent;

import android.app.Activity;
import android.accounts.AccountManager;

import android.os.AsyncTask;
import android.os.Bundle;

import java.io.IOException;

import android.util.Log;

public class GooglePlayToken extends CordovaPlugin {
 
 private final String LOG_TAG = "GooglePlayToken";
 private final int REQ_SIGN_IN_REQUIRED = 55664;
 private final int REQUEST_CODE_PICK_ACCOUNT = 1000;
 private CallbackContext tryConnectCallback = null;
 private String lastAccessToken = null;
 private String scope = "oauth2:" + Scopes.PROFILE;
 
 private void pickUserAccount (CallbackContext callbackContext) {
  Log.d (LOG_TAG, "pickUserAccount");
  tryConnectCallback = callbackContext;
  String[] accountTypes = new String[]{"com.google"};
  Intent intent = AccountPicker.newChooseAccountIntent(null, null, accountTypes, false, null, null, null, null);
  cordova.setActivityResultCallback (this);
  cordova.getActivity().startActivityForResult (intent, REQUEST_CODE_PICK_ACCOUNT);
 }
 
 public void onActivityResult (int requestCode, int resultCode, Intent data) {
    Log.d (LOG_TAG, "onActivityResult");
  if ((requestCode == REQUEST_CODE_PICK_ACCOUNT) && (resultCode == Activity.RESULT_OK)) {
   new RetrieveTokenTask().execute (data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME));
  }
 }
 
 public boolean execute (String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException {
  Log.d (LOG_TAG, "execute");
  if ("setScope".equals(action)) {;
   scope = "oauth2:" + inputs.getString(0);
  } else if ("getAccessToken".equals(action)) {
   pickUserAccount (callbackContext);
  }
  return true;
 }
 
 private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
  @Override protected String doInBackground (String... params) {
   Log.d (LOG_TAG, "doInBackground");
   String accountName = params[0];
   Context context = cordova.getActivity().getApplicationContext();
   String accessToken = null;
   try {
    if (lastAccessToken != null) {GoogleAuthUtil.clearToken (context, lastAccessToken); lastAccessToken = null;}
    accessToken = GoogleAuthUtil.getToken(context, accountName, scope);
   } catch (IOException e) {
    String errormessage = e.getMessage();
    if (tryConnectCallback != null) {tryConnectCallback.error ("Error: " + errormessage + "."); tryConnectCallback = null;}
   } catch (UserRecoverableAuthException e) {
    cordova.getActivity().startActivityForResult (e.getIntent(), REQ_SIGN_IN_REQUIRED);
   } catch (GoogleAuthException e) {
    String errormessage = e.getMessage();
    if (tryConnectCallback != null) {tryConnectCallback.error ("Error: " + errormessage + "."); tryConnectCallback = null;}
   }
   lastAccessToken = accessToken;
   if (tryConnectCallback == null) return "";
   tryConnectCallback.sendPluginResult (new PluginResult (PluginResult.Status.OK, accessToken));
   tryConnectCallback = null;
   return "";
  }
 }
}

@@ -1060,33 +1060,41 @@
  REQ
-_SIGN_IN_REQUIRED = 55664
+UEST_CODE_PICK_ACCOUNT     = 1000
 ;%0A p
@@ -1127,27 +1127,32 @@
 ODE_
-PICK_ACCOUNT = 1000
+SIGN_IN_REQUIRED = 55664
 ;%0A p
@@ -1295,16 +1295,104 @@
 FILE;%0A %0A
+ private void doSetResultCallBack () %7B%0A  cordova.setActivityResultCallback (this);%0A %7D%0A %0A
  private
@@ -1453,46 +1453,8 @@
 ) %7B%0A
-  Log.d (LOG_TAG, %22pickUserAccount%22);%0A
   tr
@@ -1597,16 +1597,17 @@
 ntIntent
+ 
 (null, n
@@ -1659,35 +1659,21 @@
 ull);%0A  
-cordova.setActivity
+doSet
 ResultCa
@@ -1670,34 +1670,30 @@
 etResultCall
-b
+B
 ack (
-this
 );%0A  cordova
@@ -1857,53 +1857,63 @@
 %7B%0A  
-  Log.d (LOG_TAG, %22onActivityResult%22);%0A  if (
+if (((requestCode == REQUEST_CODE_SIGN_IN_REQUIRED) %7C%7C 
 (req
@@ -1950,16 +1950,17 @@
 ACCOUNT)
+)
  && (res
@@ -2211,38 +2211,8 @@
 n %7B%0A
-  Log.d (LOG_TAG, %22execute%22);%0A
   if
@@ -2541,46 +2541,8 @@
 ) %7B%0A
-   Log.d (LOG_TAG, %22doInBackground%22);%0A
    S
@@ -3055,24 +3055,39 @@
 ck = null;%7D%0A
+    return %22%22;%0A
    %7D catch (
@@ -3116,24 +3116,52 @@
 eption e) %7B%0A
+    doSetResultCallBack ();%0A
     cordova.
@@ -3216,16 +3216,25 @@
 t(), REQ
+UEST_CODE
 _SIGN_IN
@@ -3245,16 +3245,31 @@
 UIRED);%0A
+    return %22%22;%0A
    %7D cat
@@ -3463,16 +3463,31 @@
  null;%7D%0A
+    return %22%22;%0A
    %7D%0A   

Update GooglePlayToken.java