package com.joker.fourfun.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.widget.Toast;

import com.joker.fourfun.FourFun;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by joker on 2016/11/27.
 */
public class SystemUtil {
    public static final String RUNNING_FONT = "running_font.ttf";
    private static Toast mToast;

    private SystemUtil() {
    }

    /**
     * 网络是否连接
     *
     * @return
     */
    public static boolean isNetworkConnected() {
        ConnectivityManager connectivityManager = (ConnectivityManager) FourFun.getInstance()
                .getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        return connectivityManager.getActiveNetworkInfo() != null;
    }

    /**
     * toast 优化显示
     *
     * @param context
     * @param content
     */
    public static void showToast(Context context, String content) {
        if (mToast == null) {
            mToast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(content);
        }

        mToast.show();
    }

    /**
     * 取消 toast
     */
    public static void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

    /**
     * 前几天的日期
     *
     * @param before 和今天相差的天数
     * @return
     */
    public static String beforeToday(int before) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA); //设置时间格式

        if (before < 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(now);
            calendar.add(Calendar.DATE, before);
            now = calendar.getTime();
        }

        return sdf.format(now);
    }

    /**
     * 像素转 sp
     *
     * @param context
     * @param textSizePixel
     * @return
     */
    public static int px2sp(Context context, float textSizePixel) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (textSizePixel / fontScale + 0.5f);
    }

    /**
     * sp 转 px
     *
     * @param context
     * @param textSizeSp
     * @return
     */
    public static float sp2px(Context context, float textSizeSp) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (textSizeSp * fontScale + 0.5f);
    }

    /**
     * dp 转 px
     * @param dp
     * @return
     */
    public static int dp2px(float dp) {
        float DENSITY = Resources.getSystem().getDisplayMetrics().density;
        return Math.round(dp * DENSITY);
    }

    public static Typeface getTypeface(Context context) {
        return Typeface.createFromAsset(context.getAssets(), "fonts/" + RUNNING_FONT);
    }
}

@@ -234,16 +234,52 @@
 FourFun;
+%0Aimport com.orhanobut.logger.Logger;
 %0A%0Aimport
@@ -940,32 +940,360 @@
 ull;%0A    %7D%0A%0A
+    public static String getCacheFileDirPath() %7B%0A        Logger.e(FourFun.getInstance().getApplicationContext().getCacheDir().getPath());%0A        Logger.e(FourFun.getInstance().getApplicationContext().getCacheDir().getAbsolutePath());%0A        return FourFun.getInstance().getApplicationContext().getCacheDir().getPath();%0A    %7D%0A%0A
     /**%0A
      * toast
@@ -1276,24 +1276,24 @@
  %7D%0A%0A    /**%0A
-
      * toast
@@ -1305,38 +1305,16 @@
 %0A     *%0A
-     * @param context%0A
      * @
@@ -1469,23 +1469,61 @@
 akeText(
-c
+FourFun.getInstance().getApplicationC
 ontext
+()
 , conten
@@ -1530,16 +1530,37 @@
 t, Toast
+%0A                    
 .LENGTH_
@@ -2979,24 +2979,31 @@
   * dp %E8%BD%AC px%0A
+     *%0A
      * @para

refactor code