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.browser.widget;
     18 
     19 import android.app.ListActivity;
     20 import android.app.LoaderManager.LoaderCallbacks;
     21 import android.appwidget.AppWidgetManager;
     22 import android.content.Context;
     23 import android.content.CursorLoader;
     24 import android.content.Intent;
     25 import android.content.Loader;
     26 import android.database.Cursor;
     27 import android.os.Bundle;
     28 import android.provider.BrowserContract.Accounts;
     29 import android.view.View;
     30 import android.view.View.OnClickListener;
     31 import android.widget.ArrayAdapter;
     32 import android.widget.ListView;
     33 
     34 import com.android.browser.AddBookmarkPage.BookmarkAccount;
     35 import com.android.browser.R;
     36 import com.android.browser.provider.BrowserProvider2;
     37 
     38 public class BookmarkWidgetConfigure extends ListActivity
     39         implements OnClickListener, LoaderCallbacks<Cursor> {
     40 
     41     static final int LOADER_ACCOUNTS = 1;
     42 
     43     private ArrayAdapter<BookmarkAccount> mAccountAdapter;
     44     private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
     45 
     46     @Override
     47     protected void onCreate(Bundle savedInstanceState) {
     48         super.onCreate(savedInstanceState);
     49         setResult(RESULT_CANCELED);
     50         setVisible(false);
     51         setContentView(R.layout.widget_account_selection);
     52         findViewById(R.id.cancel).setOnClickListener(this);
     53         mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
     54                 android.R.layout.simple_list_item_1);
     55         setListAdapter(mAccountAdapter);
     56         Intent intent = getIntent();
     57         Bundle extras = intent.getExtras();
     58         if (extras != null) {
     59             mAppWidgetId = extras.getInt(
     60                     AppWidgetManager.EXTRA_APPWIDGET_ID,
     61                     AppWidgetManager.INVALID_APPWIDGET_ID);
     62         }
     63         if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
     64             finish();
     65         } else {
     66             getLoaderManager().initLoader(LOADER_ACCOUNTS, null, this);
     67         }
     68     }
     69 
     70     @Override
     71     public void onClick(View v) {
     72         finish();
     73     }
     74 
     75     @Override
     76     protected void onListItemClick(ListView l, View v, int position, long id) {
     77         BookmarkAccount account = mAccountAdapter.getItem(position);
     78         pickAccount(account.rootFolderId);
     79     }
     80 
     81     @Override
     82     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
     83         return new AccountsLoader(this);
     84     }
     85 
     86     void pickAccount(long rootId) {
     87         BookmarkThumbnailWidgetService.setupWidgetState(this, mAppWidgetId, rootId);
     88         Intent result = new Intent();
     89         result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
     90         setResult(RESULT_OK, result);
     91         finish();
     92     }
     93 
     94     @Override
     95     public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
     96         if (cursor == null || cursor.getCount() < 1) {
     97             // We always have the local account, so fall back to that
     98             pickAccount(BrowserProvider2.FIXED_ID_ROOT);
     99         } else if (cursor.getCount() == 1) {
    100             cursor.moveToFirst();
    101             pickAccount(cursor.getLong(AccountsLoader.COLUMN_INDEX_ROOT_ID));
    102         } else {
    103             mAccountAdapter.clear();
    104             while (cursor.moveToNext()) {
    105                 mAccountAdapter.add(new BookmarkAccount(this, cursor));
    106             }
    107             setVisible(true);
    108         }
    109         getLoaderManager().destroyLoader(LOADER_ACCOUNTS);
    110     }
    111 
    112     @Override
    113     public void onLoaderReset(Loader<Cursor> loader) {
    114         // Don't care
    115     }
    116 
    117     static class AccountsLoader extends CursorLoader {
    118 
    119         static final String[] PROJECTION = new String[] {
    120             Accounts.ACCOUNT_NAME,
    121             Accounts.ACCOUNT_TYPE,
    122             Accounts.ROOT_ID,
    123         };
    124 
    125         static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
    126         static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
    127         static final int COLUMN_INDEX_ROOT_ID = 2;
    128 
    129         public AccountsLoader(Context context) {
    130             super(context, Accounts.CONTENT_URI
    131                     .buildUpon()
    132                     .appendQueryParameter(BrowserProvider2.PARAM_ALLOW_EMPTY_ACCOUNTS, "false")
    133                     .build(), PROJECTION, null, null, null);
    134         }
    135 
    136     }
    137 
    138 }
    139