The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 android.content; |
| 18 | |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 22 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
Jeff Sharkey | 32b947e | 2018-08-08 13:42:25 -0600 | [diff] [blame] | 25 | import com.android.internal.util.Preconditions; |
| 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.HashMap; |
| 29 | import java.util.Map; |
Jeff Sharkey | 32b947e | 2018-08-08 13:42:25 -0600 | [diff] [blame] | 30 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | |
| 33 | /** |
| 34 | * This class is used to store a set of values that the {@link ContentResolver} |
| 35 | * can process. |
| 36 | */ |
| 37 | public final class ContentValues implements Parcelable { |
| 38 | public static final String TAG = "ContentValues"; |
| 39 | |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 40 | /** |
| 41 | * @hide |
| 42 | * @deprecated kept around for lame people doing reflection |
| 43 | */ |
| 44 | @Deprecated |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 45 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | private HashMap<String, Object> mValues; |
| 47 | |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 48 | private final ArrayMap<String, Object> mMap; |
| 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | /** |
| 51 | * Creates an empty set of values using the default initial size |
| 52 | */ |
| 53 | public ContentValues() { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 54 | mMap = new ArrayMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates an empty set of values using the given initial size |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 59 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | * @param size the initial size of the set of values |
| 61 | */ |
| 62 | public ContentValues(int size) { |
Jeff Sharkey | 32b947e | 2018-08-08 13:42:25 -0600 | [diff] [blame] | 63 | Preconditions.checkArgumentNonnegative(size); |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 64 | mMap = new ArrayMap<>(size); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Creates a set of values copied from the given set |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 69 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | * @param from the values to copy |
| 71 | */ |
| 72 | public ContentValues(ContentValues from) { |
Jeff Sharkey | 32b947e | 2018-08-08 13:42:25 -0600 | [diff] [blame] | 73 | Objects.requireNonNull(from); |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 74 | mMap = new ArrayMap<>(from.mMap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 78 | * @hide |
| 79 | * @deprecated kept around for lame people doing reflection |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | */ |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 81 | @Deprecated |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 82 | @UnsupportedAppUsage |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 83 | private ContentValues(HashMap<String, Object> from) { |
| 84 | mMap = new ArrayMap<>(); |
| 85 | mMap.putAll(from); |
| 86 | } |
| 87 | |
| 88 | /** {@hide} */ |
| 89 | private ContentValues(Parcel in) { |
| 90 | mMap = new ArrayMap<>(in.readInt()); |
| 91 | in.readArrayMap(mMap, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public boolean equals(Object object) { |
| 96 | if (!(object instanceof ContentValues)) { |
| 97 | return false; |
| 98 | } |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 99 | return mMap.equals(((ContentValues) object).mMap); |
| 100 | } |
| 101 | |
| 102 | /** {@hide} */ |
| 103 | public ArrayMap<String, Object> getValues() { |
| 104 | return mMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int hashCode() { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 109 | return mMap.hashCode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Adds a value to the set. |
| 114 | * |
| 115 | * @param key the name of the value to put |
| 116 | * @param value the data for the value to put |
| 117 | */ |
| 118 | public void put(String key, String value) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 119 | mMap.put(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Adds all values from the passed in ContentValues. |
| 124 | * |
| 125 | * @param other the ContentValues from which to copy |
| 126 | */ |
| 127 | public void putAll(ContentValues other) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 128 | mMap.putAll(other.mMap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Adds a value to the set. |
| 133 | * |
| 134 | * @param key the name of the value to put |
| 135 | * @param value the data for the value to put |
| 136 | */ |
| 137 | public void put(String key, Byte value) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 138 | mMap.put(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Adds a value to the set. |
| 143 | * |
| 144 | * @param key the name of the value to put |
| 145 | * @param value the data for the value to put |
| 146 | */ |
| 147 | public void put(String key, Short value) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 148 | mMap.put(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Adds a value to the set. |
| 153 | * |
| 154 | * @param key the name of the value to put |
| 155 | * @param value the data for the value to put |
| 156 | */ |
| 157 | public void put(String key, Integer value) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 158 | mMap.put(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Adds a value to the set. |
| 163 | * |
| 164 | * @param key the name of the value to put |
| 165 | * @param value the data for the value to put |
| 166 | */ |
| 167 | public void put(String key, Long value) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 168 | mMap.put(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Adds a value to the set. |
| 173 | * |
| 174 | * @param key the name of the value to put |
| 175 | * @param value the data for the value to put |
| 176 | */ |
| 177 | public void put(String key, Float value) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 178 | mMap.put(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Adds a value to the set. |
| 183 | * |
| 184 | * @param key the name of the value to put |
| 185 | * @param value the data for the value to put |
| 186 | */ |
| 187 | public void put(String key, Double value) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 188 | mMap.put(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Adds a value to the set. |
| 193 | * |
| 194 | * @param key the name of the value to put |
| 195 | * @param value the data for the value to put |
| 196 | */ |
| 197 | public void put(String key, Boolean value) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 198 | mMap.put(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | /** |
| 202 | * Adds a value to the set. |
| 203 | * |
| 204 | * @param key the name of the value to put |
| 205 | * @param value the data for the value to put |
| 206 | */ |
| 207 | public void put(String key, byte[] value) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 208 | mMap.put(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Adds a null value to the set. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 213 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | * @param key the name of the value to make null |
| 215 | */ |
| 216 | public void putNull(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 217 | mMap.put(key, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Returns the number of values. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 222 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | * @return the number of values |
| 224 | */ |
| 225 | public int size() { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 226 | return mMap.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
Mike Tsao | c74ee2f | 2017-03-24 14:56:34 -0700 | [diff] [blame] | 230 | * Indicates whether this collection is empty. |
| 231 | * |
| 232 | * @return true iff size == 0 |
| 233 | * {@hide} |
| 234 | * TODO: consider exposing this new method publicly |
| 235 | */ |
| 236 | public boolean isEmpty() { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 237 | return mMap.isEmpty(); |
Mike Tsao | c74ee2f | 2017-03-24 14:56:34 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | * Remove a single value. |
| 242 | * |
| 243 | * @param key the name of the value to remove |
| 244 | */ |
| 245 | public void remove(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 246 | mMap.remove(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Removes all values. |
| 251 | */ |
| 252 | public void clear() { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 253 | mMap.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Returns true if this object has the named value. |
| 258 | * |
| 259 | * @param key the value to check for |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 260 | * @return {@code true} if the value is present, {@code false} otherwise |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | */ |
| 262 | public boolean containsKey(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 263 | return mMap.containsKey(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
Steve Pomeroy | 1276b5f | 2012-10-09 15:37:34 -0400 | [diff] [blame] | 267 | * Gets a value. Valid value types are {@link String}, {@link Boolean}, |
| 268 | * {@link Number}, and {@code byte[]} implementations. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | * |
| 270 | * @param key the value to get |
Steve Pomeroy | 1276b5f | 2012-10-09 15:37:34 -0400 | [diff] [blame] | 271 | * @return the data for the value, or {@code null} if the value is missing or if {@code null} |
| 272 | * was previously added with the given {@code key} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | */ |
| 274 | public Object get(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 275 | return mMap.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Gets a value and converts it to a String. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 280 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | * @param key the value to get |
| 282 | * @return the String for the value |
| 283 | */ |
| 284 | public String getAsString(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 285 | Object value = mMap.get(key); |
Daniel Lehmann | 1c4027b | 2010-03-22 20:35:02 -0700 | [diff] [blame] | 286 | return value != null ? value.toString() : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Gets a value and converts it to a Long. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 291 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | * @param key the value to get |
Steve Pomeroy | 1276b5f | 2012-10-09 15:37:34 -0400 | [diff] [blame] | 293 | * @return the Long value, or {@code null} if the value is missing or cannot be converted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | */ |
| 295 | public Long getAsLong(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 296 | Object value = mMap.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | try { |
| 298 | return value != null ? ((Number) value).longValue() : null; |
| 299 | } catch (ClassCastException e) { |
| 300 | if (value instanceof CharSequence) { |
| 301 | try { |
| 302 | return Long.valueOf(value.toString()); |
| 303 | } catch (NumberFormatException e2) { |
| 304 | Log.e(TAG, "Cannot parse Long value for " + value + " at key " + key); |
| 305 | return null; |
| 306 | } |
| 307 | } else { |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 308 | Log.e(TAG, "Cannot cast value for " + key + " to a Long: " + value, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | return null; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Gets a value and converts it to an Integer. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 316 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | * @param key the value to get |
Steve Pomeroy | 1276b5f | 2012-10-09 15:37:34 -0400 | [diff] [blame] | 318 | * @return the Integer value, or {@code null} if the value is missing or cannot be converted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | */ |
| 320 | public Integer getAsInteger(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 321 | Object value = mMap.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | try { |
| 323 | return value != null ? ((Number) value).intValue() : null; |
| 324 | } catch (ClassCastException e) { |
| 325 | if (value instanceof CharSequence) { |
| 326 | try { |
| 327 | return Integer.valueOf(value.toString()); |
| 328 | } catch (NumberFormatException e2) { |
| 329 | Log.e(TAG, "Cannot parse Integer value for " + value + " at key " + key); |
| 330 | return null; |
| 331 | } |
| 332 | } else { |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 333 | Log.e(TAG, "Cannot cast value for " + key + " to a Integer: " + value, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | return null; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Gets a value and converts it to a Short. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 341 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | * @param key the value to get |
Steve Pomeroy | 1276b5f | 2012-10-09 15:37:34 -0400 | [diff] [blame] | 343 | * @return the Short value, or {@code null} if the value is missing or cannot be converted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | */ |
| 345 | public Short getAsShort(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 346 | Object value = mMap.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | try { |
| 348 | return value != null ? ((Number) value).shortValue() : null; |
| 349 | } catch (ClassCastException e) { |
| 350 | if (value instanceof CharSequence) { |
| 351 | try { |
| 352 | return Short.valueOf(value.toString()); |
| 353 | } catch (NumberFormatException e2) { |
| 354 | Log.e(TAG, "Cannot parse Short value for " + value + " at key " + key); |
| 355 | return null; |
| 356 | } |
| 357 | } else { |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 358 | Log.e(TAG, "Cannot cast value for " + key + " to a Short: " + value, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | return null; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Gets a value and converts it to a Byte. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 366 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | * @param key the value to get |
Steve Pomeroy | 1276b5f | 2012-10-09 15:37:34 -0400 | [diff] [blame] | 368 | * @return the Byte value, or {@code null} if the value is missing or cannot be converted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | */ |
| 370 | public Byte getAsByte(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 371 | Object value = mMap.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | try { |
| 373 | return value != null ? ((Number) value).byteValue() : null; |
| 374 | } catch (ClassCastException e) { |
| 375 | if (value instanceof CharSequence) { |
| 376 | try { |
| 377 | return Byte.valueOf(value.toString()); |
| 378 | } catch (NumberFormatException e2) { |
| 379 | Log.e(TAG, "Cannot parse Byte value for " + value + " at key " + key); |
| 380 | return null; |
| 381 | } |
| 382 | } else { |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 383 | Log.e(TAG, "Cannot cast value for " + key + " to a Byte: " + value, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | return null; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Gets a value and converts it to a Double. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 391 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | * @param key the value to get |
Steve Pomeroy | 1276b5f | 2012-10-09 15:37:34 -0400 | [diff] [blame] | 393 | * @return the Double value, or {@code null} if the value is missing or cannot be converted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | */ |
| 395 | public Double getAsDouble(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 396 | Object value = mMap.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | try { |
| 398 | return value != null ? ((Number) value).doubleValue() : null; |
| 399 | } catch (ClassCastException e) { |
| 400 | if (value instanceof CharSequence) { |
| 401 | try { |
| 402 | return Double.valueOf(value.toString()); |
| 403 | } catch (NumberFormatException e2) { |
| 404 | Log.e(TAG, "Cannot parse Double value for " + value + " at key " + key); |
| 405 | return null; |
| 406 | } |
| 407 | } else { |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 408 | Log.e(TAG, "Cannot cast value for " + key + " to a Double: " + value, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | return null; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Gets a value and converts it to a Float. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 416 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | * @param key the value to get |
Steve Pomeroy | 1276b5f | 2012-10-09 15:37:34 -0400 | [diff] [blame] | 418 | * @return the Float value, or {@code null} if the value is missing or cannot be converted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | */ |
| 420 | public Float getAsFloat(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 421 | Object value = mMap.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | try { |
| 423 | return value != null ? ((Number) value).floatValue() : null; |
| 424 | } catch (ClassCastException e) { |
| 425 | if (value instanceof CharSequence) { |
| 426 | try { |
| 427 | return Float.valueOf(value.toString()); |
| 428 | } catch (NumberFormatException e2) { |
| 429 | Log.e(TAG, "Cannot parse Float value for " + value + " at key " + key); |
| 430 | return null; |
| 431 | } |
| 432 | } else { |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 433 | Log.e(TAG, "Cannot cast value for " + key + " to a Float: " + value, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | return null; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Gets a value and converts it to a Boolean. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 441 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | * @param key the value to get |
Steve Pomeroy | 1276b5f | 2012-10-09 15:37:34 -0400 | [diff] [blame] | 443 | * @return the Boolean value, or {@code null} if the value is missing or cannot be converted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | */ |
| 445 | public Boolean getAsBoolean(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 446 | Object value = mMap.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | try { |
| 448 | return (Boolean) value; |
| 449 | } catch (ClassCastException e) { |
| 450 | if (value instanceof CharSequence) { |
Michael Wright | 794cf19 | 2017-01-18 18:12:53 +0000 | [diff] [blame] | 451 | // Note that we also check against 1 here because SQLite's internal representation |
| 452 | // for booleans is an integer with a value of 0 or 1. Without this check, boolean |
| 453 | // values obtained via DatabaseUtils#cursorRowToContentValues will always return |
| 454 | // false. |
| 455 | return Boolean.valueOf(value.toString()) || "1".equals(value); |
Jeff Hamilton | d208c3a | 2010-09-17 08:12:19 -0500 | [diff] [blame] | 456 | } else if (value instanceof Number) { |
| 457 | return ((Number) value).intValue() != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | } else { |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 459 | Log.e(TAG, "Cannot cast value for " + key + " to a Boolean: " + value, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | return null; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Gets a value that is a byte array. Note that this method will not convert |
| 467 | * any other types to byte arrays. |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 468 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | * @param key the value to get |
Steve Pomeroy | 1276b5f | 2012-10-09 15:37:34 -0400 | [diff] [blame] | 470 | * @return the {@code byte[]} value, or {@code null} is the value is missing or not a |
| 471 | * {@code byte[]} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | */ |
| 473 | public byte[] getAsByteArray(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 474 | Object value = mMap.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | if (value instanceof byte[]) { |
| 476 | return (byte[]) value; |
| 477 | } else { |
| 478 | return null; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Returns a set of all of the keys and values |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 484 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | * @return a set of all of the keys and values |
| 486 | */ |
| 487 | public Set<Map.Entry<String, Object>> valueSet() { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 488 | return mMap.entrySet(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | } |
Fred Quintana | bda8874 | 2010-02-19 13:13:04 -0800 | [diff] [blame] | 490 | |
Vasu Nori | 532abb6 | 2010-08-02 16:45:47 -0700 | [diff] [blame] | 491 | /** |
| 492 | * Returns a set of all of the keys |
| 493 | * |
| 494 | * @return a set of all of the keys |
| 495 | */ |
| 496 | public Set<String> keySet() { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 497 | return mMap.keySet(); |
Vasu Nori | 532abb6 | 2010-08-02 16:45:47 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 500 | public static final @android.annotation.NonNull Parcelable.Creator<ContentValues> CREATOR = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | new Parcelable.Creator<ContentValues>() { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 502 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | public ContentValues createFromParcel(Parcel in) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 504 | return new ContentValues(in); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 507 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | public ContentValues[] newArray(int size) { |
| 509 | return new ContentValues[size]; |
| 510 | } |
| 511 | }; |
| 512 | |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 513 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | public int describeContents() { |
| 515 | return 0; |
| 516 | } |
| 517 | |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 518 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | public void writeToParcel(Parcel parcel, int flags) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 520 | parcel.writeInt(mMap.size()); |
| 521 | parcel.writeArrayMap(mMap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Unsupported, here until we get proper bulk insert APIs. |
| 526 | * {@hide} |
| 527 | */ |
| 528 | @Deprecated |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 529 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | public void putStringArrayList(String key, ArrayList<String> value) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 531 | mMap.put(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Unsupported, here until we get proper bulk insert APIs. |
| 536 | * {@hide} |
| 537 | */ |
| 538 | @SuppressWarnings("unchecked") |
| 539 | @Deprecated |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 540 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | public ArrayList<String> getStringArrayList(String key) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 542 | return (ArrayList<String>) mMap.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Jeff Hamilton | f924a51 | 2010-08-25 10:19:36 -0500 | [diff] [blame] | 545 | /** |
| 546 | * Returns a string containing a concise, human-readable description of this object. |
| 547 | * @return a printable representation of this object. |
| 548 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | @Override |
| 550 | public String toString() { |
| 551 | StringBuilder sb = new StringBuilder(); |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 552 | for (String name : mMap.keySet()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | String value = getAsString(name); |
| 554 | if (sb.length() > 0) sb.append(" "); |
| 555 | sb.append(name + "=" + value); |
| 556 | } |
| 557 | return sb.toString(); |
| 558 | } |
| 559 | } |