增加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 Messenger mMessenger = null;
private long mConfigModificationTime = 0;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(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"); Log.d(TAG, "Start inflate");
binding = ActivityMainBinding.inflate(getLayoutInflater()); binding = ActivityMainBinding.inflate(getLayoutInflater());
Log.d(TAG, "Finish inflate"); Log.d(TAG, "Finish inflate");
@ -68,80 +63,152 @@ public class MainActivity extends AppCompatActivity {
// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 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); ActionBar actionBar = getSupportActionBar();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String caption = "MP"; Date date = new Date(BuildConfig.BUILD_TIMESTAMP);
switch (MicroPhotoService.getCustomAppId()) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
case 1: String caption = "MP";
caption = "RP"; switch (MicroPhotoService.getCustomAppId()) {
break; case 1:
case 2: caption = "RP";
caption = "N938"; break;
break; case 2:
default: caption = "N938";
break; break;
} default:
caption += " v" + MicroPhotoContext.getVersionName(getApplicationContext()) + " " + sdf.format(date); break;
sdf = new SimpleDateFormat("MM-dd HH:mm:ss"); }
caption += " / " + sdf.format(new Date()); caption += " v" + MicroPhotoContext.getVersionName(getApplicationContext()) + " " + sdf.format(date);
actionBar.setTitle(caption); 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.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); StrictMode.setThreadPolicy(policy);
initListener(); initListener();
Context appContext = getApplicationContext(); Context appContext = getApplicationContext();
String appPath = MicroPhotoContext.buildMpAppDir(appContext); String appPath = MicroPhotoContext.buildMpAppDir(appContext);
File appPathFile = new File(appPath); File appPathFile = new File(appPath);
if (!appPathFile.exists()) { if (!appPathFile.exists()) {
try { try {
appPathFile.mkdirs(); appPathFile.mkdirs();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); 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); if (mpdataFile.exists()) {
File mstPathFile = new File(mstPath); File dataFile = new File(appPathFile, "data");
File mpdataFile = new File(mstPathFile, "mpdata"); if (dataFile.exists()) {
try {
dataFile.delete();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (mpdataFile.exists()) {
File dataFile = new File(appPathFile, "data");
if (dataFile.exists()) {
try { try {
dataFile.delete(); mpdataFile.renameTo(dataFile);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); 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 { try {
mpdataFile.renameTo(dataFile); // stopService(intent2);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
}
Intent intent = getIntent(); if (MicroPhotoContext.hasMpAppConfig(appContext)) {
final int noDelay = intent.getIntExtra("noDelay", 0); final Runnable runnable = new Runnable() {
int rebootFlag = intent.getIntExtra("reboot", 0); @Override
String reason = intent.getStringExtra("reason"); public void run() {
if (!TextUtils.isEmpty(reason)) {
Log.w(TAG, "App Started with reason: " + reason); 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)) { if (TextUtils.isEmpty(appConfig.cmdid)) {
appConfig.cmdid = MicroPhotoService.getSerialNumber(); appConfig.cmdid = MicroPhotoService.getSerialNumber();
binding.cmdid.setText(appConfig.cmdid); binding.cmdid.setText(appConfig.cmdid);
@ -159,9 +226,15 @@ public class MainActivity extends AppCompatActivity {
} }
} }
if (appConfig.networkProtocol < binding.networkProtocol.getCount()) { protocolStr = appConfig.networkProtocol + "-";
binding.networkProtocol.setSelection(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()) { if (appConfig.encryption < binding.encryptions.getCount()) {
binding.encryptions.setSelection(appConfig.encryption); binding.encryptions.setSelection(appConfig.encryption);
} }
@ -172,46 +245,7 @@ public class MainActivity extends AppCompatActivity {
binding.network.setSelection(appConfig.network); binding.network.setSelection(appConfig.network);
} }
binding.btnStartServ.setEnabled(!MicroPhotoService.isRunning); return appConfig;
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();
} }
protected void initListener() { protected void initListener() {

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

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

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

Loading…
Cancel
Save