琉璃水城 v2.1.5版本的 MD5 值为:000028611e9de8a8b372a1eb9e3a384d

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


package com.aispeech.common;

import android.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import com.aispeech.AIEngine;
import com.j256.ormlite.stmt.query.SimpleComparison;
import com.yidaomeib_c_kehu.util.PreferencesUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Util {
    public static final String UTF8 = "UTF-8";
    public static String uniqueID = null;

    public static boolean ShowInstallGuide(final Context context) {
        if (!(context instanceof Activity)) {
            a.d("AISpeech Error", "The context in AIEngineConfig should be Activity Context. Pls Fix it.");
            return false;
        }
        File file = new File(getResourceDir(context) + "/aispeech_aiservice.apk");
        if (file.exists()) {
            file.delete();
        }
        if (!getServerInstalledOrNot(context)) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage("检测到您尚未安装语音组件,是否安装?");
            builder.setNegativeButton(R.string.cancel, (DialogInterface.OnClickListener) null);
            builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public final void onClick(DialogInterface dialogInterface, int i) {
                    if (Util.installFromAssets(context, AIConstant.APK_NICK_NAME)) {
                        return;
                    }
                    Util.openDownloadWeb(context, "http://open.aispeech.com/app/aiserver/aispeech_aiservice.apk");
                }
            });
            ((Activity) context).runOnUiThread(new Runnable() {
                @Override
                public final void run() {
                    builder.show();
                }
            });
        } else if (a(context)) {
            final AlertDialog.Builder builder2 = new AlertDialog.Builder(context);
            builder2.setMessage("检测到语音组件版本需要更新,是否更新?");
            builder2.setNegativeButton(R.string.cancel, (DialogInterface.OnClickListener) null);
            builder2.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public final void onClick(DialogInterface dialogInterface, int i) {
                    if (Util.installFromAssets(context, AIConstant.APK_NICK_NAME)) {
                        return;
                    }
                    Util.openDownloadWeb(context, "http://open.aispeech.com/app/aiserver/aispeech_aiservice.apk");
                }
            });
            ((Activity) context).runOnUiThread(new Runnable() {
                @Override
                public final void run() {
                    builder2.show();
                }
            });
        }
        return true;
    }

    private static boolean a(Context context) {
        if (!getServerInstalledOrNot(context)) {
            return true;
        }
        try {
            return 8 > context.getPackageManager().getPackageInfo(AIConstant.AISERVER_PACKAGE, 4).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            a.c("aispeech", "AISpeech service not found on this device!");
            return true;
        }
    }

    private static boolean a(InputStream inputStream, File file) {
        boolean z;
        if (file.exists()) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] a = a(fileInputStream);
                byte[] a2 = a(inputStream);
                int length = a.length > a2.length ? a2.length : a.length;
                int i = 0;
                while (true) {
                    if (i >= length) {
                        z = true;
                        break;
                    }
                    if (a[i] != a2[i]) {
                        z = false;
                        break;
                    }
                    i++;
                }
                fileInputStream.close();
                return z;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
        return false;
    }

    private static byte[] a(InputStream inputStream) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] bArr = new byte[10240];
            while (true) {
                int read = inputStream.read(bArr);
                if (read == -1) {
                    return messageDigest.digest();
                }
                messageDigest.update(bArr, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new byte[0];
        } catch (NoSuchAlgorithmException e2) {
            e2.printStackTrace();
            return new byte[0];
        }
    }

    public static boolean checkPermissionAtRunTime(Context context, String str) {
        return context != null && context.checkCallingOrSelfPermission(str) == 0;
    }

    public static boolean copyResource(Context context, String str) {
        return copyResource(context, str, false, true);
    }

    public static boolean copyResource(Context context, String str, boolean z) {
        return copyResource(context, str, z, true);
    }

    public static synchronized boolean copyResource(Context context, String str, boolean z, boolean z2) {
        boolean z3 = false;
        synchronized (Util.class) {
            if (context != null) {
                try {
                    InputStream open = context.getAssets().open(str);
                    try {
                        open.read(new byte[1]);
                        open.reset();
                        File file = new File(getResourceDir(context), str);
                        if (z2 && a(open, file)) {
                            try {
                                open.close();
                                z3 = true;
                            } catch (IOException e) {
                                e.printStackTrace();
                                z3 = true;
                            }
                        } else {
                            try {
                                try {
                                    open.reset();
                                } catch (IOException e2) {
                                    e2.printStackTrace();
                                }
                                FileOutputStream openFileOutput = context.openFileOutput(str, 1);
                                byte[] bArr = new byte[10240];
                                while (true) {
                                    try {
                                        int read = open.read(bArr);
                                        if (read == -1) {
                                            break;
                                        }
                                        openFileOutput.write(bArr, 0, read);
                                    } catch (IOException e3) {
                                        e3.printStackTrace();
                                    }
                                }
                                openFileOutput.close();
                                open.close();
                            } catch (FileNotFoundException e4) {
                                e4.printStackTrace();
                            }
                            if (z) {
                                unZip(context, file);
                            }
                            z3 = true;
                        }
                    } catch (IOException e5) {
                        a.d("AISpeech Error", "file" + str + "should be one of the suffix below to avoid be compressed in assets floder.“.jpg”, “.jpeg”, “.png”, “.gif”, “.wav”, “.mp2″, “.mp3″, “.ogg”, “.aac”, “.mpg”, “.mpeg”, “.mid”, “.midi”, “.smf”, “.jet”, “.rtttl”, “.imy”, “.xmf”, “.mp4″, “.m4a”, “.m4v”, “.3gp”, “.3gpp”, “.3g2″, “.3gpp2″, “.amr”, “.awb”, “.wma”, “.wmv”");
                    }
                } catch (IOException e6) {
                    a.d("AISpeech Error", "file " + str + " not found in assest floder, Did you forget add it?");
                }
            }
        }
        return z3;
    }

    public static void executeRunnableInMainOrTestThread(Context context, Runnable runnable) {
        if (context != null) {
            HandlerThread handlerThread = null;
            boolean isUnitTesting = isUnitTesting();
            if (isUnitTesting) {
                handlerThread = new HandlerThread("TestHandlerThread");
                handlerThread.start();
            }
            new Handler(isUnitTesting ? handlerThread.getLooper() : context.getMainLooper()).post(runnable);
        }
    }

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

    public static File getAvaiableAppDataDirPerInternal(Context context, String str) {
        return getAvaiableAppDataDirPerInternal(context, str, 10485760L);
    }

    public static File getAvaiableAppDataDirPerInternal(Context context, String str, long j) {
        if (context == null) {
            return null;
        }
        if (getAvailableInternalMemorySize() >= j) {
            return new File(context.getFilesDir(), str);
        }
        if (getAvailableExternalMemorySize() >= j) {
            return new File(getAvaiableExternalDataDir(context), str);
        }
        return null;
    }

    @SuppressLint({"NewApi"})
    public static File getAvaiableExternalDataDir(Context context) {
        if (context != null && externalMemoryAvailable()) {
            return context.getExternalFilesDir(null);
        }
        return null;
    }

    public static long getAvailableExternalMemorySize() {
        if (!externalMemoryAvailable()) {
            return -1L;
        }
        StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());
        return statFs.getAvailableBlocks() * statFs.getBlockSize();
    }

    public static long getAvailableInternalMemorySize() {
        StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
        return statFs.getAvailableBlocks() * statFs.getBlockSize();
    }

    public static synchronized String getDid(Context context) {
        String str = null;
        synchronized (Util.class) {
            if (context != null) {
                SharedPreferences sharedPreferences = context.getSharedPreferences("AISPEECH_PREF", 0);
                String string = sharedPreferences.getString("AISPEECH_PREFKEY_UNIQUE_ID", null);
                uniqueID = string;
                if (string == null) {
                    byte[] bArr = new byte[64];
                    if (checkPermissionAtRunTime(context, "android.permission.READ_PHONE_STATE")) {
                        AIEngine.aiengine_get_device_id(bArr, context);
                    } else {
                        a.d("AISpeech Error", "Did you forget add android.permission.READ_PHONE_STATE permission ?");
                    }
                    uniqueID = new String(bArr).trim();
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putString("AISPEECH_PREFKEY_UNIQUE_ID", uniqueID);
                    edit.commit();
                }
                str = uniqueID;
            }
        }
        return str;
    }

    public static String getDisplayInfo(Context context) {
        if (context == null) {
            return null;
        }
        return context.getResources().getDisplayMetrics().widthPixels + "x" + context.getResources().getDisplayMetrics().heightPixels;
    }

    public static String getNetWorkType(Context context) {
        if (context == null) {
            return null;
        }
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(PreferencesUtils.PHONE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo == null) {
                return null;
            }
            switch (activeNetworkInfo.getType()) {
                case 0:
                    switch (telephonyManager.getNetworkType()) {
                        case 1:
                            return "GPRS";
                        case 2:
                            return "EDGE";
                        case 3:
                            return "UMTS";
                        case 4:
                            return "CDMA";
                        case 5:
                            return "EVDO_0";
                        case 6:
                            return "EVDO_A";
                        case 7:
                            return "1xRTT";
                        case 8:
                            return "HSDPA";
                        case 9:
                            return "HSUPA";
                        case 10:
                            return "HSPA";
                        case 11:
                            return "IDEN";
                    }
                    return "unknown_type";
                case 1:
                    return "WIFI";
                default:
                    return "unknown_type";
            }
        } catch (Exception e) {
            e.printStackTrace();
            a.d("AISpeech Error", "Did you forget add android.permission.ACCESS_NETWORK_STATE permission in your application? Add it to fix this bug!");
            return null;
        }
    }

    public static int getNetworkQuality(Context context) {
        String netWorkType = getNetWorkType(context);
        return (netWorkType == null || netWorkType.equals("EDGE") || netWorkType.equals("GPRS") || netWorkType.equals("1xRTT") || netWorkType.equals("IDEN") || netWorkType.equals("unknown_type")) ? 3 : 8;
    }

    public static String getResourceDir(Context context) {
        if (context == null) {
            return null;
        }
        return context.getFilesDir().getAbsolutePath();
    }

    public static boolean getServerInstalledOrNot(Context context) {
        if (context == null) {
            return false;
        }
        try {
            context.getPackageManager().getPackageInfo(AIConstant.AISERVER_PACKAGE, 4);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    public static boolean getServerRunningOrNot(Context context) {
        if (context == null) {
            return false;
        }
        Iterator<ActivityManager.RunningServiceInfo> it = ((ActivityManager) context.getSystemService("activity")).getRunningServices(Integer.MAX_VALUE).iterator();
        while (it.hasNext()) {
            if ("com.aispeech.server.AISpeechService".equals(it.next().service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    public static long getTotalExternalMemorySize() {
        if (!externalMemoryAvailable()) {
            return -1L;
        }
        StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());
        return statFs.getBlockCount() * statFs.getBlockSize();
    }

    public static long getTotalInternalMemorySize() {
        StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
        return statFs.getBlockCount() * statFs.getBlockSize();
    }

    public static byte[] getUTF8Bytes(String str) {
        try {
            return str.getBytes(UTF8);
        } catch (Exception e) {
            return null;
        }
    }

    public static void installApk(Context context, String str) {
        if (context == null) {
            return;
        }
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(268435456);
        intent.setDataAndType(Uri.fromFile(new File(str)), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    public static boolean installFromAssets(Context context, String str) {
        if (context == null) {
            return false;
        }
        try {
            InputStream open = context.getAssets().open(str);
            if (open == null) {
                a.c("aispeech", "assets floder not contain service apk file! Pls put aispeech_service.imy into assest floder.");
                return false;
            }
            if (context != null) {
                FileOutputStream fileOutputStream = null;
                try {
                    try {
                        fileOutputStream = context.openFileOutput("aispeech_aiservice.apk", 1);
                        byte[] bArr = new byte[10240];
                        while (true) {
                            int read = open.read(bArr, 0, bArr.length);
                            if (read != -1) {
                                fileOutputStream.write(bArr, 0, read);
                            } else {
                                try {
                                    break;
                                } catch (Exception e) {
                                }
                            }
                        }
                        open.close();
                        if (fileOutputStream != null) {
                            try {
                                fileOutputStream.close();
                            } catch (Exception e2) {
                            }
                        }
                    } catch (Throwable th) {
                        try {
                            open.close();
                        } catch (Exception e3) {
                        }
                        if (fileOutputStream == null) {
                            throw th;
                        }
                        try {
                            fileOutputStream.close();
                            throw th;
                        } catch (Exception e4) {
                            throw th;
                        }
                    }
                } catch (FileNotFoundException e5) {
                    e5.printStackTrace();
                    try {
                        open.close();
                    } catch (Exception e6) {
                    }
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (Exception e7) {
                        }
                    }
                } catch (IOException e8) {
                    e8.printStackTrace();
                    try {
                        open.close();
                    } catch (Exception e9) {
                    }
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (Exception e10) {
                        }
                    }
                }
            }
            installApk(context, getResourceDir(context) + "/aispeech_aiservice.apk");
            return true;
        } catch (IOException e11) {
            e11.printStackTrace();
            return false;
        }
    }

    public static boolean isAboveAndroid22() {
        return Build.VERSION.SDK_INT > 7;
    }

    public static boolean isOnline(Context context) {
        if (context == null) {
            return false;
        }
        try {
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
            if (activeNetworkInfo != null) {
                if (activeNetworkInfo.isConnectedOrConnecting()) {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            a.d("AISpeech Error", "Did you forget add android.permission.ACCESS_NETWORK_STATE permission in your application? Add it to fix this bug!");
            return false;
        }
    }

    public static boolean isUnitTesting() {
        return Thread.currentThread().getName().contains("android.test.InstrumentationTestRunner");
    }

    public static final void logThread(String str) {
        Thread currentThread = Thread.currentThread();
        a.a(str, SimpleComparison.LESS_THAN_OPERATION + currentThread.getName() + ">id: " + currentThread.getId() + ", Priority: " + currentThread.getPriority() + ", Group: " + currentThread.getThreadGroup().getName());
    }

    public static String newUTF8String(byte[] bArr) {
        try {
            return new String(bArr, UTF8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return new String(bArr);
        }
    }

    public static void openDownloadWeb(Context context, String str) {
        if (context == null) {
            return;
        }
        context.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(str)));
    }

    public static void unZip(Context context, File file) {
        byte[] bArr = new byte[10240];
        try {
            ZipFile zipFile = new ZipFile(file);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry nextElement = entries.nextElement();
                if (nextElement.isDirectory()) {
                    new File(getResourceDir(context) + File.separator + nextElement.getName()).mkdirs();
                } else {
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(zipFile.getInputStream(nextElement));
                    File parentFile = new File(getResourceDir(context) + File.separator + nextElement.getName()).getParentFile();
                    if (parentFile != null && !parentFile.exists()) {
                        parentFile.mkdirs();
                    }
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(context.openFileOutput(nextElement.getName(), 1), 10240);
                    while (true) {
                        int read = bufferedInputStream.read(bArr, 0, 10240);
                        if (read == -1) {
                            break;
                        } else {
                            bufferedOutputStream.write(bArr, 0, read);
                        }
                    }
                    bufferedOutputStream.flush();
                    bufferedOutputStream.close();
                    bufferedInputStream.close();
                }
            }
            zipFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String uuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}