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