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 ) &&(!(mGoogleApiClient .isConnected ()))){
122+
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,25 @@ 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 ;
283+ default :
284+ //do nothing
285+ break ;
198286 }
199287
200288 DrawerLayout drawer = (DrawerLayout ) findViewById (R .id .drawer_layout );
@@ -243,6 +331,147 @@ public void run()
243331 navigationView .setCheckedItem (R .id .nav_home );
244332 }
245333 }
334+
335+
336+ @ Override
337+ public void onConnected (@ Nullable Bundle bundle ) {
338+
339+
340+
341+ Intent signInIntent = Auth .GoogleSignInApi .getSignInIntent (mGoogleApiClient1 );
342+ startActivityForResult (signInIntent , REQUEST_GOOGLE_SIGN_IN );
343+ }
344+
345+ @ Override
346+ public void onConnectionSuspended (int i ) {
347+ if (mGoogleApiClient !=null ){
348+ mGoogleApiClient .disconnect ();
349+ }
350+
351+ }
352+
353+ @ Override
354+ public void onConnectionFailed (@ NonNull ConnectionResult result ) {
355+ if (!result .hasResolution ()) {
356+ GoogleApiAvailability .getInstance ().getErrorDialog (this , result .getErrorCode (), 0 ).show ();
357+ return ;
358+ }
359+ try {
360+ result .startResolutionForResult (this , REQUEST_DRIVE_SIGNIN );
361+ } catch (Exception e ) {
362+ e .printStackTrace ();
363+ }
364+ }
365+
366+
367+ @ Override
368+ protected void onActivityResult (final int requestCode , final int resultCode , final Intent data ) {
369+ switch (requestCode ) {
370+
371+ case REQUEST_DRIVE_SIGNIN :
372+
373+ if (resultCode == Activity .RESULT_OK ) {
374+
375+ mGoogleApiClient .connect ();
376+
377+ }
378+ else if (resultCode == RESULT_CANCELED ){
379+
380+ Log .d ("TAG" ,"result cancelled" );
381+ return ;
382+ }
383+ break ;
384+
385+ case REQUEST_GOOGLE_SIGN_IN :
386+
387+ GoogleSignInResult result = Auth .GoogleSignInApi .getSignInResultFromIntent (data );
388+
389+ if (result .isSuccess ()) {
390+
391+ String name = result .getSignInAccount ().getDisplayName ();
392+ String email = result .getSignInAccount ().getEmail ();
393+ uri = result .getSignInAccount ().getPhotoUrl ();
394+ TextView personname = (TextView )findViewById (R .id .person_name );
395+ Menu menu = navigationView .getMenu ();
396+ MenuItem aaa = menu .getItem (7 );
397+
398+ aaa .setTitle ("Sign Out" );
399+ Picasso .with (this ).load (uri ).transform (new CircleTransform ()).into ((ImageView )findViewById (R .id .profile_pic ));
400+ personname .setText ("Welcome " + name );
401+ Toast .makeText (this ," connected " +email ,Toast .LENGTH_SHORT ).show ();
402+
403+
404+
405+ }
406+ else {
407+
408+ Toast .makeText (this ,"No internet" ,Toast .LENGTH_SHORT ).show ();
409+ }
410+ break ;
411+ default :
412+ //do nothing
413+ break ;
414+
415+
416+ }
417+ }
418+
419+
420+ @ Override
421+ protected void onPause () {
422+ super .onPause ();
423+ }
424+
425+
426+ @ Override
427+ protected void onResume () {
428+
429+ super .onResume ();
430+ if (mGoogleApiClient == null ) {
431+ mGoogleApiClient = new GoogleApiClient .Builder (this )
432+ .addApi (Drive .API )
433+ .addScope (Drive .SCOPE_FILE )
434+ .addConnectionCallbacks (this )
435+ .addOnConnectionFailedListener (this )
436+ .build ();
437+ mGoogleApiClient .connect ();
438+ }
439+
440+ }
441+
442+ @ Override
443+ protected void onDestroy () {
444+ super .onDestroy ();
445+ if ((mGoogleApiClient != null )&& (mGoogleApiClient .isConnected ())){
446+
447+ mGoogleApiClient .clearDefaultAccountAndReconnect ();
448+ mGoogleApiClient .disconnect ();
449+ }
450+ if ((mGoogleApiClient1 != null )&& (mGoogleApiClient1 .isConnected ())){
451+
452+ Auth .GoogleSignInApi .signOut (mGoogleApiClient1 )
453+ .setResultCallback (logout );
454+
455+
456+ }
457+ }
458+
459+ /**
460+ * logout is result callback defined to logout user from google sign in api
461+ */
462+
463+ private ResultCallback <Status > logout = new ResultCallback <Status >() {
464+ @ Override
465+ public void onResult (@ NonNull Status status ) {
466+
467+ ImageView iv = (ImageView ) findViewById (R .id .profile_pic );
468+ Picasso .with (getApplicationContext ()).load (logo_70 ).into (iv );
469+ TextView personname = (TextView )findViewById (R .id .person_name );
470+ personname .setText ("Welcome" );
471+ personname .setGravity (Gravity .CENTER );
472+
473+ }
474+ };
246475}
247476
248477
0 commit comments