Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added google login and google drive sync #431

Open
wants to merge 1 commit into
base: bug-fixes
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions source-code/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ dependencies {
compile files('libs/prov-1.51.0.0.jar')
compile files('libs/zipio-lib-1.8.jar')

compile 'com.google.android.gms:play-services-auth:9.8.0'
compile 'com.google.android.gms:play-services-drive:9.8.0'

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'org.jsoup:jsoup:1.9.1'
androidTestCompile 'com.android.support:support-annotations:23.4.0'
androidTestCompile 'com.android.support:support-annotations:24.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0'
}
}
apply plugin: 'com.google.gms.google-services'
40 changes: 40 additions & 0 deletions source-code/app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"project_info": {
"project_number": "373868766583",
"project_id": "theta-bindery-158207"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:373868766583:android:dce0dccbf435c38d",
"android_client_info": {
"package_name": "org.buildmlearn.toolkit"
}
},
"oauth_client": [
{
"client_id": "373868766583-k2t8n2b4tajj0ota82ff7muu6ijlr0dp.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyD6aSOA0BXOe0mL2XQnPxfXd-7Uo5yZWs8"
}
],
"services": {
"analytics_service": {
"status": 1
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"ads_service": {
"status": 1
}
}
}
],
"configuration_version": "1"
}
6 changes: 6 additions & 0 deletions source-code/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@
</intent-filter>
</activity>

<service android:name=".service.MyDriveEventService" android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.drive.events.HANDLE_EVENT"/>
</intent-filter>
</service>

<meta-data
android:name="io.fabric.ApiKey"
android:value="1132ca7df3eff20168030d5099ebd5b81bbfa805" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,93 @@
package org.buildmlearn.toolkit.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;

import android.graphics.drawable.ColorDrawable;
import android.os.Build;


import android.net.Uri;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;

import android.app.FragmentManager;

import android.support.v4.content.ContextCompat;



import android.support.v4.view.GravityCompat;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;


import android.util.Log;
import android.view.Gravity;
import android.view.Menu;

import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.drive.Drive;
import com.squareup.picasso.Picasso;

import org.buildmlearn.toolkit.R;
import org.buildmlearn.toolkit.fragment.DraftsFragment;
import org.buildmlearn.toolkit.fragment.HomeFragment;
import org.buildmlearn.toolkit.fragment.LoadApkFragment;
import org.buildmlearn.toolkit.fragment.LoadProjectFragment;
import org.buildmlearn.toolkit.fragment.SettingsFragment;
import org.buildmlearn.toolkit.utilities.CircleTransform;
import org.buildmlearn.toolkit.utilities.SmoothNavigationToggle;

import static org.buildmlearn.toolkit.R.drawable.logo_70;

/**
* @brief Home screen of the application containg all the menus and settings.
*/

public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
implements NavigationView.OnNavigationItemSelectedListener ,
GoogleApiClient.OnConnectionFailedListener,GoogleApiClient.ConnectionCallbacks{

private final String FRAGMENT_TAG_HOME = "Home";
private final String FRAGMENT_TAG_PROJECT = "Project";
private final String FRAGMENT_TAG_APK = "Apk";
private boolean backPressedOnce = false;

private static final int REQUEST_DRIVE_SIGNIN = 123;
private static final int REQUEST_GOOGLE_SIGN_IN =143;
public static GoogleApiClient mGoogleApiClient,mGoogleApiClient1;

private Uri uri;

private SmoothNavigationToggle smoothNavigationToggle;

private NavigationView navigationView;


/**
* {@inheritDoc}
*/
Expand All @@ -66,7 +108,7 @@ protected void onCreate(Bundle savedInstanceState) {
navigationView.setNavigationItemSelectedListener(this);

View menuHeaderView = navigationView.getHeaderView(0);
final TextView name = (TextView) menuHeaderView.findViewById(R.id.name);
final TextView name = (TextView) menuHeaderView.findViewById(R.id.person_name);
name.setText(String.format(" %s", prefs.getString(getString(R.string.key_user_name), "")));


Expand All @@ -76,7 +118,12 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
name.setText(String.format(" %s", prefs.getString(getString(R.string.key_user_name), "")));
if((mGoogleApiClient !=null) &&(!(mGoogleApiClient.isConnected()))){

name.setText("Welcome "+String.format(" %s", prefs.getString(getString(R.string.key_user_name), "")));

}

LoadProjectFragment f = (LoadProjectFragment) getFragmentManager().findFragmentByTag(FRAGMENT_TAG_PROJECT);
if (f != null)
f.closeSearch();
Expand All @@ -96,6 +143,28 @@ public void onDrawerOpened(View drawerView) {
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.app_name);
}

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addScope(Drive.SCOPE_APPFOLDER)
.addOnConnectionFailedListener(this)
.build();


GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();


mGoogleApiClient1 = new GoogleApiClient.Builder(this)
.enableAutoManage(this , this )
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();



}

