1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui; 16 17 import android.content.BroadcastReceiver; 18 import android.content.ComponentName; 19 import android.content.ContentProvider; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.net.Uri; 24 import android.os.UserHandle; 25 import android.util.ArrayMap; 26 import android.util.ArraySet; 27 import android.util.Log; 28 29 import com.android.internal.annotations.VisibleForTesting; 30 import com.android.settingslib.SliceBroadcastRelay; 31 32 /** 33 * Allows settings to register certain broadcasts to launch the settings app for pinned slices. 34 * @see SliceBroadcastRelay 35 */ 36 public class SliceBroadcastRelayHandler extends SystemUI { 37 private static final String TAG = "SliceBroadcastRelay"; 38 private static final boolean DEBUG = false; 39 40 private final ArrayMap<Uri, BroadcastRelay> mRelays = new ArrayMap<>(); 41 42 @Override start()43 public void start() { 44 if (DEBUG) Log.d(TAG, "Start"); 45 IntentFilter filter = new IntentFilter(SliceBroadcastRelay.ACTION_REGISTER); 46 filter.addAction(SliceBroadcastRelay.ACTION_UNREGISTER); 47 mContext.registerReceiver(mReceiver, filter); 48 } 49 50 @VisibleForTesting handleIntent(Intent intent)51 void handleIntent(Intent intent) { 52 if (SliceBroadcastRelay.ACTION_REGISTER.equals(intent.getAction())) { 53 Uri uri = intent.getParcelableExtra(SliceBroadcastRelay.EXTRA_URI); 54 ComponentName receiverClass = 55 intent.getParcelableExtra(SliceBroadcastRelay.EXTRA_RECEIVER); 56 IntentFilter filter = intent.getParcelableExtra(SliceBroadcastRelay.EXTRA_FILTER); 57 if (DEBUG) Log.d(TAG, "Register " + uri + " " + receiverClass + " " + filter); 58 getOrCreateRelay(uri).register(mContext, receiverClass, filter); 59 } else if (SliceBroadcastRelay.ACTION_UNREGISTER.equals(intent.getAction())) { 60 Uri uri = intent.getParcelableExtra(SliceBroadcastRelay.EXTRA_URI); 61 if (DEBUG) Log.d(TAG, "Unregister " + uri); 62 getAndRemoveRelay(uri).unregister(mContext); 63 } 64 } 65 getOrCreateRelay(Uri uri)66 private BroadcastRelay getOrCreateRelay(Uri uri) { 67 BroadcastRelay ret = mRelays.get(uri); 68 if (ret == null) { 69 ret = new BroadcastRelay(uri); 70 mRelays.put(uri, ret); 71 } 72 return ret; 73 } 74 getAndRemoveRelay(Uri uri)75 private BroadcastRelay getAndRemoveRelay(Uri uri) { 76 return mRelays.remove(uri); 77 } 78 79 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 80 @Override 81 public void onReceive(Context context, Intent intent) { 82 handleIntent(intent); 83 } 84 }; 85 86 private static class BroadcastRelay extends BroadcastReceiver { 87 88 private final ArraySet<ComponentName> mReceivers = new ArraySet<>(); 89 private final UserHandle mUserId; 90 private final Uri mUri; 91 BroadcastRelay(Uri uri)92 public BroadcastRelay(Uri uri) { 93 mUserId = new UserHandle(ContentProvider.getUserIdFromUri(uri)); 94 mUri = uri; 95 } 96 register(Context context, ComponentName receiver, IntentFilter filter)97 public void register(Context context, ComponentName receiver, IntentFilter filter) { 98 mReceivers.add(receiver); 99 context.registerReceiver(this, filter); 100 } 101 unregister(Context context)102 public void unregister(Context context) { 103 context.unregisterReceiver(this); 104 } 105 106 @Override onReceive(Context context, Intent intent)107 public void onReceive(Context context, Intent intent) { 108 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); 109 for (ComponentName receiver : mReceivers) { 110 intent.setComponent(receiver); 111 intent.putExtra(SliceBroadcastRelay.EXTRA_URI, mUri.toString()); 112 if (DEBUG) Log.d(TAG, "Forwarding " + receiver + " " + intent + " " + mUserId); 113 context.sendBroadcastAsUser(intent, mUserId); 114 } 115 } 116 } 117 } 118