ShortcutCursor.java revision 8749e77dddec9e7984ee86a7be6f5ba4fce44362
1/*
2 * Copyright (C) 2010 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.quicksearchbox;
18
19import com.google.common.annotations.VisibleForTesting;
20
21import android.os.Handler;
22import android.util.Log;
23
24import java.util.HashSet;
25
26/**
27 * A SuggestionCursor that allows shortcuts to be updated by overlaying
28 * with results from another cursor.
29 */
30public class ShortcutCursor extends ListSuggestionCursor {
31
32    private static final boolean DBG = false;
33    private static final String TAG = "QSB.ShortcutCursor";
34
35    // mShortcuts is used to close the underlying cursor when we're closed.
36    private final SuggestionCursor mShortcuts;
37    // mRefreshed contains all the cursors that have been refreshed, so that
38    // they can be closed when ShortcutCursor is closed.
39    private final HashSet<SuggestionCursor> mRefreshed;
40
41    private boolean mClosed = false;
42
43    private final ShortcutRefresher mRefresher;
44    private final ShortcutRepository mShortcutRepo;
45    private final Handler mUiThread;
46
47    private ShortcutCursor(String query, SuggestionCursor shortcuts, Handler uiThread,
48            ShortcutRefresher refresher, ShortcutRepository repository) {
49        super(query);
50        mShortcuts = shortcuts;
51        mUiThread = uiThread;
52        mRefresher = refresher;
53        mShortcutRepo = repository;
54        mRefreshed = new HashSet<SuggestionCursor>();
55    }
56
57    @VisibleForTesting
58    ShortcutCursor(String query, Handler uiThread,
59            ShortcutRefresher refresher, ShortcutRepository repository) {
60        this(query, null, uiThread, refresher, repository);
61    }
62
63    @VisibleForTesting
64    ShortcutCursor(SuggestionCursor suggestions) {
65        this(suggestions, true, null, null, null);
66    }
67
68    public ShortcutCursor(SuggestionCursor suggestions, boolean allowWebSearchShortcuts,
69            Handler uiThread, ShortcutRefresher refresher, ShortcutRepository repository) {
70        this(suggestions.getUserQuery(), suggestions, uiThread, refresher, repository);
71        int count = suggestions.getCount();
72        if (DBG) Log.d(TAG, "Total shortcuts: " + count);
73        for (int i = 0; i < count; i++) {
74            suggestions.moveTo(i);
75            if (suggestions.getSuggestionSource() != null
76                    && (allowWebSearchShortcuts || !suggestions.isWebSearchSuggestion())) {
77                add(new SuggestionPosition(suggestions));
78            } else {
79                if (DBG) Log.d(TAG, "Skipping shortcut " + i);
80            }
81        }
82    }
83
84    /**
85     * Refresh a shortcut from this cursor.
86     *
87     * @param shortcut The shortcut to refresh. Should be a shortcut taken from this cursor.
88     */
89    public void refresh(Suggestion shortcut) {
90        mRefresher.refresh(shortcut, new ShortcutRefresher.Listener() {
91            public void onShortcutRefreshed(final Source source,
92                    final String shortcutId, final SuggestionCursor refreshed) {
93                if (DBG) Log.d(TAG, "Shortcut refreshed: " + shortcutId);
94                mShortcutRepo.updateShortcut(source, shortcutId, refreshed);
95                mUiThread.post(new Runnable() {
96                    public void run() {
97                        refresh(source, shortcutId, refreshed);
98                    }
99                });
100            }
101        });
102    }
103
104    /**
105     * Updates this SuggestionCursor with a refreshed result from another.
106     * Since this modifies the cursor, it should be called on the UI thread.
107     * This class assumes responsibility for closing refreshed.
108     */
109    private void refresh(Source source, String shortcutId, SuggestionCursor refreshed) {
110        if (DBG) Log.d(TAG, "refresh " + shortcutId);
111        if (mClosed) {
112            if (refreshed != null) {
113                refreshed.close();
114            }
115            return;
116        }
117        if (refreshed != null) {
118            mRefreshed.add(refreshed);
119        }
120        for (int i = 0; i < getCount(); i++) {
121            moveTo(i);
122            if (shortcutId.equals(getShortcutId()) && source.equals(getSuggestionSource())) {
123                if (refreshed != null && refreshed.getCount() > 0) {
124                    if (DBG) Log.d(TAG, "replacing row " + i);
125                    replaceRow(new SuggestionPosition(refreshed));
126                } else {
127                    if (DBG) Log.d(TAG, "removing row " + i);
128                    removeRow();
129                }
130                notifyDataSetChanged();
131                break;
132            }
133        }
134    }
135
136    @Override
137    public void close() {
138        if (DBG) Log.d(TAG, "close()");
139        if (mClosed) {
140            throw new IllegalStateException("double close");
141        }
142        mClosed = true;
143        if (mShortcuts != null) {
144            mShortcuts.close();
145        }
146        for (SuggestionCursor cursor : mRefreshed) {
147            cursor.close();
148        }
149        super.close();
150    }
151}