|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-2022 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | +package com.chip.casting.util; |
| 19 | + |
| 20 | +import android.content.Context; |
| 21 | +import android.content.SharedPreferences; |
| 22 | +import android.util.Log; |
| 23 | +import chip.platform.AndroidChipPlatformException; |
| 24 | +import chip.platform.ConfigurationManager; |
| 25 | +import chip.platform.KeyValueStoreManager; |
| 26 | +import java.util.Base64; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.UUID; |
| 29 | + |
| 30 | +/** Java interface for ConfigurationManager */ |
| 31 | +public class PreferencesConfigurationManager implements ConfigurationManager { |
| 32 | + |
| 33 | + private final String TAG = KeyValueStoreManager.class.getSimpleName(); |
| 34 | + private SharedPreferences preferences; |
| 35 | + |
| 36 | + public PreferencesConfigurationManager(Context context, String preferenceFileKey) { |
| 37 | + preferences = context.getSharedPreferences(preferenceFileKey, Context.MODE_PRIVATE); |
| 38 | + |
| 39 | + try { |
| 40 | + String keyUniqueId = getKey(kConfigNamespace_ChipFactory, kConfigKey_UniqueId); |
| 41 | + if (!preferences.contains(keyUniqueId)) { |
| 42 | + preferences |
| 43 | + .edit() |
| 44 | + .putString(keyUniqueId, UUID.randomUUID().toString().replaceAll("-", "")) |
| 45 | + .apply(); |
| 46 | + } |
| 47 | + } catch (AndroidChipPlatformException e) { |
| 48 | + e.printStackTrace(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public long readConfigValueLong(String namespace, String name) |
| 54 | + throws AndroidChipPlatformException { |
| 55 | + String key = getKey(namespace, name); |
| 56 | + if (preferences.contains(key)) { |
| 57 | + long value = preferences.getLong(key, Long.MAX_VALUE); |
| 58 | + return value; |
| 59 | + } else { |
| 60 | + Log.d(TAG, "Key '" + key + "' not found in shared preferences"); |
| 61 | + throw new AndroidChipPlatformException(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String readConfigValueStr(String namespace, String name) |
| 67 | + throws AndroidChipPlatformException { |
| 68 | + String key = getKey(namespace, name); |
| 69 | + if (preferences.contains(key)) { |
| 70 | + String value = preferences.getString(key, null); |
| 71 | + return value; |
| 72 | + } else { |
| 73 | + Log.d(TAG, "Key '" + key + "' not found in shared preferences"); |
| 74 | + throw new AndroidChipPlatformException(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public byte[] readConfigValueBin(String namespace, String name) |
| 80 | + throws AndroidChipPlatformException { |
| 81 | + String key = getKey(namespace, name); |
| 82 | + if (preferences.contains(key)) { |
| 83 | + String value = preferences.getString(key, null); |
| 84 | + byte[] byteValue = Base64.getDecoder().decode(value); |
| 85 | + return byteValue; |
| 86 | + } else { |
| 87 | + Log.d(TAG, "Key '" + key + "' not found in shared preferences"); |
| 88 | + throw new AndroidChipPlatformException(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void writeConfigValueLong(String namespace, String name, long val) |
| 94 | + throws AndroidChipPlatformException { |
| 95 | + String key = getKey(namespace, name); |
| 96 | + preferences.edit().putLong(key, val).apply(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void writeConfigValueStr(String namespace, String name, String val) |
| 101 | + throws AndroidChipPlatformException { |
| 102 | + String key = getKey(namespace, name); |
| 103 | + preferences.edit().putString(key, val).apply(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void writeConfigValueBin(String namespace, String name, byte[] val) |
| 108 | + throws AndroidChipPlatformException { |
| 109 | + String key = getKey(namespace, name); |
| 110 | + if (val != null) { |
| 111 | + String valStr = Base64.getEncoder().encodeToString(val); |
| 112 | + preferences.edit().putString(key, valStr).apply(); |
| 113 | + } else { |
| 114 | + preferences.edit().remove(key).apply(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void clearConfigValue(String namespace, String name) throws AndroidChipPlatformException { |
| 120 | + if (namespace != null && name != null) { |
| 121 | + preferences.edit().remove(getKey(namespace, name)).apply(); |
| 122 | + } else if (namespace != null && name == null) { |
| 123 | + String pre = getKey(namespace, null); |
| 124 | + SharedPreferences.Editor editor = preferences.edit(); |
| 125 | + Map<String, ?> allEntries = preferences.getAll(); |
| 126 | + for (Map.Entry<String, ?> entry : allEntries.entrySet()) { |
| 127 | + String key = entry.getKey(); |
| 128 | + if (key.startsWith(pre)) { |
| 129 | + editor.remove(key); |
| 130 | + } |
| 131 | + } |
| 132 | + editor.apply(); |
| 133 | + } else if (namespace == null && name == null) { |
| 134 | + preferences.edit().clear().apply(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public boolean configValueExists(String namespace, String name) |
| 140 | + throws AndroidChipPlatformException { |
| 141 | + return preferences.contains(getKey(namespace, name)); |
| 142 | + } |
| 143 | + |
| 144 | + private String getKey(String namespace, String name) throws AndroidChipPlatformException { |
| 145 | + if (namespace != null && name != null) { |
| 146 | + return namespace + ":" + name; |
| 147 | + } else if (namespace != null && name == null) { |
| 148 | + return namespace + ":"; |
| 149 | + } |
| 150 | + |
| 151 | + throw new AndroidChipPlatformException(); |
| 152 | + } |
| 153 | +} |
0 commit comments