1
1
package nl .xservices .plugins ;
2
2
3
- import java .io .IOException ;
4
-
5
3
import android .app .Activity ;
6
4
import android .app .PendingIntent ;
7
5
import android .content .Context ;
8
6
import android .content .Intent ;
9
7
import android .content .IntentSender ;
10
- import android .os .AsyncTask ;
11
8
import android .os .Bundle ;
12
-
13
9
import com .google .android .gms .auth .GoogleAuthException ;
14
10
import com .google .android .gms .auth .GoogleAuthUtil ;
15
11
import com .google .android .gms .auth .UserRecoverableAuthException ;
16
12
import com .google .android .gms .common .ConnectionResult ;
13
+ import com .google .android .gms .common .GooglePlayServicesUtil ;
17
14
import com .google .android .gms .common .Scopes ;
18
15
import com .google .android .gms .common .api .GoogleApiClient ;
19
16
import com .google .android .gms .common .api .GoogleApiClient .ConnectionCallbacks ;
22
19
import com .google .android .gms .common .api .Status ;
23
20
import com .google .android .gms .plus .Plus ;
24
21
import com .google .android .gms .plus .model .people .Person ;
25
-
26
22
import org .apache .cordova .*;
27
23
import org .json .JSONException ;
28
24
import org .json .JSONObject ;
29
25
26
+ import java .io .IOException ;
27
+
30
28
public class GooglePlus extends CordovaPlugin implements ConnectionCallbacks , OnConnectionFailedListener {
31
29
30
+ public static final String ACTION_IS_AVAILABLE = "isAvailable" ;
32
31
public static final String ACTION_LOGIN = "login" ;
33
32
public static final String ACTION_TRY_SILENT_LOGIN = "trySilentLogin" ;
34
33
public static final String ACTION_LOGOUT = "logout" ;
@@ -60,8 +59,11 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
60
59
this .apiKey = obj .optString (ARGUMENT_ANDROID_KEY , null );
61
60
}
62
61
62
+ if (ACTION_IS_AVAILABLE .equals (action )) {
63
+ final boolean avail = GooglePlayServicesUtil .isGooglePlayServicesAvailable (this .cordova .getActivity ().getApplicationContext ()) == 0 ;
64
+ savedCallbackContext .success ("" + avail );
63
65
64
- if (ACTION_LOGIN .equals (action )) {
66
+ } else if (ACTION_LOGIN .equals (action )) {
65
67
this .trySilentLogin = false ;
66
68
mGoogleApiClient .connect ();
67
69
@@ -77,23 +79,24 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
77
79
loggingOut = true ;
78
80
mGoogleApiClient = buildGoogleApiClient ();
79
81
mGoogleApiClient .connect ();
80
- } catch (IllegalStateException e ) {
81
-
82
+ } catch (IllegalStateException ignore ) {
82
83
}
83
84
savedCallbackContext .success ("logged out" );
84
85
85
86
} else if (ACTION_DISCONNECT .equals (action )) {
86
87
disconnect ();
88
+ } else {
89
+ return false ;
87
90
}
88
91
return true ;
89
92
}
90
93
91
94
private void disconnect () {
92
95
try {
93
96
Plus .AccountApi .revokeAccessAndDisconnect (mGoogleApiClient )
94
- .setResultCallback (new ResultCallback <Status >() {
97
+ .setResultCallback (new ResultCallback <Status >() {
95
98
@ Override
96
- public void onResult (Status status ) {
99
+ public void onResult (Status status ) {
97
100
// mGoogleApiClient is now disconnected and access has been revoked.
98
101
// Don't care if it was disconnected already (status != success).
99
102
mGoogleApiClient = buildGoogleApiClient ();
@@ -107,63 +110,63 @@ public void onResult(Status status) {
107
110
108
111
private GoogleApiClient buildGoogleApiClient () {
109
112
return new GoogleApiClient .Builder (webView .getContext ())
110
- .addConnectionCallbacks (this )
111
- .addOnConnectionFailedListener (this )
112
- .addApi (Plus .API , Plus .PlusOptions .builder ().build ())
113
- .addScope (Plus .SCOPE_PLUS_LOGIN )
114
- .addScope (Plus .SCOPE_PLUS_PROFILE )
115
- .build ();
113
+ .addConnectionCallbacks (this )
114
+ .addOnConnectionFailedListener (this )
115
+ .addApi (Plus .API , Plus .PlusOptions .builder ().build ())
116
+ .addScope (Plus .SCOPE_PLUS_LOGIN )
117
+ .addScope (Plus .SCOPE_PLUS_PROFILE )
118
+ .build ();
116
119
}
117
120
118
121
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
119
122
private void resolveToken (final String email , final JSONObject result ) {
120
123
final Context context = this .cordova .getActivity ().getApplicationContext ();
121
124
122
125
cordova .getThreadPool ().execute (new Runnable () {
123
- public void run () {
124
- String scope = null ;
125
- String token = null ;
126
+ public void run () {
127
+ String scope = null ;
128
+ String token = null ;
126
129
127
- try {
128
- if (GooglePlus .this .webKey != null ){
129
- // Retrieve server side tokens
130
- scope = "audience:server:client_id:" + GooglePlus .this .webKey ;
131
- token = GoogleAuthUtil .getToken (context , email , scope );
132
- result .put ("idToken" , token );
133
- } else if (GooglePlus .this .apiKey != null ) {
134
- // Retrieve the oauth token with offline mode
135
- scope = "oauth2:server:client_id:" + GooglePlus .this .apiKey ;
136
- scope += ":api_scope:" + Scopes .PLUS_LOGIN ;
137
- token = GoogleAuthUtil .getToken (context , email , scope );
138
- result .put ("oauthToken" , token );
139
- } else {
140
- // Retrieve the oauth token with offline mode
141
- scope = "oauth2:" + Scopes .PLUS_LOGIN ;
142
- token = GoogleAuthUtil .getToken (context , email , scope );
143
- result .put ("oauthToken" , token );
144
- }
145
- }
146
- catch (UserRecoverableAuthException userAuthEx ) {
147
- // Start the user recoverable action using the intent returned by
148
- // getIntent()
149
- cordova .getActivity ().startActivityForResult (userAuthEx .getIntent (),
150
- Activity .RESULT_OK );
151
- return ;
130
+ try {
131
+ if (GooglePlus .this .webKey != null ){
132
+ // Retrieve server side tokens
133
+ scope = "audience:server:client_id:" + GooglePlus .this .webKey ;
134
+ token = GoogleAuthUtil .getToken (context , email , scope );
135
+ result .put ("idToken" , token );
136
+ } else if (GooglePlus .this .apiKey != null ) {
137
+ // Retrieve the oauth token with offline mode
138
+ scope = "oauth2:server:client_id:" + GooglePlus .this .apiKey ;
139
+ scope += ":api_scope:" + Scopes .PLUS_LOGIN ;
140
+ token = GoogleAuthUtil .getToken (context , email , scope );
141
+ result .put ("oauthToken" , token );
142
+ } else {
143
+ // Retrieve the oauth token with offline mode
144
+ scope = "oauth2:" + Scopes .PLUS_LOGIN ;
145
+ token = GoogleAuthUtil .getToken (context , email , scope );
146
+ result .put ("oauthToken" , token );
152
147
}
153
- catch (IOException e ) {
154
- savedCallbackContext .error ("Failed to retrieve token: " + e .getMessage ());
155
- return ;
156
- } catch (GoogleAuthException e ) {
157
- savedCallbackContext .error ("Failed to retrieve token: " + e .getMessage ());
158
- return ;
159
- } catch (JSONException e ) {
160
- savedCallbackContext .error ("Failed to retrieve token: " + e .getMessage ());
161
- return ;
162
- }
163
-
164
- savedCallbackContext .success (result );
165
148
}
166
- });
149
+ catch (UserRecoverableAuthException userAuthEx ) {
150
+ // Start the user recoverable action using the intent returned by
151
+ // getIntent()
152
+ cordova .getActivity ().startActivityForResult (userAuthEx .getIntent (),
153
+ Activity .RESULT_OK );
154
+ return ;
155
+ }
156
+ catch (IOException e ) {
157
+ savedCallbackContext .error ("Failed to retrieve token: " + e .getMessage ());
158
+ return ;
159
+ } catch (GoogleAuthException e ) {
160
+ savedCallbackContext .error ("Failed to retrieve token: " + e .getMessage ());
161
+ return ;
162
+ } catch (JSONException e ) {
163
+ savedCallbackContext .error ("Failed to retrieve token: " + e .getMessage ());
164
+ return ;
165
+ }
166
+
167
+ savedCallbackContext .success (result );
168
+ }
169
+ });
167
170
}
168
171
169
172
/**
@@ -215,12 +218,12 @@ public void onConnected(Bundle connectionHint) {
215
218
// same as iOS values
216
219
private static String getGender (int gender ) {
217
220
switch (gender ) {
218
- case 0 :
219
- return "male" ;
220
- case 1 :
221
- return "female" ;
222
- default :
223
- return "other" ;
221
+ case 0 :
222
+ return "male" ;
223
+ case 1 :
224
+ return "female" ;
225
+ default :
226
+ return "other" ;
224
227
}
225
228
}
226
229
0 commit comments