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