1 /* 2 * Copyright (C) 2017 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.search; 19 20 import android.content.Intent; 21 22 import android.content.Context; 23 import android.os.Parcel; 24 import android.provider.Settings; 25 import com.android.internal.annotations.VisibleForTesting; 26 27 /** 28 * Abstract Payload for inline settings results. 29 */ 30 public abstract class InlinePayload extends ResultPayload { 31 32 public static final int FALSE = 0; 33 public static final int TRUE = 1; 34 35 /** 36 * Defines the key to access and store the Setting the inline result represents. 37 */ 38 private final String mSettingKey; 39 40 /** 41 * Defines where the Setting is stored. 42 */ 43 @SettingsSource final int mSettingSource; 44 45 /** 46 * True when the setting is available for the device. 47 */ 48 final boolean mIsDeviceSupported; 49 50 /** 51 * The default value for the setting. 52 */ 53 final int mDefaultvalue; 54 55 /** 56 * @param key uniquely identifies the stored setting. 57 * @param source of the setting. Used to determine where to get and set the setting. 58 * @param intent to the setting page. 59 * @param isDeviceSupported is true when the setting is valid for the given device. 60 */ 61 public InlinePayload(String key, @SettingsSource int source, Intent intent, 62 boolean isDeviceSupported, int defaultValue) { 63 super(intent); 64 mSettingKey = key; 65 mSettingSource = source; 66 mIsDeviceSupported = isDeviceSupported; 67 mDefaultvalue = defaultValue; 68 } 69 70 InlinePayload(Parcel parcel) { 71 super(parcel.readParcelable(Intent.class.getClassLoader())); 72 mSettingKey = parcel.readString(); 73 mSettingSource = parcel.readInt(); 74 mIsDeviceSupported = parcel.readInt() == TRUE; 75 mDefaultvalue = parcel.readInt(); 76 } 77 78 @Override 79 public void writeToParcel(Parcel dest, int flags) { 80 super.writeToParcel(dest, flags); 81 dest.writeString(mSettingKey); 82 dest.writeInt(mSettingSource); 83 dest.writeInt(mIsDeviceSupported ? TRUE : FALSE); 84 dest.writeInt(mDefaultvalue); 85 } 86 87 @Override 88 @PayloadType public abstract int getType(); 89 90 /** 91 * @returns the status of the underlying setting. See {@link ResultPayload.Availability} for 92 * possible values. 93 */ 94 @Availability public int getAvailability() { 95 if (mIsDeviceSupported) { 96 return Availability.AVAILABLE; 97 } 98 return Availability.DISABLED_UNSUPPORTED; 99 } 100 101 /** 102 * Checks if the input is valid for the given setting. 103 * 104 * @param input The number to be get or set for the setting. 105 * @return {@param input} mapped to the public-facing API for settings. 106 * @throws IllegalArgumentException when the input is not valid for the given inline type. 107 */ 108 protected abstract int standardizeInput(int input) throws IllegalArgumentException; 109 110 /** 111 * @returns the current value of the setting. 112 */ 113 public int getValue(Context context) { 114 int settingsValue = -1; 115 switch(mSettingSource) { 116 case SettingsSource.SECURE: 117 settingsValue = Settings.Secure.getInt(context.getContentResolver(), 118 mSettingKey, mDefaultvalue); 119 break; 120 case SettingsSource.SYSTEM: 121 settingsValue = Settings.System.getInt(context.getContentResolver(), 122 mSettingKey, mDefaultvalue); 123 break; 124 125 case SettingsSource.GLOBAL: 126 settingsValue = Settings.Global.getInt(context.getContentResolver(), 127 mSettingKey, mDefaultvalue); 128 break; 129 } 130 131 return standardizeInput(settingsValue); 132 } 133 134 /** 135 * Attempts to set the setting value. 136 * 137 * @param newValue is the requested value for the setting. 138 * @returns true when the setting was changed, and false otherwise. 139 */ 140 public boolean setValue(Context context, int newValue) { 141 newValue = standardizeInput(newValue); 142 143 switch(mSettingSource) { 144 case SettingsSource.GLOBAL: 145 return Settings.Global.putInt(context.getContentResolver(), mSettingKey, newValue); 146 case SettingsSource.SECURE: 147 return Settings.Secure.putInt(context.getContentResolver(), mSettingKey, newValue); 148 case SettingsSource.SYSTEM: 149 return Settings.System.putInt(context.getContentResolver(), mSettingKey, newValue); 150 case SettingsSource.UNKNOWN: 151 return false; 152 } 153 154 return false; 155 } 156 157 public String getKey() { 158 return mSettingKey; 159 } 160 }