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