mobile/android/base/fxa/activities/FxAccountStatusActivity.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/fxa/activities/FxAccountStatusActivity.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.fxa.activities;
     1.9 +
    1.10 +import org.mozilla.gecko.background.common.log.Logger;
    1.11 +import org.mozilla.gecko.fxa.FirefoxAccounts;
    1.12 +import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
    1.13 +import org.mozilla.gecko.sync.setup.activities.LocaleAware.LocaleAwareFragmentActivity;
    1.14 +
    1.15 +import android.accounts.Account;
    1.16 +import android.annotation.TargetApi;
    1.17 +import android.app.ActionBar;
    1.18 +import android.content.Intent;
    1.19 +import android.os.Build;
    1.20 +import android.os.Bundle;
    1.21 +import android.view.MenuItem;
    1.22 +
    1.23 +/**
    1.24 + * Activity which displays account status.
    1.25 + */
    1.26 +public class FxAccountStatusActivity extends LocaleAwareFragmentActivity {
    1.27 +  private static final String LOG_TAG = FxAccountStatusActivity.class.getSimpleName();
    1.28 +
    1.29 +  protected FxAccountStatusFragment statusFragment;
    1.30 +
    1.31 +  @Override
    1.32 +  protected void onCreate(Bundle savedInstanceState) {
    1.33 +    super.onCreate(savedInstanceState);
    1.34 +
    1.35 +    // Display the fragment as the content.
    1.36 +    statusFragment = new FxAccountStatusFragment();
    1.37 +    getSupportFragmentManager()
    1.38 +      .beginTransaction()
    1.39 +      .replace(android.R.id.content, statusFragment)
    1.40 +      .commit();
    1.41 +
    1.42 +    maybeSetHomeButtonEnabled();
    1.43 +  }
    1.44 +
    1.45 +  /**
    1.46 +   * Sufficiently recent Android versions need additional code to receive taps
    1.47 +   * on the status bar to go "up". See <a
    1.48 +   * href="http://stackoverflow.com/a/8953148">this stackoverflow answer</a> for
    1.49 +   * more information.
    1.50 +   */
    1.51 +  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    1.52 +  protected void maybeSetHomeButtonEnabled() {
    1.53 +    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    1.54 +      Logger.debug(LOG_TAG, "Not enabling home button; version too low.");
    1.55 +      return;
    1.56 +    }
    1.57 +    final ActionBar actionBar = getActionBar();
    1.58 +    if (actionBar != null) {
    1.59 +      Logger.debug(LOG_TAG, "Enabling home button.");
    1.60 +      actionBar.setHomeButtonEnabled(true);
    1.61 +      return;
    1.62 +    }
    1.63 +    Logger.debug(LOG_TAG, "Not enabling home button.");
    1.64 +  }
    1.65 +
    1.66 +  @Override
    1.67 +  public void onResume() {
    1.68 +    super.onResume();
    1.69 +
    1.70 +    final AndroidFxAccount fxAccount = getAndroidFxAccount();
    1.71 +    if (fxAccount == null) {
    1.72 +      Logger.warn(LOG_TAG, "Could not get Firefox Account.");
    1.73 +
    1.74 +      // Gracefully redirect to get started.
    1.75 +      Intent intent = new Intent(this, FxAccountGetStartedActivity.class);
    1.76 +      // Per http://stackoverflow.com/a/8992365, this triggers a known bug with
    1.77 +      // the soft keyboard not being shown for the started activity. Why, Android, why?
    1.78 +      intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    1.79 +      startActivity(intent);
    1.80 +
    1.81 +      setResult(RESULT_CANCELED);
    1.82 +      finish();
    1.83 +      return;
    1.84 +    }
    1.85 +    statusFragment.refresh(fxAccount);
    1.86 +  }
    1.87 +
    1.88 +  /**
    1.89 +   * Helper to fetch (unique) Android Firefox Account if one exists, or return null.
    1.90 +   */
    1.91 +  protected AndroidFxAccount getAndroidFxAccount() {
    1.92 +    Account account = FirefoxAccounts.getFirefoxAccount(this);
    1.93 +    if (account == null) {
    1.94 +      return null;
    1.95 +    }
    1.96 +    return new AndroidFxAccount(this, account);
    1.97 +  }
    1.98 +
    1.99 +  @Override
   1.100 +  public boolean onOptionsItemSelected(MenuItem item) {
   1.101 +    int itemId = item.getItemId();
   1.102 +    switch (itemId) {
   1.103 +    case android.R.id.home:
   1.104 +      finish();
   1.105 +      return true;
   1.106 +    }
   1.107 +    return super.onOptionsItemSelected(item);
   1.108 +  }
   1.109 +}

mercurial