| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.devicestate; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.os.Parcel; |
| 23 |
import android.os.Parcelable; |
| 24 |
|
| 25 |
import java.lang.annotation.Retention; |
| 26 |
import java.lang.annotation.RetentionPolicy; |
| 27 |
import java.util.Arrays; |
| 28 |
import java.util.Objects; |
| 29 |
import java.util.concurrent.Executor; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public final class DeviceStateInfo implements Parcelable { |
| 38 |
|
| 39 |
public static final int CHANGED_SUPPORTED_STATES = 1 << 0; |
| 40 |
|
| 41 |
|
| 42 |
public static final int CHANGED_BASE_STATE = 1 << 1; |
| 43 |
|
| 44 |
|
| 45 |
public static final int CHANGED_CURRENT_STATE = 1 << 2; |
| 46 |
|
| 47 |
@IntDef(prefix = {"CHANGED_"}, flag = true, value = { |
| 48 |
CHANGED_SUPPORTED_STATES, |
| 49 |
CHANGED_BASE_STATE, |
| 50 |
CHANGED_CURRENT_STATE, |
| 51 |
}) |
| 52 |
@Retention(RetentionPolicy.SOURCE) |
| 53 |
public @interface ChangeFlags {} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@NonNull |
| 59 |
public final int[] supportedStates; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public final int baseState; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public final int currentState; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public DeviceStateInfo(@NonNull int[] supportedStates, int baseState, int state) { |
| 80 |
this.supportedStates = supportedStates; |
| 81 |
this.baseState = baseState; |
| 82 |
this.currentState = state; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public DeviceStateInfo(@NonNull DeviceStateInfo info) { |
| 90 |
this(Arrays.copyOf(info.supportedStates, info.supportedStates.length), |
| 91 |
info.baseState, info.currentState); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public boolean equals(@Nullable Object other) { |
| 96 |
if (this == other) return true; |
| 97 |
if (other == null || getClass() != other.getClass()) return false; |
| 98 |
DeviceStateInfo that = (DeviceStateInfo) other; |
| 99 |
return baseState == that.baseState |
| 100 |
&& currentState == that.currentState |
| 101 |
&& Arrays.equals(supportedStates, that.supportedStates); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public int hashCode() { |
| 106 |
int result = Objects.hash(baseState, currentState); |
| 107 |
result = 31 * result + Arrays.hashCode(supportedStates); |
| 108 |
return result; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
@ChangeFlags |
| 113 |
public int diff(@NonNull DeviceStateInfo other) { |
| 114 |
int diff = 0; |
| 115 |
if (!Arrays.equals(supportedStates, other.supportedStates)) { |
| 116 |
diff |= CHANGED_SUPPORTED_STATES; |
| 117 |
} |
| 118 |
if (baseState != other.baseState) { |
| 119 |
diff |= CHANGED_BASE_STATE; |
| 120 |
} |
| 121 |
if (currentState != other.currentState) { |
| 122 |
diff |= CHANGED_CURRENT_STATE; |
| 123 |
} |
| 124 |
return diff; |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public void writeToParcel(Parcel dest, int flags) { |
| 129 |
dest.writeInt(supportedStates.length); |
| 130 |
for (int i = 0; i < supportedStates.length; i++) { |
| 131 |
dest.writeInt(supportedStates[i]); |
| 132 |
} |
| 133 |
|
| 134 |
dest.writeInt(baseState); |
| 135 |
dest.writeInt(currentState); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public int describeContents() { |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
public static final @NonNull Creator<DeviceStateInfo> CREATOR = new Creator<DeviceStateInfo>() { |
| 144 |
@Override |
| 145 |
public DeviceStateInfo createFromParcel(Parcel source) { |
| 146 |
final int numberOfSupportedStates = source.readInt(); |
| 147 |
final int[] supportedStates = new int[numberOfSupportedStates]; |
| 148 |
for (int i = 0; i < numberOfSupportedStates; i++) { |
| 149 |
supportedStates[i] = source.readInt(); |
| 150 |
} |
| 151 |
final int baseState = source.readInt(); |
| 152 |
final int currentState = source.readInt(); |
| 153 |
|
| 154 |
return new DeviceStateInfo(supportedStates, baseState, currentState); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public DeviceStateInfo[] newArray(int size) { |
| 159 |
return new DeviceStateInfo[size]; |
| 160 |
} |
| 161 |
}; |
| 162 |
} |