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