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