WidgetConfiguration.java revision e10215eaff7c06b44b95de87ea46030065ecbee5
1/*
2 * Copyright (C) 2011 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.widget;
18
19import android.app.Activity;
20import android.appwidget.AppWidgetManager;
21import android.content.Intent;
22import android.os.Bundle;
23import android.view.View;
24import android.view.View.OnClickListener;
25
26import com.android.email.R;
27import com.android.email.activity.ShortcutPickerFragment.AccountShortcutPickerFragment;
28import com.android.email.activity.ShortcutPickerFragment.MailboxShortcutPickerFragment;
29import com.android.email.activity.ShortcutPickerFragment.PickerCallback;
30import com.android.emailcommon.provider.Account;
31import com.android.emailcommon.provider.HostAuth;
32
33/**
34 * Activity to configure the Email widget.
35 */
36public class WidgetConfiguration extends Activity implements OnClickListener, PickerCallback {
37    /** ID of the newly created application widget */
38    private int mAppWidgetId;
39
40    @Override
41    protected void onCreate(Bundle savedInstanceState) {
42        super.onCreate(savedInstanceState);
43        setResult(RESULT_CANCELED);
44        if (!AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(getIntent().getAction())) {
45            // finish() immediately if we aren't supposed to be here
46            finish();
47            return;
48        }
49
50        Intent intent = getIntent();
51        Bundle extras = intent.getExtras();
52        if (extras != null) {
53            mAppWidgetId = extras.getInt(
54                    AppWidgetManager.EXTRA_APPWIDGET_ID,
55                    AppWidgetManager.INVALID_APPWIDGET_ID);
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            return MailboxShortcutPickerFragment.FILTER_INBOX_ONLY
83                    | MailboxShortcutPickerFragment.FILTER_ALLOW_UNREAD;
84        }
85
86        // We can't synced non-Inbox mailboxes for non-EAS accounts, so they don't sync
87        // right now and it doesn't make sense to put them in a widget.
88        return HostAuth.SCHEME_EAS.equals(account.getProtocol(this))
89                ? MailboxShortcutPickerFragment.FILTER_ALLOW_ALL
90                : MailboxShortcutPickerFragment.FILTER_INBOX_ONLY;
91    }
92
93    @Override
94    public void onSelected(Account account, long mailboxId) {
95        setupWidget(account, mailboxId);
96        finish();
97    }
98
99    @Override
100    public void onMissingData(boolean missingAccount, boolean missingMailbox) {
101        // TODO what's the proper handling if the mailbox list is '0'? display toast?
102        finish();
103    }
104
105    private void setupWidget(Account account, long mailboxId) {
106        // save user selected preferences & create initial widget view
107        WidgetManager.saveWidgetPrefs(this, mAppWidgetId, account.mId, mailboxId);
108        WidgetManager.getInstance().getOrCreateWidget(this, mAppWidgetId).start();
109
110        // Return "OK" result; make sure we pass along the original widget ID
111        Intent resultValue = new Intent();
112        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
113        setResult(RESULT_OK, resultValue);
114    }
115}
116