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 package android.databinding; 17 18 import android.os.Parcel; 19 import android.os.Parcelable; 20 21 import java.io.Serializable; 22 23 /** 24 * An observable class that holds a primitive boolean. 25 * <p> 26 * Observable field classes may be used instead of creating an Observable object: 27 * <pre><code>public class MyDataObject { 28 * public final ObservableBoolean isAdult = new ObservableBoolean(); 29 * }</code></pre> 30 * Fields of this type should be declared final because bindings only detect changes in the 31 * field's value, not of the field itself. 32 * <p> 33 * This class is parcelable and serializable but callbacks are ignored when the object is 34 * parcelled / serialized. Unless you add custom callbacks, this will not be an issue because 35 * data binding framework always re-registers callbacks when the view is bound. 36 */ 37 public class ObservableBoolean extends BaseObservable implements Parcelable, Serializable { 38 static final long serialVersionUID = 1L; 39 private boolean mValue; 40 41 /** 42 * Creates an ObservableBoolean with the given initial value. 43 * 44 * @param value the initial value for the ObservableBoolean 45 */ ObservableBoolean(boolean value)46 public ObservableBoolean(boolean value) { 47 mValue = value; 48 } 49 50 /** 51 * Creates an ObservableBoolean with the initial value of <code>false</code>. 52 */ ObservableBoolean()53 public ObservableBoolean() { 54 } 55 56 /** 57 * @return the stored value. 58 */ get()59 public boolean get() { 60 return mValue; 61 } 62 63 /** 64 * Set the stored value. 65 */ set(boolean value)66 public void set(boolean value) { 67 if (value != mValue) { 68 mValue = value; 69 notifyChange(); 70 } 71 } 72 73 @Override describeContents()74 public int describeContents() { 75 return 0; 76 } 77 78 @Override writeToParcel(Parcel dest, int flags)79 public void writeToParcel(Parcel dest, int flags) { 80 dest.writeInt(mValue ? 1 : 0); 81 } 82 83 public static final Parcelable.Creator<ObservableBoolean> CREATOR 84 = new Parcelable.Creator<ObservableBoolean>() { 85 86 @Override 87 public ObservableBoolean createFromParcel(Parcel source) { 88 return new ObservableBoolean(source.readInt() == 1); 89 } 90 91 @Override 92 public ObservableBoolean[] newArray(int size) { 93 return new ObservableBoolean[size]; 94 } 95 }; 96 } 97