Skip to content

Commit

Permalink
android/tv-casting-app: Adding ConfigurationManager as AppParameter (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon authored and pull[bot] committed Jul 26, 2023
1 parent 4320ec9 commit 4123138
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.chip.casting.DiscoveredNodeData;
import com.chip.casting.TvCastingApp;
import com.chip.casting.util.GlobalCastingConstants;
import com.chip.casting.util.PreferencesConfigurationManager;
import java.util.Random;

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

AppParameters appParameters = new AppParameters();
appParameters.setConfigurationManager(
new PreferencesConfigurationManager(
this.getApplicationContext(), "chip.platform.ConfigurationManager"));
byte[] rotatingDeviceIdUniqueId =
new byte[AppParameters.MIN_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH];
new Random().nextBytes(rotatingDeviceIdUniqueId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright (c) 2021-2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.chip.casting.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import chip.platform.AndroidChipPlatformException;
import chip.platform.ConfigurationManager;
import chip.platform.KeyValueStoreManager;
import java.util.Base64;
import java.util.Map;
import java.util.UUID;

/** Java interface for ConfigurationManager */
public class PreferencesConfigurationManager implements ConfigurationManager {

private final String TAG = KeyValueStoreManager.class.getSimpleName();
private SharedPreferences preferences;

public PreferencesConfigurationManager(Context context, String preferenceFileKey) {
preferences = context.getSharedPreferences(preferenceFileKey, Context.MODE_PRIVATE);

try {
String keyUniqueId = getKey(kConfigNamespace_ChipFactory, kConfigKey_UniqueId);
if (!preferences.contains(keyUniqueId)) {
preferences
.edit()
.putString(keyUniqueId, UUID.randomUUID().toString().replaceAll("-", ""))
.apply();
}
} catch (AndroidChipPlatformException e) {
e.printStackTrace();
}
}

@Override
public long readConfigValueLong(String namespace, String name)
throws AndroidChipPlatformException {
String key = getKey(namespace, name);
if (preferences.contains(key)) {
long value = preferences.getLong(key, Long.MAX_VALUE);
return value;
} else {
Log.d(TAG, "Key '" + key + "' not found in shared preferences");
throw new AndroidChipPlatformException();
}
}

@Override
public String readConfigValueStr(String namespace, String name)
throws AndroidChipPlatformException {
String key = getKey(namespace, name);
if (preferences.contains(key)) {
String value = preferences.getString(key, null);
return value;
} else {
Log.d(TAG, "Key '" + key + "' not found in shared preferences");
throw new AndroidChipPlatformException();
}
}

@Override
public byte[] readConfigValueBin(String namespace, String name)
throws AndroidChipPlatformException {
String key = getKey(namespace, name);
if (preferences.contains(key)) {
String value = preferences.getString(key, null);
byte[] byteValue = Base64.getDecoder().decode(value);
return byteValue;
} else {
Log.d(TAG, "Key '" + key + "' not found in shared preferences");
throw new AndroidChipPlatformException();
}
}

@Override
public void writeConfigValueLong(String namespace, String name, long val)
throws AndroidChipPlatformException {
String key = getKey(namespace, name);
preferences.edit().putLong(key, val).apply();
}

@Override
public void writeConfigValueStr(String namespace, String name, String val)
throws AndroidChipPlatformException {
String key = getKey(namespace, name);
preferences.edit().putString(key, val).apply();
}

@Override
public void writeConfigValueBin(String namespace, String name, byte[] val)
throws AndroidChipPlatformException {
String key = getKey(namespace, name);
if (val != null) {
String valStr = Base64.getEncoder().encodeToString(val);
preferences.edit().putString(key, valStr).apply();
} else {
preferences.edit().remove(key).apply();
}
}

@Override
public void clearConfigValue(String namespace, String name) throws AndroidChipPlatformException {
if (namespace != null && name != null) {
preferences.edit().remove(getKey(namespace, name)).apply();
} else if (namespace != null && name == null) {
String pre = getKey(namespace, null);
SharedPreferences.Editor editor = preferences.edit();
Map<String, ?> allEntries = preferences.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
String key = entry.getKey();
if (key.startsWith(pre)) {
editor.remove(key);
}
}
editor.apply();
} else if (namespace == null && name == null) {
preferences.edit().clear().apply();
}
}

@Override
public boolean configValueExists(String namespace, String name)
throws AndroidChipPlatformException {
return preferences.contains(getKey(namespace, name));
}

private String getKey(String namespace, String name) throws AndroidChipPlatformException {
if (namespace != null && name != null) {
return namespace + ":" + name;
} else if (namespace != null && name == null) {
return namespace + ":";
}

throw new AndroidChipPlatformException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.chip.casting;

import android.util.Log;
import chip.platform.ConfigurationManager;
import java.math.BigInteger;
import java.util.Arrays;

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

private ConfigurationManager configurationManager;
private byte[] rotatingDeviceIdUniqueId;
private DACProvider dacProvider = TEST_DAC_PROVIDER;
private String spake2pVerifierBase64;
Expand All @@ -36,6 +38,14 @@ public class AppParameters {
private int setupPasscode = TEST_SETUP_PASSCODE;
private int discriminator = TEST_DISCRIMINATOR;

public ConfigurationManager getConfigurationManager() {
return configurationManager;
}

public void setConfigurationManager(ConfigurationManager configurationManager) {
this.configurationManager = configurationManager;
}

public void setRotatingDeviceIdUniqueId(byte[] rotatingDeviceIdUniqueId) {
Log.d(
TAG,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import chip.platform.DiagnosticDataProviderImpl;
import chip.platform.NsdManagerServiceBrowser;
import chip.platform.NsdManagerServiceResolver;
import chip.platform.PreferencesConfigurationManager;
import chip.platform.PreferencesKeyValueStoreManager;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -62,7 +61,7 @@ public boolean initApp(Context applicationContext, AppParameters appParameters)
new AndroidChipPlatform(
new AndroidBleManager(),
new PreferencesKeyValueStoreManager(applicationContext),
new PreferencesConfigurationManager(applicationContext),
appParameters.getConfigurationManager(),
nsdManagerServiceResolver,
new NsdManagerServiceBrowser(applicationContext),
new ChipMdnsCallbackImpl(),
Expand Down

0 comments on commit 4123138

Please sign in to comment.