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 19 import android.annotation.SystemApi; 20 import android.os.Bundle; 21 22 /** 23 * Helper class for building an options Bundle that can be used with 24 * {@link android.content.Context#sendBroadcast(android.content.Intent) 25 * Context.sendBroadcast(Intent)} and related methods. 26 * {@hide} 27 */ 28 @SystemApi 29 public class BroadcastOptions { 30 private long mTemporaryAppWhitelistDuration; 31 32 /** 33 * How long to temporarily put an app on the power whitelist when executing this broadcast 34 * to it. 35 * @hide 36 */ 37 public static final String KEY_TEMPORARY_APP_WHITELIST_DURATION 38 = "android:broadcast.temporaryAppWhitelistDuration"; 39 40 public static BroadcastOptions makeBasic() { 41 BroadcastOptions opts = new BroadcastOptions(); 42 return opts; 43 } 44 45 private BroadcastOptions() { 46 } 47 48 /** @hide */ 49 public BroadcastOptions(Bundle opts) { 50 mTemporaryAppWhitelistDuration = opts.getLong(KEY_TEMPORARY_APP_WHITELIST_DURATION); 51 } 52 53 /** 54 * Set a duration for which the system should temporary place an application on the 55 * power whitelist when this broadcast is being delivered to it. 56 * @param duration The duration in milliseconds; 0 means to not place on whitelist. 57 */ 58 public void setTemporaryAppWhitelistDuration(long duration) { 59 mTemporaryAppWhitelistDuration = duration; 60 } 61 62 /** 63 * Return {@link #setTemporaryAppWhitelistDuration}. 64 * @hide 65 */ 66 public long getTemporaryAppWhitelistDuration() { 67 return mTemporaryAppWhitelistDuration; 68 } 69 70 /** 71 * Returns the created options as a Bundle, which can be passed to 72 * {@link android.content.Context#sendBroadcast(android.content.Intent) 73 * Context.sendBroadcast(Intent)} and related methods. 74 * Note that the returned Bundle is still owned by the ActivityOptions 75 * object; you must not modify it, but can supply it to the sendBroadcast 76 * methods that take an options Bundle. 77 */ 78 public Bundle toBundle() { 79 Bundle b = new Bundle(); 80 if (mTemporaryAppWhitelistDuration > 0) { 81 b.putLong(KEY_TEMPORARY_APP_WHITELIST_DURATION, mTemporaryAppWhitelistDuration); 82 } 83 return b.isEmpty() ? null : b; 84 } 85 } 86