diff --git a/src/com/android/email/activity/AccountSelectorAdapter.java b/src/com/android/email/activity/AccountSelectorAdapter.java new file mode 100644 index 000000000..85d4105f9 --- /dev/null +++ b/src/com/android/email/activity/AccountSelectorAdapter.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * 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.android.email.activity; + +import com.android.email.provider.EmailContent; + +import android.content.Context; +import android.content.CursorLoader; +import android.content.Loader; +import android.database.Cursor; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; +import android.widget.TextView; + +/** + * Adapter for the account selector on {@link MessageListXL}. + * + * TODO Test it! + * TODO Use layout? Or use the standard resources that ActionBarDemo uses? + * TODO Revisit the sort order when we get more detailed UI spec. (current sort order makes things + * simpler for now.) Maybe we can just use SimpleCursorAdapter. + */ +public class AccountSelectorAdapter extends CursorAdapter { + private static final String[] PROJECTION = new String[] { + EmailContent.RECORD_ID, + EmailContent.Account.DISPLAY_NAME, + EmailContent.Account.EMAIL_ADDRESS + }; + + private static final int ID_COLUMN = 0; + private static final int DISPLAY_NAME_COLUMN = 1; + private static final int EMAIL_ADDRESS_COLUMN = 2; + + /** Sort order. Show the default account first. */ + private static final String ORDER_BY = + EmailContent.Account.IS_DEFAULT + " desc, " + EmailContent.Account.RECORD_ID; + + public static Loader createLoader(Context context) { + return new CursorLoader(context, EmailContent.Account.CONTENT_URI, PROJECTION, null, null, + ORDER_BY); + } + + public AccountSelectorAdapter(Context context, Cursor c) { + super(context, c, 0 /* no auto-requery */); + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + TextView v = (TextView) view; + v.setText(cursor.getString(EMAIL_ADDRESS_COLUMN)); + } + + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + return new TextView(context); + } + + /** @return Account id extracted from a Cursor. */ + public static long getAccountId(Cursor c) { + return c.getLong(ID_COLUMN); + } +} diff --git a/src/com/android/email/activity/DefaultAccountLoader.java b/src/com/android/email/activity/DefaultAccountLoader.java deleted file mode 100644 index fbf29dc32..000000000 --- a/src/com/android/email/activity/DefaultAccountLoader.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * 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.android.email.activity; - -import com.android.email.provider.EmailContent.Account; - -import android.content.AsyncTaskLoader; -import android.content.Context; - -/** - * A Loader to load the default account id asynchronously. - * - * TODO Test it. - */ -public class DefaultAccountLoader extends AsyncTaskLoader { - public DefaultAccountLoader(Context context) { - super(context); - } - - @Override - public Long loadInBackground() { - return Account.getDefaultAccountId(getContext()); - } - - @Override - public void destroy() { - stopLoading(); - } - - @Override - public void startLoading() { - cancelLoad(); - forceLoad(); - } - - @Override - public void stopLoading() { - cancelLoad(); - } -} diff --git a/src/com/android/email/activity/MailboxListFragment.java b/src/com/android/email/activity/MailboxListFragment.java index f6b00f8b8..cc5900290 100644 --- a/src/com/android/email/activity/MailboxListFragment.java +++ b/src/com/android/email/activity/MailboxListFragment.java @@ -165,11 +165,18 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList if (Email.DEBUG_LIFECYCLE && Email.DEBUG) { Log.d(Email.LOG_TAG, "MailboxListFragment onResume"); } - mStarted = false; super.onResume(); updateMessageCount(); } + @Override + public void onPause() { + if (Email.DEBUG_LIFECYCLE && Email.DEBUG) { + Log.d(Email.LOG_TAG, "MailboxListFragment onPause"); + } + super.onPause(); + } + /** * Called when the Fragment is no longer started. */ @@ -178,6 +185,7 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList if (Email.DEBUG_LIFECYCLE && Email.DEBUG) { Log.d(Email.LOG_TAG, "MailboxListFragment onStop"); } + mStarted = false; super.onStop(); cancelAllTasks(); } diff --git a/src/com/android/email/activity/MessageListXL.java b/src/com/android/email/activity/MessageListXL.java index d2ac42ec4..e7ed84c3c 100644 --- a/src/com/android/email/activity/MessageListXL.java +++ b/src/com/android/email/activity/MessageListXL.java @@ -19,12 +19,14 @@ package com.android.email.activity; import com.android.email.Email; import com.android.email.R; +import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.app.LoaderManager.LoaderCallbacks; import android.content.Context; import android.content.Loader; import android.content.pm.ActivityInfo; +import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; @@ -39,8 +41,8 @@ import android.view.View; * The main (two-pane) activity for XL devices. */ public class MessageListXL extends Activity implements View.OnClickListener, - MessageListXLFragmentManager.TargetActivity { - private static final int LOADER_ID_DEFAULT_ACCOUNT = 0; +MessageListXLFragmentManager.TargetActivity { + private static final int LOADER_ID_ACCOUNT_LIST = 0; private Context mContext; @@ -48,6 +50,10 @@ public class MessageListXL extends Activity implements View.OnClickListener, private View mMoveToNewerButton; private View mMoveToOlderButton; + private AccountSelectorAdapter mAccountsSelectorAdapter; + private final ActionBarNavigationCallback mActionBarNavigationCallback + = new ActionBarNavigationCallback(); + private MessageOrderManager mOrderManager; private final MessageListXLFragmentManager mFragmentManager @@ -76,13 +82,12 @@ public class MessageListXL extends Activity implements View.OnClickListener, mMoveToNewerButton.setOnClickListener(this); mMoveToOlderButton.setOnClickListener(this); + mAccountsSelectorAdapter = new AccountSelectorAdapter(mContext, null); + if (isRestoring) { mFragmentManager.loadState(savedInstanceState); } - if (!mFragmentManager.isAccountSelected()) { - loadDefaultAccount(); - } - // TODO load account list and show account selector + loadAccounts(); } @Override @@ -99,7 +104,7 @@ public class MessageListXL extends Activity implements View.OnClickListener, if (Email.DEBUG_LIFECYCLE && Email.DEBUG) Log.d(Email.LOG_TAG, "MessageListXL onStart"); super.onStart(); - mFragmentManager.setStart(); + mFragmentManager.onStart(); if (mFragmentManager.isMessageSelected()) { updateMessageOrderManager(); @@ -125,7 +130,6 @@ public class MessageListXL extends Activity implements View.OnClickListener, super.onStop(); mFragmentManager.onStop(); - stopMessageOrderManager(); } @@ -213,27 +217,6 @@ public class MessageListXL extends Activity implements View.OnClickListener, } } - private void loadDefaultAccount() { - getLoaderManager().initLoader(LOADER_ID_DEFAULT_ACCOUNT, null, new LoaderCallbacks() { - @Override - public Loader onCreateLoader(int id, Bundle args) { - return new DefaultAccountLoader(mContext); - } - - @Override - public void onLoadFinished(Loader loader, Long accountId) { - if (Email.DEBUG) { - Log.d(Email.LOG_TAG, "Default account=" + accountId); - } - if (accountId == null || accountId == -1) { - onNoAccountFound(); - } else { - mFragmentManager.selectAccount(accountId); - } - } - }); - } - /** * Called when the default account is not found, i.e. there's no account set up. */ @@ -364,6 +347,60 @@ public class MessageListXL extends Activity implements View.OnClickListener, stopMessageOrderManager(); } + private void loadAccounts() { + getLoaderManager().initLoader(LOADER_ID_ACCOUNT_LIST, null, new LoaderCallbacks() { + @Override + public Loader onCreateLoader(int id, Bundle args) { + return AccountSelectorAdapter.createLoader(mContext); + } + + @Override + public void onLoadFinished(Loader loader, Cursor data) { + updateAccountList(data); + } + }); + } + + private void updateAccountList(Cursor accountsCursor) { + if (accountsCursor.getCount() == 0) { + onNoAccountFound(); + return; + } + + // Find the currently selected account, and select it. + int defaultSelection = 0; + if (mFragmentManager.isAccountSelected()) { + // Need to change the selection + accountsCursor.moveToFirst(); + int i = 0; + while (accountsCursor.moveToNext()) { + final long accountId = AccountSelectorAdapter.getAccountId(accountsCursor); + if (accountId == mFragmentManager.getAccountId()) { + defaultSelection = i; + break; + } + i++; + } + } + + // Update the dropdown list. + final ActionBar ab = getActionBar(); + mAccountsSelectorAdapter.changeCursor(accountsCursor); + if (ab.getNavigationMode() != ActionBar.NAVIGATION_MODE_DROPDOWN_LIST) { + ab.setDropdownNavigationMode(mAccountsSelectorAdapter, + mActionBarNavigationCallback, defaultSelection); + } + } + + private class ActionBarNavigationCallback implements ActionBar.NavigationCallback { + @Override + public boolean onNavigationItemSelected(int itemPosition, long accountId) { + if (Email.DEBUG) Log.d(Email.LOG_TAG, "Account selected: accountId=" + accountId); + mFragmentManager.selectAccount(accountId); + return true; + } + } + /** * STOPSHIP: Remove this. * Rotate screen when the R key is pressed. Workaround for auto-orientation not working. @@ -372,9 +409,9 @@ public class MessageListXL extends Activity implements View.OnClickListener, public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_R) { setRequestedOrientation( - (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) - ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT - : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) + ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); return true; } return super.onKeyDown(keyCode, event); diff --git a/src/com/android/email/activity/MessageListXLFragmentManager.java b/src/com/android/email/activity/MessageListXLFragmentManager.java index 0b8f3a98b..c5ed86eab 100644 --- a/src/com/android/email/activity/MessageListXLFragmentManager.java +++ b/src/com/android/email/activity/MessageListXLFragmentManager.java @@ -149,7 +149,7 @@ class MessageListXLFragmentManager { * * @see #initRestoredFragments */ - public void setStart() { + public void onStart() { if (mIsActivityStarted) { return; }