123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- package com.sunwin.visitorapp;
- import android.Manifest;
- import android.app.PendingIntent;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.pm.PackageManager;
- import android.hardware.usb.UsbDevice;
- import android.hardware.usb.UsbManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.text.TextUtils;
- import android.util.Log;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityCompat;
- import com.srp.AuthApi.AuthApi;
- import com.srp.AuthApi.AuthApplyResponse;
- import com.srp.AuthApi.ErrorCodeConfig;
- import com.sunwin.visitorapp.face.FRAbsLoopFactory;
- import com.sunwin.visitorapp.utils.AppManager;
- import com.sunwin.visitorapp.utils.Constant;
- import com.sunwin.visitorapp.utils.LogUtil;
- import com.sunwin.visitorapp.utils.SharePrefenceUtils;
- import com.sunwin.visitorapp.utils.ToastUtils;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.lang.ref.WeakReference;
- import java.util.ArrayList;
- import java.util.Iterator;
- public class SplashActivity extends AppCompatActivity {
- private static final int REQUEST_FOR_PERMISSION = 100;
- private static final String TAG = "SplashActivity";
- private ArrayList<String> permissions;
- MyHandler myHandler = new MyHandler(this);
- private AuthApi authApi;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_splash);
- AppManager.getAppManager().addActivity(this);
- permissions = new ArrayList<>();
- authApi = new AuthApi();
- initData();
- myHandler.sendEmptyMessageDelayed(0, 500);
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- AppManager.getAppManager().removeActivity(this);
- if (myHandler != null) {
- myHandler.removeMessages(0);
- }
- unregisterReceiver(mUsbReceiver);
- }
- private void initData() {
- authPermission();
- initPermission();
- requestDevicePermission();
- }
- private void authPermission() {
- // String cert = readExternal(CERT_PATH).trim();
- String cert = readFile(this);
- if (TextUtils.isEmpty(cert)) {
- // Toast.makeText(this, "cert is null", Toast.LENGTH_SHORT).show();
- return;
- }
- authApi.authDevice(this.getApplicationContext(), cert, "", new AuthApi.AuthDeviceCallBack() {
- @Override
- public void GetAuthDeviceResult(final AuthApplyResponse result) {
- if (result.errorCode == ErrorCodeConfig.AUTH_SUCCESS) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- ToastUtils.showToast("Apply update: OK");
- SharePrefenceUtils.putBoolean(Constant.ISharePrefence.ISINIT, true);
- new FRAbsLoopFactory().createFRAblLoop(SplashActivity.this, Constant.NumerValue.FRABS_TYPE);
- }
- });
- } else {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- ToastUtils.showToast("Apply update: error. error code is: " + result.errorCode + " , error message: " + result.errorMessage);
- }
- });
- }
- }
- });
- }
- public String readFile(Context context) {
- String content = "";
- try {
- InputStream instream = context.getAssets().open("CBG_Android_Face_Reco---36502-Formal-one-stage.cert");
- if (instream != null) {
- InputStreamReader inputreader = new InputStreamReader(instream);
- String line;
- for (BufferedReader buffreader = new BufferedReader(inputreader);
- (line = buffreader.readLine()) != null;
- content = line) {
- }
- instream.close();
- } else {
- ToastUtils.showToast("未找到文件");
- }
- } catch (FileNotFoundException var8) {
- LogUtil.d(TAG, "The File doesn't not exist.");
- } catch (IOException var9) {
- LogUtil.d(TAG, var9.getMessage());
- }
- return content;
- }
- private void initPermission() {
- permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
- permissions.add(Manifest.permission.READ_PHONE_STATE);
- permissions.add(Manifest.permission.CAMERA);
- }
- static class MyHandler extends Handler {
- WeakReference<SplashActivity> mWeakReference;
- public MyHandler(SplashActivity activity) {
- mWeakReference = new WeakReference<SplashActivity>(activity);
- }
- @Override
- public void handleMessage(Message msg) {
- final SplashActivity activity = mWeakReference.get();
- if (activity != null) {
- activity.getPermission();
- }
- }
- }
- private void getPermission() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- requestPer2();
- } else {
- openApplication();
- }
- }
- private void requestPer2() {
- Iterator<String> it = permissions.iterator();
- while (it.hasNext()) {
- String permission = it.next();
- //检查权限是否已经申请
- int hasPermission = ActivityCompat.checkSelfPermission(this, permission);//ActivityCompat.checkSelfPermission(this, permission)todo
- if (hasPermission == PackageManager.PERMISSION_GRANTED) {
- it.remove();
- }
- }
- if (permissions.size() == 0) {
- openApplication();
- return;
- }
- String[] permissionStr = permissions.toArray(new String[0]);
- //正式请求权限
- ActivityCompat.requestPermissions(this, permissionStr, REQUEST_FOR_PERMISSION);
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- switch (requestCode) {
- case REQUEST_FOR_PERMISSION:
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- openApplication();
- } else {
- ToastUtils.showToast("请您授予应用相应权限!");
- }
- return;
- default:
- break;
- }
- }
- /**
- * 进入应用程序
- */
- private void openApplication() {
- boolean isLogin = SharePrefenceUtils.getBoolean(Constant.ISharePrefence.LOGINTAG, false);
- if (isLogin) {
- gotoMain();
- } else {
- gotoLogin();
- }
- }
- private void gotoMain() {
- Intent intent = new Intent(this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- private void gotoLogin() {
- Intent intent = new Intent(this, LoginActivity.class);
- startActivity(intent);
- finish();
- }
- private final String ACTION_USB_PERMISSION = "com.sunwin.visitorapp.USB_PERMISSION";
- private void requestDevicePermission() {
- UsbManager musbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
- IntentFilter filter = new IntentFilter();
- filter.addAction(ACTION_USB_PERMISSION);
- filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
- registerReceiver(mUsbReceiver, filter);
- for (UsbDevice device : musbManager.getDeviceList().values()) {
- if (device.getVendorId() == Constant.NumerValue.VID && device.getProductId() == Constant.NumerValue.PID) {
- Intent intent = new Intent(ACTION_USB_PERMISSION);
- PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
- musbManager.requestPermission(device, pendingIntent);
- }
- }
- }
- private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (ACTION_USB_PERMISSION.equals(action)) {
- synchronized (this) {
- UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- if (!intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
- ToastUtils.showToast(getString(R.string.usb_unauthorized));
- } else {
- LogUtil.e(TAG, "-----");
- }
- }
- }
- }
- };
- }
|