Home | History | Annotate | Download | only in settings
      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 
     18 package com.android.mail.ui.settings;
     19 
     20 import android.app.ActionBar;
     21 import android.app.Fragment;
     22 import android.app.LoaderManager.LoaderCallbacks;
     23 import android.content.CursorLoader;
     24 import android.content.Loader;
     25 import android.database.Cursor;
     26 import android.os.Bundle;
     27 import android.preference.PreferenceActivity;
     28 import android.text.TextUtils;
     29 
     30 import com.android.mail.R;
     31 import com.android.mail.providers.Account;
     32 import com.android.mail.providers.MailAppProvider;
     33 import com.android.mail.providers.UIProvider;
     34 import com.android.mail.providers.UIProvider.AccountCapabilities;
     35 import com.google.common.annotations.VisibleForTesting;
     36 
     37 import java.lang.ref.WeakReference;
     38 import java.util.List;
     39 
     40 public class MailPreferenceActivity extends PreferenceActivity {
     41 
     42     public static final String PREFERENCE_FRAGMENT_ID = "preference_fragment_id";
     43 
     44     private static final int ACCOUNT_LOADER_ID = 0;
     45 
     46     private WeakReference<GeneralPrefsFragment> mGeneralPrefsFragmentRef;
     47 
     48     private Cursor mAccountsCursor;
     49 
     50     @Override
     51     protected void onCreate(Bundle savedInstanceState) {
     52         super.onCreate(savedInstanceState);
     53 
     54         final ActionBar actionBar = getActionBar();
     55         if (actionBar != null) {
     56             // Hide the app icon.
     57             actionBar.setIcon(android.R.color.transparent);
     58             actionBar.setDisplayUseLogoEnabled(false);
     59         }
     60 
     61         getLoaderManager().initLoader(ACCOUNT_LOADER_ID, null, new AccountLoaderCallbacks());
     62     }
     63 
     64     private class AccountLoaderCallbacks implements LoaderCallbacks<Cursor> {
     65         @Override
     66         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
     67             return new CursorLoader(MailPreferenceActivity.this,
     68                     MailAppProvider.getAccountsUri(), UIProvider.ACCOUNTS_PROJECTION,
     69                     null, null, null);
     70         }
     71 
     72         @Override
     73         public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
     74             mAccountsCursor = data;
     75             invalidateHeaders();
     76         }
     77 
     78         @Override
     79         public void onLoaderReset(Loader<Cursor> loader) {
     80             mAccountsCursor = null;
     81             invalidateHeaders();
     82         }
     83     }
     84 
     85     @VisibleForTesting
     86     GeneralPrefsFragment getGeneralPrefsFragment() {
     87         return mGeneralPrefsFragmentRef != null ? mGeneralPrefsFragmentRef.get() : null;
     88     }
     89 
     90     @Override
     91     public void onAttachFragment(Fragment fragment) {
     92         super.onAttachFragment(fragment);
     93         if (fragment instanceof GeneralPrefsFragment) {
     94             mGeneralPrefsFragmentRef =
     95                     new WeakReference<GeneralPrefsFragment>((GeneralPrefsFragment) fragment);
     96         }
     97     }
     98 
     99     @Override
    100     protected boolean isValidFragment(String fragmentName) {
    101         // TODO: STOPSHIP fix Email to use the PublicPreferenceActivity trampoline
    102         return true;
    103     }
    104 
    105     @Override
    106     public void onBuildHeaders(List<Header> target) {
    107         loadHeadersFromResource(R.xml.preference_headers, target);
    108         if (mAccountsCursor != null && mAccountsCursor.moveToFirst()) {
    109             do {
    110                 final Account account = Account.builder().buildFrom(mAccountsCursor);
    111                 // TODO: This will no longer be needed when the Combined view is moved to Unified
    112                 if (!account.supportsCapability(AccountCapabilities.VIRTUAL_ACCOUNT)) {
    113                     final Header header = new Header();
    114                     if (TextUtils.isEmpty(account.getDisplayName()) ||
    115                             TextUtils.equals(account.getDisplayName(), account.getEmailAddress())) {
    116                         // No (useful) display name, just use the email address
    117                         header.title = account.getEmailAddress();
    118                     } else {
    119                         header.title = account.getDisplayName();
    120                         header.summary = account.getEmailAddress();
    121                     }
    122                     header.fragment = account.settingsFragmentClass;
    123                     final Bundle accountBundle = new Bundle(1);
    124                     accountBundle.putString(MailAccountPrefsFragment.ARG_ACCOUNT_EMAIL,
    125                             account.getEmailAddress());
    126                     header.fragmentArguments = accountBundle;
    127 
    128                     target.add(header);
    129                 }
    130             } while (mAccountsCursor.moveToNext());
    131         }
    132         onBuildExtraHeaders(target);
    133     }
    134 
    135     /**
    136      * Override this in a subclass to add extra headers besides "General Settings" and accounts
    137      * @param target List of headers to mutate
    138      */
    139     public void onBuildExtraHeaders(List<Header> target) {
    140     }
    141 }
    142