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 
27 /**
28  * Utility class that allows Settings to use SystemUI to relay broadcasts related to pinned slices.
29  */
30 public class SliceBroadcastRelay {
31 
32     public static final String ACTION_REGISTER
33             = "com.android.settingslib.action.REGISTER_SLICE_RECEIVER";
34     public static final String ACTION_UNREGISTER
35             = "com.android.settingslib.action.UNREGISTER_SLICE_RECEIVER";
36     public static final String SYSTEMUI_PACKAGE = "com.android.systemui";
37 
38     public static final String EXTRA_URI = "uri";
39     public static final String EXTRA_RECEIVER = "receiver";
40     public static final String EXTRA_FILTER = "filter";
41 
registerReceiver(Context context, Uri registerKey, Class<? extends BroadcastReceiver> receiver, IntentFilter filter)42     public static void registerReceiver(Context context, Uri registerKey,
43             Class<? extends BroadcastReceiver> receiver, IntentFilter filter) {
44         Intent registerBroadcast = new Intent(ACTION_REGISTER);
45         registerBroadcast.setPackage(SYSTEMUI_PACKAGE);
46         registerBroadcast.putExtra(EXTRA_URI, ContentProvider.maybeAddUserId(registerKey,
47                 Process.myUserHandle().getIdentifier()));
48         registerBroadcast.putExtra(EXTRA_RECEIVER,
49                 new ComponentName(context.getPackageName(), receiver.getName()));
50         registerBroadcast.putExtra(EXTRA_FILTER, filter);
51 
52         context.sendBroadcastAsUser(registerBroadcast, UserHandle.SYSTEM);
53     }
54 
unregisterReceivers(Context context, Uri registerKey)55     public static void unregisterReceivers(Context context, Uri registerKey) {
56         Intent registerBroadcast = new Intent(ACTION_UNREGISTER);
57         registerBroadcast.setPackage(SYSTEMUI_PACKAGE);
58         registerBroadcast.putExtra(EXTRA_URI, ContentProvider.maybeAddUserId(registerKey,
59                 Process.myUserHandle().getIdentifier()));
60 
61         context.sendBroadcastAsUser(registerBroadcast, UserHandle.SYSTEM);
62     }
63 }
64