ShortcutCursor.java revision b5fc08b7f16a32d3865f44b7f26d8aaa5304a2ad
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 android.util.Log; 20 21import java.util.HashSet; 22 23/** 24 * A SuggestionCursor that allows shortcuts to be updated by overlaying 25 * with results from another cursor. 26 */ 27class ShortcutCursor extends ListSuggestionCursor { 28 29 private static final boolean DBG = false; 30 private static final String TAG = "QSB.ShortcutCursor"; 31 32 // mShortcuts is used to close the underlying cursor when we're closed. 33 private final CursorBackedSuggestionCursor mShortcuts; 34 // mRefreshed contains all the cursors that have been refreshed, so that 35 // they can be closed when ShortcutCursor is closed. 36 private final HashSet<SuggestionCursor> mRefreshed; 37 38 private boolean mClosed; 39 40 public ShortcutCursor(int maxShortcuts, CursorBackedSuggestionCursor shortcuts) { 41 super(shortcuts.getUserQuery()); 42 mShortcuts = shortcuts; 43 mRefreshed = new HashSet<SuggestionCursor>(); 44 int count = shortcuts.getCount(); 45 for (int i = 0; i < count; i++) { 46 if (getCount() >= maxShortcuts) break; 47 shortcuts.moveTo(i); 48 if (shortcuts.getSuggestionSource() != null) { 49 add(new SuggestionPosition(shortcuts)); 50 } else { 51 if (DBG) Log.d(TAG, "Skipping shortcut " + i); 52 } 53 } 54 } 55 56 /** 57 * Updates this SuggestionCursor with a refreshed result from another. 58 * Since this modifies the cursor, it should be called on the UI thread. 59 * This class assumes responsibility for closing refreshed. 60 */ 61 public void refresh(Source source, String shortcutId, SuggestionCursor refreshed) { 62 if (DBG) Log.d(TAG, "refresh " + shortcutId); 63 if (mClosed) { 64 if (refreshed != null) { 65 refreshed.close(); 66 } 67 return; 68 } 69 if (refreshed != null) { 70 mRefreshed.add(refreshed); 71 } 72 int count = getCount(); 73 for (int i = 0; i < count; i++) { 74 moveTo(i); 75 if (shortcutId.equals(getShortcutId()) && source.equals(getSuggestionSource())) { 76 if (refreshed != null && refreshed.getCount() > 0) { 77 replaceRow(new SuggestionPosition(refreshed)); 78 } else { 79 removeRow(); 80 } 81 notifyDataSetChanged(); 82 break; 83 } 84 } 85 } 86 87 @Override 88 public void close() { 89 if (DBG) Log.d(TAG, "close()"); 90 if (mClosed) { 91 throw new IllegalStateException("Double close()"); 92 } 93 mClosed = true; 94 mShortcuts.close(); 95 for (SuggestionCursor cursor : mRefreshed) { 96 cursor.close(); 97 } 98 super.close(); 99 } 100}