OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / packages / apps / QuickSearchBox / src / com / android / quicksearchbox / SourceShortcutRefresher.java
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
17 package com.android.quicksearchbox;
18
19 import com.android.quicksearchbox.util.NamedTask;
20 import com.android.quicksearchbox.util.NamedTaskExecutor;
21
22 import android.util.Log;
23
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Set;
27
28 /**
29  * Refreshes shortcuts from their source.
30  */
31 class SourceShortcutRefresher implements ShortcutRefresher {
32     private static final String TAG = "QSB.SourceShortcutRefresher";
33     private static final boolean DBG = false;
34
35     private final NamedTaskExecutor mExecutor;
36
37     private final Set<String> mRefreshed = Collections.synchronizedSet(new HashSet<String>());
38     private final Set<String> mRefreshing = Collections.synchronizedSet(new HashSet<String>());
39
40     /**
41      * Create a ShortcutRefresher that will refresh shortcuts using the given executor.
42      *
43      * @param executor Used to execute the tasks.
44      */
45     public SourceShortcutRefresher(NamedTaskExecutor executor) {
46         mExecutor = executor;
47     }
48
49     public void refresh(Suggestion shortcut, Listener listener) {
50         Source source = shortcut.getSuggestionSource();
51         if (source == null) {
52             throw new NullPointerException("source");
53         }
54         String shortcutId = shortcut.getShortcutId();
55         if (shouldRefresh(source, shortcutId) && !isRefreshing(source, shortcutId)) {
56             if (DBG) {
57                 Log.d(TAG, "Refreshing shortcut  " + shortcutId + " '" +
58                         shortcut.getSuggestionText1() + "'");
59             }
60             markShortcutRefreshing(source, shortcutId);
61             String extraData = shortcut.getSuggestionIntentExtraData();
62             ShortcutRefreshTask refreshTask = new ShortcutRefreshTask(
63                     source, shortcutId, extraData, listener);
64             mExecutor.execute(refreshTask);
65         }
66     }
67
68     /**
69      * Returns true if the given shortcut requires refreshing.
70      */
71     public boolean shouldRefresh(Source source, String shortcutId) {
72         return source != null && shortcutId != null
73                 && !mRefreshed.contains(makeKey(source, shortcutId));
74     }
75
76     public boolean isRefreshing(Source source, String shortcutId) {
77         return source != null && shortcutId != null
78                 && mRefreshing.contains(makeKey(source, shortcutId));
79     }
80
81     private void markShortcutRefreshing(Source source, String shortcutId) {
82         mRefreshing.add(makeKey(source, shortcutId));
83     }
84
85     /**
86      * Indicate that the shortcut no longer requires refreshing.
87      */
88     public void markShortcutRefreshed(Source source, String shortcutId) {
89         String key = makeKey(source, shortcutId);
90         mRefreshed.add(key);
91         mRefreshing.remove(key);
92     }
93
94     /**
95      * Reset internal state.  This results in all shortcuts requiring refreshing.
96      */
97     public void reset() {
98         mRefreshed.clear();
99     }
100
101     /**
102      * Cancel any pending shortcut refresh requests.
103      */
104     public void cancelPendingTasks() {
105         mExecutor.cancelPendingTasks();
106     }
107
108     private static String makeKey(Source source, String shortcutId) {
109         return source.getName() + "#" + shortcutId;
110     }
111
112     /**
113      * Refreshes a shortcut with a source and reports the result to a
114      * {@link ShortcutRefresher.Listener}.
115      */
116     private class ShortcutRefreshTask implements NamedTask {
117         private final Source mSource;
118         private final String mShortcutId;
119         private final String mExtraData;
120         private final Listener mListener;
121
122         /**
123          * @param source The source that should validate the shortcut.
124          * @param shortcutId The shortcut to be refreshed.
125          * @param listener Who to report back to when the result is in.
126          */
127         ShortcutRefreshTask(Source source, String shortcutId, String extraData,
128                 Listener listener) {
129             mSource = source;
130             mShortcutId = shortcutId;
131             mExtraData = extraData;
132             mListener = listener;
133         }
134
135         public String getName() {
136             return mSource.getName();
137         }
138
139         public void run() {
140             // TODO: Add latency tracking and logging.
141             SuggestionCursor refreshed = mSource.refreshShortcut(mShortcutId, mExtraData);
142             // Close cursor if empty and pass null as the refreshed cursor
143             if (refreshed != null && refreshed.getCount() == 0) {
144                 refreshed.close();
145                 refreshed = null;
146             }
147             markShortcutRefreshed(mSource, mShortcutId);
148             mListener.onShortcutRefreshed(mSource, mShortcutId, refreshed);
149         }
150
151     }
152 }