1 /* Copyright 2017, The Android Open Source Project
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 package android.os;
17 
18 /**
19  * Data class for battery saver state. It contains the data
20  * <p>
21  * 1. Whether battery saver mode is enabled
22  * 2. Specific parameters to use in battery saver mode (i.e. screen brightness, location mode)
23  *
24  * @hide
25  */
26 public class PowerSaveState implements Parcelable {
27     /**
28      * Whether we should enable battery saver for this service.
29      *
30      * @see com.android.server.power.batterysaver.BatterySaverPolicy
31      */
32     public final boolean batterySaverEnabled;
33     /**
34      * Whether the battery saver is enabled globally, which means the data we get from
35      * {@link PowerManager#isPowerSaveMode()}
36      */
37     public final boolean globalBatterySaverEnabled;
38     public final int locationMode;
39     public final int soundTriggerMode;
40     public final float brightnessFactor;
41 
PowerSaveState(Builder builder)42     public PowerSaveState(Builder builder) {
43         batterySaverEnabled = builder.mBatterySaverEnabled;
44         locationMode = builder.mLocationMode;
45         soundTriggerMode = builder.mSoundTriggerMode;
46         brightnessFactor = builder.mBrightnessFactor;
47         globalBatterySaverEnabled = builder.mGlobalBatterySaverEnabled;
48     }
49 
PowerSaveState(Parcel in)50     public PowerSaveState(Parcel in) {
51         batterySaverEnabled = in.readByte() != 0;
52         globalBatterySaverEnabled = in.readByte() != 0;
53         locationMode = in.readInt();
54         soundTriggerMode = in.readInt();
55         brightnessFactor = in.readFloat();
56     }
57 
58     @Override
describeContents()59     public int describeContents() {
60         return 0;
61     }
62 
63     @Override
writeToParcel(Parcel dest, int flags)64     public void writeToParcel(Parcel dest, int flags) {
65         dest.writeByte((byte) (batterySaverEnabled ? 1 : 0));
66         dest.writeByte((byte) (globalBatterySaverEnabled ? 1 : 0));
67         dest.writeInt(locationMode);
68         dest.writeInt(soundTriggerMode);
69         dest.writeFloat(brightnessFactor);
70     }
71 
72     public static final class Builder {
73         private boolean mBatterySaverEnabled = false;
74         private boolean mGlobalBatterySaverEnabled = false;
75         private int mLocationMode = 0;
76         private int mSoundTriggerMode = PowerManager.SOUND_TRIGGER_MODE_ALL_ENABLED;
77         private float mBrightnessFactor = 0.5f;
78 
Builder()79         public Builder() {}
80 
setBatterySaverEnabled(boolean enabled)81         public Builder setBatterySaverEnabled(boolean enabled) {
82             mBatterySaverEnabled = enabled;
83             return this;
84         }
85 
setGlobalBatterySaverEnabled(boolean enabled)86         public Builder setGlobalBatterySaverEnabled(boolean enabled) {
87             mGlobalBatterySaverEnabled = enabled;
88             return this;
89         }
90 
setSoundTriggerMode(int mode)91         public Builder setSoundTriggerMode(int mode) {
92             mSoundTriggerMode = mode;
93             return this;
94         }
95 
setLocationMode(int mode)96         public Builder setLocationMode(int mode) {
97             mLocationMode = mode;
98             return this;
99         }
100 
setBrightnessFactor(float factor)101         public Builder setBrightnessFactor(float factor) {
102             mBrightnessFactor = factor;
103             return this;
104         }
105 
build()106         public PowerSaveState build() {
107             return new PowerSaveState(this);
108         }
109     }
110 
111     public static final Parcelable.Creator<PowerSaveState>
112             CREATOR = new Parcelable.Creator<PowerSaveState>() {
113 
114         @Override
115         public PowerSaveState createFromParcel(Parcel source) {
116             return new PowerSaveState(source);
117         }
118 
119         @Override
120         public PowerSaveState[] newArray(int size) {
121             return new PowerSaveState[size];
122         }
123     };
124 }