Android Open Source - android-google-spreadsheets-api Main Activity






From Project

Back to project page android-google-spreadsheets-api.

License

The source code is released under:

Apache License

If you think the Android project android-google-spreadsheets-api 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

package com.example.android.spreadsheet.sample;
/*from   ww w . ja v  a  2  s  .  c om*/
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.github.spreadsheets.android.api.model.SpreadsheetFeed;
import com.github.spreadsheets.android.api.oauth.SpreadsheetOAuth;
import com.github.spreadsheets.android.api.oauth.SpreadsheetOAuthActivity;
import com.github.spreadsheets.android.api.requests.SpreadsheetFeedRequest;
import com.google.android.gms.auth.GoogleAuthUtil;

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    private static final int REQUEST_CODE_OAUTH_ACTIVITY = 103;
    private AccountManager mAccountManager;
    private Spinner mAccountTypesSpinner;
    private String[] mNamesArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        String saad;
//        SpreadsheetOAuth.setAuthToken("saad");
//        try {
//            saad = SpreadsheetOAuth.getAuthToken();
//        } catch (AuthFailureError authFailureError) {
//            authFailureError.printStackTrace();
//        }

        final Button btnGetAccess = (Button) findViewById(R.id.btn_oauth);
        btnGetAccess.setOnClickListener(this);

        mNamesArray = getAccountNames();
        mAccountTypesSpinner = initializeSpinner(
                R.id.accounts_tester_account_types_spinner, mNamesArray);
    }

    private Response.Listener<SpreadsheetFeed> getSuccessListener() {
        return new Response.Listener<SpreadsheetFeed>() {

            @Override
            public void onResponse(SpreadsheetFeed response) {
                Toast.makeText(MainActivity.this, "Request successful", Toast.LENGTH_LONG).show();
            }
        };
    }

    private Response.ErrorListener getErrorListener() {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("TAG", error.getMessage());
                Toast.makeText(MainActivity.this, "Request failed", Toast.LENGTH_LONG).show();
            }
        };
    }

    private Spinner initializeSpinner(int id, String[] values) {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, values);
        Spinner spinner = (Spinner) findViewById(id);
        spinner.setAdapter(adapter);
        return spinner;
    }

    private String[] getAccountNames() {
        mAccountManager = AccountManager.get(this);
        Account[] accounts = mAccountManager.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
        String[] names = new String[accounts.length];
        for (int i = 0; i < names.length; i++) {
            names[i] = accounts[i].name;
        }
        return names;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_oauth:
                int accountIndex = mAccountTypesSpinner.getSelectedItemPosition();
                if (accountIndex < 0) {
                    // this happens when the sample is run in an emulator which has no google account
                    // added yet.
                    Toast.makeText(this,
                            "No account available. Please add an account to the phone first.",
                            Toast.LENGTH_LONG).show();
                    return;
                }
                getToken(mNamesArray[accountIndex]);
                break;
        }
    }

    private void getToken(final String email) {
        final Intent intent = new Intent(this, SpreadsheetOAuthActivity.class);
        intent.putExtra(SpreadsheetOAuthActivity.EXTRA_EMAIL, email);
        intent.putExtra(SpreadsheetOAuthActivity.EXTRA_SCOPE, SpreadsheetOAuth.READ_WRITE_SCOPE);
        startActivityForResult(intent, REQUEST_CODE_OAUTH_ACTIVITY);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_OAUTH_ACTIVITY) {
            switch (resultCode) {
                case RESULT_OK:
                    useToken(data.getStringExtra(SpreadsheetOAuthActivity.EXTRA_TOKEN));
                    break;

                case RESULT_CANCELED:
                    String msg = data.getStringExtra(SpreadsheetOAuthActivity.EXTRA_RESULT_MESSAGE);
                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
                    break;
            }
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void useToken(String token) {
        SpreadsheetOAuth.setAuthToken(token);
        RequestQueue mRequestQueue = Volley.newRequestQueue(this);
        SpreadsheetFeedRequest mRequest = new SpreadsheetFeedRequest(getSuccessListener(), getErrorListener());
        mRequestQueue.add(mRequest);
    }
}




Java Source Code List

com.example.android.spreadsheet.sample.MainActivity.java
com.github.spreadsheets.android.api.model.Author.java
com.github.spreadsheets.android.api.model.BatchOperation.java
com.github.spreadsheets.android.api.model.BatchStatus.java
com.github.spreadsheets.android.api.model.CellEntry.java
com.github.spreadsheets.android.api.model.CellFeed.java
com.github.spreadsheets.android.api.model.Cell.java
com.github.spreadsheets.android.api.model.Content.java
com.github.spreadsheets.android.api.model.Entry.java
com.github.spreadsheets.android.api.model.Feed.java
com.github.spreadsheets.android.api.model.Link.java
com.github.spreadsheets.android.api.model.ListEntry.java
com.github.spreadsheets.android.api.model.ListFeed.java
com.github.spreadsheets.android.api.model.List.java
com.github.spreadsheets.android.api.model.SpreadsheetEntry.java
com.github.spreadsheets.android.api.model.SpreadsheetFeed.java
com.github.spreadsheets.android.api.model.SpreadsheetUrl.java
com.github.spreadsheets.android.api.model.WorksheetData.java
com.github.spreadsheets.android.api.model.WorksheetEntry.java
com.github.spreadsheets.android.api.model.WorksheetFeed.java
com.github.spreadsheets.android.api.oauth.SpreadsheetOAuthActivity.java
com.github.spreadsheets.android.api.oauth.SpreadsheetOAuth.java
com.github.spreadsheets.android.api.requests.CellEntryRequest.java
com.github.spreadsheets.android.api.requests.CellFeedRequest.java
com.github.spreadsheets.android.api.requests.ListEntryRequest.java
com.github.spreadsheets.android.api.requests.ListFeedRequest.java
com.github.spreadsheets.android.api.requests.SpreadsheetEntryRequest.java
com.github.spreadsheets.android.api.requests.SpreadsheetFeedRequest.java
com.github.spreadsheets.android.api.requests.SpreadsheetRequest.java
com.github.spreadsheets.android.api.requests.WorksheetEntryRequest.java
com.github.spreadsheets.android.api.requests.WorksheetFeedRequest.java