增加MQTT选项,并优化UI逻辑

PtzNew
Matthew 3 months ago
parent e05bcedb6e
commit 4d5020803c

@ -50,17 +50,12 @@ public class MainActivity extends AppCompatActivity {
private Messenger mMessenger = null;
private long mConfigModificationTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
int activeSubId = SubscriptionManager.getActiveDataSubscriptionId();
if (activeSubId == -1) {
MicroPhotoContext.selectSimCard(getApplicationContext(), 1);
}
}
Log.d(TAG, "Start inflate");
binding = ActivityMainBinding.inflate(getLayoutInflater());
Log.d(TAG, "Finish inflate");
@ -68,80 +63,152 @@ public class MainActivity extends AppCompatActivity {
// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
ActionBar actionBar = getSupportActionBar();
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
int activeSubId = SubscriptionManager.getActiveDataSubscriptionId();
if (activeSubId == -1) {
MicroPhotoContext.selectSimCard(getApplicationContext(), 1);
}
}
Date date = new Date(BuildConfig.BUILD_TIMESTAMP);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String caption = "MP";
switch (MicroPhotoService.getCustomAppId()) {
case 1:
caption = "RP";
break;
case 2:
caption = "N938";
break;
default:
break;
}
caption += " v" + MicroPhotoContext.getVersionName(getApplicationContext()) + " " + sdf.format(date);
sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
caption += " / " + sdf.format(new Date());
actionBar.setTitle(caption);
ActionBar actionBar = getSupportActionBar();
Date date = new Date(BuildConfig.BUILD_TIMESTAMP);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String caption = "MP";
switch (MicroPhotoService.getCustomAppId()) {
case 1:
caption = "RP";
break;
case 2:
caption = "N938";
break;
default:
break;
}
caption += " v" + MicroPhotoContext.getVersionName(getApplicationContext()) + " " + sdf.format(date);
sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
caption += " / " + sdf.format(new Date());
actionBar.setTitle(caption);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
initListener();
initListener();
Context appContext = getApplicationContext();
String appPath = MicroPhotoContext.buildMpAppDir(appContext);
File appPathFile = new File(appPath);
if (!appPathFile.exists()) {
try {
appPathFile.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
Context appContext = getApplicationContext();
String appPath = MicroPhotoContext.buildMpAppDir(appContext);
File appPathFile = new File(appPath);
if (!appPathFile.exists()) {
try {
appPathFile.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
if (!MicroPhotoContext.hasMpAppConfig(appContext)) {
if (!MicroPhotoContext.hasMpAppConfig(appContext)) {
String mstPath = MicroPhotoContext.buildMasterAppDir(appContext);
File mstPathFile = new File(mstPath);
File mpdataFile = new File(mstPathFile, "mpdata");
String mstPath = MicroPhotoContext.buildMasterAppDir(appContext);
File mstPathFile = new File(mstPath);
File mpdataFile = new File(mstPathFile, "mpdata");
if (mpdataFile.exists()) {
File dataFile = new File(appPathFile, "data");
if (dataFile.exists()) {
try {
dataFile.delete();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (mpdataFile.exists()) {
File dataFile = new File(appPathFile, "data");
if (dataFile.exists()) {
try {
dataFile.delete();
mpdataFile.renameTo(dataFile);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Intent intent = getIntent();
final int noDelay = intent.getIntExtra("noDelay", 0);
int rebootFlag = intent.getIntExtra("reboot", 0);
String reason = intent.getStringExtra("reason");
if (!TextUtils.isEmpty(reason)) {
Log.w(TAG, "App Started with reason: " + reason);
}
if (rebootFlag == 1) {
Log.i(TAG, "After Reboot");
}
Log.d(TAG, "MainActivity: reboot=" + rebootFlag + " noDelay=" + noDelay);
MicroPhotoContext.AppConfig appConfig = loadConfigInfo();
binding.btnStartServ.setEnabled(!MicroPhotoService.isRunning);
binding.btnStopServ.setEnabled(MicroPhotoService.isRunning);
if (MicroPhotoService.isRunning) {
Intent intent2 = new Intent(MainActivity.this, MicroPhotoService.class);
try {
mpdataFile.renameTo(dataFile);
// stopService(intent2);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Intent intent = getIntent();
final int noDelay = intent.getIntExtra("noDelay", 0);
int rebootFlag = intent.getIntExtra("reboot", 0);
String reason = intent.getStringExtra("reason");
if (!TextUtils.isEmpty(reason)) {
Log.w(TAG, "App Started with reason: " + reason);
if (MicroPhotoContext.hasMpAppConfig(appContext)) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (!MicroPhotoService.isRunning && !TextUtils.isEmpty(appConfig.cmdid) && !TextUtils.isEmpty(appConfig.server) && appConfig.port != 0) {
if (binding.btnStartServ.isEnabled()) {
Log.i(TAG, "Perform AutoStart");
binding.btnStartServ.performClick();
}
}
}
};
long timeout = 500;
if (SystemClock.elapsedRealtime() < 180000) {
// In 3 minutes
timeout = 10000; // in 10 seconds
}
Handler handler = new Handler();
handler.postDelayed(runnable, timeout);
Log.i(TAG, "Set AutoStart after " + Long.toString(timeout) + "ms");
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (rebootFlag == 1) {
Log.i(TAG, "After Reboot");
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
try {
File file = MicroPhotoContext.getMpAppConfigFile(getApplicationContext());
if (file.lastModified() > mConfigModificationTime) {
loadConfigInfo();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Log.d(TAG, "MainActivity: reboot=" + rebootFlag + " noDelay=" + noDelay);
protected MicroPhotoContext.AppConfig loadConfigInfo() {
final MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(getApplicationContext());
mConfigModificationTime = appConfig.modificationTime;
final MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(appContext);
if (TextUtils.isEmpty(appConfig.cmdid)) {
appConfig.cmdid = MicroPhotoService.getSerialNumber();
binding.cmdid.setText(appConfig.cmdid);
@ -159,9 +226,15 @@ public class MainActivity extends AppCompatActivity {
}
}
if (appConfig.networkProtocol < binding.networkProtocol.getCount()) {
binding.networkProtocol.setSelection(appConfig.networkProtocol);
protocolStr = appConfig.networkProtocol + "-";
for (int idx = 0; idx < binding.networkProtocol.getCount(); idx++) {
String item = binding.networkProtocol.getItemAtPosition(idx).toString();
if (item.startsWith(protocolStr)) {
binding.networkProtocol.setSelection(idx);
break;
}
}
if (appConfig.encryption < binding.encryptions.getCount()) {
binding.encryptions.setSelection(appConfig.encryption);
}
@ -172,46 +245,7 @@ public class MainActivity extends AppCompatActivity {
binding.network.setSelection(appConfig.network);
}
binding.btnStartServ.setEnabled(!MicroPhotoService.isRunning);
binding.btnStopServ.setEnabled(MicroPhotoService.isRunning);
if (MicroPhotoService.isRunning) {
Intent intent2 = new Intent(MainActivity.this, MicroPhotoService.class);
try {
// stopService(intent2);
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (MicroPhotoContext.hasMpAppConfig(appContext)) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (!MicroPhotoService.isRunning && !TextUtils.isEmpty(appConfig.cmdid) && !TextUtils.isEmpty(appConfig.server) && appConfig.port != 0) {
if (binding.btnStartServ.isEnabled()) {
Log.i(TAG, "Perform AutoStart");
binding.btnStartServ.performClick();
}
}
}
};
long timeout = 500;
if (SystemClock.elapsedRealtime() < 180000) {
// In 3 minutes
timeout = 10000; // in 10 seconds
}
Handler handler = new Handler();
handler.postDelayed(runnable, timeout);
Log.i(TAG, "Set AutoStart after " + Long.toString(timeout) + "ms");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
return appConfig;
}
protected void initListener() {

@ -362,9 +362,7 @@ public class MicroPhotoService extends Service {
}
@Override
public void onDestroy() {
try {
if (mAppLock != null) {
mAppLock.close();
}

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="networkProtocols">
<item>TCP</item>
<item>UDP</item>
<item>0-TCP</item>
<item>1-UDP</item>
<item>10-MQTT</item>
</string-array>
</resources>

@ -55,6 +55,7 @@ public class MicroPhotoContext {
public int packetSize;
public int encryption; //0不加密 1明文 2加密
public int channels; //摄像头通道数目
public long modificationTime = 0;
}
public static class MasterConfig {
@ -241,27 +242,39 @@ public class MicroPhotoContext {
return getMpAppConfig(context, appPath + "data/App.json");
}
public static File getMpAppConfigFile(Context context) {
String appPath = buildMpAppDir(context);
return new File(appPath + "data/App.json");
}
public static AppConfig getMpAppConfig(Context context, String path) {
AppConfig appConfig = new AppConfig();
File file = new File(path);
try {
String content = FilesUtils.readTextFile(path);
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
appConfig.cmdid = jsonObject.optString(jsonObject.has("cmdid") ? "cmdid" : "CMDID", "");
appConfig.server = jsonObject.optString(jsonObject.has("server") ? "server" : "Server", "");
appConfig.port = jsonObject.optInt(jsonObject.has("port") ? "port" : "Port", 0);
appConfig.protocol = jsonObject.optInt(jsonObject.has("protocol") ? "protocol" : "Protocol", DEFAULT_PROTOCOL);
appConfig.networkProtocol = jsonObject.optInt(jsonObject.has("networkProtocol") ? "networkProtocol" : "NetworkProtocol", 0);
appConfig.network = jsonObject.optInt(jsonObject.has("network") ? "network" : "Network", 0);
appConfig.heartbeat = jsonObject.optInt("heartbeat", 0);
appConfig.packetSize = jsonObject.optInt("packetSize", 0);
appConfig.encryption = jsonObject.optInt("encryption", 0);
appConfig.channels = jsonObject.optInt("channels", 4);
if (appConfig.protocol == 0) {
appConfig.protocol = DEFAULT_PROTOCOL;
if (file.exists()) {
appConfig.modificationTime = file.lastModified();
String content = FilesUtils.readTextFile(path);
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
appConfig.cmdid = jsonObject.optString(jsonObject.has("cmdid") ? "cmdid" : "CMDID", "");
appConfig.server = jsonObject.optString(jsonObject.has("server") ? "server" : "Server", "");
appConfig.port = jsonObject.optInt(jsonObject.has("port") ? "port" : "Port", 0);
appConfig.protocol = jsonObject.optInt(jsonObject.has("protocol") ? "protocol" : "Protocol", DEFAULT_PROTOCOL);
appConfig.networkProtocol = jsonObject.optInt(jsonObject.has("networkProtocol") ? "networkProtocol" : "NetworkProtocol", 0);
appConfig.network = jsonObject.optInt(jsonObject.has("network") ? "network" : "Network", 0);
appConfig.heartbeat = jsonObject.optInt("heartbeat", 0);
appConfig.packetSize = jsonObject.optInt("packetSize", 0);
appConfig.encryption = jsonObject.optInt("encryption", 0);
appConfig.channels = jsonObject.optInt("channels", 4);
if (appConfig.protocol == 0) {
appConfig.protocol = DEFAULT_PROTOCOL;
}
}
} catch (JSONException e) {
e.printStackTrace();

Loading…
Cancel
Save