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