1.18 瑞兴平板,读取扫描结果,使用同步加锁模式

This commit is contained in:
2024-11-14 16:05:54 +08:00
parent 12c928f059
commit 5769b7670b
3 changed files with 80 additions and 86 deletions

View File

@ -6,6 +6,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.hardware.Sensor;
@ -16,6 +17,7 @@ import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.JsonReader;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -32,13 +34,19 @@ import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import chaoran.business.BrandEnum;
import chaoran.business.R;
import chaoran.business.adapter.*;
import chaoran.business.engine.entity.NetworkSetting;
import chaoran.business.engine.impl.NetworkSettingEngine;
import chaoran.business.engine.SettingEngine;
import chaoran.business.engine.impl.TekVoiceEngine;
@ -62,8 +70,6 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
MyOrientationDetector myOrientationDetector;
private static int latestOrientation = 0; // 添加一个标志位;最后屏幕的方向
private WebView webView;
private Adapter adapter;
private VoiceEngine voiceEngine;
@ -86,6 +92,25 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
}
public static String readInitFile(Context context) {
StringBuffer init = new StringBuffer();
try {
AssetManager assetManager = context.getAssets();
// 打开文件输入流
InputStream inputStream = assetManager.open("init.json");
// 使用BufferedReader进行逐行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
init.append(line);
}
reader.close();
} catch (IOException e) {
Log.e("AssetError", "Failed to read asset file: init.json", e);
}
return init.toString();
}
private void initData() {
BrandEnum brand = BrandEnum.code(Build.MANUFACTURER);
Toast.makeText(this, Build.MANUFACTURER, Toast.LENGTH_LONG).show();
@ -305,8 +330,35 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
private String url() {
SharedPreferences spf = this.getSharedPreferences("crtech", Context.MODE_PRIVATE);
Integer port = spf.getInt("port", -1);
Map<String, Object> map = null;
if (port == -1) {
return "file:///android_asset/demo/index.html";
try {
// String init = readInitFile(this);
String init = null;
if (init != null && init.length() > 0) {
Gson gson = new Gson();
map = gson.fromJson(init, new TypeToken<Map<String, Object>>(){}.getType());
// 说明采取了配置并且存在ip才认为是一个合法的json配置
if (map.containsKey("ip") && map.get("ip").toString().length() > 0) {
NetworkSetting initData = new NetworkSetting();
initData.setAddress(map.get("ip").toString());
initData.setPort(Integer.parseInt(map.get("port").toString()));
initData.setPath(map.get("path").toString());
initData.setScreen_rotation(Integer.parseInt(map.getOrDefault("screen_rotation", "3").toString()));
initData.setHide_bar(Integer.parseInt(map.getOrDefault("hide_bar", "3").toString()));
settingEngine.saveSetting(initData);
spf = this.getSharedPreferences("crtech", Context.MODE_PRIVATE);
port = initData.getPort();
}else {
map = null;
}
}
}catch (Exception e) {
Log.e("MainActivity", "get init file error");
}
if (map == null) {
return "file:///android_asset/demo/index.html";
}
}
String address = spf.getString("address", "").replaceAll(" ", "");
String path = spf.getString("path", "").replaceAll(" ", "");
@ -416,13 +468,11 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
if (SCREEN_ROTATION == 1 || SCREEN_ROTATION == 3) {
if (rotate == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || rotate == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
activity.setRequestedOrientation(rotate);
latestOrientation = rotate;
}
}
if (SCREEN_ROTATION == 2 || SCREEN_ROTATION == 3) {
if (rotate == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE || rotate == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
activity.setRequestedOrientation(rotate);
latestOrientation = rotate;
}
}
}
@ -431,69 +481,6 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
}
/*class MyOrientationDetector implements SensorEventListener {
private Activity activity;
public MyOrientationDetector(Activity activity) {
this.activity = activity;
Sensor orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
sensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
int angle = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if (latestOrientation == angle) {
return;
}
if (SCREEN_ROTATION == 1) {
if (Surface.ROTATION_0 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
latestOrientation = angle;
}else if (Surface.ROTATION_180 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
latestOrientation = angle;
}
return;
}else if (SCREEN_ROTATION == 2) {
if (Surface.ROTATION_90 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
latestOrientation = angle;
}else if (Surface.ROTATION_270 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
latestOrientation = angle;
}
return;
}
int tmpAngle = latestOrientation;
latestOrientation = angle;
switch (angle) {
case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
default:
latestOrientation = tmpAngle;
break;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}*/
@SuppressLint("WrongConstant")
@Override
public void onConfigurationChanged(Configuration newConfig) {