げました v1.8.0版本的 MD5 值为:063bfe30082b2b30d81635060fce57b1

以下内容为反编译后的 AndroidUtilities.java 源代码,内容仅作参考


package im.nacqryqyts.messenger;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.Configuration;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.hardware.camera2.CameraManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.PowerManager;
import android.provider.CallLog;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.Settings;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.util.StateSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.webkit.MimeTypeMap;
import android.widget.EdgeEffect;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.core.content.FileProvider;
import androidx.exifinterface.media.ExifInterface;
import androidx.viewpager.widget.ViewPager;
import com.android.internal.telephony.ITelephony;
import com.bumptech.glide.load.resource.bitmap.HardwareConfigState;
import com.google.android.exoplayer2.source.hls.DefaultHlsExtractorFactory;
import com.google.android.exoplayer2.text.ttml.TtmlNode;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.gms.auth.api.phone.SmsRetriever;
import com.google.android.gms.auth.api.phone.SmsRetrieverClient;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.king.zxing.util.LogUtils;
import com.litesuits.orm.db.assit.SQLBuilder;
import im.nacqryqyts.messenger.LocaleController;
import im.nacqryqyts.messenger.SharedConfig;
import im.nacqryqyts.phoneformat.PhoneFormat;
import im.nacqryqyts.tgnet.ConnectionsManager;
import im.nacqryqyts.tgnet.TLObject;
import im.nacqryqyts.tgnet.TLRPC;
import im.nacqryqyts.ui.ThemePreviewActivity;
import im.nacqryqyts.ui.WallpapersListActivity;
import im.nacqryqyts.ui.actionbar.AlertDialog;
import im.nacqryqyts.ui.actionbar.BaseFragment;
import im.nacqryqyts.ui.actionbar.BottomSheet;
import im.nacqryqyts.ui.actionbar.Theme;
import im.nacqryqyts.ui.cells.TextDetailSettingsCell;
import im.nacqryqyts.ui.components.AlertsCreator;
import im.nacqryqyts.ui.components.BackgroundGradientDrawable;
import im.nacqryqyts.ui.components.ForegroundDetector;
import im.nacqryqyts.ui.components.LayoutHelper;
import im.nacqryqyts.ui.components.PickerBottomLayout;
import im.nacqryqyts.ui.components.TypefaceSpan;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
import kotlin.UByte;
import org.slf4j.Marker;

public class AndroidUtilities {
    public static final int FLAG_TAG_ALL = 11;
    public static final int FLAG_TAG_BOLD = 2;
    public static final int FLAG_TAG_BR = 1;
    public static final int FLAG_TAG_COLOR = 4;
    public static final int FLAG_TAG_URL = 8;
    public static Pattern WEB_URL;
    private static RectF bitmapRect;
    private static ContentObserver callLogContentObserver;
    private static int[] documentIcons;
    private static int[] documentMediaIcons;
    public static boolean firstConfigurationWas;
    private static boolean hasCallPermissions;
    public static boolean incorrectDisplaySizeFix;
    public static boolean isInMultiwindow;
    public static int leftBaseline;
    private static Field mAttachInfoField;
    private static Field mStableInsetsField;
    public static int roundMessageSize;
    private static Paint roundPaint;
    private static Runnable unregisterRunnable;
    public static boolean usingHardwareInput;
    private static final Hashtable<String, Typeface> typefaceCache = new Hashtable<>();
    private static int prevOrientation = -10;
    private static boolean waitingForSms = false;
    private static boolean waitingForCall = false;
    private static final Object smsLock = new Object();
    private static final Object callLock = new Object();
    public static int statusBarHeight = 0;
    public static float density = 1.0f;
    public static Point displaySize = new Point();
    public static Integer photoSize = null;
    public static DisplayMetrics displayMetrics = new DisplayMetrics();
    public static DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();
    public static AccelerateInterpolator accelerateInterpolator = new AccelerateInterpolator();
    public static OvershootInterpolator overshootInterpolator = new OvershootInterpolator();
    private static Boolean isTablet = null;
    private static int adjustOwnerClassGuid = 0;

    static {
        WEB_URL = null;
        try {
            Pattern IP_ADDRESS = Pattern.compile("((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9]))");
            Pattern DOMAIN_NAME = Pattern.compile("(([a-zA-Z0-9 -\ud7ff豈-\ufdcfﷰ-\uffef]([a-zA-Z0-9 -\ud7ff豈-\ufdcfﷰ-\uffef\\-]{0,61}[a-zA-Z0-9 -\ud7ff豈-\ufdcfﷰ-\uffef]){0,1}\\.)+[a-zA-Z -\ud7ff豈-\ufdcfﷰ-\uffef]{2,63}|" + IP_ADDRESS + SQLBuilder.PARENTHESES_RIGHT);
            WEB_URL = Pattern.compile("((?:(http|https|Http|Https):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?(?:" + DOMAIN_NAME + ")(?:\\:\\d{1,5})?)(\\/(?:(?:[a-zA-Z0-9 -\ud7ff豈-\ufdcfﷰ-\uffef\\;\\/\\?\\:\\@\\&\\=\\#\\~\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?(?:\\b|$)");
        } catch (Exception e) {
            FileLog.e(e);
        }
        leftBaseline = isTablet() ? 80 : 72;
        checkDisplaySize(ApplicationLoader.applicationContext, null);
        documentIcons = new int[]{R.drawable.media_doc_blue, R.drawable.media_doc_green, R.drawable.media_doc_red, R.drawable.media_doc_yellow};
        documentMediaIcons = new int[]{R.drawable.media_doc_blue_b, R.drawable.media_doc_green_b, R.drawable.media_doc_red_b, R.drawable.media_doc_yellow_b};
        hasCallPermissions = Build.VERSION.SDK_INT >= 23;
    }

    public static int getThumbForNameOrMime(String name, String mime, boolean media) {
        if (name == null || name.length() == 0) {
            return media ? documentMediaIcons[0] : documentIcons[0];
        }
        int color = -1;
        if (name.contains(".doc") || name.contains(".txt") || name.contains(".psd")) {
            color = 0;
        } else if (name.contains(".xls") || name.contains(".csv")) {
            color = 1;
        } else if (name.contains(".pdf") || name.contains(".ppt") || name.contains(".key")) {
            color = 2;
        } else if (name.contains(".zip") || name.contains(".rar") || name.contains(".ai") || name.contains(DefaultHlsExtractorFactory.MP3_FILE_EXTENSION) || name.contains(".mov") || name.contains(".avi")) {
            color = 3;
        }
        if (color == -1) {
            int idx = name.lastIndexOf(46);
            String ext = idx == -1 ? "" : name.substring(idx + 1);
            if (ext.length() != 0) {
                color = ext.charAt(0) % documentIcons.length;
            } else {
                color = name.charAt(0) % documentIcons.length;
            }
        }
        return media ? documentMediaIcons[color] : documentIcons[color];
    }

    public static int[] calcDrawableColor(Drawable drawable) {
        int[] colors;
        Bitmap b;
        int bitmapColor = -16777216;
        int[] result = new int[4];
        try {
            if (!(drawable instanceof BitmapDrawable)) {
                if (drawable instanceof ColorDrawable) {
                    bitmapColor = ((ColorDrawable) drawable).getColor();
                } else if ((drawable instanceof BackgroundGradientDrawable) && (colors = ((BackgroundGradientDrawable) drawable).getColorsList()) != null && colors.length > 0) {
                    bitmapColor = colors[0];
                }
            } else {
                Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                if (bitmap != null && (b = Bitmaps.createScaledBitmap(bitmap, 1, 1, true)) != null) {
                    bitmapColor = b.getPixel(0, 0);
                    if (bitmap != b) {
                        b.recycle();
                    }
                }
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
        double[] hsv = rgbToHsv((bitmapColor >> 16) & 255, (bitmapColor >> 8) & 255, bitmapColor & 255);
        hsv[1] = Math.min(1.0d, hsv[1] + 0.05d + ((1.0d - hsv[1]) * 0.1d));
        double v = Math.max(FirebaseRemoteConfig.DEFAULT_VALUE_FOR_DOUBLE, hsv[2] * 0.65d);
        int[] rgb = hsvToRgb(hsv[0], hsv[1], v);
        result[0] = Color.argb(102, rgb[0], rgb[1], rgb[2]);
        result[1] = Color.argb(136, rgb[0], rgb[1], rgb[2]);
        double v2 = Math.max(FirebaseRemoteConfig.DEFAULT_VALUE_FOR_DOUBLE, hsv[2] * 0.72d);
        int[] rgb2 = hsvToRgb(hsv[0], hsv[1], v2);
        result[2] = Color.argb(102, rgb2[0], rgb2[1], rgb2[2]);
        result[3] = Color.argb(136, rgb2[0], rgb2[1], rgb2[2]);
        return result;
    }

    public static double[] rgbToHsv(int r, int g, int b) {
        double h;
        double h2;
        double d = r;
        Double.isNaN(d);
        double rf = d / 255.0d;
        double d2 = g;
        Double.isNaN(d2);
        double gf = d2 / 255.0d;
        double d3 = b;
        Double.isNaN(d3);
        double bf = d3 / 255.0d;
        double max = (rf <= gf || rf <= bf) ? gf > bf ? gf : bf : rf;
        double min = (rf >= gf || rf >= bf) ? gf < bf ? gf : bf : rf;
        double d4 = max - min;
        double s = FirebaseRemoteConfig.DEFAULT_VALUE_FOR_DOUBLE;
        if (max != FirebaseRemoteConfig.DEFAULT_VALUE_FOR_DOUBLE) {
            s = d4 / max;
        }
        if (max == min) {
            h2 = FirebaseRemoteConfig.DEFAULT_VALUE_FOR_DOUBLE;
        } else {
            if (rf > gf && rf > bf) {
                double d5 = (gf - bf) / d4;
                double min2 = gf < bf ? 6 : 0;
                Double.isNaN(min2);
                h = d5 + min2;
            } else if (gf > bf) {
                h = ((bf - rf) / d4) + 2.0d;
            } else {
                h = ((rf - gf) / d4) + 4.0d;
            }
            h2 = h / 6.0d;
        }
        return new double[]{h2, s, max};
    }

    private static int[] hsvToRgb(double h, double s, double v) {
        double r;
        double g = FirebaseRemoteConfig.DEFAULT_VALUE_FOR_DOUBLE;
        double b = FirebaseRemoteConfig.DEFAULT_VALUE_FOR_DOUBLE;
        double i = (int) Math.floor(h * 6.0d);
        Double.isNaN(i);
        double f = (6.0d * h) - i;
        double p = (1.0d - s) * v;
        double q = (1.0d - (f * s)) * v;
        double t = (1.0d - ((1.0d - f) * s)) * v;
        int i2 = ((int) i) % 6;
        if (i2 == 0) {
            r = v;
            g = t;
            b = p;
        } else if (i2 == 1) {
            r = q;
            g = v;
            b = p;
        } else if (i2 == 2) {
            r = p;
            g = v;
            b = t;
        } else if (i2 == 3) {
            r = p;
            g = q;
            b = v;
        } else if (i2 == 4) {
            r = t;
            g = p;
            b = v;
        } else if (i2 != 5) {
            r = 0.0d;
        } else {
            r = v;
            g = p;
            b = q;
        }
        return new int[]{(int) (r * 255.0d), (int) (g * 255.0d), (int) (b * 255.0d)};
    }

    public static void requestAdjustResize(Activity activity, int classGuid) {
        if (activity == null || isTablet()) {
            return;
        }
        activity.getWindow().setSoftInputMode(16);
        adjustOwnerClassGuid = classGuid;
    }

    public static void setAdjustResizeToNothing(Activity activity, int classGuid) {
        if (activity != null && !isTablet() && adjustOwnerClassGuid == classGuid) {
            activity.getWindow().setSoftInputMode(48);
        }
    }

    public static void removeAdjustResize(Activity activity, int classGuid) {
        if (activity != null && !isTablet() && adjustOwnerClassGuid == classGuid) {
            activity.getWindow().setSoftInputMode(32);
        }
    }

    public static boolean isGoogleMapsInstalled(final BaseFragment fragment) {
        try {
            ApplicationLoader.applicationContext.getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            if (fragment.getParentActivity() == null) {
                return false;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(fragment.getParentActivity());
            builder.setMessage(LocaleController.getString("InstallGoogleMaps", R.string.InstallGoogleMaps));
            builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), new DialogInterface.OnClickListener() {
                @Override
                public final void onClick(DialogInterface dialogInterface, int i) {
                    AndroidUtilities.lambda$isGoogleMapsInstalled$0(BaseFragment.this, dialogInterface, i);
                }
            });
            builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
            fragment.showDialog(builder.create());
            return false;
        }
    }

    public static void lambda$isGoogleMapsInstalled$0(BaseFragment fragment, DialogInterface dialogInterface, int i) {
        try {
            Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("market://details?id=com.google.android.apps.maps"));
            fragment.getParentActivity().startActivityForResult(intent, 500);
        } catch (Exception e1) {
            FileLog.e(e1);
        }
    }

