WidgetConfiguration.java revision f5418f1f93b02e7fab9f15eb201800b65510998e
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;
28import com.android.email.activity.ShortcutPickerFragment.AccountShortcutPickerFragment;
29import com.android.email.activity.ShortcutPickerFragment.PickerCallback;
30import com.android.emailcommon.provider.Account;
31
32/**
33 * Activity to configure the Email widget.
34 */
35public class WidgetConfiguration extends Activity implements OnClickListener, PickerCallback {
36    /** ID of the newly created application widget */
37    private int mAppWidgetId;
38
39    @Override
40    protected void onCreate(Bundle savedInstanceState) {
41        super.onCreate(savedInstanceState);
42        setResult(RESULT_CANCELED);
43        if (!AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(getIntent().getAction())) {
44            // finish() immediately if we aren't supposed to be here
45            finish();
46            return;
47        }
48
49        Intent intent = getIntent();
50        Bundle extras = intent.getExtras();
51        if (extras != null) {
52            mAppWidgetId = extras.getInt(
53                    AppWidgetManager.EXTRA_APPWIDGET_ID,
54                    AppWidgetManager.INVALID_APPWIDGET_ID);
55        }
56
57        // Set handler for the "cancel" button
58        setContentView(R.layout.account_shortcut_picker);
59        findViewById(R.id.cancel).setOnClickListener(this);
60
61        if (getFragmentManager().findFragmentById(R.id.shortcut_list) == null) {
62            // Load the account picking fragment if we haven't created a fragment yet
63            // NOTE: do not add to history as this will be the first fragment in the flow
64            AccountShortcutPickerFragment fragment = new AccountShortcutPickerFragment();
65            Bundle args = new Bundle();
66            args.putInt(ShortcutPickerFragment.ARG_FILTER,
67                ShortcutPickerFragment.FILTER_INBOX_ONLY
68                | ShortcutPickerFragment.FILTER_ALLOW_UNREAD);
69            fragment.setArguments(args);
70            getFragmentManager().beginTransaction().add(R.id.shortcut_list, fragment).commit();
71        }
72    }
73
74    @Override
75    public void onClick(View v) {
76        switch (v.getId()) {
77            case R.id.cancel:
78                finish();
79                break;
80        }
81    }
82
83    @Override
84    public void onSelected(Account account, long mailboxId) {
85        setupWidget(account, mailboxId);
86        finish();
87    }
88
89    @Override
90    public void onMissingData(boolean missingAccount, boolean missingMailbox) {
91        // TODO what's the proper handling if the mailbox list is '0'? display toast?
92        finish();
93    }
94
95    private void setupWidget(Account account, long mailboxId) {
96        // save user selected preferences & create initial widget view
97        WidgetManager.saveWidgetPrefs(this, mAppWidgetId, account.mId, mailboxId);
98        WidgetManager.getInstance().getOrCreateWidget(this, mAppWidgetId).start();
99
100        // Return "OK" result; make sure we pass along the original widget ID
101        Intent resultValue = new Intent();
102        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
103        setResult(RESULT_OK, resultValue);
104    }
105}
106