Home | History | Annotate | Download | only in widget
      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 
     17 package com.android.email.widget;
     18 
     19 import android.app.Activity;
     20 import android.appwidget.AppWidgetManager;
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.util.Log;
     24 import android.view.View;
     25 import android.view.View.OnClickListener;
     26 
     27 import com.android.email.Email;
     28 import com.android.email.R;
     29 import com.android.email.activity.ShortcutPickerFragment.AccountShortcutPickerFragment;
     30 import com.android.email.activity.ShortcutPickerFragment.MailboxShortcutPickerFragment;
     31 import com.android.email.activity.ShortcutPickerFragment.PickerCallback;
     32 import com.android.emailcommon.Logging;
     33 import com.android.emailcommon.provider.Account;
     34 import com.android.emailcommon.provider.HostAuth;
     35 import com.android.emailcommon.utility.Utility;
     36 
     37 /**
     38  * Activity to configure the Email widget.
     39  */
     40 public class WidgetConfiguration extends Activity implements OnClickListener, PickerCallback {
     41     /** ID of the newly created application widget */
     42     private int mAppWidgetId;
     43 
     44     @Override
     45     protected void onCreate(Bundle savedInstanceState) {
     46         super.onCreate(savedInstanceState);
     47         setResult(RESULT_CANCELED);
     48         if (Email.DEBUG) {
     49             Log.i(Logging.LOG_TAG, "WidgetConfiguration initiated");
     50         }
     51         if (!AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(getIntent().getAction())) {
     52             // finish() immediately if we aren't supposed to be here
     53             finish();
     54             return;
     55         }
     56 
     57         Intent intent = getIntent();
     58         Bundle extras = intent.getExtras();
     59         if (extras != null) {
     60             mAppWidgetId = extras.getInt(
     61                     AppWidgetManager.EXTRA_APPWIDGET_ID,
     62                     AppWidgetManager.INVALID_APPWIDGET_ID);
     63         }
     64 
     65         // Set handler for the "cancel" button
     66         setContentView(R.layout.account_shortcut_picker);
     67         findViewById(R.id.cancel).setOnClickListener(this);
     68 
     69         if (getFragmentManager().findFragmentById(R.id.shortcut_list) == null) {
     70             // Load the account picking fragment if we haven't created a fragment yet
     71             // NOTE: do not add to history as this will be the first fragment in the flow
     72             AccountShortcutPickerFragment fragment = new AccountShortcutPickerFragment();
     73             getFragmentManager().beginTransaction().add(R.id.shortcut_list, fragment).commit();
     74         }
     75     }
     76 
     77     @Override
     78     public void onClick(View v) {
     79         switch (v.getId()) {
     80             case R.id.cancel:
     81                 finish();
     82                 break;
     83         }
     84     }
     85 
     86     @Override
     87     public Integer buildFilter(Account account) {
     88         if (!Account.isNormalAccount(account.mId)) {
     89             return MailboxShortcutPickerFragment.FILTER_INBOX_ONLY
     90                     | MailboxShortcutPickerFragment.FILTER_ALLOW_UNREAD;
     91         }
     92 
     93         // We can't synced non-Inbox mailboxes for non-EAS accounts, so they don't sync
     94         // right now and it doesn't make sense to put them in a widget.
     95         return HostAuth.SCHEME_EAS.equals(account.getProtocol(this))
     96                 ? MailboxShortcutPickerFragment.FILTER_ALLOW_ALL
     97                 : MailboxShortcutPickerFragment.FILTER_INBOX_ONLY;
     98     }
     99 
    100     @Override
    101     public void onSelected(Account account, long mailboxId) {
    102         setupWidget(account, mailboxId);
    103         finish();
    104     }
    105 
    106     @Override
    107     public void onMissingData(boolean missingAccount, boolean missingMailbox) {
    108         if (Email.DEBUG) {
    109             Log.i(Logging.LOG_TAG, "WidgetConfiguration exited abnormally. Probably no accounts.");
    110         }
    111         Utility.showToast(this, R.string.widget_no_accounts);
    112         finish();
    113     }
    114 
    115     private void setupWidget(Account account, long mailboxId) {
    116         // save user selected preferences & create initial widget view
    117         WidgetManager.saveWidgetPrefs(this, mAppWidgetId, account.mId, mailboxId);
    118         WidgetManager.getInstance().getOrCreateWidget(this, mAppWidgetId).start();
    119 
    120         // Return "OK" result; make sure we pass along the original widget ID
    121         Intent resultValue = new Intent();
    122         resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    123         setResult(RESULT_OK, resultValue);
    124     }
    125 }
    126