summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/widget/BookmarkWidgetConfigure.java
blob: 4231f37e864021da0d87b2c7baaab7d626213488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.browser.widget;

import android.app.ListActivity;
import android.app.LoaderManager.LoaderCallbacks;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.browser.AddBookmarkPage.AccountsLoader;
import com.android.browser.AddBookmarkPage.BookmarkAccount;
import com.android.browser.R;
import com.android.browser.provider.BrowserProvider2;

public class BookmarkWidgetConfigure extends ListActivity
        implements OnClickListener, LoaderCallbacks<Cursor> {

    static final int LOADER_ACCOUNTS = 1;

    private ArrayAdapter<BookmarkAccount> mAccountAdapter;
    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setResult(RESULT_CANCELED);
        setVisible(false);
        setContentView(R.layout.widget_account_selection);
        findViewById(R.id.cancel).setOnClickListener(this);
        mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
                android.R.layout.simple_list_item_1);
        setListAdapter(mAccountAdapter);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        } else {
            getLoaderManager().initLoader(LOADER_ACCOUNTS, null, this);
        }
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        BookmarkAccount account = mAccountAdapter.getItem(position);
        pickAccount(account.rootFolderId);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new AccountsLoader(this);
    }

    void pickAccount(long rootId) {
        BookmarkThumbnailWidgetService.setupWidgetState(this, mAppWidgetId, rootId);
        Intent result = new Intent();
        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
        setResult(RESULT_OK, result);
        finish();
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        if (cursor == null || cursor.getCount() <= 1) {
            // We always have the local account, so if count == 1 it must
            // be the local account
            pickAccount(BrowserProvider2.FIXED_ID_ROOT);
        } else {
            mAccountAdapter.clear();
            while (cursor.moveToNext()) {
                mAccountAdapter.add(new BookmarkAccount(this, cursor));
            }
            setVisible(true);
        }
        getLoaderManager().destroyLoader(LOADER_ACCOUNTS);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // Don't care
    }

}