package com.baidu.speech.utils.analysis; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Build; import android.util.Base64; import android.util.DisplayMetrics; import com.baidu.speech.VoiceRecognitionService; import com.efs.sdk.base.core.util.NetworkUtil; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.util.zip.GZIPOutputStream; import kotlin.UByte; public class Utility { private static final String KEY_LSAT_DOWNLOAD_STAT_PERIOD = "last_download_stat_period"; private static final String KEY_LSAT_UPLOAD_STAT_TIME = "last_upload_stat_time"; private static final String SDK_VERSION_NAME = "1.0.0-20140804"; private static final String SHARED_PREFERENCE_NAME = "tts"; public static boolean checkPermission(Context context, String str) { return context.checkCallingOrSelfPermission(str) == 0; } public static String encryptBASE64(String str) { if (!(str == null || str.length() == 0)) { try { byte[] bytes = str.getBytes("utf-8"); return new String(Base64.encode(bytes, 0, bytes.length, 0), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return null; } public static String encryptBASE64(byte[] bArr) { if (!(bArr == null || bArr.length == 0)) { try { return new String(Base64.encode(bArr, 0, bArr.length, 0), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return null; } public static byte[] encryptGZIP(String str) { if (!(str == null || str.length() == 0)) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream); gZIPOutputStream.write(str.getBytes("utf-8")); gZIPOutputStream.close(); byte[] byteArray = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.flush(); byteArrayOutputStream.close(); return byteArray; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } } return null; } public static String getAppName(Context context) { return getApplicationName(context); } private static String getApplicationName(Context context) { PackageManager packageManager; ApplicationInfo applicationInfo = null; try { packageManager = context.getPackageManager(); try { applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0); } catch (PackageManager.NameNotFoundException unused) { } } catch (PackageManager.NameNotFoundException unused2) { packageManager = null; } return (String) packageManager.getApplicationLabel(applicationInfo); } public static int getLastDownloadStatPeriod(Context context) { return context.getSharedPreferences("tts", 0).getInt(KEY_LSAT_DOWNLOAD_STAT_PERIOD, 0); } public static long getLastUploadStatTime(Context context) { return context.getSharedPreferences("tts", 0).getLong(KEY_LSAT_UPLOAD_STAT_TIME, 0); } /* JADX WARNING: Code restructure failed: missing block: B:20:0x0024, code lost: if (r0 != null) goto L_0x0026; */ /* JADX WARNING: Removed duplicated region for block: B:17:0x001f A[Catch:{ FileNotFoundException -> 0x0020, IOException -> 0x0019, all -> 0x0017 }] */ /* JADX WARNING: Removed duplicated region for block: B:27:0x0032 A[SYNTHETIC, Splitter:B:27:0x0032] */ public static int getLicenseDataFromFile(String str, byte[] bArr) { Throwable th; FileInputStream fileInputStream; FileNotFoundException e; IOException e2; FileInputStream fileInputStream2 = null; int i = -1; try { fileInputStream = new FileInputStream(str); try { i = fileInputStream.read(bArr); try { fileInputStream.close(); } catch (IOException e3) { e3.printStackTrace(); } } catch (FileNotFoundException e4) { e = e4; fileInputStream2 = fileInputStream; e.printStackTrace(); } catch (IOException e5) { e2 = e5; fileInputStream2 = fileInputStream; e2.printStackTrace(); if (fileInputStream2 != null) { } return i; } catch (Throwable th2) { th = th2; if (fileInputStream != null) { } throw th; } } catch (FileNotFoundException e6) { e = e6; e.printStackTrace(); } catch (IOException e7) { e2 = e7; e2.printStackTrace(); if (fileInputStream2 != null) { fileInputStream2.close(); } return i; } catch (Throwable th3) { th = th3; fileInputStream = fileInputStream2; if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e8) { e8.printStackTrace(); } } throw th; } return i; } public static int getNetType(Context context) { NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo(); return (activeNetworkInfo == null || !NetworkUtil.NETWORK_TYPE_WIFI.equals(activeNetworkInfo.getTypeName().toLowerCase())) ? 3 : 1; } public static NetworkInfo getNetworkInfo(Context context) { return ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo(); } public static String getOS() { return "Android"; } public static String getPackageName(Context context) { return context.getPackageName(); } public static String getPlatform(Context context) { return getOS() + "&" + Build.MODEL + "&" + Build.VERSION.RELEASE + "&" + Build.VERSION.SDK_INT + "&" + getNetType(context); } public static String getScreen(Context context) { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); int i = displayMetrics.widthPixels; int i2 = displayMetrics.heightPixels; return i + "*" + i2; } public static String getSdkName() { return "离线TTS SDK"; } public static String getSdkVersion() { return VoiceRecognitionService.getSdkVersion(); } public static String getSignatureMD5(Context context) { try { return parseSignature(context.getPackageManager().getPackageInfo(context.getPackageName(), 64).signatures[0].toByteArray()); } catch (Exception e) { e.printStackTrace(); return null; } } public static boolean isNetworkConnected(Context context) { NetworkInfo networkInfo = getNetworkInfo(context); return networkInfo != null && networkInfo.isConnected(); } private static String parseSignature(byte[] bArr) { try { MessageDigest instance = MessageDigest.getInstance("MD5"); instance.update(((X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(bArr))).getEncoded()); return toHexString(instance.digest()); } catch (CertificateException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e2) { e2.printStackTrace(); } return null; } public static void setLastDownloadStatPeriod(Context context, int i) { SharedPreferences.Editor edit = context.getSharedPreferences("tts", 0).edit(); edit.putInt(KEY_LSAT_DOWNLOAD_STAT_PERIOD, i); edit.commit(); } public static void setLastUploadStatTime(Context context, long j) { SharedPreferences.Editor edit = context.getSharedPreferences("tts", 0).edit(); edit.putLong(KEY_LSAT_UPLOAD_STAT_TIME, j); edit.commit(); } private static String toHexString(byte[] bArr) { StringBuffer stringBuffer = new StringBuffer(); for (byte b : bArr) { String hexString = Integer.toHexString(b & UByte.MAX_VALUE); if (hexString.length() == 1) { stringBuffer.append("0"); } stringBuffer.append(hexString); } return stringBuffer.toString(); } }