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