Android Open Source - connexus-android Auth Activity






From Project

Back to project page connexus-android.

License

The source code is released under:

Apache License

If you think the Android project connexus-android 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 (C) 2013 Zach Whaley, Trevor Latson
 */*w  w w . j a v a 2 s.  c om*/
 * 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 ginger.connexus.activity;

import ginger.connexus.R;
import ginger.connexus.fragment.ChooseAccountFragment;
import ginger.connexus.util.AccountUtils;
import ginger.connexus.util.AccountUtils.AuthenticateCallback;
import android.accounts.Account;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.gms.common.GooglePlayServicesUtil;

public class AuthActivity extends FragmentActivity implements ChooseAccountFragment.ChooseAccountListener,
        AuthenticateCallback {

    private static final String TAG = AuthActivity.class.toString();
    private static final String KEY_CHOSEN_ACCOUNT = "chosen_account";
    private static final String CHOOSE_ACCOUNT_TAG = "account_chooser";
    private static final String POST_AUTH_CATEGORY = "ginger.connexus.category.POST_AUTH";
    private static final int REQUEST_RECOVER_FROM_AUTH_ERROR = 101;
    private static final int REQUEST_RECOVER_FROM_PLAY_SERVICES_ERROR = 102;

    public static final String EXTRA_FINISH_INTENT = "ginger.connexus.extra.FINISH_INTENT";

    private Account mChosenAccount;
    private Intent mFinishIntent;
    private boolean mCancelAuth = false;
    private boolean mAuthInProgress = false;

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

        setContentView(R.layout.activity_auth);
        setTitle("Sign in");
        createSignInButton();

        final Intent intent = getIntent();
        if (intent.hasExtra(EXTRA_FINISH_INTENT)) {
            mFinishIntent = intent.getParcelableExtra(EXTRA_FINISH_INTENT);
        }

        if (savedInstanceState == null) {
            if (AccountUtils.isAuthenticated(this)) {
                mChosenAccount = new Account(AccountUtils.getChosenAccountName(this), "com.google");
            }
        } else {
            String accountName = savedInstanceState.getString(KEY_CHOSEN_ACCOUNT);
            if (accountName != null) {
                mChosenAccount = new Account(accountName, "com.google");
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAuthInProgress) {
            mCancelAuth = true;
        }
    }

    private void createSignInButton() {
        Button signInButton = (Button) findViewById(R.id.sign_in_button);
        signInButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ChooseAccountFragment.newInstance(AuthActivity.this)
                        .show(getSupportFragmentManager(), CHOOSE_ACCOUNT_TAG);
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (mChosenAccount != null)
            outState.putString(KEY_CHOSEN_ACCOUNT, mChosenAccount.name);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onAccountChosen(final Account account) {
        mChosenAccount = account;
        mAuthInProgress = true;
        AccountUtils.tryAuthenticate(this, this, mChosenAccount.name, REQUEST_RECOVER_FROM_AUTH_ERROR);
    }

    @Override
    public boolean shouldCancelAuthentication() {
        return mCancelAuth;
    }

    @Override
    public void onAuthTokenAvailable() {
        mAuthInProgress = false;
        if (mFinishIntent != null) {
            // Ensure the finish intent is unique within the task. Otherwise, if
            // the task was started with this intent, and it finishes like it
            // should, then startActivity on the intent again won't work.
            mFinishIntent.addCategory(POST_AUTH_CATEGORY);
            startActivity(mFinishIntent);
        }
        finish();
    }

    @Override
    public void onRecoverableException(final int code) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final Dialog d = GooglePlayServicesUtil.getErrorDialog(code, AuthActivity.this,
                        REQUEST_RECOVER_FROM_PLAY_SERVICES_ERROR);
                d.show();
            }
        });
    }

    @Override
    public void onUnRecoverableException(final String errorMessage) {
        Log.w(TAG, "Encountered unrecoverable exception: " + errorMessage);
    }

}




Java Source Code List

android.UnusedStub.java
ginger.connexus.Connexus.java
ginger.connexus.activity.AuthActivity.java
ginger.connexus.activity.BaseActivity.java
ginger.connexus.activity.ImageDetailActivity.java
ginger.connexus.activity.ImageGridActivity.java
ginger.connexus.activity.MainActivity.java
ginger.connexus.activity.SearchableActivity.java
ginger.connexus.adapter.ImageAdapter.java
ginger.connexus.fragment.ChooseAccountFragment.java
ginger.connexus.fragment.GridFragment.java
ginger.connexus.fragment.ImageDetailFragment.java
ginger.connexus.fragment.ImageGridFragment.java
ginger.connexus.fragment.SpiceFragment.java
ginger.connexus.fragment.StreamGridFragment.java
ginger.connexus.model.ConnexusImage.java
ginger.connexus.model.ConnexusStream.java
ginger.connexus.network.ConnexusApi.java
ginger.connexus.network.RequestAllStreams.java
ginger.connexus.network.RequestNearbyStreams.java
ginger.connexus.network.RequestStreamImages.java
ginger.connexus.network.RequestSubscribedStreams.java
ginger.connexus.network.RequestUploadURL.java
ginger.connexus.network.UploadImage.java
ginger.connexus.service.ConnexusService.java
ginger.connexus.ui.RecyclingBitmapDrawable.java
ginger.connexus.ui.RecyclingImageView.java
ginger.connexus.util.AccountUtils.java
ginger.connexus.util.AsyncTask.java
ginger.connexus.util.DiskLruCache.java
ginger.connexus.util.ImageCache.java
ginger.connexus.util.ImageFetcher.java
ginger.connexus.util.ImageResizer.java
ginger.connexus.util.ImageWorker.java
ginger.connexus.util.Images.java
ginger.connexus.util.Utils.java