QsbApplication.java revision 185bb2e3881452c084fde44d9bee657f65881b0e
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.SuggestionViewFactory; 20import com.android.quicksearchbox.ui.SuggestionViewInflater; 21 22import android.app.Application; 23import android.os.Handler; 24import android.os.Looper; 25 26import java.util.concurrent.ThreadFactory; 27 28public class QsbApplication extends Application { 29 30 private Config mConfig; 31 private Sources mSources; 32 private ShortcutRepository mShortcutRepository; 33 private SourceTaskExecutor mSourceTaskExecutor; 34 private SuggestionsProvider mSuggestionsProvider; 35 private SuggestionViewFactory mSuggestionViewFactory; 36 37 @Override 38 public void onTerminate() { 39 close(); 40 super.onTerminate(); 41 } 42 43 protected void close() { 44 if (mSources != null) { 45 mSources.close(); 46 mSources = null; 47 } 48 if (mShortcutRepository != null) { 49 mShortcutRepository.close(); 50 mShortcutRepository = null; 51 } 52 if (mSourceTaskExecutor != null) { 53 mSourceTaskExecutor.close(); 54 mSourceTaskExecutor = null; 55 } 56 if (mSuggestionsProvider != null) { 57 mSuggestionsProvider.close(); 58 mSuggestionsProvider = null; 59 } 60 } 61 62 public Config getConfig() { 63 if (mConfig == null) { 64 mConfig = createConfig(); 65 } 66 return mConfig; 67 } 68 69 protected Config createConfig() { 70 return new Config(this); 71 } 72 73 public SourceLookup getSources() { 74 if (mSources == null) { 75 mSources = createSources(); 76 } 77 return mSources; 78 } 79 80 protected Sources createSources() { 81 Sources sources = new Sources(this, getConfig()); 82 sources.load(); 83 return sources; 84 } 85 86 public ShortcutRepository getShortcutRepository() { 87 if (mShortcutRepository == null) { 88 mShortcutRepository = createShortcutRepository(); 89 } 90 return mShortcutRepository; 91 } 92 93 protected ShortcutRepository createShortcutRepository() { 94 return ShortcutRepositoryImplLog.create(this, getConfig(), getSources()); 95 } 96 97 public SourceTaskExecutor getSourceTaskExecutor() { 98 if (mSourceTaskExecutor == null) { 99 mSourceTaskExecutor = createSourceTaskExecutor(); 100 } 101 return mSourceTaskExecutor; 102 } 103 104 protected SourceTaskExecutor createSourceTaskExecutor() { 105 Config config = getConfig(); 106 ThreadFactory queryThreadFactory = 107 new QueryThreadFactory(config.getQueryThreadPriority()); 108 return new DelayingSourceTaskExecutor(config, queryThreadFactory); 109 } 110 111 public SuggestionsProvider getSuggestionsProvider() { 112 if (mSuggestionsProvider == null) { 113 mSuggestionsProvider = createSuggestionsProvider(); 114 } 115 return mSuggestionsProvider; 116 } 117 118 protected SuggestionsProvider createSuggestionsProvider() { 119 Handler uiThread = new Handler(Looper.myLooper()); 120 Promoter promoter = new ShortcutPromoter(new RoundRobinPromoter()); 121 GlobalSuggestionsProvider provider = new GlobalSuggestionsProvider(getConfig(), 122 getSources(), 123 getSourceTaskExecutor(), 124 uiThread, 125 promoter, 126 getShortcutRepository()); 127 return provider; 128 } 129 130 public SuggestionViewFactory getSuggestionViewFactory() { 131 if (mSuggestionViewFactory == null) { 132 mSuggestionViewFactory = createSuggestionViewFactory(); 133 } 134 return mSuggestionViewFactory; 135 } 136 137 protected SuggestionViewFactory createSuggestionViewFactory() { 138 return new SuggestionViewInflater(this); 139 } 140 141} 142