1 /* 2 * Copyright (C) 2016 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 18 package com.android.settings.search2; 19 20 import android.content.Context; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 import android.provider.Settings; 24 25 import java.util.Map; 26 27 /** 28 * Payload for inline Switch results. Mappings from integer to boolean. 29 */ 30 public class InlineSwitchPayload extends InlinePayload { 31 /** 32 * Maps Inline values to UI-consumable Values. 33 * For example, if you have a switch preference whose values are stored as ints, the two valid 34 * list of mappings would be: 35 * < (0,True), (1, false) > 36 * < (1,True), (0, false) > 37 */ 38 public final Map<Integer, Boolean> valueMap; 39 40 public InlineSwitchPayload(String newUri, @SettingsSource int settingsSource, 41 Map<Integer, Boolean> map) { 42 super(newUri, PayloadType.INLINE_SWITCH, settingsSource); 43 valueMap = map; 44 } 45 46 private InlineSwitchPayload(Parcel in) { 47 super(in.readString() /* Uri */ , in.readInt() /* Payload Type */, 48 in.readInt() /* Settings Source */); 49 valueMap = in.readHashMap(Integer.class.getClassLoader()); 50 } 51 52 @Override 53 public int getType() { 54 return inlineType; 55 } 56 57 @Override 58 public int describeContents() { 59 return 0; 60 } 61 62 @Override 63 public void writeToParcel(Parcel dest, int flags) { 64 dest.writeString(settingsUri); 65 dest.writeInt(inlineType); 66 dest.writeInt(settingSource); 67 dest.writeMap(valueMap); 68 } 69 70 public static final Parcelable.Creator<InlineSwitchPayload> CREATOR = 71 new Parcelable.Creator<InlineSwitchPayload>() { 72 @Override 73 public InlineSwitchPayload createFromParcel(Parcel in) { 74 return new InlineSwitchPayload(in); 75 } 76 77 @Override 78 public InlineSwitchPayload[] newArray(int size) { 79 return new InlineSwitchPayload[size]; 80 } 81 }; 82 83 public boolean getSwitchValue(Context context) { 84 if (valueMap == null) { 85 throw new IllegalStateException("Value map is null"); 86 } 87 88 int settingsValue = -1; 89 switch(settingSource) { 90 case SettingsSource.SECURE: 91 settingsValue = Settings.Secure.getInt(context.getContentResolver(), 92 settingsUri, 0); 93 break; 94 case SettingsSource.SYSTEM: 95 settingsValue = Settings.System.getInt(context.getContentResolver(), 96 settingsUri, 0); 97 break; 98 99 case SettingsSource.GLOBAL: 100 settingsValue = Settings.Global.getInt(context.getContentResolver(), 101 settingsUri, 0); 102 break; 103 } 104 105 if (settingsValue == -1) { 106 throw new IllegalStateException("Unable to find setting from uri: " 107 + settingsUri.toString()); 108 } 109 110 for (Integer key : valueMap.keySet()) { 111 if ((key == settingsValue)) { 112 return valueMap.get(key); 113 } 114 } 115 116 throw new IllegalStateException("No results matched the key: " + settingsValue); 117 } 118 119 public void setSwitchValue(Context context, boolean isChecked) { 120 if (valueMap == null) { 121 throw new IllegalStateException("Value map is null"); 122 } 123 int switchValue = -1; 124 125 for (Map.Entry<Integer, Boolean> pair : valueMap.entrySet()) { 126 if (pair.getValue() == isChecked) { 127 switchValue = pair.getKey(); 128 break; 129 } 130 } 131 132 if (switchValue == -1) { 133 throw new IllegalStateException("Switch value is not set"); 134 } 135 136 switch(settingSource) { 137 case SettingsSource.GLOBAL: 138 Settings.Global.putInt(context.getContentResolver(), settingsUri, switchValue); 139 return; 140 case SettingsSource.SECURE: 141 Settings.Secure.putInt(context.getContentResolver(), settingsUri, switchValue); 142 return; 143 case SettingsSource.SYSTEM: 144 Settings.System.putInt(context.getContentResolver(), settingsUri, switchValue); 145 return; 146 case SettingsSource.UNKNOWN: 147 return; 148 } 149 } 150 } 151