zezung27 / api-mymeostore

1 stars 0 forks source link

Utils #9

Closed zezung27 closed 1 year ago

zezung27 commented 1 year ago

package com.matcos.dynamicislandlight.utils;

import android.content.ContentResolver; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.provider.Settings; import android.util.DisplayMetrics; import android.view.Display; import android.view.WindowManager;

import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.matcos.dynamicislandlight.R; import com.matcos.dynamicislandlight.noti.PackageModalPref;

import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.lang.reflect.Type; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Iterator;

public class Utils {

public static int getWidthPopupWindow(int type, Context context) {
    int widthData = 0;
    if(type == Contanst.TYPE_IDLE) {
        widthData = context.getResources().getDimensionPixelSize(R.dimen.window_width_idle);
    } else if(type == Contanst.TYPE_EXPAND_NOTI || type == Contanst.TYPE_EXPAND_TYPE2) {
        widthData = Resources.getSystem().getDisplayMetrics().widthPixels - 50;
    } else {
        widthData = context.getResources().getDimensionPixelSize(R.dimen.window_width_noti_mini);
    }
    return widthData;
}

public static int getHeightPopupWindow(int type, Context context) {
    int heightData = 0;
    if(type == Contanst.TYPE_IDLE) {
        heightData = context.getResources().getDimensionPixelSize(R.dimen.window_height_idle);
    } else if(type == Contanst.TYPE_EXPAND_NOTI) {
        heightData = context.getResources().getDimensionPixelSize(R.dimen.window_height_noti_expand);
    } else if(type == Contanst.TYPE_EXPAND_TYPE2) {
        heightData = context.getResources().getDimensionPixelSize(R.dimen.window_height_noti_expand_type2);
    }else {
        heightData = context.getResources().getDimensionPixelSize(R.dimen.window_height_noti_mini);
    }
    return heightData;
}

public static boolean isAccessibilityServiceEnabled(Context context, Class accessibilityService) {
    String prefStr = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
    return prefStr != null && prefStr.contains(context.getPackageName()+"/"+accessibilityService.getName());
}

public static boolean checkNotificationEnabled(Context context) {
    try {
        boolean bl = Settings.Secure.getString((ContentResolver)context.getContentResolver(), (String)"enabled_notification_listeners").contains((CharSequence)context.getPackageName());
        return bl;
    }
    catch (Exception exception) {
        exception.printStackTrace();
        return false;
    }
}

public static boolean isSystemPackage(PackageInfo pkgInfo){
    if((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)!=0)
        return false;
    else if((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)!=0)
        return true;
    else if((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED)!=0)
        return false;
    else
        return true;

}

public static byte[] getByteArrayFromBitmap(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, (OutputStream)byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }
    return null;
}

public static byte[] getByteArrayFromBitmap2(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, (OutputStream)byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }
    return null;
}

public static Bitmap getCroppedBitmap(Drawable drawable2, Context context) {
    try {
        Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getRealMetrics(displayMetrics);
        int n = displayMetrics.widthPixels;
        int n2 = displayMetrics.heightPixels;
        Bitmap bitmap = Bitmap.createBitmap((int)drawable2.getIntrinsicWidth(), (int)drawable2.getIntrinsicHeight(), (Bitmap.Config)Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bitmap);
        drawable2.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable2.draw(canvas);
        if (bitmap.getWidth() > n && bitmap.getHeight() > n2) {
            return scaleDownImage(bitmap, n, n2);
        }
        Bitmap bitmap2 = scaleUpImage(bitmap, n, n2);
        return bitmap2;
    }
    catch (Exception exception) {
    }
    catch (OutOfMemoryError outOfMemoryError) {
        // empty catch block
    }
    return null;
}

public static Bitmap scaleDownImage(Bitmap bitmap, int n, int n2) {
    float f = bitmap.getWidth();
    float f2 = bitmap.getHeight();
    float f3 = n;
    if (!(f > f3)) return bitmap;
    float f4 = f / f2;
    float f5 = n2;
    float f6 = f3 / f5;
    if (f2 > f) {
        if (f6 > 1.0f) {
            f3 = (int)(f4 * f5);
            return Bitmap.createScaledBitmap((Bitmap)bitmap, (int)((int)f3), (int)((int)f5), (boolean)true);
        } else {
            f5 = (int)(f3 / f4);
            return Bitmap.createScaledBitmap((Bitmap)bitmap, (int)((int)f3), (int)((int)f5), (boolean)true);
        }
    }
    if (f6 < 1.0f) {
        f5 = (int)(f4 * f3);
    } else {
        f3 = (int)(f5 / f4);
    }
    float f7 = f5;
    f5 = f3;
    f3 = f7;
    return Bitmap.createScaledBitmap((Bitmap)bitmap, (int)((int)f3), (int)((int)f5), (boolean)true);
}
zezung27 commented 1 year ago

