Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.app; |
| 18 | |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 19 | import android.annotation.RequiresPermission; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 21 | import android.os.Build; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 22 | import android.os.Bundle; |
| 23 | |
| 24 | /** |
| 25 | * Helper class for building an options Bundle that can be used with |
| 26 | * {@link android.content.Context#sendBroadcast(android.content.Intent) |
| 27 | * Context.sendBroadcast(Intent)} and related methods. |
| 28 | * {@hide} |
| 29 | */ |
| 30 | @SystemApi |
| 31 | public class BroadcastOptions { |
| 32 | private long mTemporaryAppWhitelistDuration; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 33 | private int mMinManifestReceiverApiLevel = 0; |
| 34 | private int mMaxManifestReceiverApiLevel = Build.VERSION_CODES.CUR_DEVELOPMENT; |
Fyodor Kupolov | da26eb3 | 2018-03-30 16:01:26 -0700 | [diff] [blame^] | 35 | private boolean mDontSendToRestrictedApps = false; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * How long to temporarily put an app on the power whitelist when executing this broadcast |
| 39 | * to it. |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 40 | */ |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 41 | static final String KEY_TEMPORARY_APP_WHITELIST_DURATION |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 42 | = "android:broadcast.temporaryAppWhitelistDuration"; |
| 43 | |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Corresponds to {@link #setMinManifestReceiverApiLevel}. |
| 46 | */ |
| 47 | static final String KEY_MIN_MANIFEST_RECEIVER_API_LEVEL |
| 48 | = "android:broadcast.minManifestReceiverApiLevel"; |
| 49 | |
| 50 | /** |
| 51 | * Corresponds to {@link #setMaxManifestReceiverApiLevel}. |
| 52 | */ |
| 53 | static final String KEY_MAX_MANIFEST_RECEIVER_API_LEVEL |
| 54 | = "android:broadcast.maxManifestReceiverApiLevel"; |
| 55 | |
Fyodor Kupolov | da26eb3 | 2018-03-30 16:01:26 -0700 | [diff] [blame^] | 56 | /** |
| 57 | * Corresponds to {@link #setMaxManifestReceiverApiLevel}. |
| 58 | */ |
| 59 | static final String KEY_DONT_SEND_TO_RESTRICTED_APPS = |
| 60 | "android:broadcast.dontSendToRestrictedApps"; |
| 61 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 62 | public static BroadcastOptions makeBasic() { |
| 63 | BroadcastOptions opts = new BroadcastOptions(); |
| 64 | return opts; |
| 65 | } |
| 66 | |
| 67 | private BroadcastOptions() { |
| 68 | } |
| 69 | |
| 70 | /** @hide */ |
| 71 | public BroadcastOptions(Bundle opts) { |
| 72 | mTemporaryAppWhitelistDuration = opts.getLong(KEY_TEMPORARY_APP_WHITELIST_DURATION); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 73 | mMinManifestReceiverApiLevel = opts.getInt(KEY_MIN_MANIFEST_RECEIVER_API_LEVEL, 0); |
| 74 | mMaxManifestReceiverApiLevel = opts.getInt(KEY_MAX_MANIFEST_RECEIVER_API_LEVEL, |
| 75 | Build.VERSION_CODES.CUR_DEVELOPMENT); |
Fyodor Kupolov | da26eb3 | 2018-03-30 16:01:26 -0700 | [diff] [blame^] | 76 | mDontSendToRestrictedApps = opts.getBoolean(KEY_DONT_SEND_TO_RESTRICTED_APPS, false); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Set a duration for which the system should temporary place an application on the |
| 81 | * power whitelist when this broadcast is being delivered to it. |
| 82 | * @param duration The duration in milliseconds; 0 means to not place on whitelist. |
| 83 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 84 | @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 85 | public void setTemporaryAppWhitelistDuration(long duration) { |
| 86 | mTemporaryAppWhitelistDuration = duration; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Return {@link #setTemporaryAppWhitelistDuration}. |
| 91 | * @hide |
| 92 | */ |
| 93 | public long getTemporaryAppWhitelistDuration() { |
| 94 | return mTemporaryAppWhitelistDuration; |
| 95 | } |
| 96 | |
| 97 | /** |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 98 | * Set the minimum target API level of receivers of the broadcast. If an application |
| 99 | * is targeting an API level less than this, the broadcast will not be delivered to |
| 100 | * them. This only applies to receivers declared in the app's AndroidManifest.xml. |
| 101 | * @hide |
| 102 | */ |
| 103 | public void setMinManifestReceiverApiLevel(int apiLevel) { |
| 104 | mMinManifestReceiverApiLevel = apiLevel; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Return {@link #setMinManifestReceiverApiLevel}. |
| 109 | * @hide |
| 110 | */ |
| 111 | public int getMinManifestReceiverApiLevel() { |
| 112 | return mMinManifestReceiverApiLevel; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Set the maximum target API level of receivers of the broadcast. If an application |
| 117 | * is targeting an API level greater than this, the broadcast will not be delivered to |
| 118 | * them. This only applies to receivers declared in the app's AndroidManifest.xml. |
| 119 | * @hide |
| 120 | */ |
| 121 | public void setMaxManifestReceiverApiLevel(int apiLevel) { |
| 122 | mMaxManifestReceiverApiLevel = apiLevel; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Return {@link #setMaxManifestReceiverApiLevel}. |
| 127 | * @hide |
| 128 | */ |
| 129 | public int getMaxManifestReceiverApiLevel() { |
| 130 | return mMaxManifestReceiverApiLevel; |
| 131 | } |
| 132 | |
| 133 | /** |
Fyodor Kupolov | da26eb3 | 2018-03-30 16:01:26 -0700 | [diff] [blame^] | 134 | * Sets whether pending intent can be sent for an application with background restrictions |
| 135 | * @param dontSendToRestrictedApps if true, pending intent will not be sent for an application |
| 136 | * with background restrictions. Default value is {@code false} |
| 137 | */ |
| 138 | public void setDontSendToRestrictedApps(boolean dontSendToRestrictedApps) { |
| 139 | mDontSendToRestrictedApps = dontSendToRestrictedApps; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @hide |
| 144 | * @return #setDontSendToRestrictedApps |
| 145 | */ |
| 146 | public boolean isDontSendToRestrictedApps() { |
| 147 | return mDontSendToRestrictedApps; |
| 148 | } |
| 149 | |
| 150 | /** |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 151 | * Returns the created options as a Bundle, which can be passed to |
| 152 | * {@link android.content.Context#sendBroadcast(android.content.Intent) |
| 153 | * Context.sendBroadcast(Intent)} and related methods. |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 154 | * Note that the returned Bundle is still owned by the BroadcastOptions |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 155 | * object; you must not modify it, but can supply it to the sendBroadcast |
| 156 | * methods that take an options Bundle. |
| 157 | */ |
| 158 | public Bundle toBundle() { |
| 159 | Bundle b = new Bundle(); |
| 160 | if (mTemporaryAppWhitelistDuration > 0) { |
| 161 | b.putLong(KEY_TEMPORARY_APP_WHITELIST_DURATION, mTemporaryAppWhitelistDuration); |
| 162 | } |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 163 | if (mMinManifestReceiverApiLevel != 0) { |
| 164 | b.putInt(KEY_MIN_MANIFEST_RECEIVER_API_LEVEL, mMinManifestReceiverApiLevel); |
| 165 | } |
| 166 | if (mMaxManifestReceiverApiLevel != Build.VERSION_CODES.CUR_DEVELOPMENT) { |
| 167 | b.putInt(KEY_MAX_MANIFEST_RECEIVER_API_LEVEL, mMaxManifestReceiverApiLevel); |
| 168 | } |
Fyodor Kupolov | da26eb3 | 2018-03-30 16:01:26 -0700 | [diff] [blame^] | 169 | if (mDontSendToRestrictedApps) { |
| 170 | b.putBoolean(KEY_DONT_SEND_TO_RESTRICTED_APPS, true); |
| 171 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 172 | return b.isEmpty() ? null : b; |
| 173 | } |
| 174 | } |