Home | History | Annotate | Download | only in app
      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.Build;
     21 import android.os.Bundle;
     22 
     23 /**
     24  * Helper class for building an options Bundle that can be used with
     25  * {@link android.content.Context#sendBroadcast(android.content.Intent)
     26  * Context.sendBroadcast(Intent)} and related methods.
     27  * {@hide}
     28  */
     29 @SystemApi
     30 public class BroadcastOptions {
     31     private long mTemporaryAppWhitelistDuration;
     32     private int mMinManifestReceiverApiLevel = 0;
     33     private int mMaxManifestReceiverApiLevel = Build.VERSION_CODES.CUR_DEVELOPMENT;
     34 
     35     /**
     36      * How long to temporarily put an app on the power whitelist when executing this broadcast
     37      * to it.
     38      */
     39     static final String KEY_TEMPORARY_APP_WHITELIST_DURATION
     40             = "android:broadcast.temporaryAppWhitelistDuration";
     41 
     42     /**
     43      * Corresponds to {@link #setMinManifestReceiverApiLevel}.
     44      */
     45     static final String KEY_MIN_MANIFEST_RECEIVER_API_LEVEL
     46             = "android:broadcast.minManifestReceiverApiLevel";
     47 
     48     /**
     49      * Corresponds to {@link #setMaxManifestReceiverApiLevel}.
     50      */
     51     static final String KEY_MAX_MANIFEST_RECEIVER_API_LEVEL
     52             = "android:broadcast.maxManifestReceiverApiLevel";
     53 
     54     public static BroadcastOptions makeBasic() {
     55         BroadcastOptions opts = new BroadcastOptions();
     56         return opts;
     57     }
     58 
     59     private BroadcastOptions() {
     60     }
     61 
     62     /** @hide */
     63     public BroadcastOptions(Bundle opts) {
     64         mTemporaryAppWhitelistDuration = opts.getLong(KEY_TEMPORARY_APP_WHITELIST_DURATION);
     65         mMinManifestReceiverApiLevel = opts.getInt(KEY_MIN_MANIFEST_RECEIVER_API_LEVEL, 0);
     66         mMaxManifestReceiverApiLevel = opts.getInt(KEY_MAX_MANIFEST_RECEIVER_API_LEVEL,
     67                 Build.VERSION_CODES.CUR_DEVELOPMENT);
     68     }
     69 
     70     /**
     71      * Set a duration for which the system should temporary place an application on the
     72      * power whitelist when this broadcast is being delivered to it.
     73      * @param duration The duration in milliseconds; 0 means to not place on whitelist.
     74      */
     75     public void setTemporaryAppWhitelistDuration(long duration) {
     76         mTemporaryAppWhitelistDuration = duration;
     77     }
     78 
     79     /**
     80      * Return {@link #setTemporaryAppWhitelistDuration}.
     81      * @hide
     82      */
     83     public long getTemporaryAppWhitelistDuration() {
     84         return mTemporaryAppWhitelistDuration;
     85     }
     86 
     87     /**
     88      * Set the minimum target API level of receivers of the broadcast.  If an application
     89      * is targeting an API level less than this, the broadcast will not be delivered to
     90      * them.  This only applies to receivers declared in the app's AndroidManifest.xml.
     91      * @hide
     92      */
     93     public void setMinManifestReceiverApiLevel(int apiLevel) {
     94         mMinManifestReceiverApiLevel = apiLevel;
     95     }
     96 
     97     /**
     98      * Return {@link #setMinManifestReceiverApiLevel}.
     99      * @hide
    100      */
    101     public int getMinManifestReceiverApiLevel() {
    102         return mMinManifestReceiverApiLevel;
    103     }
    104 
    105     /**
    106      * Set the maximum target API level of receivers of the broadcast.  If an application
    107      * is targeting an API level greater than this, the broadcast will not be delivered to
    108      * them.  This only applies to receivers declared in the app's AndroidManifest.xml.
    109      * @hide
    110      */
    111     public void setMaxManifestReceiverApiLevel(int apiLevel) {
    112         mMaxManifestReceiverApiLevel = apiLevel;
    113     }
    114 
    115     /**
    116      * Return {@link #setMaxManifestReceiverApiLevel}.
    117      * @hide
    118      */
    119     public int getMaxManifestReceiverApiLevel() {
    120         return mMaxManifestReceiverApiLevel;
    121     }
    122 
    123     /**
    124      * Returns the created options as a Bundle, which can be passed to
    125      * {@link android.content.Context#sendBroadcast(android.content.Intent)
    126      * Context.sendBroadcast(Intent)} and related methods.
    127      * Note that the returned Bundle is still owned by the BroadcastOptions
    128      * object; you must not modify it, but can supply it to the sendBroadcast
    129      * methods that take an options Bundle.
    130      */
    131     public Bundle toBundle() {
    132         Bundle b = new Bundle();
    133         if (mTemporaryAppWhitelistDuration > 0) {
    134             b.putLong(KEY_TEMPORARY_APP_WHITELIST_DURATION, mTemporaryAppWhitelistDuration);
    135         }
    136         if (mMinManifestReceiverApiLevel != 0) {
    137             b.putInt(KEY_MIN_MANIFEST_RECEIVER_API_LEVEL, mMinManifestReceiverApiLevel);
    138         }
    139         if (mMaxManifestReceiverApiLevel != Build.VERSION_CODES.CUR_DEVELOPMENT) {
    140             b.putInt(KEY_MAX_MANIFEST_RECEIVER_API_LEVEL, mMaxManifestReceiverApiLevel);
    141         }
    142         return b.isEmpty() ? null : b;
    143     }
    144 }
    145