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.settingslib;
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.Process;
25 import android.os.UserHandle;
26 import android.util.ArraySet;
27 import android.util.Log;
28 
29 import java.util.Set;
30 
31 /**
32  * Utility class that allows Settings to use SystemUI to relay broadcasts related to pinned slices.
33  */
34 public class SliceBroadcastRelay {
35 
36     public static final String ACTION_REGISTER
37             = "com.android.settingslib.action.REGISTER_SLICE_RECEIVER";
38     public static final String ACTION_UNREGISTER
39             = "com.android.settingslib.action.UNREGISTER_SLICE_RECEIVER";
40     public static final String SYSTEMUI_PACKAGE = "com.android.systemui";
41 
42     public static final String EXTRA_URI = "uri";
43     public static final String EXTRA_RECEIVER = "receiver";
44     public static final String EXTRA_FILTER = "filter";
45     private static final String TAG = "SliceBroadcastRelay";
46 
47     private static final Set<Uri> sRegisteredUris = new ArraySet<>();
48 
49     /**
50      * Associate intent filter/sliceUri with corresponding receiver.
51      */
registerReceiver(Context context, Uri sliceUri, Class<? extends BroadcastReceiver> receiver, IntentFilter filter)52     public static void registerReceiver(Context context, Uri sliceUri,
53             Class<? extends BroadcastReceiver> receiver, IntentFilter filter) {
54 
55         Log.d(TAG, "Registering Uri for broadcast relay: " + sliceUri);
56         sRegisteredUris.add(sliceUri);
57 
58         Intent registerBroadcast = new Intent(ACTION_REGISTER);
59         registerBroadcast.setPackage(SYSTEMUI_PACKAGE);
60         registerBroadcast.putExtra(EXTRA_URI, ContentProvider.maybeAddUserId(sliceUri,
61                 Process.myUserHandle().getIdentifier()));
62         registerBroadcast.putExtra(EXTRA_RECEIVER,
63                 new ComponentName(context.getPackageName(), receiver.getName()));
64         registerBroadcast.putExtra(EXTRA_FILTER, filter);
65 
66         context.sendBroadcastAsUser(registerBroadcast, UserHandle.SYSTEM);
67     }
68 
69     /**
70      * Unregisters all receivers for a given slice uri.
71      */
72 
unregisterReceivers(Context context, Uri sliceUri)73     public static void unregisterReceivers(Context context, Uri sliceUri) {
74         if (!sRegisteredUris.contains(sliceUri)) {
75             return;
76         }
77         Log.d(TAG, "Unregistering uri broadcast relay: " + sliceUri);
78         final Intent registerBroadcast = new Intent(ACTION_UNREGISTER);
79         registerBroadcast.setPackage(SYSTEMUI_PACKAGE);
80         registerBroadcast.putExtra(EXTRA_URI, ContentProvider.maybeAddUserId(sliceUri,
81                 Process.myUserHandle().getIdentifier()));
82 
83         context.sendBroadcastAsUser(registerBroadcast, UserHandle.SYSTEM);
84         sRegisteredUris.remove(sliceUri);
85     }
86 }
87