You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TermApp/app/src/main/java/com/xypower/mpapp/ChannelActivity.java

316 lines
12 KiB
Java

package com.xypower.mpapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import com.xypower.common.MicroPhotoContext;
import com.xypower.mpapp.databinding.ActivityChannelBinding;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import android.widget.AdapterView;
public class ChannelActivity extends AppCompatActivity {
private ActivityChannelBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityChannelBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
binding.sensitivity.setOnFocusChangeListener(onFocusChangeListener);
binding.exposuretime.setOnFocusChangeListener(onFocusChangeListener);
int channel = binding.channels.getSelectedItemPosition();
loadChannelParams(channel);
binding.channels.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int channel = i + 1;
loadChannelParams(channel);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
binding.btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int channel = binding.channels.getSelectedItemPosition() + 1;
saveChannelParams(channel);
}
});
}
private View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater mMenuInflater = new MenuInflater(this);
mMenuInflater.inflate(R.menu.menu_channel, menu);
//return true;
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// todo: goto back activity from here
finish();
return true;
case R.id.action_save:
// todo: goto back activity from here
int channel = binding.channels.getSelectedItemPosition() + 1;
saveChannelParams(channel);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void loadChannelParams(int channel) {
binding.btnAutoExplosure.setChecked(true);
binding.btnAutoFocus.setChecked(true);
binding.btnHdrMode.setChecked(true);
binding.exposuretime.setText("0");
binding.sensitivity.setText("0");
String appPath = MicroPhotoContext.buildAppDir(getApplicationContext());
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
File channelFile = new File(appPath + "data/channels/" + String.valueOf(channel) + ".json");
StringBuilder stringBuilder = null;
if (channelFile.exists()) {
inputStreamReader = new InputStreamReader(new FileInputStream(channelFile), "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
}
JSONObject jsonObject = stringBuilder != null ? (new JSONObject(stringBuilder.toString())) : new JSONObject();
binding.btnUsbCamera.setChecked(jsonObject.optInt("usbCamera", 0) == 1);
binding.btnAutoExplosure.setChecked(jsonObject.optInt("autoExposure", 1) == 1);
binding.btnAutoFocus.setChecked(jsonObject.optInt("autoFocus", 1) == 1);
binding.btnHdrMode.setChecked(jsonObject.optInt("hdr", 1) == 1);
binding.exposuretime.setText(Integer.toString(jsonObject.optInt("exposureTime", 0)));
binding.sensitivity.setText(Integer.toString(jsonObject.optInt("sensibility", 0)));
binding.orientations.setSelection(jsonObject.optInt("orientation", 0));
binding.recognization.setSelection(jsonObject.optInt("recognization", 0));
if (jsonObject.has("cameraId")) {
binding.cameraId.setText(Integer.toString(jsonObject.optInt("cameraId", channel - 1)));
} else {
binding.cameraId.setText("");
}
if (jsonObject.has("resolutionCX")) {
binding.resolutionCX.setText(Integer.toString(jsonObject.optInt("resolutionCX")));
} else {
binding.resolutionCX.setText("");
}
if (jsonObject.has("resolutionCY")) {
binding.resolutionCY.setText(Integer.toString(jsonObject.optInt("resolutionCY")));
} else {
binding.resolutionCY.setText("");
}
if (jsonObject.has("quality")) {
binding.quality.setText(Integer.toString(jsonObject.optInt("quality")));
} else {
binding.quality.setText("");
}
JSONObject osdJsonObj = jsonObject.optJSONObject("osd");
if (osdJsonObj != null) {
binding.osdLeftTop.setText(osdJsonObj.optString("leftTop", ""));
binding.osdRightTop.setText(osdJsonObj.optString("rightTop", ""));
binding.osdRightBottom.setText(osdJsonObj.optString("rightBottom", ""));
binding.osdLeftBottom.setText(osdJsonObj.optString("leftBottom", ""));
} else {
binding.osdLeftTop.setText("");
binding.osdRightTop.setText("");
binding.osdRightBottom.setText("");
binding.osdLeftBottom.setText("");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception ex) {
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception ex) {
}
}
}
}
private void saveChannelParams(int channel) {
JSONObject jsonObject = null;
String appPath = MicroPhotoContext.buildAppDir(this.getApplicationContext());
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
File dataPath = new File(appPath + "data/channels/");
File file = new File(dataPath, String.valueOf(channel) + ".json");
try {
StringBuilder stringBuilder = new StringBuilder();
if (file.exists()) {
inputStreamReader = new InputStreamReader(new FileInputStream(file), "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
jsonObject =stringBuilder.length() > 0 ? (new JSONObject(stringBuilder.toString())) : (new JSONObject());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception ex) {
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception ex) {
}
}
}
if (jsonObject == null) {
jsonObject = new JSONObject();
}
try {
jsonObject.put("usbCamera", binding.btnUsbCamera.isChecked() ? 1 : 0);
jsonObject.put("autoExposure", binding.btnAutoExplosure.isChecked() ? 1 : 0);
jsonObject.put("autoFocus", binding.btnAutoFocus.isChecked() ? 1 : 0);
jsonObject.put("hdrMode", binding.btnHdrMode.isChecked() ? 1 : 0);
jsonObject.put("exposureTime", Integer.parseInt(binding.exposuretime.getText().toString()));
jsonObject.put("sensibility", Integer.parseInt(binding.sensitivity.getText().toString()));
jsonObject.put("orientation", binding.orientations.getSelectedItemPosition());
jsonObject.put("recognization", binding.recognization.getSelectedItemPosition());
// binding.cameraId.setText(jsonObject.optString("cameraId", ""));
if (!TextUtils.isEmpty(binding.cameraId.getText().toString())) {
jsonObject.put("cameraId", Integer.parseInt(binding.cameraId.getText().toString()));
} else {
jsonObject.remove("cameraId");
}
if (!TextUtils.isEmpty(binding.resolutionCX.getText().toString())) {
jsonObject.put("resolutionCX", Integer.parseInt(binding.resolutionCX.getText().toString()));
} else {
jsonObject.remove("resolutionCX");
}
if (!TextUtils.isEmpty(binding.resolutionCY.getText().toString())) {
jsonObject.put("resolutionCY", Integer.parseInt(binding.resolutionCY.getText().toString()));
} else {
jsonObject.remove("resolutionCY");
}
if (!TextUtils.isEmpty(binding.quality.getText().toString())) {
jsonObject.put("quality", Integer.parseInt(binding.quality.getText().toString()));
} else {
jsonObject.remove("quality");
}
JSONObject osdJsonObj = jsonObject.optJSONObject("osd");
if (osdJsonObj == null) {
osdJsonObj = jsonObject.put("osd", new JSONObject());
}
osdJsonObj.put("leftTop", binding.osdLeftTop.getText().toString());
osdJsonObj.put("rightTop", binding.osdRightTop.getText().toString());
osdJsonObj.put("rightBottom", binding.osdRightBottom.getText().toString());
osdJsonObj.put("leftBottom", binding.osdLeftBottom.getText().toString());
} catch (JSONException ex) {
}
OutputStreamWriter outputStreamWriter = null;
try {
if (!dataPath.exists()) {
dataPath.mkdirs();
}
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
outputStreamWriter.write(jsonObject.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStreamWriter != null) {
try {
outputStreamWriter.close();
} catch (Exception ex) {
}
}
}
}
}