Skip to content

Commit c2c75d0

Browse files
author
nikhilchowdary
committed
added google login and sync with google drive
1 parent fe089cf commit c2c75d0

25 files changed

+900
-22
lines changed

source-code/app/build.gradle

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ dependencies {
6868
compile files('libs/prov-1.51.0.0.jar')
6969
compile files('libs/zipio-lib-1.8.jar')
7070

71+
compile 'com.google.android.gms:play-services-auth:9.8.0'
72+
compile 'com.google.android.gms:play-services-drive:9.8.0'
73+
7174
compile 'com.squareup.picasso:picasso:2.5.2'
7275
compile 'org.jsoup:jsoup:1.9.1'
73-
androidTestCompile 'com.android.support:support-annotations:23.4.0'
76+
androidTestCompile 'com.android.support:support-annotations:24.0.0'
7477
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
7578
androidTestCompile 'com.android.support.test:runner:0.5'
7679
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0'
77-
}
80+
}
81+
apply plugin: 'com.google.gms.google-services'

source-code/app/google-services.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"project_info": {
3+
"project_number": "373868766583",
4+
"project_id": "theta-bindery-158207"
5+
},
6+
"client": [
7+
{
8+
"client_info": {
9+
"mobilesdk_app_id": "1:373868766583:android:dce0dccbf435c38d",
10+
"android_client_info": {
11+
"package_name": "org.buildmlearn.toolkit"
12+
}
13+
},
14+
"oauth_client": [
15+
{
16+
"client_id": "373868766583-k2t8n2b4tajj0ota82ff7muu6ijlr0dp.apps.googleusercontent.com",
17+
"client_type": 3
18+
}
19+
],
20+
"api_key": [
21+
{
22+
"current_key": "AIzaSyD6aSOA0BXOe0mL2XQnPxfXd-7Uo5yZWs8"
23+
}
24+
],
25+
"services": {
26+
"analytics_service": {
27+
"status": 1
28+
},
29+
"appinvite_service": {
30+
"status": 1,
31+
"other_platform_oauth_client": []
32+
},
33+
"ads_service": {
34+
"status": 1
35+
}
36+
}
37+
}
38+
],
39+
"configuration_version": "1"
40+
}

source-code/app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@
9292
</intent-filter>
9393
</activity>
9494

95+
<service android:name=".service.MyDriveEventService" android:exported="true">
96+
<intent-filter>
97+
<action android:name="com.google.android.gms.drive.events.HANDLE_EVENT"/>
98+
</intent-filter>
99+
</service>
100+
95101
<meta-data
96102
android:name="io.fabric.ApiKey"
97103
android:value="1132ca7df3eff20168030d5099ebd5b81bbfa805" />

source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java

Lines changed: 232 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,93 @@
11
package org.buildmlearn.toolkit.activity;
22

3+
import android.app.Activity;
34
import android.app.Fragment;
45
import android.app.FragmentTransaction;
56
import android.content.Intent;
67
import android.content.SharedPreferences;
8+
79
import android.graphics.drawable.ColorDrawable;
810
import android.os.Build;
11+
12+
13+
import android.net.Uri;
14+
915
import android.os.Bundle;
1016
import android.preference.PreferenceManager;
17+
import android.support.annotation.NonNull;
18+
import android.support.annotation.Nullable;
1119
import android.support.design.widget.NavigationView;
1220

1321
import android.app.FragmentManager;
22+
1423
import android.support.v4.content.ContextCompat;
24+
25+
26+
1527
import android.support.v4.view.GravityCompat;
1628
import android.os.Handler;
1729
import android.support.v4.widget.DrawerLayout;
1830
import android.support.v7.app.AppCompatActivity;
31+
1932
import android.support.v7.widget.Toolbar;
33+
34+
35+
import android.util.Log;
36+
import android.view.Gravity;
37+
import android.view.Menu;
38+
2039
import android.view.MenuItem;
2140
import android.view.View;
41+
import android.widget.ImageView;
2242
import android.widget.TextView;
2343
import 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+
2556
import org.buildmlearn.toolkit.R;
2657
import org.buildmlearn.toolkit.fragment.DraftsFragment;
2758
import org.buildmlearn.toolkit.fragment.HomeFragment;
2859
import org.buildmlearn.toolkit.fragment.LoadApkFragment;
2960
import org.buildmlearn.toolkit.fragment.LoadProjectFragment;
3061
import org.buildmlearn.toolkit.fragment.SettingsFragment;
62+
import org.buildmlearn.toolkit.utilities.CircleTransform;
3163
import 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

3771
public 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

Comments
 (0)