SplashActivity.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package com.sunwin.visitorapp;
  2. import android.Manifest;
  3. import android.app.PendingIntent;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.content.pm.PackageManager;
  9. import android.hardware.usb.UsbDevice;
  10. import android.hardware.usb.UsbManager;
  11. import android.os.Build;
  12. import android.os.Bundle;
  13. import android.os.Handler;
  14. import android.os.Message;
  15. import android.text.TextUtils;
  16. import android.util.Log;
  17. import androidx.annotation.NonNull;
  18. import androidx.appcompat.app.AppCompatActivity;
  19. import androidx.core.app.ActivityCompat;
  20. import com.srp.AuthApi.AuthApi;
  21. import com.srp.AuthApi.AuthApplyResponse;
  22. import com.srp.AuthApi.ErrorCodeConfig;
  23. import com.sunwin.visitorapp.face.FRAbsLoopFactory;
  24. import com.sunwin.visitorapp.utils.AppManager;
  25. import com.sunwin.visitorapp.utils.Constant;
  26. import com.sunwin.visitorapp.utils.LogUtil;
  27. import com.sunwin.visitorapp.utils.SharePrefenceUtils;
  28. import com.sunwin.visitorapp.utils.ToastUtils;
  29. import java.io.BufferedReader;
  30. import java.io.FileNotFoundException;
  31. import java.io.IOException;
  32. import java.io.InputStream;
  33. import java.io.InputStreamReader;
  34. import java.lang.ref.WeakReference;
  35. import java.util.ArrayList;
  36. import java.util.Iterator;
  37. public class SplashActivity extends AppCompatActivity {
  38. private static final int REQUEST_FOR_PERMISSION = 100;
  39. private static final String TAG = "SplashActivity";
  40. private ArrayList<String> permissions;
  41. MyHandler myHandler = new MyHandler(this);
  42. private AuthApi authApi;
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(R.layout.activity_splash);
  47. AppManager.getAppManager().addActivity(this);
  48. permissions = new ArrayList<>();
  49. authApi = new AuthApi();
  50. initData();
  51. myHandler.sendEmptyMessageDelayed(0, 500);
  52. }
  53. @Override
  54. protected void onDestroy() {
  55. super.onDestroy();
  56. AppManager.getAppManager().removeActivity(this);
  57. if (myHandler != null) {
  58. myHandler.removeMessages(0);
  59. }
  60. unregisterReceiver(mUsbReceiver);
  61. }
  62. private void initData() {
  63. authPermission();
  64. initPermission();
  65. requestDevicePermission();
  66. }
  67. private void authPermission() {
  68. // String cert = readExternal(CERT_PATH).trim();
  69. String cert = readFile(this);
  70. if (TextUtils.isEmpty(cert)) {
  71. // Toast.makeText(this, "cert is null", Toast.LENGTH_SHORT).show();
  72. return;
  73. }
  74. authApi.authDevice(this.getApplicationContext(), cert, "", new AuthApi.AuthDeviceCallBack() {
  75. @Override
  76. public void GetAuthDeviceResult(final AuthApplyResponse result) {
  77. if (result.errorCode == ErrorCodeConfig.AUTH_SUCCESS) {
  78. runOnUiThread(new Runnable() {
  79. @Override
  80. public void run() {
  81. ToastUtils.showToast("Apply update: OK");
  82. SharePrefenceUtils.putBoolean(Constant.ISharePrefence.ISINIT, true);
  83. new FRAbsLoopFactory().createFRAblLoop(SplashActivity.this, Constant.NumerValue.FRABS_TYPE);
  84. }
  85. });
  86. } else {
  87. runOnUiThread(new Runnable() {
  88. @Override
  89. public void run() {
  90. ToastUtils.showToast("Apply update: error. error code is: " + result.errorCode + " , error message: " + result.errorMessage);
  91. }
  92. });
  93. }
  94. }
  95. });
  96. }
  97. public String readFile(Context context) {
  98. String content = "";
  99. try {
  100. InputStream instream = context.getAssets().open("CBG_Android_Face_Reco---36502-Formal-one-stage.cert");
  101. if (instream != null) {
  102. InputStreamReader inputreader = new InputStreamReader(instream);
  103. String line;
  104. for (BufferedReader buffreader = new BufferedReader(inputreader);
  105. (line = buffreader.readLine()) != null;
  106. content = line) {
  107. }
  108. instream.close();
  109. } else {
  110. ToastUtils.showToast("未找到文件");
  111. }
  112. } catch (FileNotFoundException var8) {
  113. LogUtil.d(TAG, "The File doesn't not exist.");
  114. } catch (IOException var9) {
  115. LogUtil.d(TAG, var9.getMessage());
  116. }
  117. return content;
  118. }
  119. private void initPermission() {
  120. permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
  121. permissions.add(Manifest.permission.READ_PHONE_STATE);
  122. permissions.add(Manifest.permission.CAMERA);
  123. }
  124. static class MyHandler extends Handler {
  125. WeakReference<SplashActivity> mWeakReference;
  126. public MyHandler(SplashActivity activity) {
  127. mWeakReference = new WeakReference<SplashActivity>(activity);
  128. }
  129. @Override
  130. public void handleMessage(Message msg) {
  131. final SplashActivity activity = mWeakReference.get();
  132. if (activity != null) {
  133. activity.getPermission();
  134. }
  135. }
  136. }
  137. private void getPermission() {
  138. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  139. requestPer2();
  140. } else {
  141. openApplication();
  142. }
  143. }
  144. private void requestPer2() {
  145. Iterator<String> it = permissions.iterator();
  146. while (it.hasNext()) {
  147. String permission = it.next();
  148. //检查权限是否已经申请
  149. int hasPermission = ActivityCompat.checkSelfPermission(this, permission);//ActivityCompat.checkSelfPermission(this, permission)todo
  150. if (hasPermission == PackageManager.PERMISSION_GRANTED) {
  151. it.remove();
  152. }
  153. }
  154. if (permissions.size() == 0) {
  155. openApplication();
  156. return;
  157. }
  158. String[] permissionStr = permissions.toArray(new String[0]);
  159. //正式请求权限
  160. ActivityCompat.requestPermissions(this, permissionStr, REQUEST_FOR_PERMISSION);
  161. }
  162. @Override
  163. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  164. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  165. switch (requestCode) {
  166. case REQUEST_FOR_PERMISSION:
  167. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  168. openApplication();
  169. } else {
  170. ToastUtils.showToast("请您授予应用相应权限!");
  171. }
  172. return;
  173. default:
  174. break;
  175. }
  176. }
  177. /**
  178. * 进入应用程序
  179. */
  180. private void openApplication() {
  181. boolean isLogin = SharePrefenceUtils.getBoolean(Constant.ISharePrefence.LOGINTAG, false);
  182. if (isLogin) {
  183. gotoMain();
  184. } else {
  185. gotoLogin();
  186. }
  187. }
  188. private void gotoMain() {
  189. Intent intent = new Intent(this, MainActivity.class);
  190. startActivity(intent);
  191. finish();
  192. }
  193. private void gotoLogin() {
  194. Intent intent = new Intent(this, LoginActivity.class);
  195. startActivity(intent);
  196. finish();
  197. }
  198. private final String ACTION_USB_PERMISSION = "com.sunwin.visitorapp.USB_PERMISSION";
  199. private void requestDevicePermission() {
  200. UsbManager musbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
  201. IntentFilter filter = new IntentFilter();
  202. filter.addAction(ACTION_USB_PERMISSION);
  203. filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
  204. registerReceiver(mUsbReceiver, filter);
  205. for (UsbDevice device : musbManager.getDeviceList().values()) {
  206. if (device.getVendorId() == Constant.NumerValue.VID && device.getProductId() == Constant.NumerValue.PID) {
  207. Intent intent = new Intent(ACTION_USB_PERMISSION);
  208. PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
  209. musbManager.requestPermission(device, pendingIntent);
  210. }
  211. }
  212. }
  213. private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  214. @Override
  215. public void onReceive(Context context, Intent intent) {
  216. String action = intent.getAction();
  217. if (ACTION_USB_PERMISSION.equals(action)) {
  218. synchronized (this) {
  219. UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  220. if (!intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
  221. ToastUtils.showToast(getString(R.string.usb_unauthorized));
  222. } else {
  223. LogUtil.e(TAG, "-----");
  224. }
  225. }
  226. }
  227. }
  228. };
  229. }