Home | History | Annotate | Download | only in preferences
      1 /*
      2  * Copyright (C) 2010 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 package com.android.quicksearchbox.preferences;
     17 
     18 import com.android.quicksearchbox.ShortcutRepository;
     19 import com.android.quicksearchbox.util.Consumer;
     20 import com.android.quicksearchbox.util.Consumers;
     21 
     22 import android.os.Handler;
     23 import android.preference.Preference;
     24 import android.util.Log;
     25 
     26 /**
     27  * Logic behind the 'clear shortcuts' preference.
     28  */
     29 public class ClearShortcutsController implements PreferenceController {
     30 
     31     public static final String CLEAR_SHORTCUTS_PREF = "clear_shortcuts";
     32 
     33     private static final boolean DBG = false;
     34     private static final String TAG = "QSB.ClearShortcutsController";
     35 
     36     private final ShortcutRepository mShortcuts;
     37     private final Handler mHandler = new Handler();
     38 
     39     private OkCancelPreference mClearShortcutsPreference;
     40 
     41 
     42     public ClearShortcutsController(ShortcutRepository shortcuts) {
     43         mShortcuts = shortcuts;
     44     }
     45 
     46     @Override
     47     public void handlePreference(Preference p) {
     48         mClearShortcutsPreference = (OkCancelPreference) p;
     49         mClearShortcutsPreference.setListener(new OkCancelPreference.Listener() {
     50             @Override
     51             public void onDialogClosed(boolean okClicked) {
     52                 if (okClicked) {
     53                     clearShortcuts();
     54                 }
     55             }
     56         });
     57     }
     58 
     59     public void onCreateComplete() {
     60     }
     61 
     62     public void onStop() {
     63     }
     64 
     65     public void onDestroy() {
     66     }
     67 
     68     @Override
     69     public void onResume() {
     70         updateClearShortcutsPreference();
     71     }
     72 
     73     /**
     74      * Enables/disables the "Clear search shortcuts" preference depending
     75      * on whether there is any search history.
     76      */
     77     private void updateClearShortcutsPreference() {
     78         mShortcuts.hasHistory(Consumers.createAsyncConsumer(mHandler, new Consumer<Boolean>() {
     79             @Override
     80             public boolean consume(Boolean hasHistory) {
     81                 if (DBG) Log.d(TAG, "hasHistory()=" + hasHistory);
     82                 mClearShortcutsPreference.setEnabled(hasHistory);
     83                 return true;
     84             }
     85         }));
     86     }
     87 
     88     private void clearShortcuts() {
     89         Log.i(TAG, "Clearing shortcuts...");
     90         mShortcuts.clearHistory();
     91         mClearShortcutsPreference.setEnabled(false);
     92     }
     93 }
     94