WidgetConfiguration.java revision 7f4cf3c46b0dd1bc001c547807e83b7c280f074b
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.PickerCallback;
29import com.android.emailcommon.provider.EmailContent.Account;
30
31/**
32 * Activity to configure the Email widget.
33 */
34public class WidgetConfiguration extends Activity implements OnClickListener, PickerCallback {
35    /** ID of the newly created application widget */
36    private int mAppWidgetId;
37
38    @Override
39    protected void onCreate(Bundle savedInstanceState) {
40        super.onCreate(savedInstanceState);
41        setResult(RESULT_CANCELED);
42        if (!AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(getIntent().getAction())) {
43            // finish() immediately if we aren't supposed to be here
44            finish();
45            return;
46        }
47
48        Intent intent = getIntent();
49        Bundle extras = intent.getExtras();
50        if (extras != null) {
51            mAppWidgetId = extras.getInt(
52                    AppWidgetManager.EXTRA_APPWIDGET_ID,
53                    AppWidgetManager.INVALID_APPWIDGET_ID);
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    @Override
78    public void onSelected(Account account, long mailboxId) {
79        setupWidget(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    private void setupWidget(Account account, long mailboxId) {
90        // save user selected preferences & create initial widget view
91        WidgetManager.saveWidgetPrefs(this, mAppWidgetId, account.mId, mailboxId);
92        WidgetManager.getInstance().getOrCreateWidget(this, mAppWidgetId).start();
93
94        // Return "OK" result; make sure we pass along the original widget ID
95        Intent resultValue = new Intent();
96        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
97        setResult(RESULT_OK, resultValue);
98    }
99}
100