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.INVALID_USER_ID; 20 21 import android.app.PendingIntent; 22 import android.appwidget.AppWidgetManager; 23 import android.content.BroadcastReceiver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.pm.ShortcutInfo; 27 import android.util.Log; 28 29 import com.android.systemui.people.PeopleSpaceUtils; 30 31 import javax.inject.Inject; 32 33 /** Called when a People Tile widget is added via {@link AppWidgetManager.requestPinAppWidget()}. */ 34 public class PeopleSpaceWidgetPinnedReceiver extends BroadcastReceiver { 35 private static final String TAG = "PeopleSpaceWgtPinReceiver"; 36 private static final int BROADCAST_ID = 0; 37 private static final int INVALID_WIDGET_ID = -1; 38 private static final boolean DEBUG = PeopleSpaceUtils.DEBUG; 39 40 private final PeopleSpaceWidgetManager mPeopleSpaceWidgetManager; 41 42 @Inject PeopleSpaceWidgetPinnedReceiver(PeopleSpaceWidgetManager peopleSpaceWidgetManager)43 public PeopleSpaceWidgetPinnedReceiver(PeopleSpaceWidgetManager peopleSpaceWidgetManager) { 44 mPeopleSpaceWidgetManager = peopleSpaceWidgetManager; 45 } 46 47 /** Creates a {@link PendingIntent} that is passed onto this receiver when a widget is added. */ getPendingIntent(Context context, ShortcutInfo shortcutInfo)48 public static PendingIntent getPendingIntent(Context context, ShortcutInfo shortcutInfo) { 49 Intent intent = new Intent(context, PeopleSpaceWidgetPinnedReceiver.class) 50 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND); 51 intent.putExtra(Intent.EXTRA_SHORTCUT_ID, shortcutInfo.getId()); 52 intent.putExtra(Intent.EXTRA_USER_ID, shortcutInfo.getUserId()); 53 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, shortcutInfo.getPackage()); 54 55 // Intent needs to be mutable because App Widget framework populates it with app widget id. 56 return PendingIntent.getBroadcast(context, BROADCAST_ID, intent, 57 PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); 58 } 59 60 @Override onReceive(Context context, Intent intent)61 public void onReceive(Context context, Intent intent) { 62 if (DEBUG) Log.d(TAG, "Add widget broadcast received"); 63 if (context == null || intent == null) { 64 if (DEBUG) Log.w(TAG, "Skipping: context or intent are null"); 65 return; 66 } 67 68 int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, INVALID_WIDGET_ID); 69 if (widgetId == INVALID_WIDGET_ID) { 70 if (DEBUG) Log.w(TAG, "Skipping: invalid widgetId"); 71 return; 72 } 73 74 String shortcutId = intent.getStringExtra(Intent.EXTRA_SHORTCUT_ID); 75 String packageName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME); 76 int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, INVALID_USER_ID); 77 PeopleTileKey key = new PeopleTileKey(shortcutId, userId, packageName); 78 if (!PeopleTileKey.isValid(key)) { 79 if (DEBUG) Log.w(TAG, "Skipping: key is not valid: " + key.toString()); 80 return; 81 } 82 83 if (DEBUG) Log.d(TAG, "Adding widget: " + widgetId + ", key:" + key.toString()); 84 mPeopleSpaceWidgetManager.addNewWidget(widgetId, key); 85 } 86 } 87