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.provider.EmailContent;
21import com.android.email.provider.EmailContent.Account;
22import com.android.email.provider.EmailContent.Mailbox;
23
24import android.app.ListActivity;
25import android.content.Context;
26import android.content.Intent;
27import android.database.Cursor;
28import android.os.Bundle;
29import android.os.Parcelable;
30import android.view.View;
31import android.widget.AdapterView;
32import android.widget.ListView;
33import android.widget.SimpleCursorAdapter;
34import android.widget.TextView;
35import android.widget.AdapterView.OnItemClickListener;
36
37/**
38 *
39 * This class implements a launcher shortcut for directly accessing a single account.
40 *
41 * This is simply a lightweight version of Accounts, and should almost certainly be merged with it
42 * (or, one could be a base class of the other).
43 */
44public class AccountShortcutPicker extends ListActivity implements OnItemClickListener {
45
46    /**
47     * Support for list adapter
48     */
49    private final static String[] sFromColumns = new String[] {
50            EmailContent.AccountColumns.DISPLAY_NAME,
51            EmailContent.AccountColumns.EMAIL_ADDRESS,
52            EmailContent.RECORD_ID
53    };
54    private final int[] sToIds = new int[] {
55            R.id.description,
56            R.id.email,
57            R.id.new_message_count
58    };
59
60    @Override
61    public void onCreate(Bundle icicle) {
62        super.onCreate(icicle);
63
64        // finish() immediately if we aren't supposed to be here
65        if (!Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
66            finish();
67            return;
68        }
69
70        // finish() immediately if no accounts are configured
71        // TODO: lightweight projection with only those columns needed for this display
72        // TODO: query outside of UI thread
73        Cursor c = this.managedQuery(
74                EmailContent.Account.CONTENT_URI,
75                EmailContent.Account.CONTENT_PROJECTION,
76                null, null, null);
77        if (c.getCount() == 0) {
78            finish();
79            return;
80        }
81
82        setContentView(R.layout.accounts);
83        ListView listView = getListView();
84        listView.setOnItemClickListener(this);
85        listView.setItemsCanFocus(false);
86        listView.setEmptyView(findViewById(R.id.empty));
87
88        AccountsAdapter a = new AccountsAdapter(this,
89                R.layout.accounts_item, c, sFromColumns, sToIds);
90        listView.setAdapter(a);
91    }
92
93    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
94        Cursor cursor = (Cursor)parent.getItemAtPosition(position);
95        Account account = new Account().restore(cursor);
96        setupShortcut(account);
97        finish();
98    }
99
100    private static class AccountsAdapter extends SimpleCursorAdapter {
101
102        public AccountsAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
103            super(context, layout, c, from, to);
104            setViewBinder(new MyViewBinder());
105        }
106
107        /**
108         * This is only used for the unread messages count.  Most of the views are handled
109         * normally by SimpleCursorAdapter.
110         */
111        private static class MyViewBinder implements SimpleCursorAdapter.ViewBinder {
112
113            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
114                if (view.getId() == R.id.new_message_count) {
115
116                    int unreadMessageCount = 0;     // TODO get unread count from Account
117                    if (unreadMessageCount <= 0) {
118                        view.setVisibility(View.GONE);
119                    } else {
120                        ((TextView)view).setText(String.valueOf(unreadMessageCount));
121                    }
122                    return true;
123                }
124
125                return false;
126            }
127        }
128    }
129
130    /**
131     * This function creates a shortcut and returns it to the caller.  There are actually two
132     * intents that you will send back.
133     *
134     * The first intent serves as a container for the shortcut and is returned to the launcher by
135     * setResult().  This intent must contain three fields:
136     *
137     * <ul>
138     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
139     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
140     * the shortcut.</li>
141     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
142     * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
143     * a drawable resource.</li>
144     * </ul>
145     *
146     * If you use a simple drawable resource, note that you must wrapper it using
147     * {@link android.content.Intent.ShortcutIconResource}, as shown below.  This is required so
148     * that the launcher can access resources that are stored in your application's .apk file.  If
149     * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras
150     * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
151     *
152     * The shortcut intent can be any intent that you wish the launcher to send, when the user
153     * clicks on the shortcut.  Typically this will be {@link android.content.Intent#ACTION_VIEW}
154     * with an appropriate Uri for your content, but any Intent will work here as long as it
155     * triggers the desired action within your Activity.
156     */
157    private void setupShortcut(Account account) {
158        // First, set up the shortcut intent.
159
160        Intent shortcutIntent = MessageList.createAccountIntentForShortcut(
161                this, account, Mailbox.TYPE_INBOX);
162
163        // Then, set up the container intent (the response to the caller)
164
165        Intent intent = new Intent();
166        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
167        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, account.getDisplayName());
168        Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
169        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
170
171        // Now, return the result to the launcher
172
173        setResult(RESULT_OK, intent);
174    }
175
176
177}
178
179
180