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;
 
 public CordovaInterface cordova           = null;
 public CallbackContext tryConnectCallback = null;
 public String          accessToken        = "";
 
 public final int REQUEST_CODE_PICK_ACCOUNT = 1000;
 
 @Override public void initialize (CordovaInterface initCordova, CordovaWebView webView) {
  cordova = initCordova;
  super.initialize (cordova, webView);
 }
 
 private void pickUserAccount () {
  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) {
  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 {
 if ("tryConnect".equals(action)) {
   tryConnect (callbackContext);
  } else if ("getAccessToken".equals(action)) {
   callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, accessToken));
  }
  return true;
 }
 
 // tryConnect runs the callback with a value of false if Google Play Services isn't available.
 public void tryConnect (CallbackContext callbackContext) {
  tryConnectCallback = callbackContext;
  pickUserAccount ();
 }
 
 
 private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
  @Override protected String doInBackground (String... params) {
   String accountName = params[0];
   String scope = "oauth2:" + Scopes.PROFILE;
   Context context = cordova.getActivity().getApplicationContext();
   try {
    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;
   }
   return accessToken;
  }
  
  @Override protected void onPostExecute (String newAccessToken) {
   super.onPostExecute (newAccessToken);
   accessToken = newAccessToken;
   if (tryConnectCallback != null) {
    tryConnectCallback.sendPluginResult (new PluginResult (PluginResult.Status.OK, accessToken));
    tryConnectCallback = null;
   }
  }
 }
}

@@ -1088,67 +1088,68 @@
 4;%0A 
-%0A public CordovaInterface cordova           = null;%0A public
+private final int REQUEST_CODE_PICK_ACCOUNT = 1000;%0A private
  Cal
@@ -1194,21 +1194,22 @@
 ;%0A p
-ublic
+rivate
  String 
     
@@ -1208,292 +1208,151 @@
 ing 
-         accessToken        = %22%22;%0A %0A public final int REQUEST_CODE_PICK_ACCOUNT = 1000;%0A %0A @Override public void initialize (CordovaInterface initCordova, CordovaWebView webView) %7B%0A  cordova = initCordova;%0A  super.initialize (cordova, webView);%0A %7D%0A %0A private void pickUserAccount () %7B
+scope = %22oauth2:%22 + Scopes.PROFILE;%0A %0A private void pickUserAccount (CallbackContext callbackContext) %7B%0A  tryConnectCallback = callbackContext;
 %0A  S
@@ -2035,24 +2035,23 @@
 n %7B%0A
+ 
  if (%22
-tryConnect
+setScope
 %22.eq
@@ -2069,39 +2069,51 @@
 )) %7B
+;
 %0A   
-tryConnect (callbackContext
+scope = %22oauth2:%22 + inputs.getString(0
 );%0A 
@@ -2166,348 +2166,67 @@
 %0A   
-callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, accessToken));%0A  %7D%0A  return true;%0A %7D%0A %0A // tryConnect runs the callback with a value of false if Google Play Services isn't available.%0A public void tryConnect (CallbackContext callbackContext) %7B%0A  tryConnectCallback = callbackContext;%0A  pickUserAccount ()
+pickUserAccount (callbackContext);%0A  %7D%0A  return true
 ;%0A %7D%0A %0A
- %0A
  pri
@@ -2400,54 +2400,8 @@
 0%5D;%0A
-   String scope = %22oauth2:%22 + Scopes.PROFILE;%0A
    C
@@ -2464,16 +2464,46 @@
 text();%0A
+   String accessToken = null;%0A
    try %7B
@@ -2674,32 +2674,33 @@
 llback != null) 
+%7B
 tryConnectCallba
@@ -2764,24 +2764,25 @@
 back = null;
+%7D
 %0A   %7D catch 
@@ -3019,16 +3019,17 @@
 = null) 
+%7B
 tryConne
@@ -3097,32 +3097,33 @@
 Callback = null;
+%7D
 %0A   %7D%0A   return 
@@ -3191,20 +3191,17 @@
 (String 
-newA
+a
 ccessTok
@@ -3213,49 +3213,78 @@
 %0A   
-super.onPostExecute (newAccessToken);%0A   
+// accessToken can be null if doInBackground catches an error.%0A   if (
 acce
@@ -3296,13 +3296,49 @@
 en =
+=
  n
-ewA
+ull) return;%0A   super.onPostExecute (a
 cces
@@ -3339,24 +3339,25 @@
 (accessToken
+)
 ;%0A   if (try
@@ -3372,25 +3372,25 @@
 allback 
-!
+=
 = null) 
 %7B%0A    tr
@@ -3381,19 +3381,24 @@
 = null) 
-%7B%0A 
+return;%0A
    tryCo
@@ -3489,17 +3489,16 @@
 n));%0A   
- 
 tryConne
@@ -3516,21 +3516,16 @@
 = null;%0A
-   %7D%0A
   %7D%0A %7D%0A%7D

Update GooglePlayToken.java