1 /* 2 * Copyright (C) 2014 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 17 package com.android.systemui.recents; 18 19 import android.appwidget.AppWidgetHost; 20 import android.appwidget.AppWidgetProviderInfo; 21 import android.content.Context; 22 import com.android.systemui.recents.misc.SystemServicesProxy; 23 import com.android.systemui.recents.model.RecentsTaskLoader; 24 25 /** Our special app widget host for the Search widget */ 26 public class RecentsAppWidgetHost extends AppWidgetHost { 27 28 /* Callbacks to notify when an app package changes */ 29 interface RecentsAppWidgetHostCallbacks { 30 public void refreshSearchWidget(); 31 } 32 33 Context mContext; 34 RecentsAppWidgetHostCallbacks mCb; 35 RecentsConfiguration mConfig; 36 boolean mIsListening; 37 38 public RecentsAppWidgetHost(Context context, int hostId) { 39 super(context, hostId); 40 mContext = context; 41 mConfig = RecentsConfiguration.getInstance(); 42 } 43 44 public void startListening(RecentsAppWidgetHostCallbacks cb) { 45 mCb = cb; 46 if (!mIsListening) { 47 mIsListening = true; 48 super.startListening(); 49 } 50 } 51 52 @Override 53 public void stopListening() { 54 if (mIsListening) { 55 super.stopListening(); 56 } 57 // Ensure that we release any references to the callbacks 58 mCb = null; 59 mContext = null; 60 mIsListening = false; 61 } 62 63 @Override 64 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidgetInfo) { 65 if (mCb == null) return; 66 67 SystemServicesProxy ssp = RecentsTaskLoader.getInstance().getSystemServicesProxy(); 68 if (appWidgetId > -1 && appWidgetId == mConfig.searchBarAppWidgetId) { 69 // The search provider may have changed, so just delete the old widget and bind it again 70 ssp.unbindSearchAppWidget(this, appWidgetId); 71 // Update the search widget 72 mConfig.updateSearchBarAppWidgetId(mContext, -1); 73 mCb.refreshSearchWidget(); 74 } 75 } 76 } 77