1/******************************************************************************* 2 * Copyright (C) 2014 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 *******************************************************************************/ 17 18package com.android.mail.ui.settings; 19 20import android.app.Fragment; 21import android.app.LoaderManager.LoaderCallbacks; 22import android.content.CursorLoader; 23import android.content.Loader; 24import android.database.Cursor; 25import android.os.Bundle; 26import android.preference.PreferenceActivity; 27import android.text.TextUtils; 28 29import com.android.mail.R; 30import com.android.mail.providers.Account; 31import com.android.mail.providers.MailAppProvider; 32import com.android.mail.providers.UIProvider; 33import com.android.mail.providers.UIProvider.AccountCapabilities; 34import com.google.common.annotations.VisibleForTesting; 35 36import java.lang.ref.WeakReference; 37import java.util.List; 38 39public class MailPreferenceActivity extends PreferenceActivity { 40 41 public static final String PREFERENCE_FRAGMENT_ID = "preference_fragment_id"; 42 43 private static final int ACCOUNT_LOADER_ID = 0; 44 45 private WeakReference<GeneralPrefsFragment> mGeneralPrefsFragmentRef; 46 47 private Cursor mAccountsCursor; 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 53 getLoaderManager().initLoader(ACCOUNT_LOADER_ID, null, new AccountLoaderCallbacks()); 54 } 55 56 private class AccountLoaderCallbacks implements LoaderCallbacks<Cursor> { 57 @Override 58 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 59 return new CursorLoader(MailPreferenceActivity.this, 60 MailAppProvider.getAccountsUri(), UIProvider.ACCOUNTS_PROJECTION, 61 null, null, null); 62 } 63 64 @Override 65 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 66 mAccountsCursor = data; 67 invalidateHeaders(); 68 } 69 70 @Override 71 public void onLoaderReset(Loader<Cursor> loader) { 72 mAccountsCursor = null; 73 invalidateHeaders(); 74 } 75 } 76 77 @VisibleForTesting 78 GeneralPrefsFragment getGeneralPrefsFragment() { 79 return mGeneralPrefsFragmentRef != null ? mGeneralPrefsFragmentRef.get() : null; 80 } 81 82 @Override 83 public void onAttachFragment(Fragment fragment) { 84 super.onAttachFragment(fragment); 85 if (fragment instanceof GeneralPrefsFragment) { 86 mGeneralPrefsFragmentRef = 87 new WeakReference<GeneralPrefsFragment>((GeneralPrefsFragment) fragment); 88 } 89 } 90 91 @Override 92 protected boolean isValidFragment(String fragmentName) { 93 // TODO: STOPSHIP fix Email to use the PublicPreferenceActivity trampoline 94 return true; 95 } 96 97 @Override 98 public void onBuildHeaders(List<Header> target) { 99 loadHeadersFromResource(R.xml.preference_headers, target); 100 if (mAccountsCursor != null && mAccountsCursor.moveToFirst()) { 101 do { 102 final Account account = Account.builder().buildFrom(mAccountsCursor); 103 // TODO: This will no longer be needed when the Combined view is moved to Unified 104 if (!account.supportsCapability(AccountCapabilities.VIRTUAL_ACCOUNT)) { 105 final Header header = new Header(); 106 if (TextUtils.isEmpty(account.getDisplayName()) || 107 TextUtils.equals(account.getDisplayName(), account.getEmailAddress())) { 108 // No (useful) display name, just use the email address 109 header.title = account.getEmailAddress(); 110 } else { 111 header.title = account.getDisplayName(); 112 header.summary = account.getEmailAddress(); 113 } 114 header.fragment = account.settingsFragmentClass; 115 final Bundle accountBundle = new Bundle(1); 116 accountBundle.putString(MailAccountPrefsFragment.ARG_ACCOUNT_EMAIL, 117 account.getEmailAddress()); 118 header.fragmentArguments = accountBundle; 119 120 target.add(header); 121 } 122 } while (mAccountsCursor.moveToNext()); 123 } 124 onBuildExtraHeaders(target); 125 } 126 127 /** 128 * Override this in a subclass to add extra headers besides "General Settings" and accounts 129 * @param target List of headers to mutate 130 */ 131 public void onBuildExtraHeaders(List<Header> target) { 132 } 133} 134