package epson.common;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;


import android.view.KeyEvent;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import epson.print.CommonDefine;
import epson.print.Util.EPLog;
import epson.print.widgets.CustomTitleAlertDialogBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;
import java.util.UUID;

public class Utils {
    private static String TAG = "epson.common.Utils";

    private Utils() {
    }

    public static void savePref(Context context, String str, String str2, int i) {
        SharedPreferences.Editor edit = context.getSharedPreferences(str, 0).edit();
        edit.putInt(str2, i);
        edit.commit();
    }

    public static void savePref(Context context, String str, String str2, String str3) {
        SharedPreferences.Editor edit = context.getSharedPreferences(str, 0).edit();
        edit.putString(str2, str3);
        edit.commit();
    }

    public static void savePref(Context context, String str, String str2, boolean z) {
        SharedPreferences.Editor edit = context.getSharedPreferences(str, 0).edit();
        edit.putBoolean(str2, z);
        edit.commit();
    }

    public static String getPrefString(Context context, String str, String str2) {
        return context.getSharedPreferences(str, 0).getString(str2, "");
    }

    public static int getPrefInt(Context context, String str, String str2) {
        return context.getSharedPreferences(str, 0).getInt(str2, 0);
    }

    public static boolean getPrefBoolean(Context context, String str, String str2) {
        return context.getSharedPreferences(str, 0).getBoolean(str2, false);
    }

    public static Bitmap decodeFile(File file, int i, boolean z) throws FileNotFoundException, OutOfMemoryError, IOException {
        return decodeFile(file, i);
    }

