1 /* 2 * Copyright (C) 2021 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 17 package com.android.server.location.settings; 18 19 import android.content.res.Resources; 20 21 import com.android.internal.R; 22 23 import java.io.DataInput; 24 import java.io.DataOutput; 25 import java.io.IOException; 26 import java.util.Objects; 27 28 /** Holds the state of location user settings. */ 29 public final class LocationUserSettings implements SettingsStore.VersionedSettings { 30 31 // remember to bump this version code and add the appropriate upgrade logic whenever the format 32 // is changed. 33 private static final int VERSION = 1; 34 35 private final boolean mAdasGnssLocationEnabled; 36 LocationUserSettings(boolean adasGnssLocationEnabled)37 private LocationUserSettings(boolean adasGnssLocationEnabled) { 38 mAdasGnssLocationEnabled = adasGnssLocationEnabled; 39 } 40 41 @Override getVersion()42 public int getVersion() { 43 return VERSION; 44 } 45 isAdasGnssLocationEnabled()46 public boolean isAdasGnssLocationEnabled() { 47 return mAdasGnssLocationEnabled; 48 } 49 50 /** Returns an instance with ADAS GNSS location enabled state set as given. */ withAdasGnssLocationEnabled(boolean adasEnabled)51 public LocationUserSettings withAdasGnssLocationEnabled(boolean adasEnabled) { 52 if (adasEnabled == mAdasGnssLocationEnabled) { 53 return this; 54 } 55 56 return new LocationUserSettings(adasEnabled); 57 } 58 write(DataOutput out)59 void write(DataOutput out) throws IOException { 60 out.writeBoolean(mAdasGnssLocationEnabled); 61 } 62 read(Resources resources, int version, DataInput in)63 static LocationUserSettings read(Resources resources, int version, DataInput in) 64 throws IOException { 65 boolean adasGnssLocationEnabled; 66 67 // upgrade code goes here. remember to bump the version field when changing the format 68 switch (version) { 69 default: 70 // set all fields to defaults 71 adasGnssLocationEnabled = resources.getBoolean( 72 R.bool.config_defaultAdasGnssLocationEnabled); 73 break; 74 case 1: 75 adasGnssLocationEnabled = in.readBoolean(); 76 // fall through 77 } 78 79 return new LocationUserSettings(adasGnssLocationEnabled); 80 } 81 82 @Override equals(Object o)83 public boolean equals(Object o) { 84 if (this == o) { 85 return true; 86 } 87 if (!(o instanceof LocationUserSettings)) { 88 return false; 89 } 90 LocationUserSettings that = (LocationUserSettings) o; 91 return mAdasGnssLocationEnabled == that.mAdasGnssLocationEnabled; 92 } 93 94 @Override hashCode()95 public int hashCode() { 96 return Objects.hash(mAdasGnssLocationEnabled); 97 } 98 } 99