package com.flyingsoftgames.googleplayservices;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.games.Players;
import com.google.android.gms.games.Games;
import com.google.android.gms.plus.Plus;

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

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

import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.app.Activity;

import android.os.Bundle;
import org.apache.cordova.*;

public class GooglePlayServices extends CordovaPlugin implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
 
 private static final String LOGTAG = "GooglePlayServices";
 
 private CordovaInterface cordova;
 private CordovaWebView webView;
 private GoogleApiClient mGoogleApiClient;
 private CallbackContext tryConnectCallback = null;
 
 @Override public void initialize (CordovaInterface initCordova, CordovaWebView initWebView) {
  super.initialize (cordova, webView);
  cordova = initCordova;
  webView = initWebView;
 }
 
 public void onConnectionFailed (ConnectionResult result) {
  if (!result.hasResolution()) return;
  try {
   result.startResolutionForResult (cordova.getActivity(), result.SIGN_IN_REQUIRED);
  } catch (SendIntentException e) {
   // There was an error with the resolution intent. Try again.
   mGoogleApiClient.connect ();
  }
 }
 
 public void onConnected (Bundle connectionHint) {
  String playerId = Games.Players.getCurrentPlayerId (mGoogleApiClient);
  if (tryConnectCallback != null) {
   tryConnectCallback.sendPluginResult (new PluginResult (PluginResult.Status.OK, playerId));
   tryConnectCallback = null;
  }
 }
 
 public void onActivityResult (int requestCode, int responseCode, Intent intent) {
  if (!mGoogleApiClient.isConnecting()) mGoogleApiClient.connect ();
 }
 public void onConnectionSuspended (int cause) {mGoogleApiClient.connect ();}
 
 public boolean execute (String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException {
  if        ("getPlayerId".equals(action)) {
   String playerId = Games.Players.getCurrentPlayerId (mGoogleApiClient);
   callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, playerId));
  } else if ("initialize".equals(action)) {
   // Passes the callbackContext to tryConnect ().
   // tryConnect runs the callback with a value of false if Google Play Services isn't available.
   tryConnect (callbackContext);
  }
  return true;
 }
 
 public void tryConnect (CallbackContext callbackContext) {
  boolean isGpsAvailable = (GooglePlayServicesUtil.isGooglePlayServicesAvailable(cordova.getActivity()) == ConnectionResult.SUCCESS);
  Log.w (LOGTAG, String.format("isGooglePlayServicesAvailable: %s",  isGpsAvailable ? "true" : "false"));
  if (!isGpsAvailable) {
   callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, false));
   return;
  }
  Activity activity = cordova.getActivity();
  Context context   = activity.getApplicationContext();
  mGoogleApiClient = new GoogleApiClient.Builder (context)
   .addConnectionCallbacks (this)
   .addOnConnectionFailedListener (this)
   .addApi (Games.API)
   .addScope (Games.SCOPE_GAMES)
   .build ();
  mGoogleApiClient.connect ();
 }
}

@@ -470,49 +470,8 @@
 mes;
-%0Aimport com.google.android.gms.plus.Plus;
 %0A%0Aim
@@ -1268,160 +1268,40 @@
 ate 
-CordovaInterface cordova;%0A private CordovaWebView webView;%0A private GoogleApiClient mGoogleApiClient;%0A private CallbackContext tryConnectCallback = null
+GoogleApiClient mGoogleApiClient
 ;%0A %0A
@@ -1352,21 +1352,17 @@
 terface 
-initC
+c
 ordova, 
@@ -1376,21 +1376,17 @@
 WebView 
-initW
+w
 ebView) 
@@ -1432,54 +1432,611 @@
 ;%0A  
-cordova = initCordova;%0A  webView = initWebView
+boolean isGpsAvailable = (GooglePlayServicesUtil.isGooglePlayServicesAvailable(cordova.getActivity()) == ConnectionResult.SUCCESS);%0A  Log.w (LOGTAG, String.format(%22isGooglePlayServicesAvailable: %25s%22,  isGpsAvailable ? %22true%22 : %22false%22));%0A  if (!isGpsAvailable) return;%0A  Activity activity = cordova.getActivity();%0A  Context context   = activity.getApplicationContext();%0A  mGoogleApiClient = new GoogleApiClient.Builder (context)%0A   .addConnectionCallbacks (this)%0A   .addOnConnectionFailedListener (this)%0A   .addApi (Games.API)%0A   .addScope (Games.SCOPE_GAMES)%0A   .build ();%0A  mGoogleApiClient.connect ()
 ;%0A %7D
@@ -2361,34 +2361,32 @@
 nect ();%0A  %7D%0A %7D%0A
- %0A
  public void onC
@@ -2497,177 +2497,11 @@
 t);%0A
-  if (tryConnectCallback != null) %7B%0A   tryConnectCallback.sendPluginResult (new PluginResult (PluginResult.Status.OK, playerId));%0A   tryConnectCallback = null;%0A  %7D%0A
  %7D%0A
- %0A
  pub
@@ -2851,52 +2851,36 @@
 %7B%0A  
-if        (%22getPlayerId%22.equals(action)) %7B%0A 
+PluginResult result = null;%0A
   St
@@ -2954,769 +2954,177 @@
 ;%0A  
- callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, playerId));%0A  %7D else if (%22initialize%22.equals(action)) %7B%0A   // Passes the callbackContext to tryConnect ().%0A   // tryConnect runs the callback with a value of false if Google Play Services isn't available.%0A   tryConnect (callbackContext);%0A  %7D%0A  return true;%0A %7D%0A %0A public void tryConnect (CallbackContext callbackContext) %7B%0A  boolean isGpsAvailable = (GooglePlayServicesUtil.isGooglePlayServicesAvailable(cordova.getActivity()) == ConnectionResult.SUCCESS);%0A  Log.w (LOGTAG, String.format(%22isGooglePlayServicesAvailable: %25s%22,  isGpsAvailable ? %22true%22 : %22false%22));%0A  if (!isGpsAvailable) %7B%0A   callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, false)
+if (%22getPlayerId%22.equals(action)) result = new PluginResult (PluginResult.Status.OK, playerId);%0A  if (result != null) callbackContext.sendPluginResult (result
 );%0A
- 
   return
 ;%0A  
@@ -3123,348 +3123,13 @@
 turn
-;%0A  %7D%0A  Activity activity = cordova.getActivity();%0A  Context context   = activity.getApplicationContext();%0A  mGoogleApiClient = new GoogleApiClient.Builder (context)%0A   .addConnectionCallbacks (this)%0A   .addOnConnectionFailedListener (this)%0A   .addApi (Games.API)%0A   .addScope (Games.SCOPE_GAMES)%0A   .build ();%0A  mGoogleApiClient.connect ()
+ true
 ;%0A %7D

Update GooglePlayServices.java