    /* JADX WARNING: Removed duplicated region for block: B:22:0x0041  */
    /* JADX WARNING: Removed duplicated region for block: B:24:0x0046  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static void copyFile(java.io.File r9, java.io.File r10) throws java.io.IOException {
        /*
            boolean r0 = r10.exists()
            if (r0 != 0) goto L_0x0009
            r10.createNewFile()
        L_0x0009:
            r0 = 0
            java.io.FileInputStream r1 = new java.io.FileInputStream     // Catch:{ all -> 0x003d }
            r1.<init>(r9)     // Catch:{ all -> 0x003d }
            java.nio.channels.FileChannel r9 = r1.getChannel()     // Catch:{ all -> 0x003d }
            java.io.FileOutputStream r1 = new java.io.FileOutputStream     // Catch:{ all -> 0x0037 }
            r1.<init>(r10)     // Catch:{ all -> 0x0037 }
            java.nio.channels.FileChannel r10 = r1.getChannel()     // Catch:{ all -> 0x0037 }
            r4 = 0
            long r6 = r9.size()     // Catch:{ all -> 0x0032 }
            r2 = r10
            r3 = r9
            r2.transferFrom(r3, r4, r6)     // Catch:{ all -> 0x0032 }
            if (r9 == 0) goto L_0x002c
            r9.close()
        L_0x002c:
            if (r10 == 0) goto L_0x0031
            r10.close()
        L_0x0031:
            return
        L_0x0032:
            r0 = move-exception
            r8 = r0
            r0 = r9
            r9 = r8
            goto L_0x003f
        L_0x0037:
            r10 = move-exception
            r8 = r0
            r0 = r9
            r9 = r10
            r10 = r8
            goto L_0x003f
        L_0x003d:
            r9 = move-exception
            r10 = r0
        L_0x003f:
            if (r0 == 0) goto L_0x0044
            r0.close()
        L_0x0044:
            if (r10 == 0) goto L_0x0049
            r10.close()
        L_0x0049:
            throw r9
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.common.Utils.copyFile(java.io.File, java.io.File):void");
    }

    public static Bitmap decodeFile(File file, int i) throws FileNotFoundException, OutOfMemoryError {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        int max = Math.max((options.outWidth / i) + 1, (options.outHeight / i) + 1);
        if (max < 1) {
            max = 1;
        }
        options.inJustDecodeBounds = false;
        options.inSampleSize = max;
        return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    }

    public static Bitmap decodeFileByScale(File file, int i) throws FileNotFoundException, OutOfMemoryError {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = i;
        return BitmapFactory.decodeStream(new FileInputStream(file), (Rect) null, options);
    }

    public static Bitmap decodeFileBySample(File file, BitmapFactory.Options options) throws FileNotFoundException, OutOfMemoryError {
        return BitmapFactory.decodeStream(new FileInputStream(file), (Rect) null, options);
    }

    public static void deleteTempFile(String str) {
        File file = new File(str);
        if (file.exists() && file.isFile()) {
            file.delete();
        }
    }

    public static boolean isOnline(Context context) {
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    public static boolean isConnecting(@NonNull Context context) {
        NetworkInfo activeNetworkInfo;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager == null || (activeNetworkInfo = connectivityManager.getActiveNetworkInfo()) == null) {
            return false;
        }
        return activeNetworkInfo.isConnectedOrConnecting();
    }

    public static boolean isConnectedWifi(Context context) {
        NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getNetworkInfo(1);
        if (networkInfo == null || networkInfo.getType() != 1 || !networkInfo.isConnected()) {
            return false;
        }
        return true;
    }

    public static boolean isEnableMobileData(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(0);
        boolean isAvailable = networkInfo != null ? networkInfo.isAvailable() : false;
        if (isAvailable) {
            try {
                isAvailable = Boolean.TRUE.equals(connectivityManager.getClass().getMethod("getMobileDataEnabled", new Class[0]).invoke(connectivityManager, new Object[0]));
                String str = TAG;
                EPLog.i(str, "getMobileDataEnabled :" + Boolean.valueOf(isAvailable).toString());
            } catch (Exception unused) {
                EPLog.e(TAG, "getMobileDataEnabled : Error");
            }
        }
        String str2 = TAG;
        EPLog.i(str2, "isEnableMobileData :" + Boolean.valueOf(isAvailable).toString());
        return isAvailable;
    }

    public static String getSSID(Context context) {
        try {
            return ((WifiManager) context.getSystemService(Service.WIFI_SERVICE)).getConnectionInfo().getSSID();
        } catch (SecurityException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static AlertDialog makeMessageBox(Context context, String str, String str2) {
        AlertDialog.Builder makeMassageBoxInternal = makeMassageBoxInternal(context, str2);
        makeMassageBoxInternal.setTitle((CharSequence) null);
        makeMassageBoxInternal.setMessage(str);
        return makeMassageBoxInternal.create();
    }

    public static AlertDialog makeMessageBox(Context context, String str, String str2, String str3) {
        AlertDialog.Builder makeMassageBoxInternal = makeMassageBoxInternal(context, str3);
        makeMassageBoxInternal.setTitle(str);
        makeMassageBoxInternal.setMessage(str2);
        return makeMassageBoxInternal.create();
    }

    private static AlertDialog.Builder makeMassageBoxInternal(Context context, String str) {
        CustomTitleAlertDialogBuilder customTitleAlertDialogBuilder = new CustomTitleAlertDialogBuilder(context);
        customTitleAlertDialogBuilder.setCancelable(false);
        customTitleAlertDialogBuilder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return keyEvent.getKeyCode() == 84;
            }
        });
        customTitleAlertDialogBuilder.setPositiveButton(str, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        return customTitleAlertDialogBuilder;
    }

    public static CustomTitleAlertDialogBuilder getCustomTitleDialogBuilder(Context context) {
        CustomTitleAlertDialogBuilder customTitleAlertDialogBuilder = new CustomTitleAlertDialogBuilder(context);
        customTitleAlertDialogBuilder.setCancelable(false);
        customTitleAlertDialogBuilder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return keyEvent.getKeyCode() == 84;
            }
        });
        return customTitleAlertDialogBuilder;
    }

    public void deleteTempFoler(String str) {
        File file = new File(str);
        File[] listFiles = file.listFiles();
        if (listFiles != null) {
            for (int i = 0; i < listFiles.length; i++) {
                if (listFiles[i].isFile()) {
                    listFiles[i].delete();
                } else if (listFiles[i].isDirectory()) {
                    deleteTempFoler(listFiles[i].toString());
                }
            }
        }
        if (file.exists() && file.isFile()) {
            file.delete();
        }
    }

    public static String getPreffix(String str) {
        if (str == null) {
            return null;
        }
        int lastIndexOf = str.lastIndexOf(CommonDefine.DOT);
        return (lastIndexOf == -1 || lastIndexOf == 0) ? str : str.substring(0, lastIndexOf);
    }

    public static String getSuffix(String str) {
        int lastIndexOf;
        if (str == null || (lastIndexOf = str.lastIndexOf(CommonDefine.DOT)) == -1 || lastIndexOf == 0) {
            return null;
        }
        return str.substring(lastIndexOf + 1);
    }

    @Nullable
    public static String getMimeType(@Nullable String str) {
        if (str == null) {
            return null;
        }
        return getExtType(getExtention(new File(str).getName()));
    }

    public static String getExtention(String str) {
        String suffix = getSuffix(str);
        if (suffix == null) {
            return null;
        }
        return CommonDefine.DOT + suffix;
    }

    public static String getMimeExt(String str) {
        if (str == null) {
            return null;
        }
        switch (MimeType.toMimeType(str)) {
            case PDF_TYPE:
                return CommonDefine.FileType_PDF;
            case DOC_TYPE_1:
                return CommonDefine.FileType_WORD_1;
            case DOCX_TYPE_2:
                return CommonDefine.FileType_WORD_2;
            case EXCEL_TYPE_1:
                return CommonDefine.FileType_EXCEL_1;
            case EXCEL_TYPE_2:
                return CommonDefine.FileType_EXCEL_2;
            case POWERPOINT_TYPE_1:
                return CommonDefine.FileType_POWERPOINT_1;
            case POWERPOINT_TYPE_2:
                return CommonDefine.FileType_POWERPOINT_2;
            case TEXT_TYPE:
                return CommonDefine.FileType_TXT;
            case RTF_TYPE:
                return CommonDefine.FileType_RTF;
            case CSV_TYPE:
                return CommonDefine.FileType_CSV;
            case HTML_TYPE:
                return CommonDefine.FileType_HTML_1;
            case IMAGE_TYPE:
                return ".jpg";
            case IMAGE_TYPE_PNG:
                return ".png";
            case IMAGE_TYPE_BMP:
                return ".bmp";
            case IMAGE_TYPE_HEIF:
                return CommonDefine.FileType_HEIF;
            default:
                char c = 65535;
                if (str.hashCode() == -1004747228 && str.equals(CommonDefine.CSV_TYPE)) {
                    c = 0;
                }
                if (c != 0) {
                    return null;
                }
                return CommonDefine.FileType_CSV;
        }
    }

    public static String getExtType(String str) {
        if (str == null) {
            return null;
        }
        String lowerCase = str.toLowerCase(Locale.ENGLISH);
        switch (ExtType.toExtType(lowerCase)) {
            case FileType_PDF:
                return "application/pdf";
            case FileType_WORD_1:
                return CommonDefine.DOC_TYPE_1;
            case FileType_WORD_2:
                return CommonDefine.DOCX_TYPE_2;
            case FileType_EXCEL_1:
                return CommonDefine.EXCEL_TYPE_1;
            case FileType_EXCEL_2:
                return CommonDefine.EXCEL_TYPE_2;
            case FileType_POWERPOINT_1:
                return CommonDefine.POWERPOINT_TYPE_1;
            case FileType_POWERPOINT_2:
                return CommonDefine.POWERPOINT_TYPE_2;
            case FileType_TXT:
                return CommonDefine.TEXT_TYPE;
            case FileType_RTF:
                return CommonDefine.RTF_TYPE;
            case FileType_CSV:
                return CommonDefine.CSV_TYPE;
            case FileType_HTML_1:
                return CommonDefine.HTML_TYPE;
            case FileType_HTML_2:
                return CommonDefine.HTML_TYPE;
            case FileType_JPG:
            case FileType_JPEG:
                return "image/jpeg";
            case FileType_PNG:
                return "image/png";
            case FileType_BMP:
                return CommonDefine.IMAGE_TYPE_BMP;
            case FileType_HEIF:
                return CommonDefine.IMAGE_TYPE_HEIF;
            default:
                return null;
        }
    }

    public static boolean isGConvertFile(String str) {
        String extention;
        if (str == null || (extention = getExtention(str)) == null) {
            return false;
        }
        String lowerCase = extention.toLowerCase(Locale.ENGLISH);
        switch (ExtType.toExtType(lowerCase)) {
            case FileType_WORD_1:
            case FileType_WORD_2:
            case FileType_EXCEL_1:
            case FileType_EXCEL_2:
            case FileType_POWERPOINT_1:
            case FileType_POWERPOINT_2:
            case FileType_TXT:
            case FileType_RTF:
            case FileType_CSV:
            case FileType_HTML_1:
            case FileType_HTML_2:
                return true;
            default:
                return false;
        }
    }

    public static boolean isPhotoFile(String str) {
        String extention;
        if (str == null || (extention = getExtention(str)) == null || getExtType(extention) == null) {
            return false;
        }
        switch (MimeType.toMimeType(r3)) {
            case IMAGE_TYPE:
            case IMAGE_TYPE_PNG:
            case IMAGE_TYPE_BMP:
                return true;
            case IMAGE_TYPE_HEIF:
                if (Build.VERSION.SDK_INT >= 28) {
                    return true;
                }
                return false;
            default:
                return false;
        }
    }

    public static String makeTempFileName(String str) throws FileNotFoundException {
        String valueOf = String.valueOf(System.currentTimeMillis());
        String mimeExt = getMimeExt(str);
        if (mimeExt != null) {
            return valueOf + mimeExt;
        }
        throw new FileNotFoundException();
    }

    public static boolean isECConvertFile(String str) {
        String extention;
        if (str == null || (extention = getExtention(str)) == null) {
            return false;
        }
        String lowerCase = extention.toLowerCase(Locale.ENGLISH);
        switch (ExtType.toExtType(lowerCase)) {
            case FileType_WORD_1:
            case FileType_WORD_2:
            case FileType_EXCEL_1:
            case FileType_EXCEL_2:
            case FileType_POWERPOINT_1:
            case FileType_POWERPOINT_2:
                return true;
            default:
                return false;
        }
    }

    public static boolean checkMimeType(String str, String str2) {
        return str2 != null && str2.equalsIgnoreCase(getExtType(getExtention(str)));
    }

    public static String getRealMimeType(String str) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(str, options);
        return options.outMimeType;
    }

    public static String getRemoteOperationUUID(Context context) {
        String prefString = getPrefString(context, CommonDefine.DEVICE_INFO, CommonDefine.REMOTE_OPERATION_UUID);
        if (!"".equals(prefString)) {
            return prefString;
        }
        String str = "urn:uuid:" + UUID.randomUUID().toString();
        savePref(context, CommonDefine.DEVICE_INFO, CommonDefine.REMOTE_OPERATION_UUID, str);
        return str;
    }

    public static boolean isMediaMounted() {
        return "mounted".equals(Environment.getExternalStorageState());
    }

    @TargetApi(11)
    public static void setFInishOnTOuchOutside(Activity activity, boolean z) {
        if (Build.VERSION.SDK_INT >= 11) {
            activity.setFinishOnTouchOutside(z);
        }
    }

    public static void setDrawble2TextView(Context context, TextView textView, int i) {
        Drawable drawable = context.getResources().getDrawable(i);
        int textSize = (textView.getTextSize() * 1.5f);
        drawable.setBounds(0, 0, textSize, textSize);
        textView.setCompoundDrawables(drawable, (Drawable) null, (Drawable) null, (Drawable) null);
    }

    public static java.lang.String getAssetsFileContents(android.content.Context r4, java.lang.String r5) {
        throw new UnsupportedOperationException("Method not decompiled: epson.common.Utils.getAssetsFileContents(android.content.Context, java.lang.String):java.lang.String");
    }

    public static java.lang.String[] replaceMessage(java.lang.Integer[] r5, int r6, android.content.Context r7) {

        throw new UnsupportedOperationException("Method not decompiled: epson.common.Utils.replaceMessage(java.lang.Integer[], int, android.content.Context):java.lang.String[]");
    }
}
