1 /*
2  * Copyright (C) 2021 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 static com.android.systemui.people.PeopleSpaceUtils.EMPTY_KEY;
20 import static com.android.systemui.people.PeopleSpaceUtils.EMPTY_STRING;
21 import static com.android.systemui.people.PeopleSpaceUtils.INVALID_USER_ID;
22 import static com.android.systemui.people.PeopleSpaceUtils.PACKAGE_NAME;
23 import static com.android.systemui.people.PeopleSpaceUtils.SHORTCUT_ID;
24 import static com.android.systemui.people.PeopleSpaceUtils.USER_ID;
25 
26 import android.appwidget.AppWidgetManager;
27 import android.os.Bundle;
28 
29 /** Helper class encapsulating AppWidgetOptions for People Tile. */
30 public class AppWidgetOptionsHelper {
31     private static final String TAG = "AppWidgetOptionsHelper";
32 
33     /** Sets {@link PeopleTileKey} in AppWidgetOptions. */
setPeopleTileKey(AppWidgetManager appWidgetManager, int appWidgetId, PeopleTileKey key)34     public static void setPeopleTileKey(AppWidgetManager appWidgetManager, int appWidgetId,
35             PeopleTileKey key) {
36         Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
37         options.putString(SHORTCUT_ID, key.getShortcutId());
38         options.putInt(USER_ID, key.getUserId());
39         options.putString(PACKAGE_NAME, key.getPackageName());
40         appWidgetManager.updateAppWidgetOptions(appWidgetId, options);
41     }
42 
43     /** Gets {@link PeopleTileKey} from Bundle {@code options}. */
getPeopleTileKeyFromBundle(Bundle options)44     public static PeopleTileKey getPeopleTileKeyFromBundle(Bundle options) {
45         String pkg = options.getString(PACKAGE_NAME, EMPTY_STRING);
46         int userId = options.getInt(USER_ID, INVALID_USER_ID);
47         String shortcutId = options.getString(SHORTCUT_ID, EMPTY_STRING);
48         return new PeopleTileKey(shortcutId, userId, pkg);
49     }
50 
51     /** Removes {@link PeopleTileKey} from AppWidgetOptions. */
removePeopleTileKey(AppWidgetManager appWidgetManager, int appWidgetId)52     public static void removePeopleTileKey(AppWidgetManager appWidgetManager,
53             int appWidgetId) {
54         setPeopleTileKey(appWidgetManager, appWidgetId, EMPTY_KEY);
55     }
56 }
57