<commit_before>/*
 * Copyright (C) 2013-2014 University of Washington
 *
 * 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 org.opendatakit.properties;

import android.content.Context;
import org.apache.commons.lang3.CharEncoding;
import org.opendatakit.aggregate.odktables.rest.TableConstants;
import org.opendatakit.androidlibrary.R;
import org.opendatakit.consts.IntentConsts;
import org.opendatakit.logging.WebLogger;
import org.opendatakit.utilities.ODKFileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Properties are in 3 classes:
 * <p>
 * (1) general (syncable) -- the contents of config/assets/app.properties
 * (2) device -- the contents of data/device.properties
 * (3) secure -- stored in ODK Services' (private) dataDir under mAppName
 * <p>
 * The tools provide the different sets of these and default values for these settings.
 * What goes into secure and device should be predefined in CommonToolProperties
 * <p>
 * If the general (syncable) file contains values for the device and secure settings,
 * these become the default settings for those values. There is also a
 * default.device.properties
 * <p>
 * Device settings and secure settings are not overwritten by changes in the
 * general (syncable) settings. You need to Reset the device configuration to
 * re-initialize these.
 */
public final class PropertiesSingleton {

  private static final String TAG = PropertiesSingleton.class.getSimpleName();
  private static final int INVALID_REVISION = -1;

  private static final String PROPERTIES_REVISION_FILENAME = "properties.revision";
  private static final String GENERAL_PROPERTIES_FILENAME = "app.properties";
  private static final String DEFAULT_DEVICE_PROPERTIES_FILENAME = "default.device.properties";
  private static final String DEVICE_PROPERTIES_FILENAME = "device.properties";
  private static final String SECURE_PROPERTIES_FILENAME = "secure.properties";
  private static final String SECURE_INSTALLATION_ID_FILENAME_PREFIX = "secure.id.";
  private static final String TOOL_INITIALIZATION_SUFFIX = ".tool_last_initialization_start_time";
  /**
   * These three are used in services.utilities.ODKServicePropertyUtils
   */
  @SuppressWarnings("WeakerAccess")
  public final String CREDENTIAL_TYPE_NONE;
  @SuppressWarnings("WeakerAccess")
  public final String CREDENTIAL_TYPE_USERNAME_PASSWORD;
  @SuppressWarnings("WeakerAccess")
  public final String CREDENTIAL_TYPE_GOOGLE_ACCOUNT;

  private final String mAppName;
  private final ReentrantLock mAppLock;
  private final boolean mHasSecureStorage;
  private final File mSecureStorageDir;
  private final TreeMap<String, String> mGeneralDefaults;
  private final TreeMap<String, String> mDeviceDefaults;
  private final TreeMap<String, String> mSecureDefaults;
  private final Properties mGeneralProps;
  private final Properties mGlobalDeviceProps;
  private final Properties mDeviceProps;
  private final Properties mSecureProps;
  private int currentRevision = INVALID_REVISION;
  private String mInstallationId;

