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 drive api #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
108 changes: 108 additions & 0 deletions GoogleDriveApiClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@



public class GoogleDriveApiClient extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,GoogleApiClient.ConnectionCallbacks{


public GoogleApiClient mGoogleApiClient;
public static final int REQUEST_DRIVE_SIGNIN = 123;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


//create a api client to connect to drive
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addScope(Drive.SCOPE_APPFOLDER)
.addOnConnectionFailedListener(this)
.build();

//call to connect
mGoogleApiClient.connect();


}

@Override
public void onConnected(@Nullable Bundle bundle) {
//implement the code after connecting
}

@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 {
//call for connection again
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;
}
}

@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();
}
}
}