QsbApplication.java revision 94e8a2be78530170f50e7895a558bf8011bbf8e8
1/*
2 * Copyright (C) 2009 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.ui.DelayingSuggestionsAdapter;
20import com.android.quicksearchbox.ui.SuggestionViewFactory;
21import com.android.quicksearchbox.ui.SuggestionViewInflater;
22import com.android.quicksearchbox.ui.SuggestionsAdapter;
23
24import android.app.Application;
25import android.os.Handler;
26import android.os.Looper;
27
28import java.util.concurrent.ThreadFactory;
29
30public class QsbApplication extends Application {
31
32    private static final String TAG ="QSB.QsbApplication";
33
34    private Handler mUiThreadHandler;
35    private Config mConfig;
36    private Sources mSources;
37    private ShortcutRepository mShortcutRepository;
38    private ShortcutRefresher mShortcutRefresher;
39    private SourceTaskExecutor mSourceTaskExecutor;
40    private SuggestionsProvider mGlobalSuggestionsProvider;
41    private SuggestionViewFactory mSuggestionViewFactory;
42    private SourceFactory mSourceFactory;
43
44    @Override
45    public void onTerminate() {
46        close();
47        super.onTerminate();
48    }
49
50    protected void close() {
51        if (mSources != null) {
52            mSources.close();
53            mSources = null;
54        }
55        if (mShortcutRepository != null) {
56            mShortcutRepository.close();
57            mShortcutRepository = null;
58        }
59        if (mSourceTaskExecutor != null) {
60            mSourceTaskExecutor.close();
61            mSourceTaskExecutor = null;
62        }
63        if (mGlobalSuggestionsProvider != null) {
64            mGlobalSuggestionsProvider.close();
65            mGlobalSuggestionsProvider = null;
66        }
67    }
68
69    public Handler getUiThreadHandler() {
70        if (mUiThreadHandler == null) {
71            mUiThreadHandler = createUiThreadHandler();
72        }
73        return mUiThreadHandler;
74    }
75
76    protected Handler createUiThreadHandler() {
77        return new Handler(Looper.myLooper());
78    }
79
80    public Config getConfig() {
81        if (mConfig == null) {
82            mConfig = createConfig();
83        }
84        return mConfig;
85    }
86
87    protected Config createConfig() {
88        return new Config(this);
89    }
90
91    public SourceLookup getSources() {
92        if (mSources == null) {
93            mSources = createSources();
94        }
95        return mSources;
96    }
97
98    protected Sources createSources() {
99        Sources sources = new Sources(this, getConfig(), getSourceFactory());
100        sources.load();
101        return sources;
102    }
103
104    public ShortcutRepository getShortcutRepository() {
105        if (mShortcutRepository == null) {
106            mShortcutRepository = createShortcutRepository();
107        }
108        return mShortcutRepository;
109    }
110
111    public ShortcutRefresher getShortcutRefresher() {
112        if (mShortcutRefresher == null) {
113            mShortcutRefresher = createShortcutRefresher();
114        }
115        return mShortcutRefresher;
116    }
117
118    protected ShortcutRefresher createShortcutRefresher() {
119        // For now, ShortcutRefresher gets its own SourceTaskExecutor
120        return new ShortcutRefresher(createSourceTaskExecutor(), getSources());
121    }
122
123    protected ShortcutRepository createShortcutRepository() {
124        return ShortcutRepositoryImplLog.create(this, getConfig(), getSources(),
125            getShortcutRefresher(), getUiThreadHandler());
126    }
127
128    public SourceTaskExecutor getSourceTaskExecutor() {
129        if (mSourceTaskExecutor == null) {
130            mSourceTaskExecutor = createSourceTaskExecutor();
131        }
132        return mSourceTaskExecutor;
133    }
134
135    protected SourceTaskExecutor createSourceTaskExecutor() {
136        Config config = getConfig();
137        ThreadFactory queryThreadFactory =
138            new QueryThreadFactory(config.getQueryThreadPriority());
139        return new DelayingSourceTaskExecutor(config, queryThreadFactory);
140    }
141
142
143    public SuggestionsProvider getSuggestionsProvider(Source source) {
144        if (source == null) {
145            return getGlobalSuggestionsProvider();
146        }
147        // TODO: Cache this to avoid creating a new one for each key press
148        return createSuggestionsProvider(source);
149    }
150
151    protected SuggestionsProvider createSuggestionsProvider(Source source) {
152        // TODO: We could use simpler promoter here
153        Promoter promoter =  new ShortcutPromoter(new RoundRobinPromoter());
154        SingleSourceSuggestionsProvider provider = new SingleSourceSuggestionsProvider(getConfig(),
155                source,
156                getSourceTaskExecutor(),
157                getUiThreadHandler(),
158                promoter,
159                getShortcutRepository());
160        return provider;
161    }
162
163    public SuggestionsProvider getGlobalSuggestionsProvider() {
164        if (mGlobalSuggestionsProvider == null) {
165            mGlobalSuggestionsProvider = createGlobalSuggestionsProvider();
166        }
167        return mGlobalSuggestionsProvider;
168    }
169
170    protected SuggestionsProvider createGlobalSuggestionsProvider() {
171        Handler uiThread = new Handler(Looper.myLooper());
172        Promoter promoter =  new ShortcutPromoter(new RoundRobinPromoter());
173        GlobalSuggestionsProvider provider = new GlobalSuggestionsProvider(getConfig(),
174                getSources(),
175                getSourceTaskExecutor(),
176                uiThread,
177                promoter,
178                getShortcutRepository());
179        return provider;
180    }
181
182    public SuggestionViewFactory getSuggestionViewFactory() {
183        if (mSuggestionViewFactory == null) {
184            mSuggestionViewFactory = createSuggestionViewFactory();
185        }
186        return mSuggestionViewFactory;
187    }
188
189    protected SuggestionViewFactory createSuggestionViewFactory() {
190        return new SuggestionViewInflater(this);
191    }
192
193    public SuggestionsAdapter createSuggestionsAdapter() {
194        Config config = getConfig();
195        SuggestionViewFactory viewFactory = getSuggestionViewFactory();
196        DelayingSuggestionsAdapter adapter = new DelayingSuggestionsAdapter(viewFactory);
197        return adapter;
198    }
199
200    public SourceFactory getSourceFactory() {
201        if (mSourceFactory == null) {
202            mSourceFactory = createSourceFactory();
203        }
204        return mSourceFactory;
205    }
206
207    protected SourceFactory createSourceFactory() {
208        return new SearchableSourceFactory(this);
209    }
210}
211