Home | History | Annotate | Download | only in browse
      1 /*
      2  * Copyright (C) 2013 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.browse;
     19 
     20 import android.app.ActionBar;
     21 import android.app.Activity;
     22 import android.app.FragmentTransaction;
     23 import android.app.LoaderManager;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.content.Loader;
     27 import android.net.Uri;
     28 import android.os.Bundle;
     29 import android.view.Menu;
     30 import android.view.MenuItem;
     31 
     32 import com.android.mail.R;
     33 import com.android.mail.content.CursorCreator;
     34 import com.android.mail.content.ObjectCursor;
     35 import com.android.mail.content.ObjectCursorLoader;
     36 import com.android.mail.providers.Account;
     37 import com.android.mail.providers.UIProvider;
     38 import com.android.mail.ui.FeedbackEnabledActivity;
     39 import com.android.mail.utils.LogTag;
     40 import com.android.mail.utils.LogUtils;
     41 import com.android.mail.utils.MimeType;
     42 import com.android.mail.utils.Utils;
     43 
     44 public class EmlViewerActivity extends Activity implements FeedbackEnabledActivity,
     45         ConversationAccountController {
     46     public static final String EXTRA_ACCOUNT_URI = "extra-account-uri";
     47 
     48     private static final String LOG_TAG = LogTag.getLogTag();
     49 
     50     private static final String FRAGMENT_TAG = "eml_message_fragment";
     51 
     52     private static final int ACCOUNT_LOADER = 0;
     53 
     54     private static final String SAVED_ACCOUNT = "saved-account";
     55 
     56     private MenuItem mHelpItem;
     57     private MenuItem mSendFeedbackItem;
     58 
     59     private Uri mAccountUri;
     60     private Account mAccount;
     61 
     62     private final AccountLoadCallbacks mAccountLoadCallbacks = new AccountLoadCallbacks();
     63 
     64     @Override
     65     protected void onCreate(Bundle savedInstanceState) {
     66         super.onCreate(savedInstanceState);
     67         setContentView(R.layout.eml_viewer_activity);
     68 
     69         final ActionBar actionBar = getActionBar();
     70         actionBar.setDisplayHomeAsUpEnabled(true);
     71 
     72         final Intent intent = getIntent();
     73         final String action = intent.getAction();
     74         final String type = intent.getType();
     75         mAccountUri = intent.getParcelableExtra(EXTRA_ACCOUNT_URI);
     76 
     77         if (savedInstanceState == null) {
     78             if (Intent.ACTION_VIEW.equals(action) &&
     79                     MimeType.isEmlMimeType(type)) {
     80                 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
     81                 transaction.add(R.id.eml_root, EmlMessageViewFragment.newInstance(
     82                         intent.getData(), mAccountUri), FRAGMENT_TAG);
     83                 transaction.commit();
     84             } else {
     85                 LogUtils.wtf(LOG_TAG,
     86                         "Entered EmlViewerActivity with wrong intent action or type: %s, %s",
     87                         action, type);
     88                 finish(); // we should not be here. bail out. bail out.
     89                 return;
     90             }
     91         } else {
     92             if (savedInstanceState.containsKey(SAVED_ACCOUNT)) {
     93                 mAccount = savedInstanceState.getParcelable(SAVED_ACCOUNT);
     94             }
     95         }
     96 
     97         // Account uri will be null if we launched from outside of the app.
     98         // So just don't load an account at all.
     99         if (mAccountUri != null) {
    100             getLoaderManager().initLoader(ACCOUNT_LOADER, Bundle.EMPTY, mAccountLoadCallbacks);
    101         }
    102     }
    103 
    104 
    105     @Override
    106     public boolean onCreateOptionsMenu(Menu menu) {
    107         if (mAccountUri == null) {
    108             return false;
    109         }
    110 
    111         getMenuInflater().inflate(R.menu.eml_viewer_menu, menu);
    112         mHelpItem = menu.findItem(R.id.help_info_menu_item);
    113         mSendFeedbackItem = menu.findItem(R.id.feedback_menu_item);
    114         return true;
    115     }
    116 
    117     @Override
    118     public boolean onPrepareOptionsMenu(Menu menu) {
    119         if (mHelpItem != null) {
    120             mHelpItem.setVisible(mAccount != null
    121                     && mAccount.supportsCapability(UIProvider.AccountCapabilities.HELP_CONTENT));
    122         }
    123         if (mSendFeedbackItem != null) {
    124             mSendFeedbackItem.setVisible(mAccount != null
    125                     && mAccount.supportsCapability(UIProvider.AccountCapabilities.SEND_FEEDBACK));
    126         }
    127 
    128         return true;
    129     }
    130 
    131     @Override
    132     public boolean onOptionsItemSelected(MenuItem item) {
    133         final int itemId = item.getItemId();
    134         if (itemId == android.R.id.home) {
    135             finish();
    136             return true;
    137         } else if (itemId == R.id.settings) {
    138             Utils.showSettings(this, mAccount);
    139         } else if (itemId == R.id.help_info_menu_item) {
    140             Utils.showHelp(this, mAccount, getString(R.string.main_help_context));
    141         } else if (itemId == R.id.feedback_menu_item) {
    142             Utils.sendFeedback(this, mAccount, false);
    143         } else {
    144             return super.onOptionsItemSelected(item);
    145         }
    146 
    147         return true;
    148     }
    149 
    150     @Override
    151     public Context getActivityContext() {
    152         return this;
    153     }
    154 
    155     @Override
    156     public Account getAccount() {
    157         return mAccount;
    158     }
    159 
    160     private class AccountLoadCallbacks
    161             implements LoaderManager.LoaderCallbacks<ObjectCursor<Account>> {
    162 
    163         @Override
    164         public Loader<ObjectCursor<Account>> onCreateLoader(int id, Bundle args) {
    165             final String[] projection = UIProvider.ACCOUNTS_PROJECTION;
    166             final CursorCreator<Account> factory = Account.FACTORY;
    167             return new ObjectCursorLoader<Account>(
    168                     EmlViewerActivity.this, mAccountUri, projection, factory);
    169         }
    170 
    171         @Override
    172         public void onLoadFinished(Loader<ObjectCursor<Account>> loader,
    173                 ObjectCursor<Account> data) {
    174             if (data != null && data.moveToFirst()) {
    175                 mAccount = data.getModel();
    176             }
    177         }
    178 
    179         @Override
    180         public void onLoaderReset(Loader<ObjectCursor<Account>> loader) {
    181         }
    182     }
    183 }
    184