package com.mobify.astro.utilities;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.util.Log;
import android.webkit.WebSettings;

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

public class AstroWebUtilities {
    public static String ASTRO_USER_AGENT = "Astro App";
    private static final String PLATFORM_NAME = "Android";
    private static final String TAG = AstroWebUtilities.class.getName();

    public static void addAstroUserAgent(Context context, WebSettings settings) {
        String astroUserAgentString = settings.getUserAgentString() + " " + ASTRO_USER_AGENT;

        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            astroUserAgentString += "/" + info.versionName;
        } catch(Exception ex) {
            // We don't care why an exception occurred. If one does occur, we just
            // don't include the version number in the user agent.
        }

        settings.setUserAgentString(astroUserAgentString);
    }

    public static JSONObject getOSInfo() {
        JSONObject osInfo = new JSONObject();
        try {
            osInfo.put("os", PLATFORM_NAME);
            osInfo.put("version", Build.VERSION.RELEASE);
        } catch (JSONException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return osInfo;
    }
}
