翰林优商.apk(点击下载) / FaceAuth.java


package com.baidu.idl.main.facesdk;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.baidu.idl.main.facesdk.callback.Callback;
import com.baidu.idl.main.facesdk.license.BDFaceLicenseAuthInfo;
import com.baidu.idl.main.facesdk.license.BDFaceLicenseLocalInfo;
import com.baidu.idl.main.facesdk.model.BDFaceSDKCommon;
import com.baidu.idl.main.facesdk.statistic.PostDeviceInfo;
import com.baidu.idl.main.facesdk.utils.PreferencesUtil;
import com.baidu.vis.unified.license.AndroidLicenser;
import com.baidu.vis.unified.license.BDLicenseAuthInfo;
import com.baidu.vis.unified.license.BDLicenseLocalInfo;
import com.baidu.vis.unified.license.HttpStatus;
import com.baidu.vis.unified.license.HttpUtils;
import org.json.JSONException;
import org.json.JSONObject;

public class FaceAuth {
    private static int ALGORITHM_ID = 3;
    private static final String LICENSE_FILE_NAME = "idl-license.face-android";
    private static final String TAG = "FaceSDK";

    static {
        try {
            System.loadLibrary("bdface_sdk");
            System.loadLibrary("bd_unifylicense");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private native int nativeCreatInstance();

    private native void nativeSetActiveLog(int i, int i2);

    private native void nativeSetCoreConfigure(int i, int i2);

    public BDFaceLicenseAuthInfo getAuthInfo(Context context) {
        BDLicenseAuthInfo authGetAuthInfo = AndroidLicenser.getInstance().authGetAuthInfo(context, ALGORITHM_ID);
        BDFaceLicenseAuthInfo bDFaceLicenseAuthInfo = new BDFaceLicenseAuthInfo();
        bDFaceLicenseAuthInfo.algorithmId = authGetAuthInfo.algorithmId;
        bDFaceLicenseAuthInfo.deviceId = authGetAuthInfo.deviceId;
        bDFaceLicenseAuthInfo.expireTime = authGetAuthInfo.expireTime;
        bDFaceLicenseAuthInfo.functionList = authGetAuthInfo.functionList;
        bDFaceLicenseAuthInfo.licenseKey = authGetAuthInfo.licenseKey;
        bDFaceLicenseAuthInfo.md5 = authGetAuthInfo.md5;
        bDFaceLicenseAuthInfo.packageName = authGetAuthInfo.packageName;
        return bDFaceLicenseAuthInfo;
    }

    public String getDeviceId(Context context) {
        return AndroidLicenser.getDeviceId(context.getApplicationContext());
    }

    public BDFaceLicenseLocalInfo getLocalInfo(Context context) {
        BDLicenseLocalInfo authGetLocalInfo = AndroidLicenser.getInstance().authGetLocalInfo(context, ALGORITHM_ID);
        BDFaceLicenseLocalInfo bDFaceLicenseLocalInfo = new BDFaceLicenseLocalInfo();
        bDFaceLicenseLocalInfo.algorithmId = authGetLocalInfo.algorithmId;
        bDFaceLicenseLocalInfo.deviceId = authGetLocalInfo.deviceId;
        bDFaceLicenseLocalInfo.fingerVersion = authGetLocalInfo.fingerVersion;
        bDFaceLicenseLocalInfo.licenseSdkVersion = authGetLocalInfo.licenseSdkVersion;
        bDFaceLicenseLocalInfo.licenseKey = authGetLocalInfo.licenseKey;
        bDFaceLicenseLocalInfo.md5 = authGetLocalInfo.md5;
        bDFaceLicenseLocalInfo.packageName = authGetLocalInfo.packageName;
        return bDFaceLicenseLocalInfo;
    }

    public void initLicense(final Context context, final String str, final String str2, final boolean z, final Callback callback) {
        FaceQueue.getInstance().execute(new Runnable() {
            /* class com.baidu.idl.main.facesdk.FaceAuth.AnonymousClass1 */

            public void run() {
                Context context = context;
                if (context == null) {
                    callback.onResponse(1, "没有初始化上下文");
                    return;
                }
                PreferencesUtil.initPrefs(context);
                if (TextUtils.isEmpty(PreferencesUtil.getString("statics", ""))) {
                    PostDeviceInfo.uploadDeviceInfo(context, new Callback() {
                        /* class com.baidu.idl.main.facesdk.FaceAuth.AnonymousClass1.AnonymousClass1 */

                        @Override // com.baidu.idl.main.facesdk.callback.Callback
                        public void onResponse(int i, String str) {
                            if (i == 0) {
                                PreferencesUtil.putString("statics", "ok");
                            }
                        }
                    });
                }
                if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
                    callback.onResponse(2, "license 关键字为空");
                    return;
                }
                AndroidLicenser instance = AndroidLicenser.getInstance();
                AndroidLicenser.ErrorCode authFromFile = instance.authFromFile(context, str, str2, z, FaceAuth.ALGORITHM_ID);
                if (authFromFile != AndroidLicenser.ErrorCode.SUCCESS) {
                    BDLicenseLocalInfo authGetLocalInfo = instance.authGetLocalInfo(context, FaceAuth.ALGORITHM_ID);
                    if (authGetLocalInfo != null) {
                        Log.i(FaceAuth.TAG, authGetLocalInfo.toString());
                    }
                } else {
                    int nativeCreatInstance = FaceAuth.this.nativeCreatInstance();
                    Log.v(FaceAuth.TAG, "bdface_create_instance status " + nativeCreatInstance);
                }
                callback.onResponse(authFromFile.ordinal(), instance.getErrorMsg(FaceAuth.ALGORITHM_ID));
            }
        });
    }

    public void initLicenseBatchLine(final Context context, final String str, final Callback callback) {
        FaceQueue.getInstance().execute(new Runnable() {
            /* class com.baidu.idl.main.facesdk.FaceAuth.AnonymousClass3 */

            public void run() {
                Context context = context;
                if (context == null) {
                    callback.onResponse(1, "没有初始化上下文");
                    return;
                }
                PreferencesUtil.initPrefs(context);
                if (TextUtils.isEmpty(PreferencesUtil.getString("statics", ""))) {
                    PostDeviceInfo.uploadDeviceInfo(context, new Callback() {
                        /* class com.baidu.idl.main.facesdk.FaceAuth.AnonymousClass3.AnonymousClass1 */

                        @Override // com.baidu.idl.main.facesdk.callback.Callback
                        public void onResponse(int i, String str) {
                            if (i == 0) {
                                PreferencesUtil.putString("statics", "ok");
                            }
                        }
                    });
                }
                if (TextUtils.isEmpty(str) || TextUtils.isEmpty(FaceAuth.LICENSE_FILE_NAME)) {
                    callback.onResponse(2, "license 关键字为空");
                    return;
                }
                AndroidLicenser instance = AndroidLicenser.getInstance();
                AndroidLicenser.ErrorCode authFromFile = instance.authFromFile(context, str, FaceAuth.LICENSE_FILE_NAME, true, FaceAuth.ALGORITHM_ID);
                if (authFromFile != AndroidLicenser.ErrorCode.SUCCESS) {
                    BDLicenseLocalInfo authGetLocalInfo = instance.authGetLocalInfo(context, FaceAuth.ALGORITHM_ID);
                    if (authGetLocalInfo != null) {
                        Log.i(FaceAuth.TAG, authGetLocalInfo.toString());
                    }
                } else {
                    int nativeCreatInstance = FaceAuth.this.nativeCreatInstance();
                    Log.v(FaceAuth.TAG, "bdface_create_instance status " + nativeCreatInstance);
                }
                callback.onResponse(authFromFile.ordinal(), instance.getErrorMsg(FaceAuth.ALGORITHM_ID));
            }
        });
    }

    public void initLicenseOnLine(final Context context, final String str, final Callback callback) {
        FaceQueue.getInstance().execute(new Runnable() {
            /* class com.baidu.idl.main.facesdk.FaceAuth.AnonymousClass2 */

            public void run() {
                String str;
                Callback callback;
                String str2;
                String[] split;
                Context context = context;
                if (context == null) {
                    callback.onResponse(1, "没有初始化上下文");
                    return;
                }
                PreferencesUtil.initPrefs(context);
                if (TextUtils.isEmpty(PreferencesUtil.getString("statics", ""))) {
                    PostDeviceInfo.uploadDeviceInfo(context, new Callback() {
                        /* class com.baidu.idl.main.facesdk.FaceAuth.AnonymousClass2.AnonymousClass1 */

                        @Override // com.baidu.idl.main.facesdk.callback.Callback
                        public void onResponse(int i, String str) {
                            if (i == 0) {
                                PreferencesUtil.putString("statics", "ok");
                            }
                        }
                    });
                }
                if (TextUtils.isEmpty(str) || TextUtils.isEmpty(FaceAuth.LICENSE_FILE_NAME)) {
                    callback.onResponse(2, "license 关键字为空");
                    return;
                }
                AndroidLicenser instance = AndroidLicenser.getInstance();
                AndroidLicenser.ErrorCode authFromFile = instance.authFromFile(context, str, FaceAuth.LICENSE_FILE_NAME, false, FaceAuth.ALGORITHM_ID);
                Log.e(FaceAuth.TAG, "errCode = " + authFromFile);
                if (authFromFile == AndroidLicenser.ErrorCode.SUCCESS) {
                    int nativeCreatInstance = FaceAuth.this.nativeCreatInstance();
                    Log.v(FaceAuth.TAG, "bdface_create_instance status " + nativeCreatInstance);
                    callback.onResponse(authFromFile.ordinal(), instance.getErrorMsg(FaceAuth.ALGORITHM_ID));
                    return;
                }
                String deviceId = AndroidLicenser.getDeviceId(context.getApplicationContext());
                try {
                    JSONObject jSONObject = new JSONObject();
                    jSONObject.put("deviceId", deviceId);
                    jSONObject.put("key", str);
                    jSONObject.put("platformType", 2);
                    jSONObject.put("version", 5);
                    str = jSONObject.toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                    str = null;
                }
                HttpStatus requestPost = HttpUtils.requestPost("https://ai.baidu.com/activation/key/activate", str, "application/json", FaceAuth.TAG);
                int i = -1;
                if (requestPost == null) {
                    callback.onResponse(-1, "在线激活失败");
                    return;
                }
                String str3 = requestPost.responseStr;
                try {
                    JSONObject jSONObject2 = new JSONObject(str3);
                    if (jSONObject2.optInt("error_code") != 0) {
                        str2 = jSONObject2.optString("error_msg");
                        Log.i(FaceAuth.TAG, "error_msg->" + str2);
                        callback = callback;
                    } else {
                        JSONObject optJSONObject = jSONObject2.optJSONObject("result");
                        if (optJSONObject != null) {
                            String optString = optJSONObject.optString("license");
                            if (!TextUtils.isEmpty(optString) && (split = optString.split(",")) != null && split.length == 2) {
                                PreferencesUtil.putString("activate_online_key", str);
                                AndroidLicenser.ErrorCode authFromMemory = instance.authFromMemory(context, str, split, FaceAuth.LICENSE_FILE_NAME, FaceAuth.ALGORITHM_ID);
                                if (authFromMemory != AndroidLicenser.ErrorCode.SUCCESS) {
                                    BDLicenseLocalInfo authGetLocalInfo = instance.authGetLocalInfo(context, FaceAuth.ALGORITHM_ID);
                                    if (authGetLocalInfo != null) {
                                        Log.i(FaceAuth.TAG, authGetLocalInfo.toString());
                                    }
                                } else {
                                    int nativeCreatInstance2 = FaceAuth.this.nativeCreatInstance();
                                    Log.v(FaceAuth.TAG, "bdface_create_instance status " + nativeCreatInstance2);
                                }
                                str2 = instance.getErrorMsg(FaceAuth.ALGORITHM_ID);
                                Callback callback2 = callback;
                                i = authFromMemory.ordinal();
                                callback = callback2;
                            }
                        }
                        Log.i(FaceAuth.TAG, "netRequest->" + str3);
                    }
                    callback.onResponse(i, str2);
                } catch (JSONException e2) {
                    e2.printStackTrace();
                }
                Log.i(FaceAuth.TAG, "netRequest->" + str3);
            }
        });
    }

    public void setActiveLog(BDFaceSDKCommon.BDFaceLogInfo bDFaceLogInfo, int i) {
        nativeSetActiveLog(bDFaceLogInfo.ordinal(), i);
    }

    public void setCoreConfigure(BDFaceSDKCommon.BDFaceCoreRunMode bDFaceCoreRunMode, int i) {
        nativeSetCoreConfigure(bDFaceCoreRunMode.ordinal(), i);
    }
}