ShortcutPicker.java revision 2f5ee8e2d1e106340eb2a21b1ada6db1987bd98f
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
17package com.android.email.activity;
18
19import com.android.email.R;
20import com.android.email.activity.ShortcutPickerFragment.AccountShortcutPickerFragment;
21import com.android.email.activity.ShortcutPickerFragment.PickerCallback;
22import com.android.emailcommon.Logging;
23import com.android.emailcommon.provider.EmailContent.Account;
24import com.android.emailcommon.provider.EmailContent.Message;
25
26import android.app.Activity;
27import android.content.Intent;
28import android.os.Bundle;
29import android.os.Parcelable;
30import android.util.Log;
31import android.view.View;
32import android.view.View.OnClickListener;
33
34/**
35 * This class implements a launcher shortcut for directly accessing a single account.
36 */
37public class ShortcutPicker extends Activity implements OnClickListener, PickerCallback {
38    /**
39     * If true, creates pre-honeycomb style shortcuts. This allows developers to test launching
40     * the app from old style shortcuts (which point sat MessageList rather than Welcome) without
41     * actually carrying over shortcuts from previous versions.
42     */
43    private final static boolean TEST_CREATE_OLD_STYLE_SHORTCUT = false; // DO NOT SUBMIT WITH TRUE
44
45    @Override
46    public void onCreate(Bundle icicle) {
47        super.onCreate(icicle);
48
49        // TODO Relax this test slightly in order to re-use this activity for widget creation
50        if (!Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
51            // finish() immediately if we aren't supposed to be here
52            finish();
53            return;
54        }
55
56        // Set handler for the "cancel" button
57        setContentView(R.layout.account_shortcut_picker);
58        findViewById(R.id.cancel).setOnClickListener(this);
59
60        if (getFragmentManager().findFragmentById(R.id.shortcut_list) == null) {
61            // Load the account picking fragment if we haven't created a fragment yet
62            // NOTE: do not add to history as this will be the first fragment in the flow
63            AccountShortcutPickerFragment fragment = new AccountShortcutPickerFragment();
64            getFragmentManager().beginTransaction().add(R.id.shortcut_list, fragment).commit();
65        }
66    }
67
68    @Override
69    public void onClick(View v) {
70        switch (v.getId()) {
71            case R.id.cancel:
72                finish();
73                break;
74        }
75    }
76
77
78    @Override
79    public void onSelected(Account account, long mailboxId) {
80        setupShortcut(account, mailboxId);
81        finish();
82    }
83
84    @Override
85    public void onMissingData(boolean missingAccount, boolean missingMailbox) {
86        // TODO what's the proper handling if the mailbox list is '0'? display toast?
87        finish();
88    }
89
90    /**
91     * This function creates a shortcut and returns it to the caller.  There are actually two
92     * intents that you will send back.
93     *
94     * The first intent serves as a container for the shortcut and is returned to the launcher by
95     * setResult().  This intent must contain three fields:
96     *
97     * <ul>
98     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
99     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
100     * the shortcut.</li>
101     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
102     * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
103     * a drawable resource.</li>
104     * </ul>
105     *
106     * If you use a simple drawable resource, note that you must wrapper it using
107     * {@link android.content.Intent.ShortcutIconResource}, as shown below.  This is required so
108     * that the launcher can access resources that are stored in your application's .apk file.  If
109     * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras
110     * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
111     *
112     * The shortcut intent can be any intent that you wish the launcher to send, when the user
113     * clicks on the shortcut.  Typically this will be {@link android.content.Intent#ACTION_VIEW}
114     * with an appropriate Uri for your content, but any Intent will work here as long as it
115     * triggers the desired action within your Activity.
116     */
117    private void setupShortcut(Account account, long mailboxId) {
118        Activity myActivity = this;
119        // First, set up the shortcut intent.
120        final Intent shortcutIntent;
121        if (TEST_CREATE_OLD_STYLE_SHORTCUT) {
122            shortcutIntent = MessageList.createFroyoIntent(myActivity, account);
123            Log.d(Logging.LOG_TAG, "Created old style intent: " + shortcutIntent);
124        } else {
125            // TODO if we add meta-mailboxes/accounts to the database, remove this special case
126            if (account.mId == Account.ACCOUNT_ID_COMBINED_VIEW) {
127                shortcutIntent = Welcome.createOpenMessageIntent(
128                        myActivity, account.mId, mailboxId, Message.NO_MESSAGE);
129            } else {
130                String uuid = account.mCompatibilityUuid;
131                shortcutIntent = Welcome.createAccountShortcutIntent(myActivity, uuid, mailboxId);
132            }
133        }
134
135        // Then, set up the container intent (the response to the caller)
136        Intent intent = new Intent();
137        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
138        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, account.getDisplayName());
139        Parcelable iconResource
140                = Intent.ShortcutIconResource.fromContext(myActivity, R.mipmap.ic_launcher_email);
141        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
142
143        // Now, return the result to the launcher
144        myActivity.setResult(Activity.RESULT_OK, intent);
145    }
146}
147