Android Open Source - android-demos Base Demo Activity






From Project

Back to project page android-demos.

License

The source code is released under:

Apache License

If you think the Android project android-demos listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2013 Google Inc. All Rights Reserved.
 */*from  www .j a  v  a2  s  .  c o  m*/
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.gms.drive.sample.demo;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

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;

/**
 * An abstract activity that handles authorization and connection to the Drive
 * services.
 */
public abstract class BaseDemoActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    private static final String TAG = "BaseDriveActivity";

    /**
     * DriveId of an existing folder to be used as a parent folder in
     * folder operations samples.
     */
    public static final String EXISTING_FOLDER_ID = "0B2EEtIjPUdX6MERsWlYxN3J6RU0";

    /**
     * DriveId of an existing file to be used in file operation samples..
     */
    public static final String EXISTING_FILE_ID = "0ByfSjdPVs9MZTHBmMVdSeWxaNTg";

    /**
     * Extra for account name.
     */
    protected static final String EXTRA_ACCOUNT_NAME = "account_name";

    /**
     * Request code for auto Google Play Services error resolution.
     */
    protected static final int REQUEST_CODE_RESOLUTION = 1;

    /**
     * Next available request code.
     */
    protected static final int NEXT_AVAILABLE_REQUEST_CODE = 2;

    /**
     * Google API client.
     */
    private GoogleApiClient mGoogleApiClient;

    /**
     * Called when activity gets visible. A connection to Drive services need to
     * be initiated as soon as the activity is visible. Registers
     * {@code ConnectionCallbacks} and {@code OnConnectionFailedListener} on the
     * activities itself.
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        mGoogleApiClient.connect();
    }

    /**
     * Handles resolution callbacks.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
            mGoogleApiClient.connect();
        }
    }

    /**
     * Called when activity gets invisible. Connection to Drive service needs to
     * be disconnected as soon as an activity is invisible.
     */
    @Override
    protected void onPause() {
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onPause();
    }

    /**
     * Called when {@code mGoogleApiClient} is connected.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "GoogleApiClient connected");
    }

    /**
     * Called when {@code mGoogleApiClient} is disconnected.
     */
    @Override
    public void onConnectionSuspended(int cause) {
        Log.i(TAG, "GoogleApiClient connection suspended");
    }

    /**
     * Called when {@code mGoogleApiClient} is trying to connect but failed.
     * Handle {@code result.getResolution()} if there is a resolution is
     * available.
     */
    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
        if (!result.hasResolution()) {
            // show the localized error dialog.
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
            return;
        }
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
        }
    }

    /**
     * Shows a toast message.
     */
    public void showMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    /**
     * Getter for the {@code GoogleApiClient}.
     */
    public GoogleApiClient getGoogleApiClient() {
      return mGoogleApiClient;
    }
}




Java Source Code List

com.google.android.gms.drive.sample.demo.ApiClientAsyncTask.java
com.google.android.gms.drive.sample.demo.BaseDemoActivity.java
com.google.android.gms.drive.sample.demo.CreateEmptyFileActivity.java
com.google.android.gms.drive.sample.demo.CreateFileActivity.java
com.google.android.gms.drive.sample.demo.CreateFileInAppFolderActivity.java
com.google.android.gms.drive.sample.demo.CreateFileInFolderActivity.java
com.google.android.gms.drive.sample.demo.CreateFileWithCreatorActivity.java
com.google.android.gms.drive.sample.demo.CreateFolderActivity.java
com.google.android.gms.drive.sample.demo.CreateFolderInFolderActivity.java
com.google.android.gms.drive.sample.demo.DeleteCustomPropertyActivity.java
com.google.android.gms.drive.sample.demo.EditContentsActivity.java
com.google.android.gms.drive.sample.demo.EditMetadataActivity.java
com.google.android.gms.drive.sample.demo.HomeActivity.java
com.google.android.gms.drive.sample.demo.InsertUpdateCustomPropertyActivity.java
com.google.android.gms.drive.sample.demo.ListFilesActivity.java
com.google.android.gms.drive.sample.demo.ListFilesInFolderActivity.java
com.google.android.gms.drive.sample.demo.PickFileWithOpenerActivity.java
com.google.android.gms.drive.sample.demo.PickFolderWithOpenerActivity.java
com.google.android.gms.drive.sample.demo.PinFileActivity.java
com.google.android.gms.drive.sample.demo.QueryFilesActivity.java
com.google.android.gms.drive.sample.demo.QueryFilesInFolderActivity.java
com.google.android.gms.drive.sample.demo.QueryFilesSharedWithMeActivity.java
com.google.android.gms.drive.sample.demo.QueryFilesWithAInTitleActivity.java
com.google.android.gms.drive.sample.demo.QueryFilesWithCustomPropertyActivity.java
com.google.android.gms.drive.sample.demo.QueryNonTextFilesActivity.java
com.google.android.gms.drive.sample.demo.QuerySortedFilesActivity.java
com.google.android.gms.drive.sample.demo.QueryStarredTextFilesActivity.java
com.google.android.gms.drive.sample.demo.QueryTextOrHtmlFilesActivity.java
com.google.android.gms.drive.sample.demo.ResultsAdapter.java
com.google.android.gms.drive.sample.demo.RetrieveContentsActivity.java
com.google.android.gms.drive.sample.demo.RetrieveContentsWithProgressDialogActivity.java
com.google.android.gms.drive.sample.demo.RetrieveMetadataActivity.java
com.google.android.gms.drive.sample.demo.SyncRequestsActivity.java
com.google.android.gms.drive.sample.demo.events.ListenChangeEventsForFilesActivity.java