Skip to content

Commit 1805612

Browse files
sharadb-amazonpull[bot]
authored andcommitted
android/tv-casting-app: Adding ConfigurationManager as AppParameter (#143) (#25976)
1 parent 94677ee commit 1805612

File tree

4 files changed

+168
-2
lines changed

4 files changed

+168
-2
lines changed

examples/tv-casting-app/android/App/app/src/main/java/com/chip/casting/app/MainActivity.java

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.chip.casting.DiscoveredNodeData;
1212
import com.chip.casting.TvCastingApp;
1313
import com.chip.casting.util.GlobalCastingConstants;
14+
import com.chip.casting.util.PreferencesConfigurationManager;
1415
import java.util.Random;
1516

1617
public class MainActivity extends AppCompatActivity
@@ -81,6 +82,9 @@ private boolean initJni() {
8182
Context applicationContext = this.getApplicationContext();
8283

8384
AppParameters appParameters = new AppParameters();
85+
appParameters.setConfigurationManager(
86+
new PreferencesConfigurationManager(
87+
this.getApplicationContext(), "chip.platform.ConfigurationManager"));
8488
byte[] rotatingDeviceIdUniqueId =
8589
new byte[AppParameters.MIN_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH];
8690
new Random().nextBytes(rotatingDeviceIdUniqueId);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}

examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/AppParameters.java

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.chip.casting;
1919

2020
import android.util.Log;
21+
import chip.platform.ConfigurationManager;
2122
import java.math.BigInteger;
2223
import java.util.Arrays;
2324

@@ -28,6 +29,7 @@ public class AppParameters {
2829
private static final int TEST_DISCRIMINATOR = 0xF00;
2930
private DACProvider TEST_DAC_PROVIDER = new DACProviderStub();
3031

32+
private ConfigurationManager configurationManager;
3133
private byte[] rotatingDeviceIdUniqueId;
3234
private DACProvider dacProvider = TEST_DAC_PROVIDER;
3335
private String spake2pVerifierBase64;
@@ -36,6 +38,14 @@ public class AppParameters {
3638
private int setupPasscode = TEST_SETUP_PASSCODE;
3739
private int discriminator = TEST_DISCRIMINATOR;
3840

41+
public ConfigurationManager getConfigurationManager() {
42+
return configurationManager;
43+
}
44+
45+
public void setConfigurationManager(ConfigurationManager configurationManager) {
46+
this.configurationManager = configurationManager;
47+
}
48+
3949
public void setRotatingDeviceIdUniqueId(byte[] rotatingDeviceIdUniqueId) {
4050
Log.d(
4151
TAG,

examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/TvCastingApp.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import chip.platform.DiagnosticDataProviderImpl;
2929
import chip.platform.NsdManagerServiceBrowser;
3030
import chip.platform.NsdManagerServiceResolver;
31-
import chip.platform.PreferencesConfigurationManager;
3231
import chip.platform.PreferencesKeyValueStoreManager;
3332
import java.util.Arrays;
3433
import java.util.List;
@@ -62,7 +61,7 @@ public boolean initApp(Context applicationContext, AppParameters appParameters)
6261
new AndroidChipPlatform(
6362
new AndroidBleManager(),
6463
new PreferencesKeyValueStoreManager(applicationContext),
65-
new PreferencesConfigurationManager(applicationContext),
64+
appParameters.getConfigurationManager(),
6665
nsdManagerServiceResolver,
6766
new NsdManagerServiceBrowser(applicationContext),
6867
new ChipMdnsCallbackImpl(),

0 commit comments

Comments
 (0)