1 /*
2  * Copyright (C) 2020 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.people.widget;
18 
19 import android.appwidget.AppWidgetManager;
20 import android.appwidget.AppWidgetProvider;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.util.Log;
24 
25 import com.android.internal.annotations.VisibleForTesting;
26 import com.android.systemui.people.PeopleSpaceUtils;
27 
28 import javax.inject.Inject;
29 
30 /** People Space Widget Provider class. */
31 public class PeopleSpaceWidgetProvider extends AppWidgetProvider {
32     private static final String TAG = "PeopleSpaceWidgetPvd";
33     private static final boolean DEBUG = PeopleSpaceUtils.DEBUG;
34 
35     public static final String EXTRA_TILE_ID = "extra_tile_id";
36     public static final String EXTRA_PACKAGE_NAME = "extra_package_name";
37     public static final String EXTRA_USER_HANDLE = "extra_user_handle";
38     public static final String EXTRA_NOTIFICATION_KEY = "extra_notification_key";
39 
40     public PeopleSpaceWidgetManager mPeopleSpaceWidgetManager;
41 
42     @Inject
PeopleSpaceWidgetProvider(PeopleSpaceWidgetManager peopleSpaceWidgetManager)43     PeopleSpaceWidgetProvider(PeopleSpaceWidgetManager peopleSpaceWidgetManager) {
44         mPeopleSpaceWidgetManager = peopleSpaceWidgetManager;
45     }
46 
47     /** Called when widget updates. */
48     @Override
onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)49     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
50         super.onUpdate(context, appWidgetManager, appWidgetIds);
51 
52         if (DEBUG) Log.d(TAG, "onUpdate called");
53         ensurePeopleSpaceWidgetManagerInitialized();
54         mPeopleSpaceWidgetManager.updateWidgets(appWidgetIds);
55     }
56 
57     /** Called when widget updates. */
58     @Override
onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)59     public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
60             int appWidgetId, Bundle newOptions) {
61         super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
62         ensurePeopleSpaceWidgetManagerInitialized();
63         mPeopleSpaceWidgetManager.onAppWidgetOptionsChanged(appWidgetId, newOptions);
64     }
65 
66     @Override
onDeleted(Context context, int[] appWidgetIds)67     public void onDeleted(Context context, int[] appWidgetIds) {
68         super.onDeleted(context, appWidgetIds);
69         ensurePeopleSpaceWidgetManagerInitialized();
70         mPeopleSpaceWidgetManager.deleteWidgets(appWidgetIds);
71     }
72 
73     @Override
onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds)74     public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
75         super.onRestored(context, oldWidgetIds, newWidgetIds);
76         ensurePeopleSpaceWidgetManagerInitialized();
77         mPeopleSpaceWidgetManager.remapWidgets(oldWidgetIds, newWidgetIds);
78     }
79 
ensurePeopleSpaceWidgetManagerInitialized()80     private void ensurePeopleSpaceWidgetManagerInitialized() {
81         mPeopleSpaceWidgetManager.init();
82     }
83 
84     @VisibleForTesting
setPeopleSpaceWidgetManager(PeopleSpaceWidgetManager manager)85     public void setPeopleSpaceWidgetManager(PeopleSpaceWidgetManager manager) {
86         mPeopleSpaceWidgetManager = manager;
87     }
88 }
89