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 package com.android.settings.testutils.shadow; 18 19 import android.content.ContentResolver; 20 import android.provider.Settings; 21 import com.google.common.collect.HashBasedTable; 22 import com.google.common.collect.Table; 23 import java.util.Map; 24 import java.util.WeakHashMap; 25 import org.robolectric.annotation.Implementation; 26 import org.robolectric.annotation.Implements; 27 28 @Implements(Settings.Secure.class) 29 public class ShadowSecureSettings { 30 31 private static final Map<ContentResolver, Table<Integer, String, Object>> sUserDataMap = 32 new WeakHashMap<>(); 33 34 @Implementation 35 public static boolean putStringForUser(ContentResolver resolver, String name, String value, 36 int userHandle) { 37 final Table<Integer, String, Object> userTable = getUserTable(resolver); 38 synchronized (userTable) { 39 if (value != null) { 40 userTable.put(userHandle, name, value); 41 } else { 42 userTable.remove(userHandle, name); 43 } 44 return true; 45 } 46 } 47 48 @Implementation 49 public static String getStringForUser(ContentResolver resolver, String name, int userHandle) { 50 final Table<Integer, String, Object> userTable = getUserTable(resolver); 51 synchronized (userTable) { 52 return (String) userTable.get(userHandle, name); 53 } 54 } 55 56 @Implementation 57 public static boolean putIntForUser(ContentResolver resolver, String name, int value, 58 int userHandle) { 59 final Table<Integer, String, Object> userTable = getUserTable(resolver); 60 synchronized (userTable) { 61 userTable.put(userHandle, name, value); 62 return true; 63 } 64 } 65 66 @Implementation 67 public static int getIntForUser(ContentResolver resolver, String name, int def, 68 int userHandle) { 69 final Table<Integer, String, Object> userTable = getUserTable(resolver); 70 synchronized (userTable) { 71 final Object object = userTable.get(userHandle, name); 72 return object instanceof Integer ? (Integer) object : def; 73 } 74 } 75 76 @Implementation 77 public static boolean putLongForUser(ContentResolver resolver, String name, long value, 78 int userHandle) { 79 final Table<Integer, String, Object> userTable = getUserTable(resolver); 80 synchronized (userTable) { 81 userTable.put(userHandle, name, value); 82 return true; 83 } 84 } 85 86 @Implementation 87 public static long getLongForUser(ContentResolver resolver, String name, long def, 88 int userHandle) { 89 final Table<Integer, String, Object> userTable = getUserTable(resolver); 90 synchronized (userTable) { 91 final Object object = userTable.get(userHandle, name); 92 return object instanceof Long ? (Long) object : def; 93 } 94 } 95 96 @Implementation 97 public static boolean putFloatForUser( 98 ContentResolver resolver, String name, float value, int userHandle) { 99 final Table<Integer, String, Object> userTable = getUserTable(resolver); 100 synchronized (userTable) { 101 userTable.put(userHandle, name, value); 102 return true; 103 } 104 } 105 106 @Implementation 107 public static float getFloatForUser( 108 ContentResolver resolver, String name, float def, int userHandle) { 109 final Table<Integer, String, Object> userTable = getUserTable(resolver); 110 synchronized (userTable) { 111 final Object object = userTable.get(userHandle, name); 112 return object instanceof Float ? (Float) object : def; 113 } 114 } 115 116 public static void clear() { 117 synchronized (sUserDataMap) { 118 sUserDataMap.clear(); 119 } 120 } 121 122 private static Table<Integer, String, Object> getUserTable(ContentResolver contentResolver) { 123 synchronized (sUserDataMap) { 124 Table<Integer, String, Object> table = sUserDataMap.get(contentResolver); 125 if (table == null) { 126 table = HashBasedTable.create(); 127 sUserDataMap.put(contentResolver, table); 128 } 129 return table; 130 } 131 } 132 } 133