michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.fxa.activities; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.fxa.FirefoxAccounts; michael@0: import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; michael@0: import org.mozilla.gecko.sync.setup.activities.LocaleAware.LocaleAwareFragmentActivity; michael@0: michael@0: import android.accounts.Account; michael@0: import android.annotation.TargetApi; michael@0: import android.app.ActionBar; michael@0: import android.content.Intent; michael@0: import android.os.Build; michael@0: import android.os.Bundle; michael@0: import android.view.MenuItem; michael@0: michael@0: /** michael@0: * Activity which displays account status. michael@0: */ michael@0: public class FxAccountStatusActivity extends LocaleAwareFragmentActivity { michael@0: private static final String LOG_TAG = FxAccountStatusActivity.class.getSimpleName(); michael@0: michael@0: protected FxAccountStatusFragment statusFragment; michael@0: michael@0: @Override michael@0: protected void onCreate(Bundle savedInstanceState) { michael@0: super.onCreate(savedInstanceState); michael@0: michael@0: // Display the fragment as the content. michael@0: statusFragment = new FxAccountStatusFragment(); michael@0: getSupportFragmentManager() michael@0: .beginTransaction() michael@0: .replace(android.R.id.content, statusFragment) michael@0: .commit(); michael@0: michael@0: maybeSetHomeButtonEnabled(); michael@0: } michael@0: michael@0: /** michael@0: * Sufficiently recent Android versions need additional code to receive taps michael@0: * on the status bar to go "up". See this stackoverflow answer for michael@0: * more information. michael@0: */ michael@0: @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) michael@0: protected void maybeSetHomeButtonEnabled() { michael@0: if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { michael@0: Logger.debug(LOG_TAG, "Not enabling home button; version too low."); michael@0: return; michael@0: } michael@0: final ActionBar actionBar = getActionBar(); michael@0: if (actionBar != null) { michael@0: Logger.debug(LOG_TAG, "Enabling home button."); michael@0: actionBar.setHomeButtonEnabled(true); michael@0: return; michael@0: } michael@0: Logger.debug(LOG_TAG, "Not enabling home button."); michael@0: } michael@0: michael@0: @Override michael@0: public void onResume() { michael@0: super.onResume(); michael@0: michael@0: final AndroidFxAccount fxAccount = getAndroidFxAccount(); michael@0: if (fxAccount == null) { michael@0: Logger.warn(LOG_TAG, "Could not get Firefox Account."); michael@0: michael@0: // Gracefully redirect to get started. michael@0: Intent intent = new Intent(this, FxAccountGetStartedActivity.class); michael@0: // Per http://stackoverflow.com/a/8992365, this triggers a known bug with michael@0: // the soft keyboard not being shown for the started activity. Why, Android, why? michael@0: intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); michael@0: startActivity(intent); michael@0: michael@0: setResult(RESULT_CANCELED); michael@0: finish(); michael@0: return; michael@0: } michael@0: statusFragment.refresh(fxAccount); michael@0: } michael@0: michael@0: /** michael@0: * Helper to fetch (unique) Android Firefox Account if one exists, or return null. michael@0: */ michael@0: protected AndroidFxAccount getAndroidFxAccount() { michael@0: Account account = FirefoxAccounts.getFirefoxAccount(this); michael@0: if (account == null) { michael@0: return null; michael@0: } michael@0: return new AndroidFxAccount(this, account); michael@0: } michael@0: michael@0: @Override michael@0: public boolean onOptionsItemSelected(MenuItem item) { michael@0: int itemId = item.getItemId(); michael@0: switch (itemId) { michael@0: case android.R.id.home: michael@0: finish(); michael@0: return true; michael@0: } michael@0: return super.onOptionsItemSelected(item); michael@0: } michael@0: }