<commit_before>/*
 * Copyright (C) 2009-2013 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 android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.util.HashMap;
import java.util.Locale;

/**
 * Used to return device properties to JavaRosa
 *
 * @author <NAME> (<EMAIL>)
 * @author <EMAIL>
 */

public class PropertyManager {

  public interface DynamicPropertiesInterface {
    String getActiveUser();

    String getLocale();

    String getUsername();

    String getUserEmail();

    String getAppName();

    String getInstanceDirectory();

    String getUriFragmentNewInstanceFile(String uriDeviceId, String extension);
  }

  private HashMap<String, String> mProperties;

  public final static String DEVICE_ID_PROPERTY = "deviceid"; // imei
  public final static String SUBSCRIBER_ID_PROPERTY = "subscriberid"; // imsi
  public final static String SIM_SERIAL_PROPERTY = "simserial";
  public final static String PHONE_NUMBER_PROPERTY = "phonenumber";

  public final static String OR_DEVICE_ID_PROPERTY = "uri:deviceid"; // imei
  public final static String OR_SUBSCRIBER_ID_PROPERTY = "uri:subscriberid"; // imsi
  public final static String OR_SIM_SERIAL_PROPERTY = "uri:simserial";
  public final static String OR_PHONE_NUMBER_PROPERTY = "uri:phonenumber";

  /**
   * These properties are dynamic and accessed through the
   * DynamicPropertiesInterface. As with all property names,
   * they are compared in a case-insensitive manner.
   */

  public final static String LOCALE = "locale";

  public final static String ACTIVE_USER = "active_user";

  // username -- current username
  public final static String USERNAME = "username";
  public final static String OR_USERNAME = "uri:username";
  // email -- current account email
  public final static String EMAIL = "email";
  public final static String OR_EMAIL = "uri:email";
  public final static String APP_NAME = "appName";
  // instanceDirectory -- directory containing media files for current instance
  public final static String INSTANCE_DIRECTORY = "instancedirectory";
  // uriFragmentNewFile -- the appName-relative uri for a non-existent file with the given extension
  public final static String URI_FRAGMENT_NEW_INSTANCE_FILE_WITHOUT_COLON = "urifragmentnewinstancefile";
  public final static String URI_FRAGMENT_NEW_INSTANCE_FILE = URI_FRAGMENT_NEW_INSTANCE_FILE_WITHOUT_COLON + ":";

  /**
   * Constructor used within the Application object to create a singleton of the
   * property manager. Access it through
   * Survey.getInstance().getPropertyManager()
   *
   * @param context
   */
  public PropertyManager(Context context) {

    mProperties = new HashMap<String, String>();

    TelephonyManager mTelephonyManager = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);

    String deviceId = mTelephonyManager.getDeviceId();
    String orDeviceId = null;
    if (deviceId != null) {
      if ((deviceId.contains("*") || deviceId.contains("000000000000000"))) {
        deviceId = Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.ANDROID_ID);
        orDeviceId = Settings.Secure.ANDROID_ID + ":" + deviceId;
      } else {
        orDeviceId = "imei:" + deviceId;
      }
    }

    if (deviceId == null) {
      // no SIM -- WiFi only
      // Retrieve WiFiManager
      WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

      // Get WiFi status
      WifiInfo info = wifi.getConnectionInfo();
      if (info != null) {
        deviceId = info.getMacAddress();
        orDeviceId = "mac:" + deviceId;
      }
    }

    // if it is still null, use ANDROID_ID
    if (deviceId == null) {
      deviceId = Settings.Secure<commit_msg>fix: Mac Address is bogus on Android 6 -- port of patch from ODK Collect ( https://github.com/opendatakit/collect/commit/d13678faf7db76a88b7305b6b62e9c6415fc91b4 )<commit_after>/*
 * Copyright (C) 2009-2013 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 android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.util.HashMap;
import java.util.Locale;

/**
 * Used to return device properties to JavaRosa
 *
 * @author <NAME> (<EMAIL>)
 * @author <EMAIL>
 */

public class PropertyManager {

  private static final String ANDROID6_FAKE_MAC = "02:00:00:00:00:00";

  public interface DynamicPropertiesInterface {
    String getActiveUser();

    String getLocale();

    String getUsername();

    String getUserEmail();

    String getAppName();

    String getInstanceDirectory();

    String getUriFragmentNewInstanceFile(String uriDeviceId, String extension);
  }

  private HashMap<String, String> mProperties;

  public final static String DEVICE_ID_PROPERTY = "deviceid"; // imei
  public final static String SUBSCRIBER_ID_PROPERTY = "subscriberid"; // imsi
  public final static String SIM_SERIAL_PROPERTY = "simserial";
  public final static String PHONE_NUMBER_PROPERTY = "phonenumber";

  public final static String OR_DEVICE_ID_PROPERTY = "uri:deviceid"; // imei
  public final static String OR_SUBSCRIBER_ID_PROPERTY = "uri:subscriberid"; // imsi
  public final static String OR_SIM_SERIAL_PROPERTY = "uri:simserial";
  public final static String OR_PHONE_NUMBER_PROPERTY = "uri:phonenumber";

  /**
   * These properties are dynamic and accessed through the
   * DynamicPropertiesInterface. As with all property names,
   * they are compared in a case-insensitive manner.
   */

  public final static String LOCALE = "locale";

  public final static String ACTIVE_USER = "active_user";

  // username -- current username
  public final static String USERNAME = "username";
  public final static String OR_USERNAME = "uri:username";
  // email -- current account email
  public final static String EMAIL = "email";
  public final static String OR_EMAIL = "uri:email";
  public final static String APP_NAME = "appName";
  // instanceDirectory -- directory containing media files for current instance
  public final static String INSTANCE_DIRECTORY = "instancedirectory";
  // uriFragmentNewFile -- the appName-relative uri for a non-existent file with the given extension
  public final static String URI_FRAGMENT_NEW_INSTANCE_FILE_WITHOUT_COLON = "urifragmentnewinstancefile";
  public final static String URI_FRAGMENT_NEW_INSTANCE_FILE = URI_FRAGMENT_NEW_INSTANCE_FILE_WITHOUT_COLON + ":";
  /**
   * Constructor used within the Application object to create a singleton of the
   * property manager. Access it through
   * Survey.getInstance().getPropertyManager()
   *
   * @param context
   */
  public PropertyManager(Context context) {

    mProperties = new HashMap<String, String>();

    TelephonyManager mTelephonyManager = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);

    String deviceId = mTelephonyManager.getDeviceId();
    String orDeviceId = null;
    if (deviceId != null) {
      if ((deviceId.contains("*") || deviceId.contains("000000000000000"))) {
        deviceId = Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.ANDROID_ID);
        orDeviceId = Settings.Secure.ANDROID_ID + ":" + deviceId;
      } else {
        orDeviceId = "imei:" + deviceId;
      }
    }

    if (deviceId == null) {
      // no SIM -- WiFi only
      // Retrieve WiFiManager
      WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

      // Get WiFi status
      WifiInfo info = wifi.getConnectionInfo();
      if (info != null) {
        String macId = info.getMacAddress();
        if ( macId != null && !ANDROID6_FAKE_MAC.equals(macId) ) {
          deviceId = macId;
          orDeviceId = "mac:" + deviceId;
        }
      }
    }

    // if it is still null, use ANDROID_ID
    if (deviceId == null) {
      deviceId = Settings.Secure
{'fraction_non_alphanumeric': 0.08843389239817033, 'fraction_numerical': 0.010237421041167502, 'mean_word_length': 3.8003136434918976, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '7394975', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2557, 'n_words': 813}