ShortcutCursor.java revision b83882b9efa37ec0f20a0f1c85cf5ccc93194aee
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 */
30class 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, null, null, null);
66    }
67
68    public ShortcutCursor(SuggestionCursor suggestions, Handler uiThread,
69            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                add(new SuggestionPosition(suggestions));
77            } else {
78                if (DBG) Log.d(TAG, "Skipping shortcut " + i);
79            }
80        }
81    }
82
83    /**
84     * Refresh a shortcut from this cursor.
85     *
86     * @param shortcut The shortcut to refresh. Should be a shortcut taken from this cursor.
87     */
88    public void refresh(Suggestion shortcut) {
89        mRefresher.refresh(shortcut, new ShortcutRefresher.Listener() {
90            public void onShortcutRefreshed(final Source source,
91                    final String shortcutId, final SuggestionCursor refreshed) {
92                if (DBG) Log.d(TAG, "Shortcut refreshed: " + shortcutId);
93                mShortcutRepo.updateShortcut(source, shortcutId, refreshed);
94                mUiThread.post(new Runnable() {
95                    public void run() {
96                        refresh(source, shortcutId, refreshed);
97                    }
98                });
99            }
100        });
101    }
102
103    /**
104     * Updates this SuggestionCursor with a refreshed result from another.
105     * Since this modifies the cursor, it should be called on the UI thread.
106     * This class assumes responsibility for closing refreshed.
107     */
108    private void refresh(Source source, String shortcutId, SuggestionCursor refreshed) {
109        if (DBG) Log.d(TAG, "refresh " + shortcutId);
110        if (mClosed) {
111            if (refreshed != null) {
112                refreshed.close();
113            }
114            return;
115        }
116        if (refreshed != null) {
117            mRefreshed.add(refreshed);
118        }
119        for (int i = 0; i < getCount(); i++) {
120            moveTo(i);
121            if (shortcutId.equals(getShortcutId()) && source.equals(getSuggestionSource())) {
122                if (refreshed != null && refreshed.getCount() > 0) {
123                    if (DBG) Log.d(TAG, "replacing row " + i);
124                    replaceRow(new SuggestionPosition(refreshed));
125                } else {
126                    if (DBG) Log.d(TAG, "removing row " + i);
127                    removeRow();
128                }
129                notifyDataSetChanged();
130                break;
131            }
132        }
133    }
134
135    @Override
136    public void close() {
137        if (DBG) Log.d(TAG, "close()");
138        if (mClosed) {
139            throw new IllegalStateException("double close");
140        }
141        mClosed = true;
142        if (mShortcuts != null) {
143            mShortcuts.close();
144        }
145        for (SuggestionCursor cursor : mRefreshed) {
146            cursor.close();
147        }
148        super.close();
149    }
150}