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