public static Bitmap scaleUpImage(Bitmap bitmap, int n, int n2) { block4 : { Bitmap bitmap2; block3 : { try { float f = bitmap.getWidth(); if (bitmap.getWidth() > n) { bitmap2 = Bitmap.createBitmap((Bitmap)bitmap, (int)((int)(f / 2.0f - (float)(n / 2))), (int)0, (int)n, (int)bitmap.getHeight()); bitmap.recycle(); break block3; } if (bitmap.getHeight() <= n2) break block4; bitmap2 = Bitmap.createBitmap((Bitmap)bitmap, (int)0, (int)0, (int)bitmap.getWidth(), (int)n2); bitmap.recycle(); } catch (OutOfMemoryError outOfMemoryError) { outOfMemoryError.printStackTrace(); return null; } } bitmap = bitmap2; } Bitmap bitmap3 = null; if (bitmap == null) return bitmap3; Bitmap bitmap4 = bitmap.getWidth() <= n ? Bitmap.createScaledBitmap((Bitmap)bitmap, (int)n, (int)((int)((float)bitmap.getHeight() / (float)bitmap.getWidth() (float)n)), (boolean)true) : (bitmap.getHeight() < n2 ? Bitmap.createScaledBitmap((Bitmap)bitmap, (int)((int)((float)bitmap.getHeight() / (float)bitmap.getWidth() (float)n2)), (int)n2, (boolean)true) : null); bitmap.recycle(); return bitmap4; }

public static float convertDpToPixel(float f, Context context) {
    Resources resources = context == null ? Resources.getSystem() : context.getResources();
    return f * ((float)resources.getDisplayMetrics().densityDpi / 160.0f);
}

public static Bitmap drawableToBmp(Context context, Drawable drawable2, int n) {
    Bitmap bitmap;
    if (drawable2 == null) return null;
    if (drawable2.getIntrinsicWidth() > 0 && drawable2.getIntrinsicHeight() > 0) {
        if (n <= 0) return getCroppedBitmap(drawable2, context);
        int n2 = (int)convertDpToPixel(n, context);
        bitmap = Bitmap.createBitmap((int)n2, (int)n2, (Bitmap.Config)Bitmap.Config.ARGB_4444);
    } else {
        bitmap = Bitmap.createBitmap((int)1, (int)1, (Bitmap.Config)Bitmap.Config.ARGB_4444);
    }
    Canvas canvas = new Canvas(bitmap);
    drawable2.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable2.draw(canvas);
    return bitmap;
}

public static void saveArrayList(ArrayList<PackageModalPref> list, String key, Context context){
    // save the task list to preference
    SharedPreferences prefs = context.getSharedPreferences(Contanst.PREF_NAME_PACKAGE, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    Gson gson = new Gson();
    String json = gson.toJson(list);
    editor.putString(key, json);
    editor.commit();
}

public static ArrayList<PackageModalPref> getArrayList(String key, Context context){
    SharedPreferences sharedPrefs = context.getSharedPreferences(Contanst.PREF_NAME_PACKAGE, Context.MODE_PRIVATE);;
    Gson gson = new Gson();
    String json = sharedPrefs.getString(key, "");
    Type type = new TypeToken<ArrayList<PackageModalPref>>() {}.getType();
    ArrayList<PackageModalPref> arrayList = gson.fromJson(json, type);
    return arrayList;
}

public static String getFormattedTime(long l) {
    long l2 = l / 1000L;
    long l3 = l2 / 60L;
    long l4 = l2 % 60L;
    DecimalFormat decimalFormat = new DecimalFormat("00");;
    String string2 = decimalFormat.format(l3);
    String string3 = decimalFormat.format(l4);
    return string2 + ":" + string3;
}

}