<commit_before>/*
 * Copyright (c) 2016 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.rm3l.maoni.common.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.opengl.GLES10;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Information about the current device (at the moment Maoni is called)
 */
public class DeviceInfo {

    private static final String GET_MOBILE_DATA_ENABLED = "getMobileDataEnabled";

    /*
     * ---
     * App / SDK
     * ---
     */
    public final int sdkVersion = Build.VERSION.SDK_INT;

    /*
     * ---
     * Device
     * ---
     */
    public final String board = android.os.Build.BOARD;
    public final String brand = Build.BRAND;
    public final String device = Build.DEVICE;
    public final String model = Build.MODEL;
    public final String product = Build.PRODUCT;
    public final String tags = Build.TAGS;
    public final String linuxVersion = System.getProperty("os.version");
    public final String manufacturer = Build.MANUFACTURER;
    public final String hardware = Build.HARDWARE;
    public final String cpuAbi = Build.CPU_ABI;
    public final String cpuAbi2 = Build.CPU_ABI2;
    public final String[] supportedAbis = {cpuAbi, cpuAbi2};
    public final boolean isTablet;

    /*
     * ---
     * OS
     * ---
     */
    public final String androidReleaseVersion = Build.VERSION.RELEASE;
    public final String buildVersion = Build.VERSION.INCREMENTAL;
    public final String buildDisplay = Build.DISPLAY;
    public final String buildFingerprint = Build.FINGERPRINT;
    public final String buildId = Build.ID;
    public final long buildTime = Build.TIME;
    public final String buildType = Build.TYPE;
    public final String buildUser = Build.USER;

    public final String language = Locale.getDefault().getDisplayName();

    public final String openGlVersion = GLES10.glGetString(GLES10.GL_VERSION);

    /*
     * ---
     * Density
     * ---
     */
    public final float density;
    public final int densityDpi;
    public final float scaledDensity;
    public final float xdpi;
    public final float ydpi;

    /*
     * ---
     * Screen
     * ---
     */
    public final int heightPixels;
    public final int widthPixels;
    public final String resolution;

    /*
     * ---
     * Other useful info
     * ---
     */
    public final Boolean gpsEnabled;
    public final SupplicantState supplicantState;
    public final Boolean mobileDataEnabled;

    /**
     * Constructor
     *
     * @param activity the calling activity
     */
    @SuppressLint("DefaultLocale")
    public DeviceInfo(final Activity activity) {

        this.isTablet = ((activity.getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);

        SupplicantState supplicantState = null;
        try {
            final WifiManager wifiManager = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);
            @SuppressWarnings("MissingPermission")
            final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            supplicantState = wifiInfo.getSupplicantState();
        } catch (Exception e) {
            //No worries
        }
        this.supplicantState = supplicantState;

        final ConnectivityManager cm =
                (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        Boolean mobileDataEnabled = null;
        try {
            final Class cmClass = Class.forName(cm.getClass().getName());
            @SuppressWarnings("unchecked")
            final Method method = cmClass.getDeclaredMethod(GET_MOBILE_DATA_ENABLED);
            method.setAccessible(true);
            mobileDataEnabled = (Boolean) method.invoke(cm);
        } catch (Exception e) {
            // Private API access - no worries
        }
        this.mobileDataEnabled = mobileDataEnabled;

        Boolean gpsEnabled = null;
        try {
            final LocationManager manager =
                    (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
            gpsEnabled = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);<commit_msg>Prevent potential memory leak in retrieving the Wifi Service<commit_after>/*
 * Copyright (c) 2016 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.rm3l.maoni.common.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.opengl.GLES10;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Information about the current device (at the moment Maoni is called)
 */
public class DeviceInfo {

    private static final String GET_MOBILE_DATA_ENABLED = "getMobileDataEnabled";

    /*
     * ---
     * App / SDK
     * ---
     */
    public final int sdkVersion = Build.VERSION.SDK_INT;

    /*
     * ---
     * Device
     * ---
     */
    public final String board = android.os.Build.BOARD;
    public final String brand = Build.BRAND;
    public final String device = Build.DEVICE;
    public final String model = Build.MODEL;
    public final String product = Build.PRODUCT;
    public final String tags = Build.TAGS;
    public final String linuxVersion = System.getProperty("os.version");
    public final String manufacturer = Build.MANUFACTURER;
    public final String hardware = Build.HARDWARE;
    public final String cpuAbi = Build.CPU_ABI;
    public final String cpuAbi2 = Build.CPU_ABI2;
    public final String[] supportedAbis = {cpuAbi, cpuAbi2};
    public final boolean isTablet;

    /*
     * ---
     * OS
     * ---
     */
    public final String androidReleaseVersion = Build.VERSION.RELEASE;
    public final String buildVersion = Build.VERSION.INCREMENTAL;
    public final String buildDisplay = Build.DISPLAY;
    public final String buildFingerprint = Build.FINGERPRINT;
    public final String buildId = Build.ID;
    public final long buildTime = Build.TIME;
    public final String buildType = Build.TYPE;
    public final String buildUser = Build.USER;

    public final String language = Locale.getDefault().getDisplayName();

    public final String openGlVersion = GLES10.glGetString(GLES10.GL_VERSION);

    /*
     * ---
     * Density
     * ---
     */
    public final float density;
    public final int densityDpi;
    public final float scaledDensity;
    public final float xdpi;
    public final float ydpi;

    /*
     * ---
     * Screen
     * ---
     */
    public final int heightPixels;
    public final int widthPixels;
    public final String resolution;

    /*
     * ---
     * Other useful info
     * ---
     */
    public final Boolean gpsEnabled;
    public final SupplicantState supplicantState;
    public final Boolean mobileDataEnabled;

    /**
     * Constructor
     *
     * @param activity the calling activity
     */
    @SuppressLint("DefaultLocale")
    public DeviceInfo(final Activity activity) {

        this.isTablet = ((activity.getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);

        SupplicantState supplicantState = null;
        try {
            final WifiManager wifiManager = (WifiManager)
                activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            @SuppressWarnings("MissingPermission")
            final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            supplicantState = wifiInfo.getSupplicantState();
        } catch (Exception e) {
            //No worries
        }
        this.supplicantState = supplicantState;

        final ConnectivityManager cm =
                (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        Boolean mobileDataEnabled = null;
        try {
            final Class cmClass = Class.forName(cm.getClass().getName());
            @SuppressWarnings("unchecked")
            final Method method = cmClass.getDeclaredMethod(GET_MOBILE_DATA_ENABLED);
            method.setAccessible(true);
            mobileDataEnabled = (Boolean) method.invoke(cm);
        } catch (Exception e) {
            // Private API access - no worries
        }
        this.mobileDataEnabled = mobileDataEnabled;

        Boolean gpsEnabled = null;
        try {
            final LocationManager manager =
                    (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
            gpsEnabled = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
{'fraction_non_alphanumeric': 0.08147388219201504, 'fraction_numerical': 0.0023937761819269898, 'mean_word_length': 2.976206662134602, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5108246', 'n_tokens_mistral': 3137, 'n_tokens_neox': 3078, 'n_words': 977}