blob: 8446253c63026b2d1f864477306484a4de912abb [file] [log] [blame]
Jordan Liud0f439c2019-11-22 11:00:05 -08001/*
2 * Copyright (C) 2019 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
17package android.telephony;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.annotation.SystemApi;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.PackageManager;
26import android.os.Bundle;
27import android.os.Handler;
28import android.os.UserHandle;
Jordan Liud0f439c2019-11-22 11:00:05 -080029
30/**
31 * A static helper class used to send Intents with prepopulated flags.
32 * <p>
33 * This is intended to be used by the CellBroadcastService and will throw a security exception if
34 * used from a UID besides the network stack UID.
35 *
36 * @hide
37 */
38@SystemApi
39public class CellBroadcastIntents {
40 private static final String LOG_TAG = "CellBroadcastIntents";
41
42 /**
43 * @hide
44 */
45 private CellBroadcastIntents() {
46 }
47
48 /**
49 * Returns an intent which can be received by background BroadcastReceivers. This is only
50 * intended to be used by the CellBroadcastService and will throw a security exception if called
51 * from another UID.
52 *
53 * @param context The context from which to send the broadcast
54 * @param user The user from which to send the broadcast
55 * @param intent The Intent to broadcast; all receivers matching this Intent will
56 * receive the broadcast.
57 * @param receiverPermission String naming a permissions that a receiver must hold in order to
58 * receive your broadcast. If null, no permission is required.
59 * @param receiverAppOp The app op associated with the broadcast. If null, no appOp is
60 * required. If both receiverAppOp and receiverPermission are
61 * non-null, a receiver must have both of them to receive the
62 * broadcast
63 * @param resultReceiver Your own BroadcastReceiver to treat as the final receiver of the
64 * broadcast.
65 * @param scheduler A custom Handler with which to schedule the resultReceiver
66 * callback; if null it will be scheduled in the Context's main
67 * thread.
68 * @param initialCode An initial value for the result code. Often Activity.RESULT_OK.
69 * @param initialData An initial value for the result data. Often null.
70 * @param initialExtras An initial value for the result extras. Often null.
71 */
72 public static void sendOrderedBroadcastForBackgroundReceivers(@NonNull Context context,
73 @Nullable UserHandle user, @NonNull Intent intent, @Nullable String receiverPermission,
74 @Nullable String receiverAppOp, @Nullable BroadcastReceiver resultReceiver,
75 @Nullable Handler scheduler, int initialCode, @Nullable String initialData,
76 @Nullable Bundle initialExtras) {
Jordan Liud0f439c2019-11-22 11:00:05 -080077 int status = context.checkCallingOrSelfPermission(
78 "android.permission.GRANT_RUNTIME_PERMISSIONS_TO_TELEPHONY_DEFAULTS");
79 if (status == PackageManager.PERMISSION_DENIED) {
80 throw new SecurityException(
81 "Caller does not have permission to send broadcast for background receivers");
82 }
Jordan Liu3fd12642019-12-09 13:45:07 -080083 Intent backgroundIntent = new Intent(intent);
84 backgroundIntent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
Jordan Liud0f439c2019-11-22 11:00:05 -080085 if (user != null) {
Jordan Liu3fd12642019-12-09 13:45:07 -080086 context.createContextAsUser(user, 0).sendOrderedBroadcast(backgroundIntent,
87 receiverPermission, receiverAppOp, resultReceiver, scheduler, initialCode,
88 initialData, initialExtras);
Jordan Liud0f439c2019-11-22 11:00:05 -080089 } else {
Jordan Liu3fd12642019-12-09 13:45:07 -080090 context.sendOrderedBroadcast(backgroundIntent, receiverPermission,
Jordan Liud0f439c2019-11-22 11:00:05 -080091 receiverAppOp, resultReceiver, scheduler, initialCode, initialData,
92 initialExtras);
93 }
94 }
95}