package de.xikolo.lanalytics.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import java.util.LinkedHashMap;
import java.util.Map;

import de.xikolo.lanalytics.Lanalytics;

public class ContextUtil {

    public static Map<String, String> getDefaultContextData(Context context) {
        Map<String, String> contextMap = new LinkedHashMap<>();

        // os details
        contextMap.put("platform", "Android");
        contextMap.put("os_version", Build.VERSION.RELEASE);
        contextMap.put("api_level", String.valueOf(Build.VERSION.SDK_INT));

        // device details
        contextMap.put("device", DeviceUtil.getDeviceName());

        // app details
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            contextMap.put("app_version_name", String.valueOf(pi.versionName));
            contextMap.put("app_version_code", String.valueOf(pi.versionCode));
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(Lanalytics.class.getSimpleName(), "Could not get package info for " + context.getPackageName(), e);
        }

        // screen details
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (Build.VERSION.SDK_INT >= 17) {
            wm.getDefaultDisplay().getRealMetrics(metrics);
        } else {
            wm.getDefaultDisplay().getMetrics(metrics);
        }
        contextMap.put("screen_width", String.valueOf(metrics.widthPixels));
        contextMap.put("screen_height", String.valueOf(metrics.heightPixels));
        contextMap.put("screen_density", String.valueOf((int) metrics.density * 160));

        // network details
        String network = "unknown";
        switch (NetworkUtil.getConnectivityStatus(context)) {
            case WIFI:
                network = "wifi";
                break;
            case MOBILE:
                network = "mobile";
                break;
            case NOT_CONNECTED:
                network = "offline";
                break;
        }
        contextMap.put("network", network);

        return contextMap;
    }

}

@@ -625,18 +625,136 @@
 ap.put(%22
-os
+platform_version%22, Build.VERSION.RELEASE);%0A        contextMap.put(%22runtime%22, %22Android%22);%0A        contextMap.put(%22runtime
 _version
@@ -804,16 +804,24 @@
 ap.put(%22
+runtime_
 api_leve
@@ -1121,27 +1121,29 @@
 ap.put(%22
-app
+build
 _version
 _name%22, 
@@ -1138,13 +1138,8 @@
 sion
-_name
 %22, S
@@ -1162,19 +1162,19 @@
 .version
-Nam
+Cod
 e));%0A   
@@ -1198,19 +1198,21 @@
 ap.put(%22
-app
+build
 _version
@@ -1216,11 +1216,11 @@
 ion_
-cod
+nam
 e%22, 
@@ -1236,35 +1236,35 @@
 lueOf(pi.version
-Cod
+Nam
 e));%0A        %7D c

Update default context entries.