-
Notifications
You must be signed in to change notification settings - Fork 227
/
GooglePlusSocialNetwork.java
662 lines (619 loc) · 31.1 KB
/
GooglePlusSocialNetwork.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*******************************************************************************
* Copyright (c) 2014 Evgeny Gorbin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*******************************************************************************/
package com.github.gorbin.asne.googleplus;
import android.content.Context;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import com.github.gorbin.asne.core.AccessToken;
import com.github.gorbin.asne.core.SocialNetwork;
import com.github.gorbin.asne.core.SocialNetworkException;
import com.github.gorbin.asne.core.listener.OnCheckIsFriendCompleteListener;
import com.github.gorbin.asne.core.listener.OnLoginCompleteListener;
import com.github.gorbin.asne.core.listener.OnPostingCompleteListener;
import com.github.gorbin.asne.core.listener.OnRequestAccessTokenCompleteListener;
import com.github.gorbin.asne.core.listener.OnRequestAddFriendCompleteListener;
import com.github.gorbin.asne.core.listener.OnRequestDetailedSocialPersonCompleteListener;
import com.github.gorbin.asne.core.listener.OnRequestGetFriendsCompleteListener;
import com.github.gorbin.asne.core.listener.OnRequestRemoveFriendCompleteListener;
import com.github.gorbin.asne.core.listener.OnRequestSocialPersonCompleteListener;
import com.github.gorbin.asne.core.listener.OnRequestSocialPersonsCompleteListener;
import com.github.gorbin.asne.core.persons.SocialPerson;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.plus.People;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.PlusShare;
import com.google.android.gms.plus.model.people.Person;
import com.google.android.gms.plus.model.people.PersonBuffer;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Class for Google plus social network integration
*
* @author Anton Krasov
* @author Evgeny Gorbin (gorbin.e.o@gmail.com)
*/
public class GooglePlusSocialNetwork extends SocialNetwork implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
/*** Social network ID in asne modules, should be unique*/
public static final int ID = 3;
private static final String TAG = GooglePlusSocialNetwork.class.getSimpleName();
// max 16 bit to use in startActivityForResult
private static final int REQUEST_AUTH = UUID.randomUUID().hashCode() & 0xFFFF;
/**
* googleApiClient.isConntected() works really strange, it returs false right after init and then true,
* so let's handle state by ourselves
*/
private static final String SAVE_STATE_KEY_IS_CONNECTED = "GooglePlusSocialNetwork.SAVE_STATE_KEY_OAUTH_TOKEN";
private static Activity mActivity;
private GoogleApiClient googleApiClient;
private ConnectionResult mConnectionResult;
private boolean mConnectRequested;
private Handler mHandler = new Handler();
public GooglePlusSocialNetwork(Fragment fragment) {
super(fragment);
}
public GooglePlusSocialNetwork(Fragment fragment, Context context) {
super(fragment, context);
}
/**
* Check is social network connected
* @return true if connected to Google Plus social network and false if not
*/
@Override
public boolean isConnected() {
// return googleApiClient.isConnecting() || googleApiClient.isConnected();
return mSharedPreferences.getBoolean(SAVE_STATE_KEY_IS_CONNECTED, false);
}
/**
* Make login request - authorize in Google plus social network
* @param onLoginCompleteListener listener for login complete
*/
@Override
public void requestLogin(OnLoginCompleteListener onLoginCompleteListener) {
super.requestLogin(onLoginCompleteListener);
mConnectRequested = true;
try {
mConnectionResult.startResolutionForResult(mActivity, REQUEST_AUTH);
} catch (Exception e) {
if (!googleApiClient.isConnecting()) {
googleApiClient.connect();
}
}
}
/**
* Logout from Google plus social network
*/
@Override
public void logout() {
mConnectRequested = false;
if (googleApiClient.isConnected()) {
mSharedPreferences.edit().remove(SAVE_STATE_KEY_IS_CONNECTED).commit();
Plus.AccountApi.clearDefaultAccount(googleApiClient);
googleApiClient.disconnect();
googleApiClient.connect();
}
}
/**
* Get id of Google plus social network
* @return Social network id for Google Plus = 3
*/
@Override
public int getID() {
return ID;
}
/**
* Not supported in Google plus sdk
*/
@Override
public AccessToken getAccessToken() {
throw new SocialNetworkException("Not supported for GooglePlusSocialNetwork");
}
/**
* Request {@link com.github.gorbin.asne.core.AccessToken} of Google plus social network that you can get from onRequestAccessTokenCompleteListener
* @param onRequestAccessTokenCompleteListener listener for {@link com.github.gorbin.asne.core.AccessToken} request
*/
@Override
public void requestAccessToken(OnRequestAccessTokenCompleteListener onRequestAccessTokenCompleteListener) {
super.requestAccessToken(onRequestAccessTokenCompleteListener);
AsyncTask<Activity, Void, String> task = new AsyncTask<Activity, Void, String>() {
Exception mException;
@Override
protected String doInBackground(Activity... params) {
String scope = "oauth2:profile email";
String account = Plus.AccountApi.getAccountName(googleApiClient);
String token = null;
try {
token = GoogleAuthUtil.getToken(params[0],
account, scope);
} catch (UserRecoverableAuthException e) {
mConnectRequested = true;
mActivity.startActivityForResult(e.getIntent(), REQUEST_AUTH);
} catch (Exception e) {
e.printStackTrace();
mException = e;
}
return token;
}
@Override
protected void onPostExecute(String token) {
if(token != null) {
((OnRequestAccessTokenCompleteListener) mLocalListeners.get(REQUEST_ACCESS_TOKEN))
.onRequestAccessTokenComplete(getID(), new AccessToken(token, null));
}
else if(mException != null) {
mLocalListeners.get(REQUEST_ACCESS_TOKEN).onError(getID(), REQUEST_ACCESS_TOKEN, mException.getMessage(), mException);
}
}
};
task.execute(mActivity);
}
/**
* Request current user {@link com.github.gorbin.asne.core.persons.SocialPerson}
* @param onRequestSocialPersonCompleteListener listener for {@link com.github.gorbin.asne.core.persons.SocialPerson} request
*/
@Override
public void requestCurrentPerson(OnRequestSocialPersonCompleteListener onRequestSocialPersonCompleteListener) {
super.requestCurrentPerson(onRequestSocialPersonCompleteListener);
requestPerson("me", onRequestSocialPersonCompleteListener);
}
/**
* Request {@link com.github.gorbin.asne.core.persons.SocialPerson} by user id
* @param userID id of Google plus user
* @param onRequestSocialPersonCompleteListener listener for {@link com.github.gorbin.asne.core.persons.SocialPerson} request
*/
@Override
public void requestSocialPerson(String userID, OnRequestSocialPersonCompleteListener onRequestSocialPersonCompleteListener) {
super.requestSocialPerson(userID, onRequestSocialPersonCompleteListener);
requestPerson(userID, onRequestSocialPersonCompleteListener);
}
/**
* Request ArrayList of {@link com.github.gorbin.asne.core.persons.SocialPerson} by array of userIds
* @param userID array of user ids in social network
* @param onRequestSocialPersonsCompleteListener listener for request ArrayList of {@link com.github.gorbin.asne.core.persons.SocialPerson}
*/
@Override
public void requestSocialPersons(final String[] userID, OnRequestSocialPersonsCompleteListener onRequestSocialPersonsCompleteListener) {
super.requestSocialPersons(userID, onRequestSocialPersonsCompleteListener);
Plus.PeopleApi.load(googleApiClient, userID).setResultCallback(new ResultCallback<People.LoadPeopleResult>() {
@Override
public void onResult(final People.LoadPeopleResult loadPeopleResult) {
if (loadPeopleResult.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
PersonBuffer personBuffer = loadPeopleResult.getPersonBuffer();
try {
int count = personBuffer.getCount();
SocialPerson socialPerson = new SocialPerson();
final ArrayList<SocialPerson> socialPersons = new ArrayList<SocialPerson>();
for (int i = 0; i < count; i++) {
getSocialPerson(socialPerson, personBuffer.get(i), userID[i]);
socialPersons.add(socialPerson);
socialPerson = new SocialPerson();
}
if (mLocalListeners.get(REQUEST_GET_PERSONS) != null) {
((OnRequestSocialPersonsCompleteListener) mLocalListeners.get(REQUEST_GET_PERSONS))
.onRequestSocialPersonsSuccess(getID(), socialPersons);
mLocalListeners.remove(REQUEST_GET_PERSONS);
}
} finally {
personBuffer.close();
}
} else {
if (mLocalListeners.get(REQUEST_GET_PERSONS) != null) {
mLocalListeners.get(REQUEST_GET_PERSONS)
.onError(getID(), REQUEST_GET_PERSONS, "Can't get persons"
+ loadPeopleResult.getStatus(), null);
mLocalListeners.remove(REQUEST_GET_PERSONS);
}
}
}
});
}
/**
* Request user {@link com.github.gorbin.asne.googleplus.GooglePlusPerson} by userId - detailed user data
* @param userId id of Google plus user
* @param onRequestDetailedSocialPersonCompleteListener listener for request detailed social person
*/
@Override
public void requestDetailedSocialPerson(final String userId, OnRequestDetailedSocialPersonCompleteListener onRequestDetailedSocialPersonCompleteListener) {
super.requestDetailedSocialPerson(userId, onRequestDetailedSocialPersonCompleteListener);
final String user = userId == null ? "me" : userId;
Plus.PeopleApi.load(googleApiClient, user).setResultCallback(new ResultCallback<People.LoadPeopleResult>() {
@Override
public void onResult(final People.LoadPeopleResult loadPeopleResult) {
if (loadPeopleResult.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
PersonBuffer personBuffer = loadPeopleResult.getPersonBuffer();
try {
int count = personBuffer.getCount();
final GooglePlusPerson googlePlusPerson = new GooglePlusPerson();
for (int i = 0; i < count; i++) {
getDetailedSocialPerson(googlePlusPerson, personBuffer.get(i), user);
}
if (mLocalListeners.get(REQUEST_GET_DETAIL_PERSON) != null) {
((OnRequestDetailedSocialPersonCompleteListener) mLocalListeners.get(REQUEST_GET_DETAIL_PERSON))
.onRequestDetailedSocialPersonSuccess(getID(), googlePlusPerson);
mLocalListeners.remove(REQUEST_GET_DETAIL_PERSON);
}
} finally {
personBuffer.close();
}
} else {
if (mLocalListeners.get(REQUEST_GET_DETAIL_PERSON) != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
mLocalListeners.get(REQUEST_GET_DETAIL_PERSON)
.onError(getID(), REQUEST_GET_DETAIL_PERSON, "Can't get person"
+ loadPeopleResult.getStatus(), null);
mLocalListeners.remove(REQUEST_GET_DETAIL_PERSON);
}
});
}
}
}
});
}
private void requestPerson(final String userID, OnRequestSocialPersonCompleteListener onRequestSocialPersonCompleteListener){
Plus.PeopleApi.load(googleApiClient, userID).setResultCallback(new ResultCallback<People.LoadPeopleResult>() {
@Override
public void onResult(final People.LoadPeopleResult loadPeopleResult) {
if (loadPeopleResult.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
PersonBuffer personBuffer = loadPeopleResult.getPersonBuffer();
try {
int count = personBuffer.getCount();
final SocialPerson socialPerson = new SocialPerson();
for (int i = 0; i < count; i++) {
getSocialPerson(socialPerson, personBuffer.get(i), userID);
}
if (mLocalListeners.get(REQUEST_GET_PERSON) != null) {
((OnRequestSocialPersonCompleteListener) mLocalListeners
.get(REQUEST_GET_PERSON))
.onRequestSocialPersonSuccess(getID(), socialPerson);
mLocalListeners.remove(REQUEST_GET_PERSON);
} else if (mLocalListeners.get(REQUEST_GET_CURRENT_PERSON) != null) {
((OnRequestSocialPersonCompleteListener) mLocalListeners
.get(REQUEST_GET_CURRENT_PERSON))
.onRequestSocialPersonSuccess(getID(), socialPerson);
mLocalListeners.remove(REQUEST_GET_CURRENT_PERSON);
}
} finally {
personBuffer.close();
}
} else {
if (mLocalListeners.get(REQUEST_GET_PERSON) != null) {
mLocalListeners.get(REQUEST_GET_PERSON).onError(getID(), REQUEST_GET_PERSON, "Can't get person"
+ loadPeopleResult.getStatus(), null);
mLocalListeners.remove(REQUEST_GET_PERSON);
} else if (mLocalListeners.get(REQUEST_GET_CURRENT_PERSON) != null) {
mLocalListeners.get(REQUEST_GET_CURRENT_PERSON).onError(getID(), REQUEST_GET_CURRENT_PERSON, "Can't get person"
+ loadPeopleResult.getStatus(), null);
mLocalListeners.remove(REQUEST_GET_CURRENT_PERSON);
}
}
}
});
}
private SocialPerson getSocialPerson(SocialPerson socialPerson, Person person, String userId){
socialPerson.id = person.getId();
socialPerson.name = person.getDisplayName();
if ((person.hasImage()) && (person.getImage().hasUrl())) {
socialPerson.avatarURL = person.getImage().getUrl().replace("?sz=50", "?sz=200");
}
socialPerson.profileURL = person.getUrl();
if(userId.equals("me")) {
socialPerson.email = Plus.AccountApi.getAccountName(googleApiClient);
}
return socialPerson;
}
private GooglePlusPerson getDetailedSocialPerson(GooglePlusPerson googlePlusPerson, Person person, String userId){
getSocialPerson(googlePlusPerson, person, userId);
googlePlusPerson.aboutMe = person.getAboutMe();
googlePlusPerson.birthday = person.getBirthday();
googlePlusPerson.braggingRights = person.getBraggingRights();
Person.Cover cover = person.getCover();
if (cover != null) {
Person.Cover.CoverPhoto coverPhoto = cover.getCoverPhoto();
if (coverPhoto != null) {
String coverPhotoURL = coverPhoto.getUrl();
if(coverPhotoURL != null){
googlePlusPerson.coverURL = coverPhotoURL;
}
}
}
googlePlusPerson.currentLocation = person.getCurrentLocation();
googlePlusPerson.gender = person.getGender();
googlePlusPerson.lang = person.getLanguage();
googlePlusPerson.nickname = person.getNickname();
googlePlusPerson.objectType = person.getObjectType();
List<Person.Organizations> organizations = person.getOrganizations();
if(organizations != null && organizations.size() > 0) {
String organizationsName = organizations.get(organizations.size()-1).getName();
if (organizationsName != null) {
googlePlusPerson.company = organizationsName;
}
String organizationsTitle = organizations.get(organizations.size()-1).getTitle();
if (organizationsTitle != null) {
googlePlusPerson.position = organizationsTitle;
}
}
List<Person.PlacesLived> placesLived = person.getPlacesLived();
if(placesLived != null && placesLived.size() > 0) {
String placeLivedValue = placesLived.get(placesLived.size()-1).getValue();
if (placeLivedValue != null) {
googlePlusPerson.placeLivedValue = placeLivedValue;
}
}
googlePlusPerson.relationshipStatus = person.getRelationshipStatus();
googlePlusPerson.tagline = person.getTagline();
return googlePlusPerson;
}
/**
* Not supported via Google plus sdk.
* @throws com.github.gorbin.asne.core.SocialNetworkException
* @param message message that should be shared
* @param onPostingCompleteListener listener for posting request
*/
@Override
public void requestPostMessage(String message, OnPostingCompleteListener onPostingCompleteListener) {
throw new SocialNetworkException("requestPostMessage isn't allowed for GooglePlusSocialNetwork");
}
/**
* Not supported via Google plus sdk.
* @throws com.github.gorbin.asne.core.SocialNetworkException
* @param photo photo that should be shared
* @param message message that should be shared with photo
* @param onPostingCompleteListener listener for posting request
*/
@Override
public void requestPostPhoto(File photo, String message, OnPostingCompleteListener onPostingCompleteListener) {
throw new SocialNetworkException("requestPostPhoto isn't allowed for GooglePlusSocialNetwork");
}
/**
* Not supported via Google plus sdk.
* @throws com.github.gorbin.asne.core.SocialNetworkException
* @param bundle bundle containing information that should be shared(Bundle constants in {@link com.github.gorbin.asne.core.SocialNetwork})
* @param message message that should be shared with bundle
* @param onPostingCompleteListener listener for posting request
*/
@Override
public void requestPostLink(Bundle bundle, String message, OnPostingCompleteListener onPostingCompleteListener) {
throw new SocialNetworkException("requestPostLink isn't allowed for GooglePlusSocialNetwork");
}
/**
* Request Google plus share dialog
* @param bundle bundle containing information that should be shared(Bundle constants in {@link com.github.gorbin.asne.core.SocialNetwork})
* @param onPostingCompleteListener listener for posting request
*/
@Override
public void requestPostDialog(Bundle bundle, OnPostingCompleteListener onPostingCompleteListener) {
super.requestPostDialog(bundle, onPostingCompleteListener);
PlusShare.Builder plusShare = new PlusShare.Builder(mActivity)
.setType("text/plain");
if(bundle != null){
if(bundle.containsKey(BUNDLE_MESSAGE)){
plusShare.setText(bundle.getString(BUNDLE_MESSAGE));
}
if(bundle.containsKey(BUNDLE_LINK)){
plusShare.setContentUrl(Uri.parse(bundle.getString(BUNDLE_LINK)));
}
}
Intent shareIntent = plusShare.getIntent();
mActivity.startActivityForResult(shareIntent, 0);
}
/**
* Not supported via Google plus sdk.
* @throws com.github.gorbin.asne.core.SocialNetworkException
* @param userID user id that should be checked as friend of current user
* @param onCheckIsFriendCompleteListener listener for checking friend request
*/
@Override
public void requestCheckIsFriend(String userID, OnCheckIsFriendCompleteListener onCheckIsFriendCompleteListener) {
throw new SocialNetworkException("requestCheckIsFriend isn't allowed for GooglePlusSocialNetwork");
}
/**
* Get current user friends list
* @param onRequestGetFriendsCompleteListener listener for getting list of current user friends
*/
@Override
public void requestGetFriends(OnRequestGetFriendsCompleteListener onRequestGetFriendsCompleteListener) {
super.requestGetFriends(onRequestGetFriendsCompleteListener);
final ArrayList<SocialPerson> socialPersons = new ArrayList<SocialPerson>();
final ArrayList<String> ids = new ArrayList<String>();
getAllFriends(null, socialPersons, ids);
}
private void getAllFriends(String pageToken, final ArrayList<SocialPerson> socialPersons, final ArrayList<String> ids){
Plus.PeopleApi.loadVisible(googleApiClient, pageToken).setResultCallback(new ResultCallback<People.LoadPeopleResult>() {
@Override
public void onResult(final People.LoadPeopleResult loadPeopleResult) {
if (loadPeopleResult.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
PersonBuffer personBuffer = loadPeopleResult.getPersonBuffer();
try {
SocialPerson socialPerson = new SocialPerson();
for(Person person : personBuffer){
getSocialPerson(socialPerson, person, "not me");
ids.add(person.getId());
socialPersons.add(socialPerson);
socialPerson = new SocialPerson();
}
if(loadPeopleResult.getNextPageToken() != null) {
getAllFriends(loadPeopleResult.getNextPageToken(), socialPersons, ids);
} else {
if (mLocalListeners.get(REQUEST_GET_FRIENDS) != null) {
((OnRequestGetFriendsCompleteListener) mLocalListeners.get(REQUEST_GET_FRIENDS))
.onGetFriendsIdComplete(getID(), ids.toArray(new String[ids.size()]));
((OnRequestGetFriendsCompleteListener) mLocalListeners.get(REQUEST_GET_FRIENDS))
.onGetFriendsComplete(getID(), socialPersons);
mLocalListeners.remove(REQUEST_GET_FRIENDS);
}
}
} finally {
personBuffer.close();
}
} else {
if (mLocalListeners.get(REQUEST_GET_FRIENDS) != null) {
mLocalListeners.get(REQUEST_GET_FRIENDS)
.onError(getID(), REQUEST_GET_DETAIL_PERSON, "Can't get person"
+ loadPeopleResult.getStatus(), null);
}
}
}
});
}
/**
* Not supported via Google plus sdk.
* @throws com.github.gorbin.asne.core.SocialNetworkException
* @param userID id of user that should be invited
* @param onRequestAddFriendCompleteListener listener for invite result
*/
@Override
public void requestAddFriend(String userID, OnRequestAddFriendCompleteListener onRequestAddFriendCompleteListener) {
throw new SocialNetworkException("requestAddFriend isn't allowed for GooglePlusSocialNetwork");
}
/**
* Not supported via Google plus sdk.
* @throws com.github.gorbin.asne.core.SocialNetworkException
* @param userID user id that should be removed from friends
* @param onRequestRemoveFriendCompleteListener listener to remove friend request response
*/
@Override
public void requestRemoveFriend(String userID, OnRequestRemoveFriendCompleteListener onRequestRemoveFriendCompleteListener) {
throw new SocialNetworkException("requestRemoveFriend isn't allowed for GooglePlusSocialNetwork");
}
/**
* Overrided for Google plus
* @param savedInstanceState If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = mSocialNetworkManager.getActivity();
Plus.PlusOptions plusOptions = new Plus.PlusOptions.Builder()
.addActivityTypes(MomentUtil.ACTIONS)
.build();
googleApiClient = new GoogleApiClient.Builder(mActivity)
.addApi(Plus.API, plusOptions)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
/**
* Overrided for Google plus
*/
@Override
public void onStart() {
googleApiClient.connect();
}
/**
* Overrided for Google plus
*/
@Override
public void onStop() {
if (googleApiClient.isConnected()) {
googleApiClient.disconnect();
}
}
/**
* Overrided for Google plus
* @param requestCode The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int sanitizedRequestCode = requestCode & 0xFFFF;
if (sanitizedRequestCode == REQUEST_AUTH) {
if (resultCode == Activity.RESULT_OK && !googleApiClient.isConnected() && !googleApiClient.isConnecting()) {
// This time, connect should succeed.
googleApiClient.connect();
} else if (resultCode == Activity.RESULT_CANCELED) {
if (mLocalListeners.get(REQUEST_LOGIN) != null) {
mLocalListeners.get(REQUEST_LOGIN).onError(getID(), REQUEST_LOGIN,
"canceled", null);
}
}
}
}
/**
* After calling connect(), this method will be invoked asynchronously when the connect request has successfully completed.
* @param bundle Bundle of data provided to clients by Google Play services. May be null if no content is provided by the service.
*/
@Override
public void onConnected(Bundle bundle) {
if (mConnectRequested) {
if (mLocalListeners.get(REQUEST_LOGIN) != null) {
mSharedPreferences.edit().putBoolean(SAVE_STATE_KEY_IS_CONNECTED, true).commit();
((OnLoginCompleteListener) mLocalListeners.get(REQUEST_LOGIN)).onLoginSuccess(getID());
return;
}
if (mLocalListeners.get(REQUEST_LOGIN) != null) {
mLocalListeners.get(REQUEST_LOGIN).onError(getID(), REQUEST_LOGIN,
"get person == null", null);
}
}
mConnectRequested = false;
}
/**
* Called when the client is temporarily in a disconnected state.
* @param i The reason for the disconnection. Defined by constants CAUSE_*.
*/
@Override
public void onConnectionSuspended(int i) {
if (mLocalListeners.get(REQUEST_LOGIN) != null) {
mLocalListeners.get(REQUEST_LOGIN).onError(getID(), REQUEST_LOGIN,
"get person == null", null);
}
mConnectRequested = false;
}
/**
* Called when the client is disconnected.
*/
@Override
public void onDisconnected() {
mConnectRequested = false;
}
/**
* Called when there was an error connecting the client to the service.
* @param connectionResult A ConnectionResult that can be used for resolving the error, and deciding what sort of error occurred.
*/
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
mConnectionResult = connectionResult;
if (mConnectRequested && mLocalListeners.get(REQUEST_LOGIN) != null) {
mLocalListeners.get(REQUEST_LOGIN).onError(getID(), REQUEST_LOGIN,
"error: " + connectionResult.getErrorCode(), null);
}
mConnectRequested = false;
}
}