| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.compat; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.content.pm.PackageInfo; |
| 23 |
import android.os.Parcel; |
| 24 |
|
| 25 |
import java.lang.annotation.Retention; |
| 26 |
import java.lang.annotation.RetentionPolicy; |
| 27 |
import java.util.Objects; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
@SystemApi |
| 39 |
public final class PackageOverride { |
| 40 |
|
| 41 |
|
| 42 |
@IntDef({ |
| 43 |
VALUE_UNDEFINED, |
| 44 |
VALUE_ENABLED, |
| 45 |
VALUE_DISABLED |
| 46 |
}) |
| 47 |
@Retention(RetentionPolicy.SOURCE) |
| 48 |
public @interface EvaluatedOverride { |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static final int VALUE_UNDEFINED = 0; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static final int VALUE_ENABLED = 1; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static final int VALUE_DISABLED = 2; |
| 69 |
|
| 70 |
private final long mMinVersionCode; |
| 71 |
private final long mMaxVersionCode; |
| 72 |
private final boolean mEnabled; |
| 73 |
|
| 74 |
private PackageOverride(long minVersionCode, |
| 75 |
long maxVersionCode, |
| 76 |
boolean enabled) { |
| 77 |
this.mMinVersionCode = minVersionCode; |
| 78 |
this.mMaxVersionCode = maxVersionCode; |
| 79 |
this.mEnabled = enabled; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public @EvaluatedOverride int evaluate(long versionCode) { |
| 88 |
if (versionCode >= mMinVersionCode && versionCode <= mMaxVersionCode) { |
| 89 |
return mEnabled ? VALUE_ENABLED : VALUE_DISABLED; |
| 90 |
} |
| 91 |
return VALUE_UNDEFINED; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public int evaluateForAllVersions() { |
| 100 |
if (mMinVersionCode == Long.MIN_VALUE && mMaxVersionCode == Long.MAX_VALUE) { |
| 101 |
return mEnabled ? VALUE_ENABLED : VALUE_DISABLED; |
| 102 |
} |
| 103 |
return VALUE_UNDEFINED; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public long getMinVersionCode() { |
| 112 |
return mMinVersionCode; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public long getMaxVersionCode() { |
| 121 |
return mMaxVersionCode; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
public boolean isEnabled() { |
| 126 |
return mEnabled; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
public void writeToParcel(Parcel dest) { |
| 131 |
dest.writeLong(mMinVersionCode); |
| 132 |
dest.writeLong(mMaxVersionCode); |
| 133 |
dest.writeBoolean(mEnabled); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
public static PackageOverride createFromParcel(Parcel in) { |
| 138 |
return new PackageOverride(in.readLong(), in.readLong(), in.readBoolean()); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
@Override |
| 143 |
public boolean equals(Object o) { |
| 144 |
if (this == o) return true; |
| 145 |
if (o == null || getClass() != o.getClass()) return false; |
| 146 |
PackageOverride that = (PackageOverride) o; |
| 147 |
return mMinVersionCode == that.mMinVersionCode && mMaxVersionCode == that.mMaxVersionCode |
| 148 |
&& mEnabled == that.mEnabled; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
@Override |
| 153 |
public int hashCode() { |
| 154 |
return Objects.hash(mMinVersionCode, mMaxVersionCode, mEnabled); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
@Override |
| 159 |
public String toString() { |
| 160 |
if (mMinVersionCode == Long.MIN_VALUE && mMaxVersionCode == Long.MAX_VALUE) { |
| 161 |
return Boolean.toString(mEnabled); |
| 162 |
} |
| 163 |
return String.format("[%d,%d,%b]", mMinVersionCode, mMaxVersionCode, mEnabled); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public static final class Builder { |
| 170 |
private long mMinVersionCode = Long.MIN_VALUE; |
| 171 |
private long mMaxVersionCode = Long.MAX_VALUE; |
| 172 |
private boolean mEnabled; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
@NonNull |
| 182 |
public Builder setMinVersionCode(long minVersionCode) { |
| 183 |
mMinVersionCode = minVersionCode; |
| 184 |
return this; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
@NonNull |
| 195 |
public Builder setMaxVersionCode(long maxVersionCode) { |
| 196 |
mMaxVersionCode = maxVersionCode; |
| 197 |
return this; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
@NonNull |
| 206 |
public Builder setEnabled(boolean enabled) { |
| 207 |
mEnabled = enabled; |
| 208 |
return this; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
@NonNull |
| 218 |
public PackageOverride build() { |
| 219 |
if (mMinVersionCode > mMaxVersionCode) { |
| 220 |
throw new IllegalArgumentException("minVersionCode must not be larger than " |
| 221 |
+ "maxVersionCode"); |
| 222 |
} |
| 223 |
return new PackageOverride(mMinVersionCode, mMaxVersionCode, mEnabled); |
| 224 |
} |
| 225 |
}; |
| 226 |
} |