SourceShortcutRefresher.java revision f252dc7a25ba08b973ecc1cfbbce58eb78d42167
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.android.quicksearchbox.util.NamedTask; 20import com.android.quicksearchbox.util.NamedTaskExecutor; 21 22import java.util.Collections; 23import java.util.HashSet; 24import java.util.Set; 25 26/** 27 * Refreshes shortcuts from their source. 28 */ 29class SourceShortcutRefresher implements ShortcutRefresher { 30 31 private final NamedTaskExecutor mExecutor; 32 33 private final Set<String> mRefreshed = Collections.synchronizedSet(new HashSet<String>()); 34 35 /** 36 * Create a ShortcutRefresher that will refresh shortcuts using the given executor. 37 * 38 * @param executor Used to execute the tasks. 39 */ 40 public SourceShortcutRefresher(NamedTaskExecutor executor) { 41 mExecutor = executor; 42 } 43 44 /** 45 * Sends off the refresher tasks. 46 * 47 * @param shortcuts The shortcuts to refresh. 48 * @param listener Who to report back to. 49 */ 50 public void refresh(SuggestionCursor shortcuts, final Listener listener) { 51 int count = shortcuts.getCount(); 52 for (int i = 0; i < count; i++) { 53 shortcuts.moveTo(i); 54 if (shouldRefresh(shortcuts)) { 55 String shortcutId = shortcuts.getShortcutId(); 56 Source source = shortcuts.getSuggestionSource(); 57 58 // If we can't find the source then invalidate the shortcut. 59 // Otherwise, send off the refresh task. 60 if (source == null) { 61 listener.onShortcutRefreshed(source, shortcutId, null); 62 } else { 63 String extraData = shortcuts.getSuggestionIntentExtraData(); 64 ShortcutRefreshTask refreshTask = new ShortcutRefreshTask( 65 source, shortcutId, extraData, listener); 66 mExecutor.execute(refreshTask); 67 } 68 } 69 } 70 } 71 72 /** 73 * Returns true if the given shortcut requires refreshing. 74 */ 75 public boolean shouldRefresh(SuggestionCursor shortcut) { 76 return shortcut.getShortcutId() != null 77 && ! mRefreshed.contains(makeKey(shortcut)); 78 } 79 80 /** 81 * Indicate that the shortcut no longer requires refreshing. 82 */ 83 public void onShortcutRefreshed(SuggestionCursor shortcut) { 84 mRefreshed.add(makeKey(shortcut)); 85 } 86 87 /** 88 * Reset internal state. This results in all shortcuts requiring refreshing. 89 */ 90 public void reset() { 91 mRefreshed.clear(); 92 } 93 94 /** 95 * Cancel any pending shortcut refresh requests. 96 */ 97 public void cancelPendingTasks() { 98 mExecutor.cancelPendingTasks(); 99 } 100 101 private static String makeKey(SuggestionCursor shortcut) { 102 return shortcut.getSuggestionSource().getName() + "#" 103 + shortcut.getShortcutId(); 104 } 105 106 /** 107 * Refreshes a shortcut with a source and reports the result to a 108 * {@link ShortcutRefresher.Listener}. 109 */ 110 private class ShortcutRefreshTask implements NamedTask { 111 private final Source mSource; 112 private final String mShortcutId; 113 private final String mExtraData; 114 private final Listener mListener; 115 116 /** 117 * @param source The source that should validate the shortcut. 118 * @param shortcutId The shortcut to be refreshed. 119 * @param listener Who to report back to when the result is in. 120 */ 121 ShortcutRefreshTask(Source source, String shortcutId, String extraData, 122 Listener listener) { 123 mSource = source; 124 mShortcutId = shortcutId; 125 mExtraData = extraData; 126 mListener = listener; 127 } 128 129 public String getName() { 130 return mSource.getName(); 131 } 132 133 public void run() { 134 // TODO: Add latency tracking and logging. 135 SuggestionCursor refreshed = mSource.refreshShortcut(mShortcutId, mExtraData); 136 onShortcutRefreshed(refreshed); 137 mListener.onShortcutRefreshed(mSource, mShortcutId, refreshed); 138 } 139 140 } 141} 142