1
1
package org .buildmlearn .toolkit .activity ;
2
2
3
+ import android .app .Activity ;
3
4
import android .app .Fragment ;
4
5
import android .app .FragmentTransaction ;
5
6
import android .content .Intent ;
6
7
import android .content .SharedPreferences ;
8
+
7
9
import android .graphics .drawable .ColorDrawable ;
8
10
import android .os .Build ;
11
+
12
+
13
+ import android .net .Uri ;
14
+
9
15
import android .os .Bundle ;
10
16
import android .preference .PreferenceManager ;
17
+ import android .support .annotation .NonNull ;
18
+ import android .support .annotation .Nullable ;
11
19
import android .support .design .widget .NavigationView ;
12
20
13
21
import android .app .FragmentManager ;
22
+
14
23
import android .support .v4 .content .ContextCompat ;
24
+
25
+
26
+
15
27
import android .support .v4 .view .GravityCompat ;
16
28
import android .os .Handler ;
17
29
import android .support .v4 .widget .DrawerLayout ;
18
30
import android .support .v7 .app .AppCompatActivity ;
31
+
19
32
import android .support .v7 .widget .Toolbar ;
33
+
34
+
35
+ import android .util .Log ;
36
+ import android .view .Gravity ;
37
+ import android .view .Menu ;
38
+
20
39
import android .view .MenuItem ;
21
40
import android .view .View ;
41
+ import android .widget .ImageView ;
22
42
import android .widget .TextView ;
23
43
import android .widget .Toast ;
24
44
45
+ import com .google .android .gms .auth .api .Auth ;
46
+ import com .google .android .gms .auth .api .signin .GoogleSignInOptions ;
47
+ import com .google .android .gms .auth .api .signin .GoogleSignInResult ;
48
+ import com .google .android .gms .common .ConnectionResult ;
49
+ import com .google .android .gms .common .GoogleApiAvailability ;
50
+ import com .google .android .gms .common .api .GoogleApiClient ;
51
+ import com .google .android .gms .common .api .ResultCallback ;
52
+ import com .google .android .gms .common .api .Status ;
53
+ import com .google .android .gms .drive .Drive ;
54
+ import com .squareup .picasso .Picasso ;
55
+
25
56
import org .buildmlearn .toolkit .R ;
26
57
import org .buildmlearn .toolkit .fragment .DraftsFragment ;
27
58
import org .buildmlearn .toolkit .fragment .HomeFragment ;
28
59
import org .buildmlearn .toolkit .fragment .LoadApkFragment ;
29
60
import org .buildmlearn .toolkit .fragment .LoadProjectFragment ;
30
61
import org .buildmlearn .toolkit .fragment .SettingsFragment ;
62
+ import org .buildmlearn .toolkit .utilities .CircleTransform ;
31
63
import org .buildmlearn .toolkit .utilities .SmoothNavigationToggle ;
32
64
65
+ import static org .buildmlearn .toolkit .R .drawable .logo_70 ;
66
+
33
67
/**
34
68
* @brief Home screen of the application containg all the menus and settings.
35
69
*/
36
70
37
71
public class HomeActivity extends AppCompatActivity
38
- implements NavigationView .OnNavigationItemSelectedListener {
72
+ implements NavigationView .OnNavigationItemSelectedListener ,
73
+ GoogleApiClient .OnConnectionFailedListener ,GoogleApiClient .ConnectionCallbacks {
39
74
40
75
private final String FRAGMENT_TAG_HOME = "Home" ;
41
76
private final String FRAGMENT_TAG_PROJECT = "Project" ;
42
77
private final String FRAGMENT_TAG_APK = "Apk" ;
43
78
private boolean backPressedOnce = false ;
44
79
80
+ private static final int REQUEST_DRIVE_SIGNIN = 123 ;
81
+ private static final int REQUEST_GOOGLE_SIGN_IN =143 ;
82
+ public static GoogleApiClient mGoogleApiClient ,mGoogleApiClient1 ;
83
+
84
+ private Uri uri ;
85
+
45
86
private SmoothNavigationToggle smoothNavigationToggle ;
46
87
47
88
private NavigationView navigationView ;
48
89
90
+
49
91
/**
50
92
* {@inheritDoc}
51
93
*/
@@ -66,7 +108,7 @@ protected void onCreate(Bundle savedInstanceState) {
66
108
navigationView .setNavigationItemSelectedListener (this );
67
109
68
110
View menuHeaderView = navigationView .getHeaderView (0 );
69
- final TextView name = (TextView ) menuHeaderView .findViewById (R .id .name );
111
+ final TextView name = (TextView ) menuHeaderView .findViewById (R .id .person_name );
70
112
name .setText (String .format (" %s" , prefs .getString (getString (R .string .key_user_name ), "" )));
71
113
72
114
@@ -76,7 +118,12 @@ protected void onCreate(Bundle savedInstanceState) {
76
118
@ Override
77
119
public void onDrawerOpened (View drawerView ) {
78
120
super .onDrawerOpened (drawerView );
79
- name .setText (String .format (" %s" , prefs .getString (getString (R .string .key_user_name ), "" )));
121
+ if (mGoogleApiClient !=null ){
122
+ if (!(mGoogleApiClient .isConnected ())){
123
+ name .setText ("Welcome " +String .format (" %s" , prefs .getString (getString (R .string .key_user_name ), "" )));
124
+ }
125
+ }
126
+
80
127
LoadProjectFragment f = (LoadProjectFragment ) getFragmentManager ().findFragmentByTag (FRAGMENT_TAG_PROJECT );
81
128
if (f != null )
82
129
f .closeSearch ();
@@ -96,6 +143,28 @@ public void onDrawerOpened(View drawerView) {
96
143
if (getSupportActionBar () != null ) {
97
144
getSupportActionBar ().setTitle (R .string .app_name );
98
145
}
146
+
147
+ mGoogleApiClient = new GoogleApiClient .Builder (this )
148
+ .addApi (Drive .API )
149
+ .addScope (Drive .SCOPE_FILE )
150
+ .addConnectionCallbacks (this )
151
+ .addScope (Drive .SCOPE_APPFOLDER )
152
+ .addOnConnectionFailedListener (this )
153
+ .build ();
154
+
155
+
156
+ GoogleSignInOptions gso = new GoogleSignInOptions .Builder (GoogleSignInOptions .DEFAULT_SIGN_IN )
157
+ .requestEmail ()
158
+ .build ();
159
+
160
+
161
+ mGoogleApiClient1 = new GoogleApiClient .Builder (this )
162
+ .enableAutoManage (this , this )
163
+ .addApi (Auth .GOOGLE_SIGN_IN_API , gso )
164
+ .build ();
165
+
166
+
167
+
99
168
}
100
169
101
170
@ SuppressWarnings ("StatementWithEmptyBody" )
@@ -195,6 +264,22 @@ public void run() {
195
264
}
196
265
});
197
266
break ;
267
+ case R .id .sign_in :
268
+ Menu menu = navigationView .getMenu ();
269
+ MenuItem aaa = menu .getItem (7 );
270
+ if (mGoogleApiClient !=null ){
271
+ if ("Sign Out" .equals (aaa .getTitle ())){
272
+ aaa .setTitle ("Sign In" );
273
+ mGoogleApiClient .clearDefaultAccountAndReconnect ();
274
+ mGoogleApiClient .disconnect ();
275
+ Auth .GoogleSignInApi .signOut (mGoogleApiClient1 )
276
+ .setResultCallback (logout );
277
+ }
278
+ else {
279
+ mGoogleApiClient .connect ();
280
+ }
281
+ }
282
+ break ;
198
283
}
199
284
200
285
DrawerLayout drawer = (DrawerLayout ) findViewById (R .id .drawer_layout );
@@ -243,6 +328,142 @@ public void run()
243
328
navigationView .setCheckedItem (R .id .nav_home );
244
329
}
245
330
}
331
+
332
+
333
+ @ Override
334
+ public void onConnected (@ Nullable Bundle bundle ) {
335
+
336
+
337
+
338
+ Intent signInIntent = Auth .GoogleSignInApi .getSignInIntent (mGoogleApiClient1 );
339
+ startActivityForResult (signInIntent , REQUEST_GOOGLE_SIGN_IN );
340
+ }
341
+
342
+ @ Override
343
+ public void onConnectionSuspended (int i ) {
344
+ if (mGoogleApiClient !=null ){
345
+ mGoogleApiClient .disconnect ();
346
+ }
347
+
348
+ }
349
+
350
+ @ Override
351
+ public void onConnectionFailed (@ NonNull ConnectionResult result ) {
352
+ if (!result .hasResolution ()) {
353
+ GoogleApiAvailability .getInstance ().getErrorDialog (this , result .getErrorCode (), 0 ).show ();
354
+ return ;
355
+ }
356
+ try {
357
+ result .startResolutionForResult (this , REQUEST_DRIVE_SIGNIN );
358
+ } catch (Exception e ) {
359
+ e .printStackTrace ();
360
+ }
361
+ }
362
+
363
+
364
+ @ Override
365
+ protected void onActivityResult (final int requestCode , final int resultCode , final Intent data ) {
366
+ switch (requestCode ) {
367
+
368
+ case REQUEST_DRIVE_SIGNIN :
369
+
370
+ if (resultCode == Activity .RESULT_OK ) {
371
+
372
+ mGoogleApiClient .connect ();
373
+
374
+ }
375
+ else if (resultCode == RESULT_CANCELED ){
376
+
377
+ Log .d ("TAG" ,"result cancelled" );
378
+ return ;
379
+ }
380
+ break ;
381
+
382
+ case REQUEST_GOOGLE_SIGN_IN :
383
+
384
+ GoogleSignInResult result = Auth .GoogleSignInApi .getSignInResultFromIntent (data );
385
+
386
+ if (result .isSuccess ()) {
387
+
388
+ String name = result .getSignInAccount ().getDisplayName ();
389
+ String email = result .getSignInAccount ().getEmail ();
390
+ uri = result .getSignInAccount ().getPhotoUrl ();
391
+ TextView personname = (TextView )findViewById (R .id .person_name );
392
+ Menu menu = navigationView .getMenu ();
393
+ MenuItem aaa = menu .getItem (7 );
394
+
395
+ aaa .setTitle ("Sign Out" );
396
+ Picasso .with (this ).load (uri ).transform (new CircleTransform ()).into ((ImageView )findViewById (R .id .profile_pic ));
397
+ personname .setText ("Welcome " + name );
398
+ Toast .makeText (this ," connected " +email ,Toast .LENGTH_SHORT ).show ();
399
+
400
+
401
+
402
+ }
403
+ else {
404
+
405
+ Toast .makeText (this ,"No internet" ,Toast .LENGTH_SHORT ).show ();
406
+ }
407
+ }
408
+ }
409
+
410
+
411
+ @ Override
412
+ protected void onPause () {
413
+ super .onPause ();
414
+ }
415
+
416
+
417
+ @ Override
418
+ protected void onResume () {
419
+
420
+ super .onResume ();
421
+ if (mGoogleApiClient == null ) {
422
+ mGoogleApiClient = new GoogleApiClient .Builder (this )
423
+ .addApi (Drive .API )
424
+ .addScope (Drive .SCOPE_FILE )
425
+ .addConnectionCallbacks (this )
426
+ .addOnConnectionFailedListener (this )
427
+ .build ();
428
+ mGoogleApiClient .connect ();
429
+ }
430
+
431
+ }
432
+
433
+ @ Override
434
+ protected void onDestroy () {
435
+ super .onDestroy ();
436
+ if (mGoogleApiClient != null ) {
437
+ if (mGoogleApiClient .isConnected ()) {
438
+ mGoogleApiClient .clearDefaultAccountAndReconnect ();
439
+ mGoogleApiClient .disconnect ();
440
+ }
441
+ }
442
+ if (mGoogleApiClient1 != null ) {
443
+ if (mGoogleApiClient1 .isConnected ()) {
444
+ Auth .GoogleSignInApi .signOut (mGoogleApiClient1 )
445
+ .setResultCallback (logout );
446
+ }
447
+
448
+ }
449
+ }
450
+
451
+ /**
452
+ * logout is result callback defined to logout user from google sign in api
453
+ */
454
+
455
+ private ResultCallback <Status > logout = new ResultCallback <Status >() {
456
+ @ Override
457
+ public void onResult (@ NonNull Status status ) {
458
+
459
+ ImageView iv = (ImageView ) findViewById (R .id .profile_pic );
460
+ Picasso .with (getApplicationContext ()).load (logo_70 ).into (iv );
461
+ TextView personname = (TextView )findViewById (R .id .person_name );
462
+ personname .setText ("Welcome" );
463
+ personname .setGravity (Gravity .CENTER );
464
+
465
+ }
466
+ };
246
467
}
247
468
248
469
0 commit comments