<commit_before>/*
 *   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 chip.platform;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
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 final String PREFERENCE_FILE_KEY = "chip.platform.ConfigurationManager";
  private SharedPreferences preferences;

  public PreferencesConfigurationManager(Context context) {
    preferences = context.getSharedPreferences(PREFERENCE_FILE_KEY, 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);

    switch (key) {
        /**
         * The unique id assigned by the device vendor to identify the product or device type. This
         * number is scoped to the device vendor id. return a different value than
         * src/include/platform/CHIPDeviceConfig.h for debug
         */
      case kConfigNamespace_ChipFactory + ":" + kConfigKey_ProductId:
        return 65278;

        /**
         * The default hardware version number assigned to the device or product by the device
         * vendor.
         *
         * <p>Hardware versions are specific to a particular device vendor and product id, and
         * typically correspond to a revision of the physical device, a change to its packaging,
         * and/or a change to its marketing presentation. This value is generally *not* incremented
         * for device software revisions.
         *
         * <p>This is a default value which is used when a hardware version has not been stored in
         * device persistent storage (e.g. by a factory provisioning process).
         *
         * <p>return a different value than src/include/platform/CHIPDeviceConfig.h for debug
         */
      case kConfigNamespace_ChipFactory + ":" + kConfigKey_HardwareVersion:
        return 1;

        /**
         * A monothonic number identifying the software version running on the device.
         *
         * <p>return a different value than src/include/platform/CHIPDeviceConfig.h for debug
         */
      case kConfigNamespace_ChipFactory + ":" + kConfigKey_SoftwareVersion:
        return 1;

      case kConfigNamespace_ChipFactory + ":" + kConfigKey_SetupPinCode:
        return 20202021;

      case kConfigNamespace_ChipFactory + ":" + kConfigKey_SetupDiscriminator:
        return 0xF00;

      case kConfigNamespace_ChipFactory + ":" + kConfigKey_Spake2pIterationCount:
        return 100;
    }

    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);

    switch (key) {
        /**
         * Human readable name of the device model. return a different value than
         * src/include/platform/CHIPDeviceConfig.h for debug
         */<commit_msg>remove spake2p iteration count ( using default ) and change productId ( to fit Certification ) in android tv example (#15245)<commit_after>/*
 *   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 chip.platform;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
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 final String PREFERENCE_FILE_KEY = "chip.platform.ConfigurationManager";
  private SharedPreferences preferences;

  public PreferencesConfigurationManager(Context context) {
    preferences = context.getSharedPreferences(PREFERENCE_FILE_KEY, 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);

    switch (key) {
        /**
         * The unique id assigned by the device vendor to identify the product or device type. This
         * number is scoped to the device vendor id. return a different value than
         * src/include/platform/CHIPDeviceConfig.h for debug
         */
      case kConfigNamespace_ChipFactory + ":" + kConfigKey_ProductId:
        return 0x8003;

        /**
         * The default hardware version number assigned to the device or product by the device
         * vendor.
         *
         * <p>Hardware versions are specific to a particular device vendor and product id, and
         * typically correspond to a revision of the physical device, a change to its packaging,
         * and/or a change to its marketing presentation. This value is generally *not* incremented
         * for device software revisions.
         *
         * <p>This is a default value which is used when a hardware version has not been stored in
         * device persistent storage (e.g. by a factory provisioning process).
         *
         * <p>return a different value than src/include/platform/CHIPDeviceConfig.h for debug
         */
      case kConfigNamespace_ChipFactory + ":" + kConfigKey_HardwareVersion:
        return 1;

        /**
         * A monothonic number identifying the software version running on the device.
         *
         * <p>return a different value than src/include/platform/CHIPDeviceConfig.h for debug
         */
      case kConfigNamespace_ChipFactory + ":" + kConfigKey_SoftwareVersion:
        return 1;

      case kConfigNamespace_ChipFactory + ":" + kConfigKey_SetupPinCode:
        return 20202021;

      case kConfigNamespace_ChipFactory + ":" + kConfigKey_SetupDiscriminator:
        return 0xF00;
    }

    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);

    switch (key) {
        /**
         * Human readable name of the device model. return a different value than
         * src/include/platform/CHIPDeviceConfig.h for debug
         */
{'fraction_non_alphanumeric': 0.07594361073215097, 'fraction_numerical': 0.008412914961346066, 'mean_word_length': 2.913256227758007, 'pattern_counts': {'":': 11, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1375976', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2215, 'n_words': 781}