@SuppressWarnings("StatementWithEmptyBody")
Expand Down Expand Up @@ -195,6 +264,25 @@ public void run() {
}
});
break;
case R.id.sign_in:
Menu menu = navigationView.getMenu();
MenuItem aaa = menu.getItem(7);
if(mGoogleApiClient !=null){
if("Sign Out".equals(aaa.getTitle())){
aaa.setTitle("Sign In");
mGoogleApiClient.clearDefaultAccountAndReconnect();
mGoogleApiClient.disconnect();
Auth.GoogleSignInApi.signOut(mGoogleApiClient1)
.setResultCallback(logout);
}
else {
mGoogleApiClient.connect();
}
}
break;
default:
//do nothing
break;
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Expand Down Expand Up @@ -243,6 +331,147 @@ public void run()
navigationView.setCheckedItem(R.id.nav_home);
}
}


@Override
public void onConnected(@Nullable Bundle bundle) {



Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient1);
startActivityForResult(signInIntent, REQUEST_GOOGLE_SIGN_IN);
}

@Override
public void onConnectionSuspended(int i) {
if(mGoogleApiClient!=null){
mGoogleApiClient.disconnect();
}

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult result) {
if (!result.hasResolution()) {
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
try {
result.startResolutionForResult(this, REQUEST_DRIVE_SIGNIN);
} catch (Exception e) {
e.printStackTrace();
}
}


@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {

case REQUEST_DRIVE_SIGNIN:

if (resultCode == Activity.RESULT_OK) {

mGoogleApiClient.connect();

}
else if (resultCode == RESULT_CANCELED){

Log.d("TAG","result cancelled");
return ;
}
break;

case REQUEST_GOOGLE_SIGN_IN:

GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

if (result.isSuccess()) {

String name = result.getSignInAccount().getDisplayName();
String email = result.getSignInAccount().getEmail();
uri = result.getSignInAccount().getPhotoUrl();
TextView personname = (TextView)findViewById(R.id.person_name);
Menu menu = navigationView.getMenu();
MenuItem aaa = menu.getItem(7);

aaa.setTitle("Sign Out");
Picasso.with(this).load(uri).transform(new CircleTransform()).into((ImageView)findViewById(R.id.profile_pic));
personname.setText("Welcome " + name);
Toast.makeText(this," connected "+email,Toast.LENGTH_SHORT).show();



}
else{

Toast.makeText(this,"No internet",Toast.LENGTH_SHORT).show();
}
break;
default:
//do nothing
break;


}
}


@Override
protected void onPause() {
super.onPause();
}


@Override
protected void onResume() {

super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}

}

@Override
protected void onDestroy() {
super.onDestroy();
if ((mGoogleApiClient != null)&& (mGoogleApiClient.isConnected())){

mGoogleApiClient.clearDefaultAccountAndReconnect();
mGoogleApiClient.disconnect();
}
if ((mGoogleApiClient1 != null)&& (mGoogleApiClient1.isConnected())){

Auth.GoogleSignInApi.signOut(mGoogleApiClient1)
.setResultCallback(logout);


}
}

/**
* logout is result callback defined to logout user from google sign in api
*/

private ResultCallback<Status> logout= new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {

ImageView iv = (ImageView) findViewById(R.id.profile_pic);
Picasso.with(getApplicationContext()).load(logo_70).into(iv);
TextView personname = (TextView)findViewById(R.id.person_name);
personname.setText("Welcome");
personname.setGravity(Gravity.CENTER);

}
};
}


Expand Down
Loading