    public static int[] toIntArray(List<Integer> integers) {
        int[] ret = new int[integers.size()];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = integers.get(i).intValue();
        }
        return ret;
    }

    public static boolean isInternalUri(Uri uri) {
        String pathString = uri.getPath();
        if (pathString == null) {
            return false;
        }
        if (pathString.matches(Pattern.quote(new File(ApplicationLoader.applicationContext.getCacheDir(), "voip_logs").getAbsolutePath()) + "/\\d+\\.log")) {
            return false;
        }
        int tries = 0;
        do {
            if (pathString != null && pathString.length() > 4096) {
                return true;
            }
            try {
                String newPath = Utilities.readlink(pathString);
                if (newPath != null && !newPath.equals(pathString)) {
                    pathString = newPath;
                    tries++;
                } else {
                    if (pathString != null) {
                        try {
                            String path = new File(pathString).getCanonicalPath();
                            if (path != null) {
                                pathString = path;
                            }
                        } catch (Exception e) {
                            pathString.replace("/./", "/");
                        }
                    }
                    if (pathString.endsWith(".attheme") || pathString == null) {
                        return false;
                    }
                    String lowerCase = pathString.toLowerCase();
                    StringBuilder sb = new StringBuilder();
                    sb.append("/data/data/");
                    sb.append(ApplicationLoader.applicationContext.getPackageName());
                    return lowerCase.contains(sb.toString());
                }
            } catch (Throwable th) {
                return true;
            }
        } while (tries < 10);
        return true;
    }

    public static void lockOrientation(Activity activity) {
        if (activity == null || prevOrientation != -10) {
            return;
        }
        try {
            prevOrientation = activity.getRequestedOrientation();
            WindowManager manager = (WindowManager) activity.getSystemService("window");
            if (manager != null && manager.getDefaultDisplay() != null) {
                int rotation = manager.getDefaultDisplay().getRotation();
                int orientation = activity.getResources().getConfiguration().orientation;
                if (rotation == 3) {
                    if (orientation == 1) {
                        activity.setRequestedOrientation(1);
                    } else {
                        activity.setRequestedOrientation(8);
                    }
                } else if (rotation == 1) {
                    if (orientation == 1) {
                        activity.setRequestedOrientation(9);
                    } else {
                        activity.setRequestedOrientation(0);
                    }
                } else if (rotation == 0) {
                    if (orientation == 2) {
                        activity.setRequestedOrientation(0);
                    } else {
                        activity.setRequestedOrientation(1);
                    }
                } else if (orientation == 2) {
                    activity.setRequestedOrientation(8);
                } else {
                    activity.setRequestedOrientation(9);
                }
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static void unlockOrientation(Activity activity) {
        if (activity == null) {
            return;
        }
        try {
            if (prevOrientation != -10) {
                activity.setRequestedOrientation(prevOrientation);
                prevOrientation = -10;
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    private static class VcardData {
        String name;
        ArrayList<String> phones;
        StringBuilder vcard;

        private VcardData() {
            this.phones = new ArrayList<>();
            this.vcard = new StringBuilder();
        }
    }

    public static class VcardItem {
        public int type;
        public ArrayList<String> vcardData = new ArrayList<>();
        public String fullData = "";
        public boolean checked = true;

        public String[] getRawValue() {
            byte[] bytes;
            int idx = this.fullData.indexOf(58);
            if (idx >= 0) {
                String valueType = this.fullData.substring(0, idx);
                String value = this.fullData.substring(idx + 1);
                String nameEncoding = null;
                String nameCharset = "UTF-8";
                String[] params = valueType.split(";");
                for (String str : params) {
                    String[] args2 = str.split("=");
                    if (args2.length == 2) {
                        if (args2[0].equals("CHARSET")) {
                            nameCharset = args2[1];
                        } else if (args2[0].equals("ENCODING")) {
                            nameEncoding = args2[1];
                        }
                    }
                }
                String[] args = value.split(";");
                for (int a = 0; a < args.length; a++) {
                    if (!TextUtils.isEmpty(args[a]) && nameEncoding != null && nameEncoding.equalsIgnoreCase("QUOTED-PRINTABLE") && (bytes = AndroidUtilities.decodeQuotedPrintable(AndroidUtilities.getStringBytes(args[a]))) != null && bytes.length != 0) {
                        try {
                            args[a] = new String(bytes, nameCharset);
                        } catch (Exception e) {
                        }
                    }
                }
                return args;
            }
            return new String[0];
        }

        public String getValue(boolean format) {
            byte[] bytes;
            StringBuilder result = new StringBuilder();
            int idx = this.fullData.indexOf(58);
            if (idx < 0) {
                return "";
            }
            if (result.length() > 0) {
                result.append(", ");
            }
            String valueType = this.fullData.substring(0, idx);
            String value = this.fullData.substring(idx + 1);
            String[] params = valueType.split(";");
            String nameEncoding = null;
            String nameCharset = "UTF-8";
            for (String str : params) {
                String[] args2 = str.split("=");
                if (args2.length == 2) {
                    if (args2[0].equals("CHARSET")) {
                        nameCharset = args2[1];
                    } else if (args2[0].equals("ENCODING")) {
                        nameEncoding = args2[1];
                    }
                }
            }
            String[] args = value.split(";");
            boolean added = false;
            for (int a = 0; a < args.length; a++) {
                if (!TextUtils.isEmpty(args[a])) {
                    if (nameEncoding != null && nameEncoding.equalsIgnoreCase("QUOTED-PRINTABLE") && (bytes = AndroidUtilities.decodeQuotedPrintable(AndroidUtilities.getStringBytes(args[a]))) != null && bytes.length != 0) {
                        try {
                            args[a] = new String(bytes, nameCharset);
                        } catch (Exception e) {
                        }
                    }
                    if (added && result.length() > 0) {
                        result.append(" ");
                    }
                    result.append(args[a]);
                    if (!added) {
                        added = args[a].length() > 0;
                    }
                }
            }
            if (format) {
                int i = this.type;
                if (i == 0) {
                    return PhoneFormat.getInstance().format(result.toString());
                }
                if (i == 5) {
                    String[] date = result.toString().split(ExifInterface.GPS_DIRECTION_TRUE);
                    if (date.length > 0) {
                        String[] date2 = date[0].split("-");
                        if (date2.length == 3) {
                            Calendar calendar = Calendar.getInstance();
                            calendar.set(1, Utilities.parseInt(date2[0]).intValue());
                            calendar.set(2, Utilities.parseInt(date2[1]).intValue() - 1);
                            calendar.set(5, Utilities.parseInt(date2[2]).intValue());
                            return LocaleController.getInstance().formatterYearMax.format(calendar.getTime());
                        }
                    }
                }
            }
            return result.toString();
        }

        public String getRawType(boolean first) {
            int idx = this.fullData.indexOf(58);
            if (idx < 0) {
                return "";
            }
            String value = this.fullData.substring(0, idx);
            if (this.type == 20) {
                String[] args = value.substring(2).split(";");
                if (first) {
                    return args[0];
                }
                if (args.length > 1) {
                    return args[args.length - 1];
                }
                return "";
            }
            String[] args2 = value.split(";");
            for (int a = 0; a < args2.length; a++) {
                if (args2[a].indexOf(61) < 0) {
                    value = args2[a];
                }
            }
            return value;
        }

        public String getType() {
            String value;
            int i = this.type;
            if (i == 5) {
                return LocaleController.getString("ContactBirthday", R.string.ContactBirthday);
            }
            if (i == 6) {
                if ("ORG".equalsIgnoreCase(getRawType(true))) {
                    return LocaleController.getString("ContactJob", R.string.ContactJob);
                }
                return LocaleController.getString("ContactJobTitle", R.string.ContactJobTitle);
            }
            int idx = this.fullData.indexOf(58);
            if (idx < 0) {
                return "";
            }
            String value2 = this.fullData.substring(0, idx);
            if (this.type == 20) {
                value = value2.substring(2).split(";")[0];
            } else {
                String[] args = value2.split(";");
                for (int a = 0; a < args.length; a++) {
                    if (args[a].indexOf(61) < 0) {
                        value2 = args[a];
                    }
                }
                if (value2.startsWith("X-")) {
                    value2 = value2.substring(2);
                }
                char c = 65535;
                switch (value2.hashCode()) {
                    case -2015525726:
                        if (value2.equals("MOBILE")) {
                            c = 2;
                            break;
                        }
                        break;
                    case 2064738:
                        if (value2.equals("CELL")) {
                            c = 3;
                            break;
                        }
                        break;
                    case 2223327:
                        if (value2.equals("HOME")) {
                            c = 1;
                            break;
                        }
                        break;
                    case 2464291:
                        if (value2.equals("PREF")) {
                            c = 0;
                            break;
                        }
                        break;
                    case 2670353:
                        if (value2.equals("WORK")) {
                            c = 5;
                            break;
                        }
                        break;
                    case 75532016:
                        if (value2.equals("OTHER")) {
                            c = 4;
                            break;
                        }
                        break;
                }
                if (c == 0) {
                    value = LocaleController.getString("PhoneMain", R.string.PhoneMain);
                } else if (c == 1) {
                    value = LocaleController.getString("PhoneHome", R.string.PhoneHome);
                } else if (c == 2 || c == 3) {
                    value = LocaleController.getString("PhoneMobile", R.string.PhoneMobile);
                } else if (c == 4) {
                    value = LocaleController.getString("PhoneOther", R.string.PhoneOther);
                } else {
                    value = c != 5 ? value2 : LocaleController.getString("PhoneWork", R.string.PhoneWork);
                }
            }
            return value.substring(0, 1).toUpperCase() + value.substring(1).toLowerCase();
        }
    }

    public static byte[] getStringBytes(String src) {
        try {
            return src.getBytes("UTF-8");
        } catch (Exception e) {
            return new byte[0];
        }
    }

    public static ArrayList<TLRPC.User> loadVCardFromStream(Uri uri, int currentAccount, boolean asset, ArrayList<VcardItem> items, String name) {
        InputStream stream;
        ArrayList<TLRPC.User> result;
        InputStream stream2;
        String[] args;
        String pendingLine;
        boolean currentIsPhoto;
        VcardItem currentItem;
        byte[] bytes;
        ArrayList<VcardItem> arrayList = items;
        ArrayList<TLRPC.User> result2 = null;
        if (asset) {
            try {
                AssetFileDescriptor fd = ApplicationLoader.applicationContext.getContentResolver().openAssetFileDescriptor(uri, "r");
                stream = fd.createInputStream();
            } catch (Throwable th) {
                e = th;
            }
        } else {
            try {
                ContentResolver cr = ApplicationLoader.applicationContext.getContentResolver();
                stream = cr.openInputStream(uri);
            } catch (Throwable th2) {
                e = th2;
            }
        }
        ArrayList<VcardData> vcardDatas = new ArrayList<>();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        VcardItem currentItem2 = null;
        boolean currentIsPhoto2 = false;
        String pendingLine2 = null;
        VcardData currentData = null;
        while (true) {
            String originalLine = bufferedReader.readLine();
            String line = originalLine;
            if (originalLine == null) {
                break;
            }
            if (originalLine.startsWith("PHOTO")) {
                currentIsPhoto2 = true;
            } else {
                if (originalLine.indexOf(58) >= 0) {
                    VcardItem currentItem3 = null;
                    currentIsPhoto2 = false;
                    try {
                        if (originalLine.startsWith("BEGIN:VCARD")) {
                            VcardData currentData2 = new VcardData();
                            vcardDatas.add(currentData2);
                            currentData2.name = name;
                            currentItem2 = null;
                            currentData = currentData2;
                        } else {
                            if (!originalLine.startsWith("END:VCARD") && arrayList != null) {
                                if (originalLine.startsWith("TEL")) {
                                    currentItem3 = new VcardItem();
                                    currentItem3.type = 0;
                                } else if (originalLine.startsWith("EMAIL")) {
                                    currentItem3 = new VcardItem();
                                    currentItem3.type = 1;
                                } else {
                                    if (!originalLine.startsWith("ADR") && !originalLine.startsWith("LABEL") && !originalLine.startsWith("GEO")) {
                                        if (originalLine.startsWith("URL")) {
                                            currentItem3 = new VcardItem();
                                            currentItem3.type = 3;
                                        } else if (originalLine.startsWith("NOTE")) {
                                            currentItem3 = new VcardItem();
                                            currentItem3.type = 4;
                                        } else if (originalLine.startsWith("BDAY")) {
                                            currentItem3 = new VcardItem();
                                            currentItem3.type = 5;
                                        } else if (!originalLine.startsWith("ORG") && !originalLine.startsWith("TITLE") && !originalLine.startsWith("ROLE")) {
                                            if (originalLine.startsWith("X-ANDROID")) {
                                                currentItem3 = new VcardItem();
                                                currentItem3.type = -1;
                                            } else if (originalLine.startsWith("X-PHONETIC")) {
                                                currentItem3 = null;
                                            } else if (originalLine.startsWith("X-")) {
                                                currentItem3 = new VcardItem();
                                                currentItem3.type = 20;
                                            }
                                        }
                                    }
                                    currentItem3 = new VcardItem();
                                    currentItem3.type = 2;
                                }
                                if (currentItem3 != null && currentItem3.type >= 0) {
                                    arrayList.add(currentItem3);
                                }
                            }
                            currentItem2 = currentItem3;
                        }
                    } catch (Throwable th3) {
                        e = th3;
                    }
                }
                if (!currentIsPhoto2 && currentData != null) {
                    if (currentItem2 == null) {
                        if (currentData.vcard.length() > 0) {
                            currentData.vcard.append('\n');
                        }
                        currentData.vcard.append(originalLine);
                    } else {
                        currentItem2.vcardData.add(originalLine);
                    }
                }
                if (pendingLine2 != null) {
                    line = pendingLine2 + line;
                    pendingLine2 = null;
                }
                String str = "=";
                if (line.contains("=QUOTED-PRINTABLE") && line.endsWith("=")) {
                    pendingLine2 = line.substring(0, line.length() - 1);
                } else {
                    if (!currentIsPhoto2 && currentData != null && currentItem2 != null) {
                        currentItem2.fullData = line;
                    }
                    int idx = line.indexOf(LogUtils.COLON);
                    if (idx >= 0) {
                        result = result2;
                        try {
                            args = new String[]{line.substring(0, idx), line.substring(idx + 1).trim()};
                        } catch (Throwable th4) {
                            e = th4;
                            result2 = result;
                        }
                    } else {
                        result = result2;
                        args = new String[]{line.trim()};
                    }
                    if (args.length < 2) {
                        pendingLine = pendingLine2;
                        currentIsPhoto = currentIsPhoto2;
                        currentItem = currentItem2;
                    } else if (currentData == null) {
                        pendingLine = pendingLine2;
                        currentIsPhoto = currentIsPhoto2;
                        currentItem = currentItem2;
                    } else {
                        if (!args[0].startsWith("FN") && (!args[0].startsWith("ORG") || !TextUtils.isEmpty(currentData.name))) {
                            if (args[0].startsWith("TEL")) {
                                currentData.phones.add(args[1]);
                                pendingLine = pendingLine2;
                                currentIsPhoto = currentIsPhoto2;
                                currentItem = currentItem2;
                            } else {
                                pendingLine = pendingLine2;
                                currentIsPhoto = currentIsPhoto2;
                                currentItem = currentItem2;
                            }
                        }
                        String nameEncoding = null;
                        String nameCharset = null;
                        String[] params = args[0].split(";");
                        int length = params.length;
                        pendingLine = pendingLine2;
                        int i = 0;
                        while (i < length) {
                            String param = params[i];
                            boolean currentIsPhoto3 = currentIsPhoto2;
                            String[] args2 = param.split(str);
                            String str2 = str;
                            VcardItem currentItem4 = currentItem2;
                            if (args2.length == 2) {
                                if (args2[0].equals("CHARSET")) {
                                    nameCharset = args2[1];
                                } else if (args2[0].equals("ENCODING")) {
                                    nameEncoding = args2[1];
                                }
                            }
                            i++;
                            currentIsPhoto2 = currentIsPhoto3;
                            str = str2;
                            currentItem2 = currentItem4;
                        }
                        currentIsPhoto = currentIsPhoto2;
                        currentItem = currentItem2;
                        currentData.name = args[1];
                        if (nameEncoding != null && nameEncoding.equalsIgnoreCase("QUOTED-PRINTABLE") && (bytes = decodeQuotedPrintable(getStringBytes(currentData.name))) != null && bytes.length != 0) {
                            String decodedName = new String(bytes, nameCharset);
                            currentData.name = decodedName;
                        }
                    }
                    arrayList = items;
                    result2 = result;
                    pendingLine2 = pendingLine;
                    currentIsPhoto2 = currentIsPhoto;
                    currentItem2 = currentItem;
                }
            }
            e = th4;
            result2 = result;
            FileLog.e(e);
            return result2;
        }
        result = result2;
        bufferedReader.close();
        stream.close();
        int a = 0;
        result2 = result;
        while (a < vcardDatas.size()) {
            VcardData vcardData = vcardDatas.get(a);
            if (vcardData.name == null || vcardData.phones.isEmpty()) {
                stream2 = stream;
            } else {
                if (result2 == null) {
                    result2 = new ArrayList<>();
                }
                String phoneToUse = vcardData.phones.get(0);
                int b = 0;
                while (true) {
                    if (b >= vcardData.phones.size()) {
                        stream2 = stream;
                        break;
                    }
                    String phone = vcardData.phones.get(b);
                    String phoneToUse2 = phoneToUse;
                    stream2 = stream;
                    String sphone = phone.substring(Math.max(0, phone.length() - 7));
                    if (ContactsController.getInstance(currentAccount).contactsByShortPhone.get(sphone) != null) {
                        phoneToUse = phone;
                        break;
                    }
                    b++;
                    stream = stream2;
                    phoneToUse = phoneToUse2;
                }
                TLRPC.User user = new TLRPC.TL_userContact_old2();
                user.phone = phoneToUse;
                user.first_name = vcardData.name;
                user.last_name = "";
                user.id = 0;
                TLRPC.TL_restrictionReason reason = new TLRPC.TL_restrictionReason();
                reason.text = vcardData.vcard.toString();
                reason.platform = "";
                reason.reason = "";
                user.restriction_reason.add(reason);
                result2.add(user);
            }
            a++;
            stream = stream2;
        }
        return result2;
    }

    public static Typeface getTypeface(String assetPath) {
        Typeface t;
        Typeface typeface;
        synchronized (typefaceCache) {
            if (!typefaceCache.containsKey(assetPath)) {
                try {
                    if (Build.VERSION.SDK_INT >= 26) {
                        Typeface.Builder builder = new Typeface.Builder(ApplicationLoader.applicationContext.getAssets(), assetPath);
                        if (assetPath.contains("medium")) {
                            builder.setWeight(HardwareConfigState.DEFAULT_MAXIMUM_FDS_FOR_HARDWARE_CONFIGS);
                        }
                        if (assetPath.contains(TtmlNode.ITALIC)) {
                            builder.setItalic(true);
                        }
                        t = builder.build();
                    } else {
                        t = Typeface.createFromAsset(ApplicationLoader.applicationContext.getAssets(), assetPath);
                    }
                    typefaceCache.put(assetPath, t);
                } catch (Exception e) {
                    if (BuildVars.LOGS_ENABLED) {
                        FileLog.e("Could not get typeface '" + assetPath + "' because " + e.getMessage());
                    }
                    return null;
                }
            }
            typeface = typefaceCache.get(assetPath);
        }
        return typeface;
    }

    public static boolean isWaitingForSms() {
        boolean value;
        synchronized (smsLock) {
            value = waitingForSms;
        }
        return value;
    }

    public static void setWaitingForSms(boolean value) {
        synchronized (smsLock) {
            waitingForSms = value;
            if (value) {
                try {
                    SmsRetrieverClient client = SmsRetriever.getClient(ApplicationLoader.applicationContext);
                    Task<Void> task = client.startSmsRetriever();
                    task.addOnSuccessListener(new OnSuccessListener() {
                        @Override
                        public final void onSuccess(Object obj) {
                            AndroidUtilities.lambda$setWaitingForSms$1((Void) obj);
                        }
                    });
                } catch (Throwable e) {
                    FileLog.e(e);
                }
            }
        }
    }

    public static void lambda$setWaitingForSms$1(Void aVoid) {
        if (BuildVars.DEBUG_VERSION) {
            FileLog.d("sms listener registered");
        }
    }

    public static int getShadowHeight() {
        float f = density;
        if (f >= 4.0f) {
            return 3;
        }
        if (f >= 2.0f) {
            return 2;
        }
        return 1;
    }

    public static boolean isWaitingForCall() {
        boolean value;
        synchronized (callLock) {
            value = waitingForCall;
        }
        return value;
    }

    public static void setWaitingForCall(boolean value) {
        synchronized (callLock) {
            waitingForCall = value;
        }
    }

    public static boolean showKeyboard(View view) {
        if (view == null) {
            return false;
        }
        try {
            InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService("input_method");
            return inputManager.showSoftInput(view, 1);
        } catch (Exception e) {
            FileLog.e(e);
            return false;
        }
    }

    public static boolean isKeyboardShowed(View view) {
        if (view == null) {
            return false;
        }
        try {
            InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService("input_method");
            return inputManager.isActive(view);
        } catch (Exception e) {
            FileLog.e(e);
            return false;
        }
    }

    public static String[] getCurrentKeyboardLanguage() {
        String str;
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) ApplicationLoader.applicationContext.getSystemService("input_method");
            InputMethodSubtype currentInputMethodSubtype = inputMethodManager.getCurrentInputMethodSubtype();
            String str2 = null;
            if (currentInputMethodSubtype != null) {
                if (Build.VERSION.SDK_INT >= 24) {
                    str2 = currentInputMethodSubtype.getLanguageTag();
                }
                if (TextUtils.isEmpty(str2)) {
                    str2 = currentInputMethodSubtype.getLocale();
                }
            } else {
                InputMethodSubtype lastInputMethodSubtype = inputMethodManager.getLastInputMethodSubtype();
                if (lastInputMethodSubtype != null) {
                    if (Build.VERSION.SDK_INT >= 24) {
                        str2 = lastInputMethodSubtype.getLanguageTag();
                    }
                    if (TextUtils.isEmpty(str2)) {
                        str2 = lastInputMethodSubtype.getLocale();
                    }
                }
            }
            if (TextUtils.isEmpty(str2)) {
                String systemLocaleStringIso639 = LocaleController.getSystemLocaleStringIso639();
                LocaleController.LocaleInfo currentLocaleInfo = LocaleController.getInstance().getCurrentLocaleInfo();
                String baseLangCode = currentLocaleInfo.getBaseLangCode();
                boolean isEmpty = TextUtils.isEmpty(baseLangCode);
                String str3 = baseLangCode;
                if (isEmpty) {
                    str3 = currentLocaleInfo.getLangCode();
                }
                if (!systemLocaleStringIso639.contains(str3)) {
                    boolean contains = str3.contains(systemLocaleStringIso639);
                    str = str3;
                }
                if (!systemLocaleStringIso639.contains("en")) {
                    str = "en";
                } else {
                    str = null;
                }
                if (!TextUtils.isEmpty(str)) {
                    return new String[]{systemLocaleStringIso639.replace('_', '-'), str};
                }
                return new String[]{systemLocaleStringIso639.replace('_', '-')};
            }
            return new String[]{str2.replace('_', '-')};
        } catch (Exception e) {
            return new String[]{"en"};
        }
    }

    public static void hideKeyboard(View view) {
        if (view == null) {
            return;
        }
        try {
            InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService("input_method");
            if (!imm.isActive()) {
                return;
            }
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static File getCacheDir() {
        String state = null;
        try {
            state = Environment.getExternalStorageState();
        } catch (Exception e) {
            FileLog.e(e);
        }
        if (state == null || state.startsWith("mounted")) {
            try {
                File file = ApplicationLoader.applicationContext.getExternalCacheDir();
                if (file != null) {
                    return file;
                }
            } catch (Exception e2) {
                FileLog.e(e2);
            }
        }
        try {
            File file2 = ApplicationLoader.applicationContext.getCacheDir();
            if (file2 != null) {
                return file2;
            }
        } catch (Exception e3) {
            FileLog.e(e3);
        }
        return new File("");
    }

    public static int dp(float value) {
        if (value == 0.0f) {
            return 0;
        }
        return (int) Math.ceil(density * value);
    }

    public static int dpr(float value) {
        if (value == 0.0f) {
            return 0;
        }
        return Math.round(density * value);
    }

    public static int dp2(float value) {
        if (value == 0.0f) {
            return 0;
        }
        return (int) Math.floor(density * value);
    }

    public static int compare(int lhs, int rhs) {
        if (lhs == rhs) {
            return 0;
        }
        if (lhs > rhs) {
            return 1;
        }
        return -1;
    }

    public static float dpf2(float value) {
        if (value == 0.0f) {
            return 0.0f;
        }
        return density * value;
    }

    public static float sp2px(float spValue) {
        if (spValue == 0.0f || ApplicationLoader.applicationContext == null) {
            return 0.0f;
        }
        return (ApplicationLoader.applicationContext.getResources().getDisplayMetrics().scaledDensity * spValue) + 0.5f;
    }

    public static void checkDisplaySize(Context context, Configuration newConfiguration) {
        Display display;
        try {
            int oldDensity = (int) density;
            float f = context.getResources().getDisplayMetrics().density;
            density = f;
            int newDensity = (int) f;
            if (firstConfigurationWas && oldDensity != newDensity) {
                Theme.reloadAllResources(context);
            }
            boolean z = true;
            firstConfigurationWas = true;
            Configuration configuration = newConfiguration;
            if (configuration == null) {
                configuration = context.getResources().getConfiguration();
            }
            if (configuration.keyboard == 1 || configuration.hardKeyboardHidden != 1) {
                z = false;
            }
            usingHardwareInput = z;
            WindowManager manager = (WindowManager) context.getSystemService("window");
            if (manager != null && (display = manager.getDefaultDisplay()) != null) {
                display.getMetrics(displayMetrics);
                display.getSize(displaySize);
            }
            if (configuration.screenWidthDp != 0) {
                int newSize = (int) Math.ceil(configuration.screenWidthDp * density);
                if (Math.abs(displaySize.x - newSize) > 3) {
                    displaySize.x = newSize;
                }
            }
            int newSize2 = configuration.screenHeightDp;
            if (newSize2 != 0) {
                int newSize3 = (int) Math.ceil(configuration.screenHeightDp * density);
                if (Math.abs(displaySize.y - newSize3) > 3) {
                    displaySize.y = newSize3;
                }
            }
            int newSize4 = roundMessageSize;
            if (newSize4 == 0) {
                if (isTablet()) {
                    roundMessageSize = (int) (getMinTabletSide() * 0.6f);
                } else {
                    roundMessageSize = (int) (Math.min(displaySize.x, displaySize.y) * 0.6f);
                }
            }
            if (BuildVars.LOGS_ENABLED) {
                FileLog.e("display size = " + displaySize.x + " " + displaySize.y + " " + displayMetrics.xdpi + "x" + displayMetrics.ydpi);
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static double fixLocationCoord(double value) {
        double d = (long) (value * 1000000.0d);
        Double.isNaN(d);
        return d / 1000000.0d;
    }

    public static String formapMapUrl(int account, double lat, double lon, int width, int height, boolean marker, int zoom) {
        int scale = Math.min(2, (int) Math.ceil(density));
        int provider = MessagesController.getInstance(account).mapProvider;
        if (provider == 1 || provider == 3) {
            String lang = null;
            String[] availableLangs = {"ru_RU", "tr_TR"};
            LocaleController.LocaleInfo localeInfo = LocaleController.getInstance().getCurrentLocaleInfo();
            for (int a = 0; a < availableLangs.length; a++) {
                if (availableLangs[a].toLowerCase().contains(localeInfo.shortName)) {
                    lang = availableLangs[a];
                }
            }
            if (lang == null) {
                lang = "en_US";
            }
            return marker ? String.format(Locale.US, "https://static-maps.yandex.ru/1.x/?ll=%.6f,%.6f&z=%d&size=%d,%d&l=map&scale=%d&pt=%.6f,%.6f,vkbkm&lang=%s", Double.valueOf(lon), Double.valueOf(lat), Integer.valueOf(zoom), Integer.valueOf(width * scale), Integer.valueOf(height * scale), Integer.valueOf(scale), Double.valueOf(lon), Double.valueOf(lat), lang) : String.format(Locale.US, "https://static-maps.yandex.ru/1.x/?ll=%.6f,%.6f&z=%d&size=%d,%d&l=map&scale=%d&lang=%s", Double.valueOf(lon), Double.valueOf(lat), Integer.valueOf(zoom), Integer.valueOf(width * scale), Integer.valueOf(height * scale), Integer.valueOf(scale), lang);
        }
        String k = ApplicationLoader.applicationContext.getResources().getString(R.string.baidu_map_key);
        String mCode = ApplicationLoader.applicationContext.getResources().getString(R.string.baidu_map_code);
        return marker ? String.format(Locale.US, "http://api.map.baidu.com/staticimage/v2?ak=%s&mcode=%s&center=%.6f,%.6f&width=%d&height=%d&zoom=%d&scale=%d&coordtype=gcj02ll&copyright=1&dpiType=ph&markers=%.6f,%.6f&markerStyles=l,,red", k, mCode, Double.valueOf(lon), Double.valueOf(lat), Integer.valueOf(width), Integer.valueOf(height), Integer.valueOf(zoom), Integer.valueOf(scale), Double.valueOf(lon), Double.valueOf(lat)) : String.format(Locale.US, "http://api.map.baidu.com/staticimage/v2?ak=%s&mcode=%s&center=%.6f,%.6f&width=%d&height=%d&zoom=%d&scale=%d&coordtype=gcj02ll&copyright=1&dpiType=ph", k, mCode, Double.valueOf(lon), Double.valueOf(lat), Integer.valueOf(width), Integer.valueOf(height), Integer.valueOf(zoom), Integer.valueOf(scale));
    }

    public static float getPixelsInCM(float cm, boolean isX) {
        float f = cm / 2.54f;
        DisplayMetrics displayMetrics2 = displayMetrics;
        return f * (isX ? displayMetrics2.xdpi : displayMetrics2.ydpi);
    }

    public static int getMyLayerVersion(int layer) {
        return 65535 & layer;
    }

    public static int getPeerLayerVersion(int layer) {
        return (layer >> 16) & 65535;
    }

    public static int setMyLayerVersion(int layer, int version) {
        return ((-65536) & layer) | version;
    }

    public static int setPeerLayerVersion(int layer, int version) {
        return (65535 & layer) | (version << 16);
    }

    public static void runOnUIThread(Runnable runnable) {
        runOnUIThread(runnable, 0L);
    }

    public static void runOnUIThread(Runnable runnable, long delay) {
        if (delay == 0) {
            ApplicationLoader.applicationHandler.post(runnable);
        } else {
            ApplicationLoader.applicationHandler.postDelayed(runnable, delay);
        }
    }

    public static void cancelRunOnUIThread(Runnable runnable) {
        ApplicationLoader.applicationHandler.removeCallbacks(runnable);
    }

    public static boolean isTablet() {
        if (isTablet == null) {
            isTablet = Boolean.valueOf(ApplicationLoader.applicationContext.getResources().getBoolean(R.bool.isTablet));
        }
        return isTablet.booleanValue();
    }

    public static boolean isSmallTablet() {
        float minSide = Math.min(displaySize.x, displaySize.y) / density;
        return minSide <= 700.0f;
    }

    public static int getMinTabletSide() {
        if (!isSmallTablet()) {
            int smallSide = Math.min(displaySize.x, displaySize.y);
            int leftSide = (smallSide * 35) / 100;
            if (leftSide < dp(320.0f)) {
                leftSide = dp(320.0f);
            }
            return smallSide - leftSide;
        }
        int smallSide2 = Math.min(displaySize.x, displaySize.y);
        int maxSide = Math.max(displaySize.x, displaySize.y);
        int leftSide2 = (maxSide * 35) / 100;
        if (leftSide2 < dp(320.0f)) {
            leftSide2 = dp(320.0f);
        }
        return Math.min(smallSide2, maxSide - leftSide2);
    }

    public static int getPhotoSize() {
        if (photoSize == null) {
            photoSize = 1280;
        }
        return photoSize.intValue();
    }

    public static void endIncomingCall() {
        if (!hasCallPermissions) {
            return;
        }
        try {
            TelephonyManager tm = (TelephonyManager) ApplicationLoader.applicationContext.getSystemService("phone");
            Class c = Class.forName(tm.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony", new Class[0]);
            m.setAccessible(true);
            ITelephony telephonyService = (ITelephony) m.invoke(tm, new Object[0]);
            telephonyService.silenceRinger();
            telephonyService.endCall();
        } catch (Throwable e) {
            FileLog.e(e);
        }
    }

    public static String obtainLoginPhoneCall(String pattern) {
        if (!hasCallPermissions) {
            return null;
        }
        try {
            Cursor cursor = ApplicationLoader.applicationContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, new String[]{"number", "date"}, "type IN (3,1,5)", null, "date DESC LIMIT 5");
            while (cursor.moveToNext()) {
                try {
                    String number = cursor.getString(0);
                    long date = cursor.getLong(1);
                    if (BuildVars.LOGS_ENABLED) {
                        FileLog.e("number = " + number);
                    }
                    if (Math.abs(System.currentTimeMillis() - date) < 3600000 && checkPhonePattern(pattern, number)) {
                        if (cursor != null) {
                            cursor.close();
                        }
                        return number;
                    }
                } finally {
                }
            }
            if (cursor != null) {
                cursor.close();
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
        return null;
    }

    public static boolean checkPhonePattern(String pattern, String phone) {
        if (TextUtils.isEmpty(pattern) || pattern.equals("*")) {
            return true;
        }
        String[] args = pattern.split("\\*");
        String phone2 = PhoneFormat.stripExceptNumbers(phone);
        int checkStart = 0;
        for (String arg : args) {
            if (!TextUtils.isEmpty(arg)) {
                int index = phone2.indexOf(arg, checkStart);
                if (index == -1) {
                    return false;
                }
                checkStart = arg.length() + index;
            }
        }
        return true;
    }

    public static int getViewInset(View view) {
        if (view == null || Build.VERSION.SDK_INT < 21 || view.getHeight() == displaySize.y || view.getHeight() == displaySize.y - statusBarHeight) {
            return 0;
        }
        try {
            if (mAttachInfoField == null) {
                Field declaredField = View.class.getDeclaredField("mAttachInfo");
                mAttachInfoField = declaredField;
                declaredField.setAccessible(true);
            }
            Object mAttachInfo = mAttachInfoField.get(view);
            if (mAttachInfo != null) {
                if (mStableInsetsField == null) {
                    Field declaredField2 = mAttachInfo.getClass().getDeclaredField("mStableInsets");
                    mStableInsetsField = declaredField2;
                    declaredField2.setAccessible(true);
                }
                Rect insets = (Rect) mStableInsetsField.get(mAttachInfo);
                return insets.bottom;
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
        return 0;
    }

    public static Point getRealScreenSize() {
        Point size = new Point();
        try {
            WindowManager windowManager = (WindowManager) ApplicationLoader.applicationContext.getSystemService("window");
            if (Build.VERSION.SDK_INT >= 17) {
                windowManager.getDefaultDisplay().getRealSize(size);
            } else {
                try {
                    Method mGetRawW = Display.class.getMethod("getRawWidth", new Class[0]);
                    Method mGetRawH = Display.class.getMethod("getRawHeight", new Class[0]);
                    size.set(((Integer) mGetRawW.invoke(windowManager.getDefaultDisplay(), new Object[0])).intValue(), ((Integer) mGetRawH.invoke(windowManager.getDefaultDisplay(), new Object[0])).intValue());
                } catch (Exception e) {
                    size.set(windowManager.getDefaultDisplay().getWidth(), windowManager.getDefaultDisplay().getHeight());
                    FileLog.e(e);
                }
            }
        } catch (Exception e2) {
            FileLog.e(e2);
        }
        return size;
    }

    public static void setEnabled(View view, boolean enabled) {
        if (view == null) {
            return;
        }
        view.setEnabled(enabled);
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                setEnabled(viewGroup.getChildAt(i), enabled);
            }
        }
    }

    public static CharSequence getTrimmedString(CharSequence src) {
        if (src == null || src.length() == 0) {
            return src;
        }
        while (src.length() > 0 && (src.charAt(0) == '\n' || src.charAt(0) == ' ')) {
            src = src.subSequence(1, src.length());
        }
        while (src.length() > 0 && (src.charAt(src.length() - 1) == '\n' || src.charAt(src.length() - 1) == ' ')) {
            src = src.subSequence(0, src.length() - 1);
        }
        return src;
    }

    public static void setViewPagerEdgeEffectColor(ViewPager viewPager, int color) {
        if (Build.VERSION.SDK_INT >= 21) {
            try {
                Field field = ViewPager.class.getDeclaredField("mLeftEdge");
                field.setAccessible(true);
                EdgeEffect mLeftEdge = (EdgeEffect) field.get(viewPager);
                if (mLeftEdge != null) {
                    mLeftEdge.setColor(color);
                }
                Field field2 = ViewPager.class.getDeclaredField("mRightEdge");
                field2.setAccessible(true);
                EdgeEffect mRightEdge = (EdgeEffect) field2.get(viewPager);
                if (mRightEdge != null) {
                    mRightEdge.setColor(color);
                }
            } catch (Exception e) {
            }
        }
    }

    public static void setScrollViewEdgeEffectColor(HorizontalScrollView scrollView, int color) {
        if (Build.VERSION.SDK_INT >= 21) {
            try {
                Field field = HorizontalScrollView.class.getDeclaredField("mEdgeGlowLeft");
                field.setAccessible(true);
                EdgeEffect mEdgeGlowTop = (EdgeEffect) field.get(scrollView);
                if (mEdgeGlowTop != null) {
                    mEdgeGlowTop.setColor(color);
                }
                Field field2 = HorizontalScrollView.class.getDeclaredField("mEdgeGlowRight");
                field2.setAccessible(true);
                EdgeEffect mEdgeGlowBottom = (EdgeEffect) field2.get(scrollView);
                if (mEdgeGlowBottom != null) {
                    mEdgeGlowBottom.setColor(color);
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    }

    public static void setScrollViewEdgeEffectColor(ScrollView scrollView, int color) {
        if (Build.VERSION.SDK_INT >= 21) {
            try {
                Field field = ScrollView.class.getDeclaredField("mEdgeGlowTop");
                field.setAccessible(true);
                EdgeEffect mEdgeGlowTop = (EdgeEffect) field.get(scrollView);
                if (mEdgeGlowTop != null) {
                    mEdgeGlowTop.setColor(color);
                }
                Field field2 = ScrollView.class.getDeclaredField("mEdgeGlowBottom");
                field2.setAccessible(true);
                EdgeEffect mEdgeGlowBottom = (EdgeEffect) field2.get(scrollView);
                if (mEdgeGlowBottom != null) {
                    mEdgeGlowBottom.setColor(color);
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    }

    public static void clearDrawableAnimation(View view) {
        if (Build.VERSION.SDK_INT < 21 || view == null) {
            return;
        }
        if (view instanceof ListView) {
            Drawable drawable = ((ListView) view).getSelector();
            if (drawable != null) {
                drawable.setState(StateSet.NOTHING);
                return;
            }
            return;
        }
        Drawable drawable2 = view.getBackground();
        if (drawable2 != null) {
            drawable2.setState(StateSet.NOTHING);
            drawable2.jumpToCurrentState();
        }
    }

    public static SpannableStringBuilder replaceTags(String str) {
        return replaceTags(str, 11, new Object[0]);
    }

    public static SpannableStringBuilder replaceTags(String str, int flag, Object... args) {
        try {
            StringBuilder stringBuilder = new StringBuilder(str);
            if ((flag & 1) != 0) {
                while (true) {
                    int start = stringBuilder.indexOf("<br>");
                    if (start == -1) {
                        break;
                    }
                    stringBuilder.replace(start, start + 4, "\n");
                }
                while (true) {
                    int start2 = stringBuilder.indexOf("<br/>");
                    if (start2 == -1) {
                        break;
                    }
                    stringBuilder.replace(start2, start2 + 5, "\n");
                }
            }
            ArrayList<Integer> bolds = new ArrayList<>();
            if ((flag & 2) != 0) {
                while (true) {
                    int start3 = stringBuilder.indexOf("<b>");
                    if (start3 == -1) {
                        break;
                    }
                    stringBuilder.replace(start3, start3 + 3, "");
                    int end = stringBuilder.indexOf("</b>");
                    if (end == -1) {
                        end = stringBuilder.indexOf("<b>");
                    }
                    stringBuilder.replace(end, end + 4, "");
                    bolds.add(Integer.valueOf(start3));
                    bolds.add(Integer.valueOf(end));
                }
                while (true) {
                    int start4 = stringBuilder.indexOf("**");
                    if (start4 == -1) {
                        break;
                    }
                    stringBuilder.replace(start4, start4 + 2, "");
                    int end2 = stringBuilder.indexOf("**");
                    if (end2 >= 0) {
                        stringBuilder.replace(end2, end2 + 2, "");
                        bolds.add(Integer.valueOf(start4));
                        bolds.add(Integer.valueOf(end2));
                    }
                }
            }
            if ((flag & 8) != 0) {
                while (true) {
                    int start5 = stringBuilder.indexOf("**");
                    if (start5 == -1) {
                        break;
                    }
                    stringBuilder.replace(start5, start5 + 2, "");
                    int end3 = stringBuilder.indexOf("**");
                    if (end3 >= 0) {
                        stringBuilder.replace(end3, end3 + 2, "");
                        bolds.add(Integer.valueOf(start5));
                        bolds.add(Integer.valueOf(end3));
                    }
                }
            }
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(stringBuilder);
            for (int a = 0; a < bolds.size() / 2; a++) {
                spannableStringBuilder.setSpan(new TypefaceSpan(getTypeface("fonts/rmedium.ttf")), bolds.get(a * 2).intValue(), bolds.get((a * 2) + 1).intValue(), 33);
            }
            return spannableStringBuilder;
        } catch (Exception e) {
            FileLog.e(e);
            return new SpannableStringBuilder(str);
        }
    }

    public static class LinkMovementMethodMy extends LinkMovementMethod {
        @Override
        public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
            try {
                boolean result = super.onTouchEvent(widget, buffer, event);
                if (event.getAction() == 1 || event.getAction() == 3) {
                    Selection.removeSelection(buffer);
                }
                return result;
            } catch (Exception e) {
                FileLog.e(e);
                return false;
            }
        }
    }

    public static boolean needShowPasscode() {
        return needShowPasscode(false);
    }

    public static boolean needShowPasscode(boolean reset) {
        boolean wasInBackground = ForegroundDetector.getInstance().isWasInBackground(reset);
        if (reset) {
            ForegroundDetector.getInstance().resetBackgroundVar();
        }
        return SharedConfig.passcodeHash.length() > 0 && wasInBackground && (SharedConfig.appLocked || (!(SharedConfig.autoLockIn == 0 || SharedConfig.lastPauseTime == 0 || SharedConfig.appLocked || SharedConfig.lastPauseTime + SharedConfig.autoLockIn > ConnectionsManager.getInstance(UserConfig.selectedAccount).getCurrentTime()) || ConnectionsManager.getInstance(UserConfig.selectedAccount).getCurrentTime() + 5 < SharedConfig.lastPauseTime));
    }

    public static void shakeView(final View view, final float x, final int num) {
        if (view == null) {
            return;
        }
        if (num == 6) {
            view.setTranslationX(0.0f);
            return;
        }
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(ObjectAnimator.ofFloat(view, "translationX", dp(x)));
        animatorSet.setDuration(50L);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                AndroidUtilities.shakeView(view, num == 5 ? 0.0f : -x, num + 1);
            }
        });
        animatorSet.start();
    }

    public static void checkForCrashes(Activity context) {
    }

    public static void checkForUpdates(Activity context) {
        boolean z = BuildVars.DEBUG_VERSION;
    }

    public static void unregisterUpdates() {
        boolean z = BuildVars.DEBUG_VERSION;
    }

    public static void addToClipboard(CharSequence str) {
        try {
            ClipboardManager clipboard = (ClipboardManager) ApplicationLoader.applicationContext.getSystemService("clipboard");
            ClipData clip = ClipData.newPlainText("label", str);
            clipboard.setPrimaryClip(clip);
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static void addMediaToGallery(String fromPath) {
        if (fromPath == null) {
            return;
        }
        File f = new File(fromPath);
        Uri contentUri = Uri.fromFile(f);
        addMediaToGallery(contentUri);
    }

    public static void addMediaToGallery(Uri uri) {
        if (uri == null) {
            return;
        }
        try {
            Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
            mediaScanIntent.setData(uri);
            ApplicationLoader.applicationContext.sendBroadcast(mediaScanIntent);
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static File getAlbumDir(boolean secretChat) {
        if (secretChat || (Build.VERSION.SDK_INT >= 23 && ApplicationLoader.applicationContext.checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") != 0)) {
            return FileLoader.getDirectory(4);
        }
        File storageDir = null;
        if ("mounted".equals(Environment.getExternalStorageState())) {
            storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Xfff");
            if (!storageDir.mkdirs() && !storageDir.exists()) {
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.d("failed to create directory");
                }
                File storageDir2 = ApplicationLoader.applicationContext.getExternalFilesDir("Xfff");
                if (!storageDir2.mkdirs()) {
                    storageDir2.exists();
                    return null;
                }
                return null;
            }
        } else if (BuildVars.LOGS_ENABLED) {
            FileLog.d("External storage is not mounted READ/WRITE.");
        }
        return storageDir;
    }

    public static String getPath(Uri uri) {
        try {
            boolean isKitKat = Build.VERSION.SDK_INT >= 19;
            if (isKitKat && DocumentsContract.isDocumentUri(ApplicationLoader.applicationContext, uri)) {
                if (isExternalStorageDocument(uri)) {
                    String docId = DocumentsContract.getDocumentId(uri);
                    String[] split = docId.split(LogUtils.COLON);
                    if ("primary".equalsIgnoreCase(split[0])) {
                        return Environment.getExternalStorageDirectory() + "/" + split[1];
                    }
                } else {
                    if (isDownloadsDocument(uri)) {
                        String id = DocumentsContract.getDocumentId(uri);
                        Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id).longValue());
                        return getDataColumn(ApplicationLoader.applicationContext, contentUri, null, null);
                    }
                    if (isMediaDocument(uri)) {
                        String docId2 = DocumentsContract.getDocumentId(uri);
                        String[] split2 = docId2.split(LogUtils.COLON);
                        String type = split2[0];
                        Uri contentUri2 = null;
                        char c = 65535;
                        int hashCode = type.hashCode();
                        if (hashCode == 93166550) {
                            if (type.equals(MimeTypes.BASE_TYPE_AUDIO)) {
                                c = 2;
                                if (c != 0) {
                                }
                                String[] selectionArgs = {split2[1]};
                                return getDataColumn(ApplicationLoader.applicationContext, contentUri2, "_id=?", selectionArgs);
                            }
                            if (c != 0) {
                            }
                            String[] selectionArgs2 = {split2[1]};
                            return getDataColumn(ApplicationLoader.applicationContext, contentUri2, "_id=?", selectionArgs2);
                        }
                        if (hashCode != 100313435) {
                            if (hashCode == 112202875 && type.equals(MimeTypes.BASE_TYPE_VIDEO)) {
                                c = 1;
                                if (c != 0) {
                                    contentUri2 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                                } else if (c == 1) {
                                    contentUri2 = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                                } else if (c == 2) {
                                    contentUri2 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                                }
                                String[] selectionArgs22 = {split2[1]};
                                return getDataColumn(ApplicationLoader.applicationContext, contentUri2, "_id=?", selectionArgs22);
                            }
                            if (c != 0) {
                            }
                            String[] selectionArgs222 = {split2[1]};
                            return getDataColumn(ApplicationLoader.applicationContext, contentUri2, "_id=?", selectionArgs222);
                        }
                        if (type.equals(TtmlNode.TAG_IMAGE)) {
                            c = 0;
                            if (c != 0) {
                            }
                            String[] selectionArgs2222 = {split2[1]};
                            return getDataColumn(ApplicationLoader.applicationContext, contentUri2, "_id=?", selectionArgs2222);
                        }
                        if (c != 0) {
                        }
                        String[] selectionArgs22222 = {split2[1]};
                        return getDataColumn(ApplicationLoader.applicationContext, contentUri2, "_id=?", selectionArgs22222);
                    }
                }
            } else {
                if ("content".equalsIgnoreCase(uri.getScheme())) {
                    return getDataColumn(ApplicationLoader.applicationContext, uri, null, null);
                }
                if ("file".equalsIgnoreCase(uri.getScheme())) {
                    return uri.getPath();
                }
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
        return null;
    }

    public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
        String[] projection = {"_data"};
        try {
            Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
            if (cursor != null) {
                try {
                    if (cursor.moveToFirst()) {
                        int column_index = cursor.getColumnIndexOrThrow("_data");
                        String value = cursor.getString(column_index);
                        if (!value.startsWith("content://")) {
                            if (!value.startsWith("/")) {
                            }
                            if (cursor != null) {
                                cursor.close();
                            }
                            return value;
                        }
                        if (cursor != null) {
                            cursor.close();
                        }
                        return null;
                    }
                } finally {
                }
            }
            if (cursor != null) {
                cursor.close();
            }
        } catch (Exception e) {
        }
        return null;
    }

    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    public static File generatePicturePath() {
        return generatePicturePath(false);
    }

    public static File generatePicturePath(boolean secretChat) {
        try {
            File storageDir = getAlbumDir(secretChat);
            Date date = new Date();
            date.setTime(System.currentTimeMillis() + Utilities.random.nextInt(1000) + 1);
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS", Locale.US).format(date);
            return new File(storageDir, "IMG_" + timeStamp + ".jpg");
        } catch (Exception e) {
            FileLog.e(e);
            return null;
        }
    }

    public static CharSequence generateSearchName(String name, String name2, String q) {
        if (name == null && name2 == null) {
            return "";
        }
        SpannableStringBuilder builder = new SpannableStringBuilder();
        String wholeString = name;
        if (wholeString == null || wholeString.length() == 0) {
            wholeString = name2;
        } else if (name2 != null && name2.length() != 0) {
            wholeString = wholeString + " " + name2;
        }
        String wholeString2 = wholeString.trim();
        String lower = " " + wholeString2.toLowerCase();
        int lastIndex = 0;
        while (true) {
            int index = lower.indexOf(" " + q, lastIndex);
            if (index == -1) {
                break;
            }
            int idx = index - (index == 0 ? 0 : 1);
            int end = q.length() + (index == 0 ? 0 : 1) + idx;
            if (lastIndex != 0 && lastIndex != idx + 1) {
                builder.append((CharSequence) wholeString2.substring(lastIndex, idx));
            } else if (lastIndex == 0 && idx != 0) {
                builder.append((CharSequence) wholeString2.substring(0, idx));
            }
            String query = wholeString2.substring(idx, Math.min(wholeString2.length(), end));
            if (query.startsWith(" ")) {
                builder.append((CharSequence) " ");
            }
            String query2 = query.trim();
            int start = builder.length();
            builder.append((CharSequence) query2);
            builder.setSpan(new ForegroundColorSpan(Theme.getColor(Theme.key_windowBackgroundWhiteBlueText4)), start, query2.length() + start, 33);
            lastIndex = end;
        }
        if (lastIndex != -1 && lastIndex < wholeString2.length()) {
            builder.append((CharSequence) wholeString2.substring(lastIndex));
        }
        return builder;
    }

    public static boolean isAirplaneModeOn() {
        return Build.VERSION.SDK_INT < 17 ? Settings.System.getInt(ApplicationLoader.applicationContext.getContentResolver(), "airplane_mode_on", 0) != 0 : Settings.Global.getInt(ApplicationLoader.applicationContext.getContentResolver(), "airplane_mode_on", 0) != 0;
    }

    public static File generateVideoPath() {
        return generateVideoPath(false);
    }

    public static File generateVideoPath(boolean secretChat) {
        try {
            File storageDir = getAlbumDir(secretChat);
            Date date = new Date();
            date.setTime(System.currentTimeMillis() + Utilities.random.nextInt(1000) + 1);
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS", Locale.US).format(date);
            return new File(storageDir, "VID_" + timeStamp + ".mp4");
        } catch (Exception e) {
            FileLog.e(e);
            return null;
        }
    }

    public static String formatFileSize(long size) {
        return formatFileSize(size, false);
    }

    public static String formatFileSize(long size, boolean removeZero) {
        if (size < 1024) {
            return String.format("%d B", Long.valueOf(size));
        }
        if (size < 1048576) {
            float value = ((float) size) / 1024.0f;
            return (removeZero && (value - ((float) ((int) value))) * 10.0f == 0.0f) ? String.format("%d KB", Integer.valueOf((int) value)) : String.format("%.1f KB", Float.valueOf(value));
        }
        if (size < 1073741824) {
            float value2 = (((float) size) / 1024.0f) / 1024.0f;
            return (removeZero && (value2 - ((float) ((int) value2))) * 10.0f == 0.0f) ? String.format("%d MB", Integer.valueOf((int) value2)) : String.format("%.1f MB", Float.valueOf(value2));
        }
        float value3 = ((((float) size) / 1024.0f) / 1024.0f) / 1024.0f;
        return (removeZero && (value3 - ((float) ((int) value3))) * 10.0f == 0.0f) ? String.format("%d GB", Integer.valueOf((int) value3)) : String.format("%.1f GB", Float.valueOf(value3));
    }

    public static byte[] decodeQuotedPrintable(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int i = 0;
        while (i < bytes.length) {
            int b = bytes[i];
            if (b == 61) {
                int i2 = i + 1;
                try {
                    int u = Character.digit((char) bytes[i2], 16);
                    i = i2 + 1;
                    int l = Character.digit((char) bytes[i], 16);
                    buffer.write((char) ((u << 4) + l));
                } catch (Exception e) {
                    FileLog.e(e);
                    return null;
                }
            } else {
                buffer.write(b);
            }
            i++;
        }
        byte[] array = buffer.toByteArray();
        try {
            buffer.close();
        } catch (Exception e2) {
            FileLog.e(e2);
        }
        return array;
    }

    public static boolean copyFile(InputStream sourceFile, File destFile) throws IOException {
        OutputStream out = new FileOutputStream(destFile);
        byte[] buf = new byte[4096];
        while (true) {
            int len = sourceFile.read(buf);
            if (len > 0) {
                Thread.yield();
                out.write(buf, 0, len);
            } else {
                out.close();
                return true;
            }
        }
    }

    public static boolean copyFile(File sourceFile, File destFile) throws IOException {
        if (sourceFile.equals(destFile)) {
            return true;
        }
        if (!destFile.exists()) {
            destFile.createNewFile();
        }
        try {
            FileInputStream source = new FileInputStream(sourceFile);
            try {
                FileOutputStream destination = new FileOutputStream(destFile);
                try {
                    destination.getChannel().transferFrom(source.getChannel(), 0L, source.getChannel().size());
                    destination.close();
                    source.close();
                    return true;
                } finally {
                }
            } finally {
            }
        } catch (Exception e) {
            FileLog.e(e);
            return false;
        }
    }

    public static byte[] calcAuthKeyHash(byte[] auth_key) {
        byte[] sha1 = Utilities.computeSHA1(auth_key);
        byte[] key_hash = new byte[16];
        System.arraycopy(sha1, 0, key_hash, 0, 16);
        return key_hash;
    }

    public static void openDocument(MessageObject message, Activity activity, BaseFragment parentFragment) {
        TLRPC.Document document;
        File f;
        if (message == null || (document = message.getDocument()) == null) {
            return;
        }
        File f2 = null;
        String fileName = message.messageOwner.media != null ? FileLoader.getAttachFileName(document) : "";
        if (message.messageOwner.attachPath != null && message.messageOwner.attachPath.length() != 0) {
            f2 = new File(message.messageOwner.attachPath);
        }
        if (f2 == null || (f2 != null && !f2.exists())) {
            f = FileLoader.getPathToMessage(message.messageOwner);
        } else {
            f = f2;
        }
        if (f == null || !f.exists()) {
            return;
        }
        if (parentFragment != null && f.getName().toLowerCase().endsWith("attheme")) {
            Theme.ThemeInfo themeInfo = Theme.applyThemeFile(f, message.getDocumentName(), null, true);
            if (themeInfo != null) {
                parentFragment.presentFragment(new ThemePreviewActivity(themeInfo));
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
            builder.setMessage(LocaleController.getString("IncorrectTheme", R.string.IncorrectTheme));
            builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
            parentFragment.showDialog(builder.create());
            return;
        }
        String realMimeType = null;
        try {
            Intent intent = new Intent("android.intent.action.VIEW");
            intent.setFlags(1);
            MimeTypeMap myMime = MimeTypeMap.getSingleton();
            int idx = fileName.lastIndexOf(46);
            if (idx != -1) {
                String ext = fileName.substring(idx + 1);
                realMimeType = myMime.getMimeTypeFromExtension(ext.toLowerCase());
                if (realMimeType == null && ((realMimeType = document.mime_type) == null || realMimeType.length() == 0)) {
                    realMimeType = null;
                }
            }
            if (Build.VERSION.SDK_INT >= 24) {
                intent.setDataAndType(FileProvider.getUriForFile(activity, "im.nacqryqyts.messenger.provider", f), realMimeType != null ? realMimeType : "text/plain");
            } else {
                intent.setDataAndType(Uri.fromFile(f), realMimeType != null ? realMimeType : "text/plain");
            }
            if (realMimeType != null) {
                try {
                    activity.startActivityForResult(intent, 500);
                    return;
                } catch (Exception e) {
                    if (Build.VERSION.SDK_INT >= 24) {
                        intent.setDataAndType(FileProvider.getUriForFile(activity, "im.nacqryqyts.messenger.provider", f), "text/plain");
                    } else {
                        intent.setDataAndType(Uri.fromFile(f), "text/plain");
                    }
                    activity.startActivityForResult(intent, 500);
                    return;
                }
            }
            activity.startActivityForResult(intent, 500);
        } catch (Exception e2) {
            if (activity == null) {
                return;
            }
            AlertDialog.Builder builder2 = new AlertDialog.Builder(activity);
            builder2.setTitle(LocaleController.getString("AppName", R.string.AppName));
            builder2.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
            builder2.setMessage(LocaleController.formatString("NoHandleAppInstalled", R.string.NoHandleAppInstalled, message.getDocument().mime_type));
            if (parentFragment != null) {
                parentFragment.showDialog(builder2.create());
            } else {
                builder2.show();
            }
        }
    }

    public static void openForView(MessageObject message, final Activity activity) {
        File f = null;
        String fileName = message.getFileName();
        if (message.messageOwner.attachPath != null && message.messageOwner.attachPath.length() != 0) {
            f = new File(message.messageOwner.attachPath);
        }
        if (f == null || !f.exists()) {
            f = FileLoader.getPathToMessage(message.messageOwner);
        }
        if (f != null && f.exists()) {
            String realMimeType = null;
            Intent intent = new Intent("android.intent.action.VIEW");
            intent.setFlags(1);
            MimeTypeMap myMime = MimeTypeMap.getSingleton();
            int idx = fileName.lastIndexOf(46);
            if (idx != -1) {
                String ext = fileName.substring(idx + 1);
                realMimeType = myMime.getMimeTypeFromExtension(ext.toLowerCase());
                if (realMimeType == null) {
                    if (message.type == 9 || message.type == 0) {
                        realMimeType = message.getDocument().mime_type;
                    }
                    if (realMimeType == null || realMimeType.length() == 0) {
                        realMimeType = null;
                    }
                }
            }
            if (Build.VERSION.SDK_INT >= 26 && realMimeType != null && realMimeType.equals("application/vnd.android.package-archive") && !ApplicationLoader.applicationContext.getPackageManager().canRequestPackageInstalls()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                builder.setMessage(LocaleController.getString("ApkRestricted", R.string.ApkRestricted));
                builder.setPositiveButton(LocaleController.getString("PermissionOpenSettings", R.string.PermissionOpenSettings), new DialogInterface.OnClickListener() {
                    @Override
                    public final void onClick(DialogInterface dialogInterface, int i) {
                        AndroidUtilities.lambda$openForView$2(activity, dialogInterface, i);
                    }
                });
                builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                builder.show();
                return;
            }
            if (Build.VERSION.SDK_INT >= 24) {
                intent.setDataAndType(FileProvider.getUriForFile(activity, "im.nacqryqyts.messenger.provider", f), realMimeType != null ? realMimeType : "text/plain");
            } else {
                intent.setDataAndType(Uri.fromFile(f), realMimeType != null ? realMimeType : "text/plain");
            }
            if (realMimeType != null) {
                try {
                    activity.startActivityForResult(intent, 500);
                    return;
                } catch (Exception e) {
                    if (Build.VERSION.SDK_INT >= 24) {
                        intent.setDataAndType(FileProvider.getUriForFile(activity, "im.nacqryqyts.messenger.provider", f), "text/plain");
                    } else {
                        intent.setDataAndType(Uri.fromFile(f), "text/plain");
                    }
                    activity.startActivityForResult(intent, 500);
                    return;
                }
            }
            activity.startActivityForResult(intent, 500);
        }
    }

    public static void lambda$openForView$2(Activity activity, DialogInterface dialogInterface, int i) {
        try {
            activity.startActivity(new Intent("android.settings.MANAGE_UNKNOWN_APP_SOURCES", Uri.parse("package:" + activity.getPackageName())));
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static void openForView(TLObject media, Activity activity) {
        if (media == null || activity == null) {
            return;
        }
        String fileName = FileLoader.getAttachFileName(media);
        File f = FileLoader.getPathToAttach(media, true);
        if (f != null && f.exists()) {
            String realMimeType = null;
            Intent intent = new Intent("android.intent.action.VIEW");
            intent.setFlags(1);
            MimeTypeMap myMime = MimeTypeMap.getSingleton();
            int idx = fileName.lastIndexOf(46);
            if (idx != -1) {
                String ext = fileName.substring(idx + 1);
                realMimeType = myMime.getMimeTypeFromExtension(ext.toLowerCase());
                if (realMimeType == null) {
                    if (media instanceof TLRPC.TL_document) {
                        realMimeType = ((TLRPC.TL_document) media).mime_type;
                    }
                    if (realMimeType == null || realMimeType.length() == 0) {
                        realMimeType = null;
                    }
                }
            }
            if (Build.VERSION.SDK_INT >= 24) {
                intent.setDataAndType(FileProvider.getUriForFile(activity, "im.nacqryqyts.messenger.provider", f), realMimeType != null ? realMimeType : "text/plain");
            } else {
                intent.setDataAndType(Uri.fromFile(f), realMimeType != null ? realMimeType : "text/plain");
            }
            if (realMimeType != null) {
                try {
                    activity.startActivityForResult(intent, 500);
                    return;
                } catch (Exception e) {
                    if (Build.VERSION.SDK_INT >= 24) {
                        intent.setDataAndType(FileProvider.getUriForFile(activity, "im.nacqryqyts.messenger.provider", f), "text/plain");
                    } else {
                        intent.setDataAndType(Uri.fromFile(f), "text/plain");
                    }
                    activity.startActivityForResult(intent, 500);
                    return;
                }
            }
            activity.startActivityForResult(intent, 500);
        }
    }

    public static boolean isBannedForever(TLRPC.TL_chatBannedRights rights) {
        return rights == null || Math.abs(((long) rights.until_date) - (System.currentTimeMillis() / 1000)) > 157680000;
    }

    public static void setRectToRect(Matrix matrix, RectF src, RectF dst, int rotation, boolean translate) {
        float sx;
        float sy;
        float tx;
        float ty;
        float diff;
        boolean xLarger = false;
        if (rotation == 90 || rotation == 270) {
            float sx2 = dst.height();
            sx = sx2 / src.width();
            sy = dst.width() / src.height();
        } else {
            sx = dst.width() / src.width();
            sy = dst.height() / src.height();
        }
        if (sx < sy) {
            sx = sy;
            xLarger = true;
        } else {
            sy = sx;
        }
        if (translate) {
            matrix.setTranslate(dst.left, dst.top);
        }
        if (rotation == 90) {
            matrix.preRotate(90.0f);
            matrix.preTranslate(0.0f, -dst.width());
        } else if (rotation == 180) {
            matrix.preRotate(180.0f);
            matrix.preTranslate(-dst.width(), -dst.height());
        } else if (rotation == 270) {
            matrix.preRotate(270.0f);
            matrix.preTranslate(-dst.height(), 0.0f);
        }
        if (translate) {
            tx = (-src.left) * sx;
            ty = (-src.top) * sy;
        } else {
            float tx2 = dst.left;
            tx = tx2 - (src.left * sx);
            ty = dst.top - (src.top * sy);
        }
        if (xLarger) {
            diff = dst.width() - (src.width() * sy);
        } else {
            float diff2 = dst.height();
            diff = diff2 - (src.height() * sy);
        }
        float diff3 = diff / 2.0f;
        if (xLarger) {
            tx += diff3;
        } else {
            ty += diff3;
        }
        matrix.preScale(sx, sy);
        if (translate) {
            matrix.preTranslate(tx, ty);
        }
    }

    public static boolean handleProxyIntent(Activity activity, Intent intent) {
        Uri data;
        String user;
        String password;
        String user2;
        String password2;
        String user3;
        String password3;
        String path;
        if (intent == null) {
            return false;
        }
        try {
            if ((intent.getFlags() & 1048576) == 0 && (data = intent.getData()) != null) {
                String port = null;
                String address = null;
                String secret = null;
                String scheme = data.getScheme();
                if (scheme == null) {
                    user = null;
                    password = null;
                } else {
                    if (scheme.equals("http")) {
                        user3 = null;
                        password3 = null;
                    } else if (scheme.equals("https")) {
                        user3 = null;
                        password3 = null;
                    } else if (!scheme.equals("hchat")) {
                        user = null;
                        password = null;
                    } else {
                        String url = data.toString();
                        user = null;
                        password = null;
                        if (url.startsWith("hchat:proxy") || url.startsWith("hchat://proxy") || url.startsWith("hchat:socks") || url.startsWith("hchat://socks")) {
                            Uri data2 = Uri.parse(url.replace("hchat:proxy", "hchat://lovechat323.com").replace("hchat://proxy", "hchat://lovechat323.com").replace("hchat://socks", "hchat://lovechat323.com").replace("hchat:socks", "hchat://lovechat323.com"));
                            address = data2.getQueryParameter("server");
                            port = data2.getQueryParameter("port");
                            user2 = data2.getQueryParameter("user");
                            password2 = data2.getQueryParameter("pass");
                            secret = data2.getQueryParameter("secret");
                            if (!TextUtils.isEmpty(address) && !TextUtils.isEmpty(port)) {
                                if (user2 == null) {
                                    user2 = "";
                                }
                                if (password2 == null) {
                                    password2 = "";
                                }
                                if (secret == null) {
                                    secret = "";
                                }
                                showProxyAlert(activity, address, port, user2, password2, secret);
                                return true;
                            }
                        }
                    }
                    String host = data.getHost();
                    if (host != null && "lovechat323.com".equals(host.toLowerCase()) && (path = data.getPath()) != null && (path.startsWith("/socks") || path.startsWith("/proxy"))) {
                        address = data.getQueryParameter("server");
                        port = data.getQueryParameter("port");
                        user2 = data.getQueryParameter("user");
                        password2 = data.getQueryParameter("pass");
                        secret = data.getQueryParameter("secret");
                    } else {
                        user2 = user3;
                        password2 = password3;
                    }
                    return !TextUtils.isEmpty(address) ? false : false;
                }
                user2 = user;
                password2 = password;
                if (!TextUtils.isEmpty(address)) {
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean shouldEnableAnimation() {
        if (Build.VERSION.SDK_INT < 26 || Build.VERSION.SDK_INT >= 28) {
            return true;
        }
        PowerManager powerManager = (PowerManager) ApplicationLoader.applicationContext.getSystemService("power");
        if (powerManager.isPowerSaveMode()) {
            return false;
        }
        float scale = Settings.Global.getFloat(ApplicationLoader.applicationContext.getContentResolver(), "animator_duration_scale", 1.0f);
        return scale > 0.0f;
    }

    public static void showProxyAlert(Activity activity, final String address, final String port, final String user, final String password, final String secret) {
        BottomSheet.Builder builder = new BottomSheet.Builder(activity);
        final Runnable dismissRunnable = builder.getDismissRunnable();
        builder.setApplyTopPadding(false);
        builder.setApplyBottomPadding(false);
        LinearLayout linearLayout = new LinearLayout(activity);
        builder.setCustomView(linearLayout);
        boolean z = true;
        linearLayout.setOrientation(1);
        if (!TextUtils.isEmpty(secret)) {
            TextView titleTextView = new TextView(activity);
            titleTextView.setText(LocaleController.getString("UseProxySettingsTips", R.string.UseProxySettingsTips));
            titleTextView.setTextColor(Theme.getColor(Theme.key_dialogTextGray4));
            titleTextView.setTextSize(1, 14.0f);
            titleTextView.setGravity(49);
            linearLayout.addView(titleTextView, LayoutHelper.createLinear(-2, -2, (LocaleController.isRTL ? 5 : 3) | 48, 17, 8, 17, 8));
            View lineView = new View(activity);
            lineView.setBackgroundColor(Theme.getColor(Theme.key_divider));
            linearLayout.addView(lineView, new LinearLayout.LayoutParams(-1, 1));
        }
        int a = 0;
        for (int i = 5; a < i; i = 5) {
            String text = null;
            String detail = null;
            if (a == 0) {
                text = address;
                detail = LocaleController.getString("UseProxyAddress", R.string.UseProxyAddress);
            } else if (a != z) {
                if (a == 2) {
                    text = secret;
                    detail = LocaleController.getString("UseProxySecret", R.string.UseProxySecret);
                } else if (a == 3) {
                    text = user;
                    detail = LocaleController.getString("UseProxyUsername", R.string.UseProxyUsername);
                } else if (a == 4) {
                    text = password;
                    detail = LocaleController.getString("UseProxyPassword", R.string.UseProxyPassword);
                }
            } else {
                text = "" + port;
                detail = LocaleController.getString("UseProxyPort", R.string.UseProxyPort);
            }
            if (!TextUtils.isEmpty(text)) {
                TextDetailSettingsCell cell = new TextDetailSettingsCell(activity);
                cell.setTextAndValue(text, detail, z);
                cell.getTextView().setTextColor(Theme.getColor(Theme.key_dialogTextBlack));
                cell.getValueTextView().setTextColor(Theme.getColor(Theme.key_dialogTextGray3));
                linearLayout.addView(cell, LayoutHelper.createLinear(-1, -2));
                if (a == 2) {
                    break;
                }
            }
            a++;
            z = true;
        }
        PickerBottomLayout pickerBottomLayout = new PickerBottomLayout(activity, false);
        pickerBottomLayout.setBackgroundColor(Theme.getColor(Theme.key_dialogBackground));
        linearLayout.addView(pickerBottomLayout, LayoutHelper.createFrame(-1, 48, 83));
        pickerBottomLayout.cancelButton.setPadding(dp(18.0f), 0, dp(18.0f), 0);
        pickerBottomLayout.cancelButton.setTextColor(Theme.getColor(Theme.key_dialogTextBlue2));
        pickerBottomLayout.cancelButton.setText(LocaleController.getString("Cancel", R.string.Cancel).toUpperCase());
        pickerBottomLayout.cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                dismissRunnable.run();
            }
        });
        pickerBottomLayout.doneButtonTextView.setTextColor(Theme.getColor(Theme.key_dialogTextBlue2));
        pickerBottomLayout.doneButton.setPadding(dp(18.0f), 0, dp(18.0f), 0);
        pickerBottomLayout.doneButtonBadgeTextView.setVisibility(8);
        pickerBottomLayout.doneButtonTextView.setText(LocaleController.getString("ConnectingConnectProxy", R.string.ConnectingConnectProxy).toUpperCase());
        pickerBottomLayout.doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                AndroidUtilities.lambda$showProxyAlert$4(address, port, secret, password, user, dismissRunnable, view);
            }
        });
        builder.show();
    }

    public static void lambda$showProxyAlert$4(String address, String port, String secret, String password, String user, Runnable dismissRunnable, View v) {
        SharedConfig.ProxyInfo info;
        SharedPreferences.Editor editor = MessagesController.getGlobalMainSettings().edit();
        editor.putBoolean("proxy_enabled", true);
        editor.putString("proxy_ip", address);
        int p = Utilities.parseInt(port).intValue();
        editor.putInt("proxy_port", p);
        if (TextUtils.isEmpty(secret)) {
            editor.remove("proxy_secret");
            if (TextUtils.isEmpty(password)) {
                editor.remove("proxy_pass");
            } else {
                editor.putString("proxy_pass", password);
            }
            if (TextUtils.isEmpty(user)) {
                editor.remove("proxy_user");
            } else {
                editor.putString("proxy_user", user);
            }
            info = new SharedConfig.ProxyInfo(address, p, user, password, "");
        } else {
            editor.remove("proxy_pass");
            editor.remove("proxy_user");
            editor.putString("proxy_secret", secret);
            info = new SharedConfig.ProxyInfo(address, p, "", "", secret);
        }
        editor.commit();
        SharedConfig.currentProxy = SharedConfig.addProxy(info);
        ConnectionsManager.setProxySettings(true, address, p, user, password, secret);
        NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.proxySettingsChanged, new Object[0]);
        dismissRunnable.run();
    }

    public static String getSystemProperty(String key) {
        try {
            Class props = Class.forName("android.os.SystemProperties");
            return (String) props.getMethod("get", String.class).invoke(null, key);
        } catch (Exception e) {
            return null;
        }
    }

    public static CharSequence concat(CharSequence... text) {
        if (text.length == 0) {
            return "";
        }
        int i = 0;
        if (text.length == 1) {
            return text[0];
        }
        boolean spanned = false;
        int length = text.length;
        int i2 = 0;
        while (true) {
            if (i2 >= length) {
                break;
            }
            if (!(text[i2] instanceof Spanned)) {
                i2++;
            } else {
                spanned = true;
                break;
            }
        }
        if (spanned) {
            SpannableStringBuilder ssb = new SpannableStringBuilder();
            int length2 = text.length;
            while (i < length2) {
                CharSequence piece = text[i];
                ssb.append(piece == null ? "null" : piece);
                i++;
            }
            return new SpannedString(ssb);
        }
        StringBuilder sb = new StringBuilder();
        int length3 = text.length;
        while (i < length3) {
            sb.append(text[i]);
            i++;
        }
        return sb.toString();
    }

    public static float[] RGBtoHSB(int r, int g, int b) {
        float saturation;
        float hue;
        float hue2;
        float[] hsbvals = new float[3];
        int cmax = r > g ? r : g;
        if (b > cmax) {
            cmax = b;
        }
        int cmin = r < g ? r : g;
        if (b < cmin) {
            cmin = b;
        }
        float brightness = cmax / 255.0f;
        if (cmax != 0) {
            saturation = (cmax - cmin) / cmax;
        } else {
            saturation = 0.0f;
        }
        if (saturation == 0.0f) {
            hue2 = 0.0f;
        } else {
            float redc = (cmax - r) / (cmax - cmin);
            float greenc = (cmax - g) / (cmax - cmin);
            float bluec = (cmax - b) / (cmax - cmin);
            if (r == cmax) {
                hue = bluec - greenc;
            } else if (g == cmax) {
                hue = (2.0f + redc) - bluec;
            } else {
                hue = (4.0f + greenc) - redc;
            }
            float hue3 = hue / 6.0f;
            if (hue3 >= 0.0f) {
                hue2 = hue3;
            } else {
                hue2 = 1.0f + hue3;
            }
        }
        hsbvals[0] = hue2;
        hsbvals[1] = saturation;
        hsbvals[2] = brightness;
        return hsbvals;
    }

    public static int HSBtoRGB(float hue, float saturation, float brightness) {
        int r = 0;
        int g = 0;
        int b = 0;
        if (saturation == 0.0f) {
            int i = (int) ((255.0f * brightness) + 0.5f);
            b = i;
            g = i;
            r = i;
        } else {
            float h = (hue - ((float) Math.floor(hue))) * 6.0f;
            float f = h - ((float) Math.floor(h));
            float p = (1.0f - saturation) * brightness;
            float q = (1.0f - (saturation * f)) * brightness;
            float t = (1.0f - ((1.0f - f) * saturation)) * brightness;
            int i2 = (int) h;
            if (i2 == 0) {
                r = (int) ((brightness * 255.0f) + 0.5f);
                g = (int) ((t * 255.0f) + 0.5f);
                b = (int) ((255.0f * p) + 0.5f);
            } else if (i2 == 1) {
                r = (int) ((q * 255.0f) + 0.5f);
                g = (int) ((brightness * 255.0f) + 0.5f);
                b = (int) ((255.0f * p) + 0.5f);
            } else if (i2 == 2) {
                r = (int) ((p * 255.0f) + 0.5f);
                g = (int) ((brightness * 255.0f) + 0.5f);
                b = (int) ((255.0f * t) + 0.5f);
            } else if (i2 == 3) {
                r = (int) ((p * 255.0f) + 0.5f);
                g = (int) ((q * 255.0f) + 0.5f);
                b = (int) ((255.0f * brightness) + 0.5f);
            } else if (i2 == 4) {
                r = (int) ((t * 255.0f) + 0.5f);
                g = (int) ((p * 255.0f) + 0.5f);
                b = (int) ((255.0f * brightness) + 0.5f);
            } else if (i2 == 5) {
                r = (int) ((brightness * 255.0f) + 0.5f);
                g = (int) ((p * 255.0f) + 0.5f);
                b = (int) ((255.0f * q) + 0.5f);
            }
        }
        return (-16777216) | ((r & 255) << 16) | ((g & 255) << 8) | (b & 255);
    }

    public static int getPatternColor(int color) {
        float[] hsb = RGBtoHSB(Color.red(color), Color.green(color), Color.blue(color));
        if (hsb[1] > 0.0f || (hsb[2] < 1.0f && hsb[2] > 0.0f)) {
            hsb[1] = Math.min(1.0f, hsb[1] + 0.05f + ((1.0f - hsb[1]) * 0.1f));
        }
        if (hsb[2] > 0.5f) {
            hsb[2] = Math.max(0.0f, hsb[2] * 0.65f);
        } else {
            hsb[2] = Math.max(0.0f, Math.min(1.0f, 1.0f - (hsb[2] * 0.65f)));
        }
        return HSBtoRGB(hsb[0], hsb[1], hsb[2]) & 1728053247;
    }

    public static int getPatternSideColor(int color) {
        float[] hsb = RGBtoHSB(Color.red(color), Color.green(color), Color.blue(color));
        hsb[1] = Math.min(1.0f, hsb[1] + 0.05f);
        if (hsb[2] > 0.5f) {
            hsb[2] = Math.max(0.0f, hsb[2] * 0.9f);
        } else {
            hsb[2] = Math.max(0.0f, hsb[2] * 0.9f);
        }
        return HSBtoRGB(hsb[0], hsb[1], hsb[2]) | (-16777216);
    }

    public static String getWallPaperUrl(Object object, int currentAccount) {
        if (object instanceof TLRPC.TL_wallPaper) {
            TLRPC.TL_wallPaper wallPaper = (TLRPC.TL_wallPaper) object;
            String link = "https://" + MessagesController.getInstance(currentAccount).linkPrefix + "/bg/" + wallPaper.slug;
            StringBuilder modes = new StringBuilder();
            if (wallPaper.settings != null) {
                if (wallPaper.settings.blur) {
                    modes.append("blur");
                }
                if (wallPaper.settings.motion) {
                    if (modes.length() > 0) {
                        modes.append(Marker.ANY_NON_NULL_MARKER);
                    }
                    modes.append("motion");
                }
            }
            if (modes.length() > 0) {
                return link + "?mode=" + modes.toString();
            }
            return link;
        }
        if (object instanceof WallpapersListActivity.ColorWallpaper) {
            WallpapersListActivity.ColorWallpaper wallPaper2 = (WallpapersListActivity.ColorWallpaper) object;
            String color = String.format("%02x%02x%02x", Integer.valueOf(((byte) (wallPaper2.color >> 16)) & UByte.MAX_VALUE), Integer.valueOf(((byte) (wallPaper2.color >> 8)) & UByte.MAX_VALUE), Byte.valueOf((byte) (wallPaper2.color & 255))).toLowerCase();
            if (wallPaper2.pattern != null) {
                String link2 = "https://" + MessagesController.getInstance(currentAccount).linkPrefix + "/bg/" + wallPaper2.pattern.slug + "?intensity=" + ((int) (wallPaper2.intensity * 100.0f)) + "&bg_color=" + color;
                return link2;
            }
            String link3 = "https://" + MessagesController.getInstance(currentAccount).linkPrefix + "/bg/" + color;
            return link3;
        }
        return null;
    }

    public static float distanceInfluenceForSnapDuration(float f) {
        return (float) Math.sin((f - 0.5f) * 0.47123894f);
    }

    public static void makeAccessibilityAnnouncement(CharSequence what) {
        AccessibilityManager am = (AccessibilityManager) ApplicationLoader.applicationContext.getSystemService("accessibility");
        if (am.isEnabled()) {
            AccessibilityEvent ev = AccessibilityEvent.obtain();
            ev.setEventType(16384);
            ev.getText().add(what);
            am.sendAccessibilityEvent(ev);
        }
    }

    public static int getOffsetColor(int color1, int color2, float offset, float alpha) {
        int rF = Color.red(color2);
        int gF = Color.green(color2);
        int bF = Color.blue(color2);
        int aF = Color.alpha(color2);
        int rS = Color.red(color1);
        int gS = Color.green(color1);
        int bS = Color.blue(color1);
        int aS = Color.alpha(color1);
        return Color.argb((int) ((aS + ((aF - aS) * offset)) * alpha), (int) (rS + ((rF - rS) * offset)), (int) (gS + ((gF - gS) * offset)), (int) (bS + ((bF - bS) * offset)));
    }

    public static int indexOfIgnoreCase(String origin, String searchStr) {
        if (searchStr.isEmpty() || origin.isEmpty()) {
            return origin.indexOf(searchStr);
        }
        for (int i = 0; i < origin.length() && searchStr.length() + i <= origin.length(); i++) {
            int j = 0;
            for (int ii = i; ii < origin.length() && j < searchStr.length(); ii++) {
                char c = Character.toLowerCase(origin.charAt(ii));
                char c2 = Character.toLowerCase(searchStr.charAt(j));
                if (c != c2) {
                    break;
                }
                j++;
            }
            if (j == searchStr.length()) {
                return i;
            }
        }
        return -1;
    }

    public static float computePerceivedBrightness(int color) {
        return (((Color.red(color) * 0.2126f) + (Color.green(color) * 0.7152f)) + (Color.blue(color) * 0.0722f)) / 255.0f;
    }

    public static void setLightNavigationBar(Window window, boolean enable) {
        int flags;
        if (Build.VERSION.SDK_INT >= 26) {
            View decorView = window.getDecorView();
            int flags2 = decorView.getSystemUiVisibility();
            if (enable) {
                flags = flags2 | 16;
            } else {
                flags = flags2 & (-17);
            }
            decorView.setSystemUiVisibility(flags);
        }
    }

    public static int getVersionCode(Context mContext) {
        try {
            int versionCode = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode;
            return versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String getVersionName(Context context) {
        try {
            String verName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
            return verName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static byte[] getBlock(long offset, File file, int blockSize) {
        byte[] result = new byte[blockSize];
        RandomAccessFile accessFile = null;
        try {
            try {
                accessFile = new RandomAccessFile(file, "r");
                accessFile.seek(offset);
                int readSize = accessFile.read(result);
                if (readSize == -1) {
                    try {
                        accessFile.close();
                    } catch (IOException e) {
                    }
                    return null;
                }
                if (readSize == blockSize) {
                    try {
                        accessFile.close();
                    } catch (IOException e2) {
                    }
                    return result;
                }
                byte[] tmpByte = new byte[readSize];
                System.arraycopy(result, 0, tmpByte, 0, readSize);
                try {
                    accessFile.close();
                } catch (IOException e3) {
                }
                return tmpByte;
            } catch (IOException e4) {
                e4.printStackTrace();
                if (accessFile != null) {
                    try {
                        accessFile.close();
                    } catch (IOException e5) {
                    }
                }
                return null;
            }
        } catch (Throwable th) {
            if (accessFile != null) {
                try {
                    accessFile.close();
                } catch (IOException e6) {
                }
            }
            throw th;
        }
    }

    public static String getFileMD5(File file) {
        if (!file.isFile()) {
            return null;
        }
        byte[] buffer = new byte[1024];
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            FileInputStream in = new FileInputStream(file);
            while (true) {
                int len = in.read(buffer, 0, 1024);
                if (len != -1) {
                    digest.update(buffer, 0, len);
                } else {
                    in.close();
                    byte[] resultByteArray = digest.digest();
                    return byteArrayToHex(resultByteArray);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String byteArrayToHex(byte[] byteArray) {
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        char[] resultCharArray = new char[byteArray.length * 2];
        int index = 0;
        for (byte b : byteArray) {
            int index2 = index + 1;
            resultCharArray[index] = hexDigits[(b >>> 4) & 15];
            index = index2 + 1;
            resultCharArray[index2] = hexDigits[b & 15];
        }
        return new String(resultCharArray);
    }

    public static boolean checkHasExitsUserOrDeletedEverAndShowDialog(BaseFragment host, String phone) {
        if (phone == null) {
            return false;
        }
        for (int i = 0; i < 3; i++) {
            UserConfig userConfig = UserConfig.getInstance(i);
            if (UserObject.isDeleted(userConfig.getCurrentUser())) {
                AlertsCreator.showSimpleAlert(host, LocaleController.getString("ReminderDeletedEverPleaseUseOtherAccount", R.string.ReminderDeletedEverPleaseUseOtherAccount));
                return true;
            }
            String userPhone = userConfig.getCurrentUser().phone;
            if (PhoneNumberUtils.compare(phone, userPhone)) {
                if (i == UserConfig.selectedAccount) {
                    AlertsCreator.showSimpleAlert(host, LocaleController.getString("AccountAlreadyLoggedIn", R.string.AccountAlreadyLoggedIn));
                } else {
                    AlertsCreator.showSimpleAlert(host, LocaleController.getString("ReminderAccountHadExitsAndSwitchAccount", R.string.ReminderAccountHadExitsAndSwitchAccount));
                }
                return true;
            }
        }
        return false;
    }

    public static boolean checkCamera(Context context) {
        CameraManager manager = (CameraManager) context.getSystemService("camera");
        String[] cameraIds = new String[0];
        try {
            cameraIds = manager.getCameraIdList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (cameraIds != null && cameraIds.length > 0) {
            String str = cameraIds[0];
            if (cameraIds[1] != null) {
                return true;
            }
        }
        return false;
    }

    public static boolean isAppOnForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService("activity");
        String packageName = context.getApplicationContext().getPackageName();
        List<ActivityManager.RunningAppProcessInfo> appProcesses = null;
        if (activityManager != null) {
            appProcesses = activityManager.getRunningAppProcesses();
        }
        if (appProcesses == null) {
            return false;
        }
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(packageName) && appProcess.importance == 100) {
                return true;
            }
        }
        return false;
    }

    public static int getSystemVersion() {
        return Integer.parseInt(Build.VERSION.RELEASE);
    }

    public static void handleKeyboardShelterProblem(EditText editText) {
        handleKeyboardShelterProblem(editText, false);
    }

    public static void handleKeyboardShelterProblem(EditText editText, boolean callSuper) {
        if (editText != null && isEMUI() && Build.VERSION.SDK_INT >= 27) {
            int inputType = editText.getInputType();
            int variation = inputType & 4095;
            boolean passwordInputType = variation == 129;
            boolean webPasswordInputType = variation == 225;
            boolean numberPasswordInputType = variation == 18;
            if (passwordInputType) {
                editText.setInputType(1);
            } else if (webPasswordInputType) {
                editText.setInputType(1);
            } else if (numberPasswordInputType) {
                editText.setInputType(2);
            }
            if (passwordInputType || webPasswordInputType || numberPasswordInputType) {
                editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        }
    }

    public static boolean isMIUI() {
        String manufacturer = Build.MANUFACTURER;
        if ("xiaomi".equalsIgnoreCase(manufacturer)) {
            return true;
        }
        return false;
    }

    public static boolean isEMUI() {
        String manufacturer = Build.MANUFACTURER;
        if ("HUAWEI".equalsIgnoreCase(manufacturer)) {
            return true;
        }
        return false;
    }

    public static boolean isOPPO() {
        String manufacturer = Build.MANUFACTURER;
        if ("OPPO".equalsIgnoreCase(manufacturer)) {
            return true;
        }
        return false;
    }

    public static boolean isVIVO() {
        String manufacturer = Build.MANUFACTURER;
        if ("vivo".equalsIgnoreCase(manufacturer)) {
            return true;
        }
        return false;
    }

    public static int alphaColor(float fraction, int color) {
        int r = (color >> 16) & 255;
        int g = (color >> 8) & 255;
        int b = color & 255;
        return (((int) (256.0f * fraction)) << 24) | (r << 16) | (g << 8) | b;
    }

    public static boolean containsEmoji(CharSequence source) {
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (!isEmojiCharacter(codePoint)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isEmojiCharacter(char codePoint) {
        return codePoint == 0 || codePoint == '\t' || codePoint == '\n' || codePoint == '\r' || (codePoint >= ' ' && codePoint <= 55295) || ((codePoint >= 57344 && codePoint <= 65533) || (codePoint >= 0 && codePoint <= 65535));
    }

    public static boolean isScreenOriatationPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == 1;
    }

    public static Bitmap blurBitmap(Context context, Bitmap bitmap) {
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        RenderScript rs = RenderScript.create(context);
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
        blurScript.setRadius(15.0f);
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);
        allOut.copyTo(outBitmap);
        rs.destroy();
        return outBitmap;
    }

    public static Bitmap getCacheBitmapFromView(View view) {
        try {
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache(true);
            Bitmap drawingCache = view.getDrawingCache();
            if (drawingCache != null) {
                Bitmap bitmap = Bitmap.createBitmap(drawingCache);
                view.setDrawingCacheEnabled(false);
                return bitmap;
            }
            return null;
        } catch (Exception e) {
            FileLog.e(AndroidUtilities.class.getSimpleName(), "getCacheBitmapFromView error", e);
            return null;
        }
    }
}