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 float brightnessFactor;
40 
PowerSaveState(Builder builder)41     public PowerSaveState(Builder builder) {
42         batterySaverEnabled = builder.mBatterySaverEnabled;
43         locationMode = builder.mLocationMode;
44         brightnessFactor = builder.mBrightnessFactor;
45         globalBatterySaverEnabled = builder.mGlobalBatterySaverEnabled;
46     }
47 
PowerSaveState(Parcel in)48     public PowerSaveState(Parcel in) {
49         batterySaverEnabled = in.readByte() != 0;
50         globalBatterySaverEnabled = in.readByte() != 0;
51         locationMode = in.readInt();
52         brightnessFactor = in.readFloat();
53     }
54 
55     @Override
describeContents()56     public int describeContents() {
57         return 0;
58     }
59 
60     @Override
writeToParcel(Parcel dest, int flags)61     public void writeToParcel(Parcel dest, int flags) {
62         dest.writeByte((byte) (batterySaverEnabled ? 1 : 0));
63         dest.writeByte((byte) (globalBatterySaverEnabled ? 1 : 0));
64         dest.writeInt(locationMode);
65         dest.writeFloat(brightnessFactor);
66     }
67 
68     public static final class Builder {
69         private boolean mBatterySaverEnabled = false;
70         private boolean mGlobalBatterySaverEnabled = false;
71         private int mLocationMode = 0;
72         private float mBrightnessFactor = 0.5f;
73 
Builder()74         public Builder() {}
75 
setBatterySaverEnabled(boolean enabled)76         public Builder setBatterySaverEnabled(boolean enabled) {
77             mBatterySaverEnabled = enabled;
78             return this;
79         }
80 
setGlobalBatterySaverEnabled(boolean enabled)81         public Builder setGlobalBatterySaverEnabled(boolean enabled) {
82             mGlobalBatterySaverEnabled = enabled;
83             return this;
84         }
85 
setLocationMode(int mode)86         public Builder setLocationMode(int mode) {
87             mLocationMode = mode;
88             return this;
89         }
90 
setBrightnessFactor(float factor)91         public Builder setBrightnessFactor(float factor) {
92             mBrightnessFactor = factor;
93             return this;
94         }
95 
build()96         public PowerSaveState build() {
97             return new PowerSaveState(this);
98         }
99     }
100 
101     public static final Parcelable.Creator<PowerSaveState>
102             CREATOR = new Parcelable.Creator<PowerSaveState>() {
103 
104         @Override
105         public PowerSaveState createFromParcel(Parcel source) {
106             return new PowerSaveState(source);
107         }
108 
109         @Override
110         public PowerSaveState[] newArray(int size) {
111             return new PowerSaveState[size];
112         }
113     };
114 }