altermarkive.uploader.Report.java Source code

Java tutorial

Introduction

Here is the source code for altermarkive.uploader.Report.java

Source

/*
The MIT License (MIT)
    
Copyright (c) 2016
    
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 altermarkive.uploader;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Build;
import android.provider.Settings.Secure;

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

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class Report {
    private final static String TAG = Report.class.getName();

    @SuppressWarnings("unused")
    public static String probe(Context context) throws JSONException {
        SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        List<Sensor> list = manager.getSensorList(Sensor.TYPE_ALL);
        int[] types = new int[list.size()];
        String[] vendors = new String[list.size()];
        String[] names = new String[list.size()];
        float[] resolutions = new float[list.size()];
        int[] delays = new int[list.size()];
        float[] ranges = new float[list.size()];
        float[] powers = new float[list.size()];
        int i = 0;
        for (Sensor sensor : list) {
            types[i] = sensor.getType();
            vendors[i] = sensor.getVendor();
            names[i] = sensor.getName();
            resolutions[i] = sensor.getResolution();
            delays[i] = sensor.getMinDelay();
            ranges[i] = sensor.getMaximumRange();
            powers[i] = sensor.getPower();
            i++;
        }
        return report(context, types, vendors, names, resolutions, delays, ranges, powers);
    }

    public static String report(Context context, int[] types, String[] vendors, String[] names, float[] resolutions,
            int[] delays, float[] ranges, float[] powers) throws JSONException {
        JSONObject report = new JSONObject();
        JSONObject device = reportDevice(context);
        report.put("device", device);
        JSONArray sensors = new JSONArray();
        device.put("sensors", sensors);
        int i = 0;
        do {
            JSONObject sensor = new JSONObject();
            putInt(types, i, sensor, "type");
            putString(vendors, i, sensor, "vendor");
            putString(names, i, sensor, "name");
            putFloat(resolutions, i, sensor, "resolution");
            putInt(delays, i, sensor, "delay");
            putFloat(ranges, i, sensor, "range");
            putFloat(powers, i, sensor, "power");
            if (0 < sensor.length()) {
                sensors.put(sensor);
            }
            i++;
        } while (i == sensors.length());
        return report.toString();
    }

    private static void putInt(int[] array, int index, JSONObject values, String name) throws JSONException {
        if (array != null && index < array.length) {
            values.put(name, array[index]);
        }
    }

    private static void putFloat(float[] array, int index, JSONObject values, String name) throws JSONException {
        if (array != null && index < array.length) {
            values.put(name, reportFloat(array[index]));
        }
    }

    private static void putString(String[] array, int index, JSONObject values, String name) throws JSONException {
        if (array != null && index < array.length) {
            values.put(name, array[index]);
        }
    }

    private static JSONObject reportFloat(float value) throws JSONException {
        JSONObject hifi = new JSONObject();
        long bits = Float.floatToRawIntBits(value);
        hifi.put("sign", bits >> 31);
        bits &= 0x7FFFFFFF;
        hifi.put("exponent", bits >> 23);
        bits &= 0x007FFFFF;
        hifi.put("mantissa", bits);
        return hifi;
    }

    private static JSONObject reportDevice(Context context) throws JSONException {
        JSONObject device = new JSONObject();
        device.put("id", hash(id(context)));
        device.put("manufacturer", Build.MANUFACTURER);
        device.put("brand", Build.BRAND);
        device.put("product", Build.PRODUCT);
        device.put("model", Build.MODEL);
        device.put("design", Build.DEVICE);
        device.put("board", Build.BOARD);
        device.put("hardware", Build.HARDWARE);
        device.put("build", Build.FINGERPRINT);
        return device;
    }

    public static String id(Context context) {
        return Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
    }

    private static String hash(String text) {
        String hash = null;
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(text.getBytes());
            BigInteger integer = new BigInteger(1, md.digest());
            hash = String.format("%1$032X", integer);
        } catch (NoSuchAlgorithmException exception) {
            String trace = Log.getStackTraceString(exception);
            String message = String.format("MD5 is not available:\n%s", trace);
            Log.e(TAG, message);
        }
        return hash;
    }
}