package com.grarak.kerneladiutor.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by willi on 30.11.14.
 */
public class Utils implements Constants {

    public static void launchUrl(Context context, String link) {
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
    }

    public static boolean isTablet(Context context) {
        boolean device_large = ((context.getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK) ==
                Configuration.SCREENLAYOUT_SIZE_LARGE);

        if (device_large) {
            DisplayMetrics metrics = new DisplayMetrics();
            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

            if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                    || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                    || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                    || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH)
                return true;
        }
        return false;
    }

    public static int getScreenOrientation(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels <
                context.getResources().getDisplayMetrics().heightPixels ?
                Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
    }

    public static void toast(String message, Context context) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public static boolean getBoolean(String name, boolean defaults, Context context) {
        Log.i(TAG, "getting " + name);
        return context.getSharedPreferences(PREF_NAME, 0).getBoolean(name, defaults);
    }

    public static void saveBoolean(String name, boolean value, Context context) {
        Log.i(TAG, "saving " + name + " as " + value);
        context.getSharedPreferences(PREF_NAME, 0).edit().putBoolean(name, value).apply();
    }

    public static boolean existFile(String file) {
        return new File(file).exists();
    }

    public static String readFile(String filepath) {
        if (filepath == null) return null;

        try {
            BufferedReader buffreader = new BufferedReader(new FileReader(filepath), 256);
            String line;
            StringBuilder text = new StringBuilder();
            while ((line = buffreader.readLine()) != null) {
                text.append(line);
                text.append("\n");
            }
            buffreader.close();
            return text.toString().trim();
        } catch (FileNotFoundException e) {
            Log.e(TAG, filepath + " does not exist");
        } catch (IOException e) {
            Log.e(TAG, "I/O read error: " + filepath);
        }
        return null;
    }

}

@@ -38,37 +38,8 @@
 s;%0A%0A
-import android.app.Activity;%0A
 impo
@@ -66,16 +66,16 @@
 ontext;%0A
+
 import a
@@ -167,44 +167,8 @@
 ri;%0A
-import android.util.DisplayMetrics;%0A
 impo
@@ -666,32 +666,15 @@
     
-boolean device_large = (
+return 
 (con
@@ -726,32 +726,16 @@
 Layout &
-%0A               
  Configu
@@ -764,19 +764,16 @@
 ZE_MASK)
- ==
 %0A       
@@ -772,32 +772,35 @@
 %0A               
+ %3E=
  Configuration.S
@@ -825,540 +825,8 @@
 ARGE
-);%0A%0A        if (device_large) %7B%0A            DisplayMetrics metrics = new DisplayMetrics();%0A            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);%0A%0A            if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT%0A                    %7C%7C metrics.densityDpi == DisplayMetrics.DENSITY_HIGH%0A                    %7C%7C metrics.densityDpi == DisplayMetrics.DENSITY_TV%0A                    %7C%7C metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH)%0A                return true;%0A        %7D%0A        return false
 ;%0A  

Fix navigation drawer on 10 inch tablets