| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.time; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.StringDef; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.os.Bundle; |
| 24 |
import android.os.Parcel; |
| 25 |
import android.os.Parcelable; |
| 26 |
|
| 27 |
import java.lang.annotation.Retention; |
| 28 |
import java.lang.annotation.RetentionPolicy; |
| 29 |
import java.util.Objects; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
@SystemApi |
| 47 |
public final class TimeZoneConfiguration implements Parcelable { |
| 48 |
|
| 49 |
public static final @NonNull Creator<TimeZoneConfiguration> CREATOR = |
| 50 |
new Creator<TimeZoneConfiguration>() { |
| 51 |
public TimeZoneConfiguration createFromParcel(Parcel in) { |
| 52 |
return TimeZoneConfiguration.createFromParcel(in); |
| 53 |
} |
| 54 |
|
| 55 |
public TimeZoneConfiguration[] newArray(int size) { |
| 56 |
return new TimeZoneConfiguration[size]; |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
@StringDef({ SETTING_AUTO_DETECTION_ENABLED, SETTING_GEO_DETECTION_ENABLED }) |
| 66 |
@Retention(RetentionPolicy.SOURCE) |
| 67 |
@interface Setting {} |
| 68 |
|
| 69 |
|
| 70 |
@Setting |
| 71 |
private static final String SETTING_AUTO_DETECTION_ENABLED = "autoDetectionEnabled"; |
| 72 |
|
| 73 |
|
| 74 |
@Setting |
| 75 |
private static final String SETTING_GEO_DETECTION_ENABLED = "geoDetectionEnabled"; |
| 76 |
|
| 77 |
@NonNull private final Bundle mBundle; |
| 78 |
|
| 79 |
private TimeZoneConfiguration(Builder builder) { |
| 80 |
this.mBundle = Objects.requireNonNull(builder.mBundle); |
| 81 |
} |
| 82 |
|
| 83 |
private static TimeZoneConfiguration createFromParcel(Parcel in) { |
| 84 |
return new TimeZoneConfiguration.Builder() |
| 85 |
.setPropertyBundleInternal(in.readBundle()) |
| 86 |
.build(); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 91 |
dest.writeBundle(mBundle); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public boolean isComplete() { |
| 100 |
return hasIsAutoDetectionEnabled() |
| 101 |
&& hasIsGeoDetectionEnabled(); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public boolean isAutoDetectionEnabled() { |
| 115 |
enforceSettingPresent(SETTING_AUTO_DETECTION_ENABLED); |
| 116 |
return mBundle.getBoolean(SETTING_AUTO_DETECTION_ENABLED); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public boolean hasIsAutoDetectionEnabled() { |
| 125 |
return mBundle.containsKey(SETTING_AUTO_DETECTION_ENABLED); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public boolean isGeoDetectionEnabled() { |
| 140 |
enforceSettingPresent(SETTING_GEO_DETECTION_ENABLED); |
| 141 |
return mBundle.getBoolean(SETTING_GEO_DETECTION_ENABLED); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public boolean hasIsGeoDetectionEnabled() { |
| 150 |
return mBundle.containsKey(SETTING_GEO_DETECTION_ENABLED); |
| 151 |
} |
| 152 |
|
| 153 |
@Override |
| 154 |
public int describeContents() { |
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public boolean equals(@Nullable Object o) { |
| 160 |
if (this == o) { |
| 161 |
return true; |
| 162 |
} |
| 163 |
if (o == null || getClass() != o.getClass()) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
TimeZoneConfiguration that = (TimeZoneConfiguration) o; |
| 167 |
return mBundle.kindofEquals(that.mBundle); |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public int hashCode() { |
| 172 |
return Objects.hash(mBundle); |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public String toString() { |
| 177 |
return "TimeZoneConfiguration{" |
| 178 |
+ "mBundle=" + mBundle |
| 179 |
+ '}'; |
| 180 |
} |
| 181 |
|
| 182 |
private void enforceSettingPresent(@Setting String setting) { |
| 183 |
if (!mBundle.containsKey(setting)) { |
| 184 |
throw new IllegalStateException(setting + " is not set"); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
@SystemApi |
| 194 |
public static final class Builder { |
| 195 |
|
| 196 |
private final Bundle mBundle = new Bundle(); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public Builder() { |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public Builder(@NonNull TimeZoneConfiguration toCopy) { |
| 208 |
mergeProperties(toCopy); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
@NonNull |
| 218 |
public Builder mergeProperties(@NonNull TimeZoneConfiguration other) { |
| 219 |
this.mBundle.putAll(other.mBundle); |
| 220 |
return this; |
| 221 |
} |
| 222 |
|
| 223 |
@NonNull |
| 224 |
Builder setPropertyBundleInternal(@NonNull Bundle bundle) { |
| 225 |
this.mBundle.putAll(bundle); |
| 226 |
return this; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
@NonNull |
| 233 |
public Builder setAutoDetectionEnabled(boolean enabled) { |
| 234 |
this.mBundle.putBoolean(SETTING_AUTO_DETECTION_ENABLED, enabled); |
| 235 |
return this; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
@NonNull |
| 242 |
public Builder setGeoDetectionEnabled(boolean enabled) { |
| 243 |
this.mBundle.putBoolean(SETTING_GEO_DETECTION_ENABLED, enabled); |
| 244 |
return this; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
@NonNull |
| 249 |
public TimeZoneConfiguration build() { |
| 250 |
return new TimeZoneConfiguration(this); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|