16b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood/* 26b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * Copyright (C) 2010 The Android Open Source Project 36b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * 46b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * Licensed under the Apache License, Version 2.0 (the "License"); 56b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * you may not use this file except in compliance with the License. 66b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * You may obtain a copy of the License at 76b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * 86b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * http://www.apache.org/licenses/LICENSE-2.0 96b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * 106b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * Unless required by applicable law or agreed to in writing, software 116b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * distributed under the License is distributed on an "AS IS" BASIS, 126b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * See the License for the specific language governing permissions and 146b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * limitations under the License. 156b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood */ 166b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwoodpackage com.android.quicksearchbox.preferences; 176b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 186b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwoodimport com.android.quicksearchbox.ShortcutRepository; 196b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwoodimport com.android.quicksearchbox.util.Consumer; 206b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwoodimport com.android.quicksearchbox.util.Consumers; 216b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 226b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwoodimport android.os.Handler; 236b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwoodimport android.preference.Preference; 246b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwoodimport android.util.Log; 256b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 266b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood/** 276b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * Logic behind the 'clear shortcuts' preference. 286b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood */ 296b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwoodpublic class ClearShortcutsController implements PreferenceController { 306b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 316b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood public static final String CLEAR_SHORTCUTS_PREF = "clear_shortcuts"; 326b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 336b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood private static final boolean DBG = false; 346b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood private static final String TAG = "QSB.ClearShortcutsController"; 356b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 366b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood private final ShortcutRepository mShortcuts; 376b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood private final Handler mHandler = new Handler(); 386b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 396b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood private OkCancelPreference mClearShortcutsPreference; 406b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 416b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 426b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood public ClearShortcutsController(ShortcutRepository shortcuts) { 436b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood mShortcuts = shortcuts; 446b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood } 456b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 466b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood @Override 476b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood public void handlePreference(Preference p) { 486b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood mClearShortcutsPreference = (OkCancelPreference) p; 496b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood mClearShortcutsPreference.setListener(new OkCancelPreference.Listener() { 506b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood @Override 516b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood public void onDialogClosed(boolean okClicked) { 526b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood if (okClicked) { 536b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood clearShortcuts(); 546b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood } 556b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood } 566b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood }); 576b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood } 586b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 596b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood public void onCreateComplete() { 606b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood } 616b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 62ce476118902a8c52a0ff5b398573caa88e018a70Bjorn Bringert public void onStop() { 63ce476118902a8c52a0ff5b398573caa88e018a70Bjorn Bringert } 64ce476118902a8c52a0ff5b398573caa88e018a70Bjorn Bringert 656b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood public void onDestroy() { 666b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood } 676b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 686b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood @Override 696b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood public void onResume() { 706b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood updateClearShortcutsPreference(); 716b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood } 726b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 736b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood /** 746b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * Enables/disables the "Clear search shortcuts" preference depending 756b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood * on whether there is any search history. 766b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood */ 776b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood private void updateClearShortcutsPreference() { 786b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood mShortcuts.hasHistory(Consumers.createAsyncConsumer(mHandler, new Consumer<Boolean>() { 796b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood @Override 806b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood public boolean consume(Boolean hasHistory) { 816b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood if (DBG) Log.d(TAG, "hasHistory()=" + hasHistory); 826b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood mClearShortcutsPreference.setEnabled(hasHistory); 836b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood return true; 846b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood } 856b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood })); 866b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood } 876b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood 886b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood private void clearShortcuts() { 896b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood Log.i(TAG, "Clearing shortcuts..."); 906b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood mShortcuts.clearHistory(); 916b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood mClearShortcutsPreference.setEnabled(false); 926b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood } 936b3427a174f1e51555db05aa27b4c14ac99ede72Mathew Inwood} 94