ShortcutCursor.java revision 94e8a2be78530170f50e7895a558bf8011bbf8e8
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.content.ComponentName; 20import android.util.Log; 21 22import java.util.HashSet; 23 24/** 25 * A SuggestionCursor that allows shortcuts to be updated by overlaying 26 * with results from another cursor. 27 */ 28class ShortcutCursor extends ListSuggestionCursor { 29 private static final boolean DBG = true; 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(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 shortcuts.moveTo(i); 47 if (shortcuts.getSource() != null) { 48 add(new SuggestionPosition(shortcuts)); 49 } 50 } 51 } 52 53 /** 54 * Updates this SuggestionCursor with a refreshed result from another. 55 * Since this modifies the cursor, it should be called on the UI thread. 56 * This class assumes responsibility for closing refreshed. 57 */ 58 public void refresh(ComponentName source, String shortcutId, SuggestionCursor refreshed) { 59 if (DBG) Log.d(TAG, "refresh " + shortcutId); 60 if (mClosed) { 61 if (refreshed != null) { 62 refreshed.close(); 63 } 64 return; 65 } 66 if (refreshed != null) { 67 mRefreshed.add(refreshed); 68 } 69 int count = getCount(); 70 for (int i = 0; i < count; i++) { 71 moveTo(i); 72 if (shortcutId.equals(getShortcutId()) && source.equals(getSourceComponentName())) { 73 if (refreshed != null && refreshed.getCount() > 0) { 74 replaceRow(new SuggestionPosition(refreshed)); 75 } else { 76 removeRow(); 77 } 78 notifyDataSetChanged(); 79 break; 80 } 81 } 82 } 83 84 @Override 85 public void close() { 86 if (DBG) Log.d(TAG, "close()"); 87 if (mClosed) { 88 throw new IllegalStateException("Double close()"); 89 } 90 mClosed = true; 91 mShortcuts.close(); 92 for (SuggestionCursor cursor : mRefreshed) { 93 cursor.close(); 94 } 95 super.close(); 96 } 97}