blob: 69c3632b7ab24a8eb0b43012b7e9c3fcf07dad77 [file] [log] [blame]
Dianne Hackborna750a632015-06-16 17:18:23 -07001/*
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
17package android.app;
18
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060019import android.annotation.RequiresPermission;
Dianne Hackborna750a632015-06-16 17:18:23 -070020import android.annotation.SystemApi;
Dianne Hackborne0e413e2015-12-09 17:22:26 -080021import android.os.Build;
Dianne Hackborna750a632015-06-16 17:18:23 -070022import 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
31public class BroadcastOptions {
32 private long mTemporaryAppWhitelistDuration;
Dianne Hackborne0e413e2015-12-09 17:22:26 -080033 private int mMinManifestReceiverApiLevel = 0;
34 private int mMaxManifestReceiverApiLevel = Build.VERSION_CODES.CUR_DEVELOPMENT;
Fyodor Kupolovda26eb32018-03-30 16:01:26 -070035 private boolean mDontSendToRestrictedApps = false;
Dianne Hackborna750a632015-06-16 17:18:23 -070036
37 /**
38 * How long to temporarily put an app on the power whitelist when executing this broadcast
39 * to it.
Dianne Hackborna750a632015-06-16 17:18:23 -070040 */
Dianne Hackborne0e413e2015-12-09 17:22:26 -080041 static final String KEY_TEMPORARY_APP_WHITELIST_DURATION
Dianne Hackborna750a632015-06-16 17:18:23 -070042 = "android:broadcast.temporaryAppWhitelistDuration";
43
Dianne Hackborne0e413e2015-12-09 17:22:26 -080044 /**
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 Kupolovda26eb32018-03-30 16:01:26 -070056 /**
57 * Corresponds to {@link #setMaxManifestReceiverApiLevel}.
58 */
59 static final String KEY_DONT_SEND_TO_RESTRICTED_APPS =
60 "android:broadcast.dontSendToRestrictedApps";
61
Dianne Hackborna750a632015-06-16 17:18:23 -070062 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 Hackborne0e413e2015-12-09 17:22:26 -080073 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 Kupolovda26eb32018-03-30 16:01:26 -070076 mDontSendToRestrictedApps = opts.getBoolean(KEY_DONT_SEND_TO_RESTRICTED_APPS, false);
Dianne Hackborna750a632015-06-16 17:18:23 -070077 }
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 Sharkeyd86b8fe2017-06-02 17:36:26 -060084 @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST)
Dianne Hackborna750a632015-06-16 17:18:23 -070085 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 Hackborne0e413e2015-12-09 17:22:26 -080098 * 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 Kupolovda26eb32018-03-30 16:01:26 -0700134 * 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 Hackborna750a632015-06-16 17:18:23 -0700151 * 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 Hackborne0e413e2015-12-09 17:22:26 -0800154 * Note that the returned Bundle is still owned by the BroadcastOptions
Dianne Hackborna750a632015-06-16 17:18:23 -0700155 * 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 Hackborne0e413e2015-12-09 17:22:26 -0800163 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 Kupolovda26eb32018-03-30 16:01:26 -0700169 if (mDontSendToRestrictedApps) {
170 b.putBoolean(KEY_DONT_SEND_TO_RESTRICTED_APPS, true);
171 }
Dianne Hackborna750a632015-06-16 17:18:23 -0700172 return b.isEmpty() ? null : b;
173 }
174}