<commit_before>/**
 * Copyright 2014-present Liquid Data Intelligence S.A.
 *
 * 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 io.lqd.sdk.model;

import io.lqd.sdk.LQLog;
import io.lqd.sdk.LiquidTools;

import java.util.HashMap;
import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;

import android.Manifest.permission;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.WindowManager;

public class LQDevice {
	private String mVendor;
	private String mDeviceModel;
	private String mSystemVersion;
	private String mScreenSize;
	private String mCarrier;
	private String mInternetConnectivity;
	private String mUid;
	private String mAppBundle;
	private String mAppVersion;
	private String mAppName;
	private String mReleaseVersion;
	private String mLiquidVersion;
	private HashMap<String, Object> mAttributes;

	private Context mContext;

	// Initialization
	public LQDevice(Context context, String liquidVersion) {
		mAttributes = new HashMap<String,Object>();
		mContext = context;
		mVendor = LQDevice.getDeviceVendor();
		mDeviceModel = LQDevice.getDeviceModel();
		mSystemVersion = LQDevice.getSystemVersion();
		mScreenSize = LQDevice.getScreenSize(context);
		mCarrier = LQDevice.getCarrier(context);
		mInternetConnectivity = LQDevice.getInternetConnectivity(context);
		mUid = LQDevice.getDeviceID(context);
		mAppBundle = LQDevice.getAppBundle(context);
		mAppName = LQDevice.getAppName(context);
		mAppVersion = LQDevice.getAppVersion(context);
		mReleaseVersion = LQDevice.getReleaseVersion(context);
		mLiquidVersion = liquidVersion;
	}

	public LQDevice(Context context, String liquidVersion, Location location) {
		this(context,liquidVersion);
		setLocation(location);
	}

	// Attributes
	public String getUID() {
		return mUid;
	}

	public void setLocation(Location location) {
		if (location == null) {
			mAttributes.remove("latitude");
			mAttributes.remove("longitude");
		} else {
			mAttributes.put("latitude", Double.valueOf(location.getLatitude()));
			mAttributes.put("longitude",Double.valueOf(location.getLongitude()));
		}
	}

	public void setPushId(String id) {
		if(id == null || id.equals("")) {
			mAttributes.remove("push_token");
		} else {
			mAttributes.put("push_token", id);
		}
	}

	// JSON
	public JSONObject toJSON() {
		// Updating to avoid callbacks
		mInternetConnectivity = LQDevice.getInternetConnectivity(mContext);

		HashMap<String, Object> attrs = new HashMap<String, Object>();
		if(mAttributes != null) {
			attrs.putAll(mAttributes);
		}
		attrs.put("vendor", mVendor);
		attrs.put("platform", "Android");
		attrs.put("model", mDeviceModel);
		try {
			attrs.put("system_version", Integer.parseInt(mSystemVersion));
		} catch (NumberFormatException e) {
			attrs.put("system_version", mSystemVersion);
		}
		attrs.put("screen_size", mScreenSize);
		attrs.put("carrier", mCarrier);
		attrs.put("internet_connectivity", mInternetConnectivity);
		attrs.put("unique_id", mUid);
		attrs.put("app_bundle", mAppBundle);
		attrs.put("app_name", mAppName);
		attrs.put("app_version", mAppVersion);
		attrs.put("release_version", mReleaseVersion);
		attrs.put("liquid_version", mLiquidVersion);

		JSONObject json = new JSONObject();
		try {
			if(attrs != null){
				for(String key : attrs.keySet()){
					json.put(key, attrs.get(key));
				}
			}
			return json;
		} catch (JSONException e) {
			LQLog.error("LQDevice toJSON: " + e.getMessage());
		}
		return null;
	}

	// Device Info
	private static String getDeviceVendor() {
		return android.os.Build.MANUFACTURER;
	}

	private static String getDeviceModel() {
		return android.os.Build.MODEL;
	}

	private static String getSystemVersion() {
		return String.valueOf(android.os.Build.VERSION.SDK_INT);
	}

	@SuppressWarnings("deprecation")
	private static String getScreenSize(Context context) {
		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = windowManager.getDefaultDisplay();
		int width = display.getWidth(); // deprecated<commit_msg>Add system_language attribute to LQDevice<commit_after>/**
 * Copyright 2014-present Liquid Data Intelligence S.A.
 *
 * 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 io.lqd.sdk.model;

import io.lqd.sdk.LQLog;
import io.lqd.sdk.LiquidTools;

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

import org.json.JSONException;
import org.json.JSONObject;

import android.Manifest.permission;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.WindowManager;

public class LQDevice {
	private String mVendor;
	private String mDeviceModel;
	private String mSystemVersion;
	private String mScreenSize;
	private String mCarrier;
	private String mInternetConnectivity;
	private String mUid;
	private String mAppBundle;
	private String mAppVersion;
	private String mAppName;
	private String mReleaseVersion;
	private String mLiquidVersion;
	private HashMap<String, Object> mAttributes;

	private Context mContext;
	private String mSystemLanguage;

	// Initialization
	public LQDevice(Context context, String liquidVersion) {
		mAttributes = new HashMap<String,Object>();
		mContext = context;
		mVendor = LQDevice.getDeviceVendor();
		mDeviceModel = LQDevice.getDeviceModel();
		mSystemVersion = LQDevice.getSystemVersion();
		mScreenSize = LQDevice.getScreenSize(context);
		mCarrier = LQDevice.getCarrier(context);
		mInternetConnectivity = LQDevice.getInternetConnectivity(context);
		mUid = LQDevice.getDeviceID(context);
		mAppBundle = LQDevice.getAppBundle(context);
		mAppName = LQDevice.getAppName(context);
		mAppVersion = LQDevice.getAppVersion(context);
		mReleaseVersion = LQDevice.getReleaseVersion(context);
		mLiquidVersion = liquidVersion;
		mSystemLanguage = LQDevice.getSystemLanguage();
	}

	public LQDevice(Context context, String liquidVersion, Location location) {
		this(context,liquidVersion);
		setLocation(location);
	}

	// Attributes
	public String getUID() {
		return mUid;
	}

	public void setLocation(Location location) {
		if (location == null) {
			mAttributes.remove("latitude");
			mAttributes.remove("longitude");
		} else {
			mAttributes.put("latitude", Double.valueOf(location.getLatitude()));
			mAttributes.put("longitude",Double.valueOf(location.getLongitude()));
		}
	}

	public void setPushId(String id) {
		if(id == null || id.equals("")) {
			mAttributes.remove("push_token");
		} else {
			mAttributes.put("push_token", id);
		}
	}

	// JSON
	public JSONObject toJSON() {
		// Updating to avoid callbacks
		mInternetConnectivity = LQDevice.getInternetConnectivity(mContext);

		HashMap<String, Object> attrs = new HashMap<String, Object>();
		if(mAttributes != null) {
			attrs.putAll(mAttributes);
		}
		attrs.put("vendor", mVendor);
		attrs.put("platform", "Android");
		attrs.put("model", mDeviceModel);
		try {
			attrs.put("system_version", Integer.parseInt(mSystemVersion));
		} catch (NumberFormatException e) {
			attrs.put("system_version", mSystemVersion);
		}
		attrs.put("screen_size", mScreenSize);
		attrs.put("carrier", mCarrier);
		attrs.put("internet_connectivity", mInternetConnectivity);
		attrs.put("unique_id", mUid);
		attrs.put("app_bundle", mAppBundle);
		attrs.put("app_name", mAppName);
		attrs.put("app_version", mAppVersion);
		attrs.put("release_version", mReleaseVersion);
		attrs.put("liquid_version", mLiquidVersion);
		attrs.put("system_language", mSystemLanguage);

		JSONObject json = new JSONObject();
		try {
			if(attrs != null){
				for(String key : attrs.keySet()){
					json.put(key, attrs.get(key));
				}
			}
			return json;
		} catch (JSONException e) {
			LQLog.error("LQDevice toJSON: " + e.getMessage());
		}
		return null;
	}

	// Device Info
	private static String getDeviceVendor() {
		return android.os.Build.MANUFACTURER;
	}

	private static String getDeviceModel() {
		return android.os.Build.MODEL;
	}

	private static String getSystemVersion() {
		return String.valueOf(android.os.Build.VERSION.SDK_INT);
	}

	private static String getSystemLanguage() {
		return Locale.getDefault().toString();
	}

	@SuppressWarnings("deprecation")
	private static String getScreenSize(Context context) {
		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = windowManager.getDefaultDisplay();
		int width = display.getWidth(); // deprecated
{'fraction_non_alphanumeric': 0.11117705242334322, 'fraction_numerical': 0.001582591493570722, 'mean_word_length': 5.094635322483423, 'pattern_counts': {'":': 0, '<': 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': '6707091', 'n_tokens_mistral': 3276, 'n_tokens_neox': 3031, 'n_words': 724}