Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.email.activity;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 import android.os.Parcelable;
     23 import android.util.Log;
     24 import android.view.View;
     25 import android.view.View.OnClickListener;
     26 
     27 import com.android.email.R;
     28 import com.android.email.activity.ShortcutPickerFragment.AccountShortcutPickerFragment;
     29 import com.android.email.activity.ShortcutPickerFragment.MailboxShortcutPickerFragment;
     30 import com.android.email.activity.ShortcutPickerFragment.PickerCallback;
     31 import com.android.emailcommon.Logging;
     32 import com.android.emailcommon.provider.Account;
     33 import com.android.emailcommon.provider.EmailContent.Message;
     34 import com.android.emailcommon.provider.Mailbox;
     35 
     36 /**
     37  * This class implements a launcher shortcut for directly accessing a single account.
     38  */
     39 public class ShortcutPicker extends Activity implements OnClickListener, PickerCallback {
     40     /**
     41      * If true, creates pre-honeycomb style shortcuts. This allows developers to test launching
     42      * the app from old style shortcuts (which point sat MessageList rather than Welcome) without
     43      * actually carrying over shortcuts from previous versions.
     44      */
     45     private final static boolean TEST_CREATE_OLD_STYLE_SHORTCUT = false; // DO NOT SUBMIT WITH TRUE
     46 
     47     @Override
     48     public void onCreate(Bundle icicle) {
     49         super.onCreate(icicle);
     50 
     51         // TODO Relax this test slightly in order to re-use this activity for widget creation
     52         if (!Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
     53             // finish() immediately if we aren't supposed to be here
     54             finish();
     55             return;
     56         }
     57 
     58         // Set handler for the "cancel" button
     59         setContentView(R.layout.account_shortcut_picker);
     60         findViewById(R.id.cancel).setOnClickListener(this);
     61 
     62         if (getFragmentManager().findFragmentById(R.id.shortcut_list) == null) {
     63             // Load the account picking fragment if we haven't created a fragment yet
     64             // NOTE: do not add to history as this will be the first fragment in the flow
     65             AccountShortcutPickerFragment fragment = new AccountShortcutPickerFragment();
     66             getFragmentManager().beginTransaction().add(R.id.shortcut_list, fragment).commit();
     67         }
     68     }
     69 
     70     @Override
     71     public void onClick(View v) {
     72         switch (v.getId()) {
     73             case R.id.cancel:
     74                 finish();
     75                 break;
     76         }
     77     }
     78 
     79     @Override
     80     public Integer buildFilter(Account account) {
     81         if (!Account.isNormalAccount(account.mId)) {
     82             // Shortcuts for combined accounts can only be for inboxes.
     83             return MailboxShortcutPickerFragment.FILTER_INBOX_ONLY;
     84         }
     85 
     86         return MailboxShortcutPickerFragment.FILTER_ALLOW_ALL;
     87     }
     88 
     89     @Override
     90     public void onSelected(Account account, long mailboxId) {
     91         String shortcutName;
     92         if (Account.isNormalAccount(account.mId) &&
     93                 (Mailbox.getMailboxType(this, mailboxId) != Mailbox.TYPE_INBOX)) {
     94             shortcutName = Mailbox.getDisplayName(this, mailboxId);
     95         } else {
     96             shortcutName = account.getDisplayName();
     97         }
     98         setupShortcut(account, mailboxId, shortcutName);
     99         finish();
    100     }
    101 
    102     @Override
    103     public void onMissingData(boolean missingAccount, boolean missingMailbox) {
    104         // TODO what's the proper handling if the mailbox list is '0'? display toast?
    105         finish();
    106     }
    107 
    108     /**
    109      * This function creates a shortcut and returns it to the caller.  There are actually two
    110      * intents that you will send back.
    111      *
    112      * The first intent serves as a container for the shortcut and is returned to the launcher by
    113      * setResult().  This intent must contain three fields:
    114      *
    115      * <ul>
    116      * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
    117      * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
    118      * the shortcut.</li>
    119      * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
    120      * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
    121      * a drawable resource.</li>
    122      * </ul>
    123      *
    124      * If you use a simple drawable resource, note that you must wrapper it using
    125      * {@link android.content.Intent.ShortcutIconResource}, as shown below.  This is required so
    126      * that the launcher can access resources that are stored in your application's .apk file.  If
    127      * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras
    128      * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
    129      *
    130      * The shortcut intent can be any intent that you wish the launcher to send, when the user
    131      * clicks on the shortcut.  Typically this will be {@link android.content.Intent#ACTION_VIEW}
    132      * with an appropriate Uri for your content, but any Intent will work here as long as it
    133      * triggers the desired action within your Activity.
    134      */
    135     private void setupShortcut(Account account, long mailboxId, String shortcutName) {
    136         Activity myActivity = this;
    137         // First, set up the shortcut intent.
    138         final Intent shortcutIntent;
    139         if (TEST_CREATE_OLD_STYLE_SHORTCUT) {
    140             shortcutIntent = MessageList.createFroyoIntent(myActivity, account);
    141             Log.d(Logging.LOG_TAG, "Created old style intent: " + shortcutIntent);
    142         } else {
    143             // TODO if we add meta-mailboxes/accounts to the database, remove this special case
    144             if (account.mId == Account.ACCOUNT_ID_COMBINED_VIEW) {
    145                 shortcutIntent = Welcome.createOpenMessageIntent(
    146                         myActivity, account.mId, mailboxId, Message.NO_MESSAGE);
    147             } else {
    148                 String uuid = account.mCompatibilityUuid;
    149                 shortcutIntent = Welcome.createAccountShortcutIntent(myActivity, uuid, mailboxId);
    150             }
    151         }
    152 
    153         // Then, set up the container intent (the response to the caller)
    154         Intent intent = new Intent();
    155         intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    156         intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
    157         Parcelable iconResource
    158                 = Intent.ShortcutIconResource.fromContext(myActivity, R.mipmap.ic_launcher_email);
    159         intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
    160 
    161         // Now, return the result to the launcher
    162         myActivity.setResult(Activity.RESULT_OK, intent);
    163     }
    164 }
    165