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