  PropertiesSingleton(Context context, String appName, ReentrantLock appLock,
                      TreeMap<String, String> plainDefaults,
                      TreeMap<String, String> deviceDefaults,
                      TreeMap<String, String> secureDefaults) {
    mAppName = appName;
    mAppLock = appLock;
    mHasSecureStorage = context.getPackageName()
        .equals(IntentConsts.AppProperties.APPLICATION_NAME);
    if (mHasSecureStorage) {
      mSecureStorageDir = context.getDir(mAppName, 0);
    } else {
      mSecureStorageDir = null;
    }
    // these strings are set as untranslateable
    CREDENTIAL_TYPE_NONE = context.getString(R.string.credential_type_none);
    CREDENTIAL_TYPE_USERNAME_PASSWORD = context
        .getString(R.string.credential_type_username_password);
    CREDENTIAL_TYPE_GOOGLE_ACCOUNT = context.getString(R.string.credential_type_google_account);<commit_msg>fix errors related to lint fix<commit_after>/*
 * Copyright (C) 2013-2014 University of Washington
 *
 * 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 org.opendatakit.properties;

import android.content.Context;
import org.apache.commons.lang3.CharEncoding;
import org.opendatakit.aggregate.odktables.rest.TableConstants;
import org.opendatakit.androidlibrary.R;
import org.opendatakit.consts.IntentConsts;
import org.opendatakit.logging.WebLogger;
import org.opendatakit.utilities.ODKFileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Properties are in 3 classes:
 * <p>
 * (1) general (syncable) -- the contents of config/assets/app.properties
 * (2) device -- the contents of data/device.properties
 * (3) secure -- stored in ODK Services' (private) dataDir under mAppName
 * <p>
 * The tools provide the different sets of these and default values for these settings.
 * What goes into secure and device should be predefined in CommonToolProperties
 * <p>
 * If the general (syncable) file contains values for the device and secure settings,
 * these become the default settings for those values. There is also a
 * default.device.properties
 * <p>
 * Device settings and secure settings are not overwritten by changes in the
 * general (syncable) settings. You need to Reset the device configuration to
 * re-initialize these.
 */
public final class PropertiesSingleton {

  private static final String TAG = PropertiesSingleton.class.getSimpleName();
  private static final int INVALID_REVISION = -1;

  private static final String PROPERTIES_REVISION_FILENAME = "properties.revision";
  private static final String GENERAL_PROPERTIES_FILENAME = "app.properties";
  private static final String DEFAULT_DEVICE_PROPERTIES_FILENAME = "default.device.properties";
  private static final String DEVICE_PROPERTIES_FILENAME = "device.properties";
  private static final String SECURE_PROPERTIES_FILENAME = "secure.properties";
  private static final String SECURE_INSTALLATION_ID_FILENAME_PREFIX = "secure.id.";
  private static final String TOOL_INITIALIZATION_SUFFIX = ".tool_last_initialization_start_time";
  /**
   * These three are used in services.utilities.ODKServicePropertyUtils
   */
  @SuppressWarnings("WeakerAccess")
  public final String CREDENTIAL_TYPE_NONE;
  @SuppressWarnings("WeakerAccess")
  public final String CREDENTIAL_TYPE_USERNAME_PASSWORD;

  private final String mAppName;
  private final ReentrantLock mAppLock;
  private final boolean mHasSecureStorage;
  private final File mSecureStorageDir;
  private final TreeMap<String, String> mGeneralDefaults;
  private final TreeMap<String, String> mDeviceDefaults;
  private final TreeMap<String, String> mSecureDefaults;
  private final Properties mGeneralProps;
  private final Properties mGlobalDeviceProps;
  private final Properties mDeviceProps;
  private final Properties mSecureProps;
  private int currentRevision = INVALID_REVISION;
  private String mInstallationId;

  PropertiesSingleton(Context context, String appName, ReentrantLock appLock,
                      TreeMap<String, String> plainDefaults,
                      TreeMap<String, String> deviceDefaults,
                      TreeMap<String, String> secureDefaults) {
    mAppName = appName;
    mAppLock = appLock;
    mHasSecureStorage = context.getPackageName()
        .equals(IntentConsts.AppProperties.APPLICATION_NAME);
    if (mHasSecureStorage) {
      mSecureStorageDir = context.getDir(mAppName, 0);
    } else {
      mSecureStorageDir = null;
    }
    // these strings are set as untranslateable
    CREDENTIAL_TYPE_NONE = context.getString(R.string.credential_type_none);
    CREDENTIAL_TYPE_USERNAME_PASSWORD = context
        .getString(R.string.credential_type_username_password);
{'fraction_non_alphanumeric': 0.07274086733603931, 'fraction_numerical': 0.004058961760307627, 'mean_word_length': 4.6987218502738894, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '3406894', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2566, 'n_words': 819}