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