Jordan Liu | d0f439c | 2019-11-22 11:00:05 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.telephony; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.SystemApi; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.os.Bundle; |
| 27 | import android.os.Handler; |
| 28 | import android.os.UserHandle; |
Jordan Liu | d0f439c | 2019-11-22 11:00:05 -0800 | [diff] [blame] | 29 | |
| 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 |
| 39 | public 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 Liu | d0f439c | 2019-11-22 11:00:05 -0800 | [diff] [blame] | 77 | 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 Liu | 3fd1264 | 2019-12-09 13:45:07 -0800 | [diff] [blame] | 83 | Intent backgroundIntent = new Intent(intent); |
| 84 | backgroundIntent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Jordan Liu | d0f439c | 2019-11-22 11:00:05 -0800 | [diff] [blame] | 85 | if (user != null) { |
Jordan Liu | 3fd1264 | 2019-12-09 13:45:07 -0800 | [diff] [blame] | 86 | context.createContextAsUser(user, 0).sendOrderedBroadcast(backgroundIntent, |
| 87 | receiverPermission, receiverAppOp, resultReceiver, scheduler, initialCode, |
| 88 | initialData, initialExtras); |
Jordan Liu | d0f439c | 2019-11-22 11:00:05 -0800 | [diff] [blame] | 89 | } else { |
Jordan Liu | 3fd1264 | 2019-12-09 13:45:07 -0800 | [diff] [blame] | 90 | context.sendOrderedBroadcast(backgroundIntent, receiverPermission, |
Jordan Liu | d0f439c | 2019-11-22 11:00:05 -0800 | [diff] [blame] | 91 | receiverAppOp, resultReceiver, scheduler, initialCode, initialData, |
| 92 | initialExtras); |
| 93 | } |
| 94 | } |
| 95 | } |