11package org .buildmlearn .toolkit .activity ;
22
3+ import android .app .Activity ;
34import android .app .Fragment ;
45import android .app .FragmentTransaction ;
56import android .content .Intent ;
67import android .content .SharedPreferences ;
8+
79import android .graphics .drawable .ColorDrawable ;
810import android .os .Build ;
11+
12+
13+ import android .net .Uri ;
14+
915import android .os .Bundle ;
1016import android .preference .PreferenceManager ;
17+ import android .support .annotation .NonNull ;
18+ import android .support .annotation .Nullable ;
1119import android .support .design .widget .NavigationView ;
1220
1321import android .app .FragmentManager ;
22+
1423import android .support .v4 .content .ContextCompat ;
24+
25+
26+
1527import android .support .v4 .view .GravityCompat ;
1628import android .os .Handler ;
1729import android .support .v4 .widget .DrawerLayout ;
1830import android .support .v7 .app .AppCompatActivity ;
31+
1932import android .support .v7 .widget .Toolbar ;
33+
34+
35+ import android .util .Log ;
36+ import android .view .Gravity ;
37+ import android .view .Menu ;
38+
2039import android .view .MenuItem ;
2140import android .view .View ;
41+ import android .widget .ImageView ;
2242import android .widget .TextView ;
2343import android .widget .Toast ;
2444
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+
2556import org .buildmlearn .toolkit .R ;
2657import org .buildmlearn .toolkit .fragment .DraftsFragment ;
2758import org .buildmlearn .toolkit .fragment .HomeFragment ;
2859import org .buildmlearn .toolkit .fragment .LoadApkFragment ;
2960import org .buildmlearn .toolkit .fragment .LoadProjectFragment ;
3061import org .buildmlearn .toolkit .fragment .SettingsFragment ;
62+ import org .buildmlearn .toolkit .utilities .CircleTransform ;
3163import org .buildmlearn .toolkit .utilities .SmoothNavigationToggle ;
3264
65+ import static org .buildmlearn .toolkit .R .drawable .logo_70 ;
66+
3367/**
3468 * @brief Home screen of the application containg all the menus and settings.
3569 */
3670
3771public class HomeActivity extends AppCompatActivity
38- implements NavigationView .OnNavigationItemSelectedListener {
72+ implements NavigationView .OnNavigationItemSelectedListener ,
73+ GoogleApiClient .OnConnectionFailedListener ,GoogleApiClient .ConnectionCallbacks {
3974
4075 private final String FRAGMENT_TAG_HOME = "Home" ;
4176 private final String FRAGMENT_TAG_PROJECT = "Project" ;
4277 private final String FRAGMENT_TAG_APK = "Apk" ;
4378 private boolean backPressedOnce = false ;
4479
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+
4586 private SmoothNavigationToggle smoothNavigationToggle ;
4687
4788 private NavigationView navigationView ;
4889
90+
4991 /**
5092 * {@inheritDoc}
5193 */
@@ -66,7 +108,7 @@ protected void onCreate(Bundle savedInstanceState) {
66108 navigationView .setNavigationItemSelectedListener (this );
67109
68110 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 );
70112 name .setText (String .format (" %s" , prefs .getString (getString (R .string .key_user_name ), "" )));
71113
72114
@@ -76,7 +118,12 @@ protected void onCreate(Bundle savedInstanceState) {
76118 @ Override
77119 public void onDrawerOpened (View drawerView ) {
78120 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+
80127 LoadProjectFragment f = (LoadProjectFragment ) getFragmentManager ().findFragmentByTag (FRAGMENT_TAG_PROJECT );
81128 if (f != null )
82129 f .closeSearch ();
@@ -96,6 +143,28 @@ public void onDrawerOpened(View drawerView) {
96143 if (getSupportActionBar () != null ) {
97144 getSupportActionBar ().setTitle (R .string .app_name );
98145 }
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+
99168 }
100169
101170 @ SuppressWarnings ("StatementWithEmptyBody" )
@@ -195,6 +264,22 @@ public void run() {
195264 }
196265 });
197266 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 ;
198283 }
199284
200285 DrawerLayout drawer = (DrawerLayout ) findViewById (R .id .drawer_layout );
@@ -243,6 +328,142 @@ public void run()
243328 navigationView .setCheckedItem (R .id .nav_home );
244329 }
245330 }
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+ };
246467}
247468
248469
0 commit comments