Back to project page vocabulary-list.
The source code is released under:
Apache License
If you think the Android project vocabulary-list listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.rulych.vocabularylist.activities; /*from w w w. jav a 2 s . c o m*/ import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.drive.Drive; import com.rulych.vocabularylist.R; import com.rulych.vocabularylist.fragments.DriveApiFragment; import android.app.Activity; import android.app.Fragment; import android.content.Intent; import android.content.IntentSender; import android.os.Bundle; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import rx.android.schedulers.AndroidSchedulers; import rx.functions.ActionN; import rx.util.async.Async; import timber.log.Timber; public class VocabularyListActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { protected static final int RESOLVE_CONNECTION_REQUEST_CODE = 1; protected GoogleApiClient mApiClient; protected final List<WeakReference<DriveApiFragment>> mFragments = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_vocabulary_list); mApiClient = new GoogleApiClient.Builder(this).addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addScope(Drive.SCOPE_APPFOLDER) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); mApiClient.connect(); } @Override public void onAttachFragment(Fragment fragment) { super.onAttachFragment(fragment); if (!(fragment instanceof DriveApiFragment)) { return; } DriveApiFragment driveApiFragment = (DriveApiFragment) fragment; mFragments.add(new WeakReference<>(driveApiFragment)); if (isApiClientReady()) { notifyFragment(driveApiFragment); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case RESOLVE_CONNECTION_REQUEST_CODE: if (resultCode == RESULT_OK) { mApiClient.connect(); } break; } } @Override public void onConnected(Bundle bundle) { Timber.d("Connected to Google Drive API."); notifyFragments(); } @Override public void onConnectionSuspended(int cause) { Timber.d("Connection to Google Drive API suspended. Cause=%d.", cause); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { Timber.d("Connection to Google Drive API failed: %s", connectionResult); if (connectionResult.hasResolution()) { try { connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE); } catch (IntentSender.SendIntentException e) { Timber.w(e, "Error trying to connect to Google Drive API."); // TODO Unable to resolve, message user appropriately } } else { GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show(); } } private void notifyFragments() { for (WeakReference<DriveApiFragment> fragmentRef : mFragments) { DriveApiFragment fragment = fragmentRef.get(); if (fragment == null) { continue; } notifyFragment(fragment); } } private void notifyFragment(final DriveApiFragment driveApiFragment) { Async.asyncAction(new ActionN() { @Override public void call(Object... objects) { driveApiFragment.onApiClientReady(mApiClient); } }, AndroidSchedulers.mainThread()).call(); } private boolean isApiClientReady() { return mApiClient != null && mApiClient.isConnected(); } }