修复资源泄漏、修复拍照时间表的bug

serial
BlueMatthew 1 year ago
parent aaaa6e131f
commit e1ce0d81dc

@ -175,10 +175,9 @@ CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPa
m_javaService = env->NewGlobalRef(service); m_javaService = env->NewGlobalRef(service);
jclass classService = env->GetObjectClass(m_javaService); jclass classService = env->GetObjectClass(m_javaService);
mRegisterTimerMid = env->GetMethodID(classService, "registerTimer", "(JIJ)Z");
mRegisterHeartbeatMid = env->GetMethodID(classService, "registerHeartbeatTimer", "(I)V"); mRegisterHeartbeatMid = env->GetMethodID(classService, "registerHeartbeatTimer", "(I)V");
mUnregisterTimerMid = env->GetMethodID(classService, "unregisterTimer", "(J)Z");
mUpdateTimeMid = env->GetMethodID(classService, "updateTime", "(J)Z"); mUpdateTimeMid = env->GetMethodID(classService, "updateTime", "(J)Z");
mUpdateCaptureScheduleMid = env->GetMethodID(classService, "updateCaptureSchedule", "(JJ)Z");
mStartRecordingMid = env->GetMethodID(classService, "startRecording", "(IJIIII)V"); mStartRecordingMid = env->GetMethodID(classService, "startRecording", "(IJIIII)V");
mRequestWakelockMid = env->GetMethodID(classService, "requestWakelock", "(Ljava/lang/String;J)V"); mRequestWakelockMid = env->GetMethodID(classService, "requestWakelock", "(Ljava/lang/String;J)V");
@ -212,6 +211,13 @@ CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPa
CPhoneDevice::~CPhoneDevice() CPhoneDevice::~CPhoneDevice()
{ {
for (auto it = mTimers.begin(); it != mTimers.end(); ++it)
{
timer_delete((timer_t)it->first);
delete it->second;
}
mTimers.clear();
JNIEnv* env = NULL; JNIEnv* env = NULL;
bool didAttachThread = false; bool didAttachThread = false;
bool res = GetJniEnv(m_vm, &env, didAttachThread); bool res = GetJniEnv(m_vm, &env, didAttachThread);
@ -292,7 +298,24 @@ bool CPhoneDevice::UpdateTime(time_t ts)
bool CPhoneDevice::UpdateSchedules() bool CPhoneDevice::UpdateSchedules()
{ {
return true; JNIEnv* env = NULL;
jboolean ret = JNI_FALSE;
bool didAttachThread = false;
bool res = GetJniEnv(m_vm, &env, didAttachThread);
if (!res)
{
ALOGE("Failed to get JNI Env");
}
time_t ts = time(NULL);
time_t zeroPoint = GetTimeOfZeroPoint(ts);
ret = env->CallBooleanMethod(m_javaService, mUpdateCaptureScheduleMid, zeroPoint, ts - zeroPoint);
if (didAttachThread)
{
m_vm->DetachCurrentThread();
}
return (ret == JNI_TRUE);
} }
bool CPhoneDevice::QuerySystemProperties(std::map<std::string, std::string>& properties) bool CPhoneDevice::QuerySystemProperties(std::map<std::string, std::string>& properties)

@ -250,10 +250,9 @@ protected:
jclass m_sysApiClass; jclass m_sysApiClass;
std::string m_appPath; std::string m_appPath;
jmethodID mRegisterTimerMid;
jmethodID mRegisterHeartbeatMid; jmethodID mRegisterHeartbeatMid;
jmethodID mUnregisterTimerMid; jmethodID mUpdateCaptureScheduleMid;
jmethodID mUpdateTimeMid; jmethodID mUpdateTimeMid;
jmethodID mStartRecordingMid; jmethodID mStartRecordingMid;
jmethodID mRequestWakelockMid; jmethodID mRequestWakelockMid;

@ -1,223 +0,0 @@
package com.xypower.mpapp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.PowerManager;
import android.text.TextUtils;
import android.util.Pair;
import com.dev.devapi.api.SysApi;
import com.xypower.common.FileDownloader;
import com.xypower.common.MicroPhotoContext;
import org.json.JSONObject;
public class AppMaster {
private MicroPhotoService mService;
private String mCmdid;
private PowerManager.WakeLock mWakelock;
private final static String MASTER_URL = "http://180.166.218.222:40101/?cmdid=";
public AppMaster(MicroPhotoService service, String cmdid) {
PowerManager powerManager = (PowerManager) service.getSystemService(Context.POWER_SERVICE);
mWakelock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "com.xinyingpower.microphoto:Upgrader");
mService = service;
mCmdid = cmdid;
}
@Override
protected void finalize() {
try {
if (mWakelock != null) {
mWakelock.setReferenceCounted(false);
mWakelock.release();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
mWakelock = null;
}
mService = null;
}
public void start() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection httpURLConnection = null;
InputStream inputStream = null;
try {
String url = MASTER_URL + URLEncoder.encode(mCmdid, "UTF-8");
URL mUrl = new URL(url);
httpURLConnection = (HttpURLConnection) mUrl.openConnection();
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setReadTimeout(15000);
httpURLConnection.setRequestMethod("POST");
// httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
List<Pair<String, String>> postParams = new ArrayList<>();
postParams.add(new Pair<String, String>("id", mCmdid));
// postParams(httpURLConnection.getOutputStream(), postParams);
buildParams(httpURLConnection.getOutputStream());
httpURLConnection.connect();
inputStream = httpURLConnection.getInputStream();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//在子线程中不能操作UI线程通过handler在UI线程中进行操作
// handler.sendEmptyMessage(0x00);
String response = convertStreamToString(inputStream);
process(response);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (AppMaster.this.mWakelock != null) {
try {
AppMaster.this.mWakelock.setReferenceCounted(false);
AppMaster.this.mWakelock.release();
} catch (Exception e) {
e.printStackTrace();
}
AppMaster.this.mWakelock = null;
}
}
}
}).start();
}
private void process(String content) {
if (TextUtils.isEmpty(content)) {
return;
}
try {
JSONObject jsonObject = new JSONObject(content);
int isUpgrade = jsonObject.optInt("isUpgrade", 0);
String url = jsonObject.optString("url", null);
if (isUpgrade == 1 && !TextUtils.isEmpty(url)) {
upgradeApp(url);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void upgradeApp(String url) {
FileDownloader dl = new FileDownloader();
File path = new File(MicroPhotoContext.buildAppDir(mService.getApplicationContext()), "packages");
if (!path.exists()) {
path.mkdirs();
}
File file = new File(path, "app.apk");
if (file.exists()) {
file.delete();
}
String apkPath = file.getAbsolutePath();
if (dl.download(url, apkPath)) {
Context context = mService.getApplicationContext();
SysApi.installApk(context, apkPath, context.getPackageName(), true);
}
}
private void buildParams(OutputStream output) {
BufferedWriter bufferedWriter = null;
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", mCmdid);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
bufferedWriter.write(jsonObject.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void postParams(OutputStream output, List<Pair<String, String>> paramsList) {
BufferedWriter bufferedWriter = null;
try {
StringBuilder stringBuilder = new StringBuilder();
for (Pair<String, String> pair : paramsList) {
if (!TextUtils.isEmpty(stringBuilder)) {
stringBuilder.append("&");
}
stringBuilder.append(URLEncoder.encode(pair.first, "UTF-8"));
stringBuilder.append("=");
stringBuilder.append(URLEncoder.encode(pair.second, "UTF-8"));
bufferedWriter = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
bufferedWriter.write(stringBuilder.toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String convertStreamToString(InputStream inputStream) {
BufferedReader bufferedReader = null;
StringBuffer stringBuffer = new StringBuffer();
String line;
try {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line).append("\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return stringBuffer.toString();
}
}

@ -11,6 +11,7 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import com.xypower.common.JSONUtils;
import com.xypower.common.MicroPhotoContext; import com.xypower.common.MicroPhotoContext;
import com.xypower.mpapp.databinding.ActivityChannelBinding; import com.xypower.mpapp.databinding.ActivityChannelBinding;
@ -118,23 +119,13 @@ public class ChannelActivity extends AppCompatActivity {
String appPath = MicroPhotoContext.buildAppDir(getApplicationContext()); String appPath = MicroPhotoContext.buildAppDir(getApplicationContext());
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try { try {
File channelFile = new File(appPath + "data/channels/" + String.valueOf(channel) + ".json"); String path = 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(); JSONObject jsonObject = JSONUtils.loadJson(path);
if (jsonObject == null) {
jsonObject = new JSONObject();
}
binding.btnUsbCamera.setChecked(jsonObject.optInt("usbCamera", 0) == 1); binding.btnUsbCamera.setChecked(jsonObject.optInt("usbCamera", 0) == 1);
binding.btnAutoExplosure.setChecked(jsonObject.optInt("autoExposure", 1) == 1); binding.btnAutoExplosure.setChecked(jsonObject.optInt("autoExposure", 1) == 1);
binding.btnAutoFocus.setChecked(jsonObject.optInt("autoFocus", 1) == 1); binding.btnAutoFocus.setChecked(jsonObject.optInt("autoFocus", 1) == 1);
@ -177,26 +168,8 @@ public class ChannelActivity extends AppCompatActivity {
binding.osdRightBottom.setText(""); binding.osdRightBottom.setText("");
binding.osdLeftBottom.setText(""); binding.osdLeftBottom.setText("");
} }
} catch (Exception e) {
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); 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) {
}
}
} }
} }
@ -204,45 +177,10 @@ public class ChannelActivity extends AppCompatActivity {
JSONObject jsonObject = null; JSONObject jsonObject = null;
String appPath = MicroPhotoContext.buildAppDir(this.getApplicationContext()); String appPath = MicroPhotoContext.buildAppDir(this.getApplicationContext());
InputStreamReader inputStreamReader = null; String dataPath = appPath + "data/channels/";
BufferedReader bufferedReader = null; String filePath = dataPath + String.valueOf(channel) + ".json";
File dataPath = new File(appPath + "data/channels/");
File file = new File(dataPath, String.valueOf(channel) + ".json");
try {
StringBuilder stringBuilder = new StringBuilder();
if (file.exists()) { jsonObject = JSONUtils.loadJson(filePath);
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) { if (jsonObject == null) {
jsonObject = new JSONObject(); jsonObject = new JSONObject();
@ -291,26 +229,15 @@ public class ChannelActivity extends AppCompatActivity {
} catch (JSONException ex) { } catch (JSONException ex) {
} }
OutputStreamWriter outputStreamWriter = null;
try { try {
if (!dataPath.exists()) { File dataPathFile = new File(dataPath);
dataPath.mkdirs(); if (!dataPathFile.exists()) {
dataPathFile.mkdirs();
} }
JSONUtils.saveJson(filePath, jsonObject);
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); } catch (Exception e) {
outputStreamWriter.write(jsonObject.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (outputStreamWriter != null) {
try {
outputStreamWriter.close();
} catch (Exception ex) {
}
}
} }
} }
} }

@ -263,50 +263,50 @@ public class MainActivity extends AppCompatActivity {
this.binding.btnTakePhoto.setOnClickListener(new View.OnClickListener() { this.binding.btnTakePhoto.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
MicroPhotoService.takePhoto(view.getContext().getApplicationContext(), 1, 255, true); takePhoto(1, 255, true);
} }
}); });
this.binding.btnTakePhoto2.setOnClickListener(new View.OnClickListener() { this.binding.btnTakePhoto2.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
MicroPhotoService.takePhoto(view.getContext().getApplicationContext(), 2, 255, true); takePhoto(2, 255, true);
} }
}); });
this.binding.btnTakePhoto3.setOnClickListener(new View.OnClickListener() { this.binding.btnTakePhoto3.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
MicroPhotoService.takePhoto(view.getContext().getApplicationContext(), 3, 255, true); takePhoto(3, 255, true);
} }
}); });
this.binding.btnTakePhoto4.setOnClickListener(new View.OnClickListener() { this.binding.btnTakePhoto4.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
MicroPhotoService.takePhoto(view.getContext().getApplicationContext(), 4, 255, true); takePhoto(4, 255, true);
} }
}); });
this.binding.takeVideoBtn.setOnClickListener(new View.OnClickListener() { this.binding.takeVideoBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
MicroPhotoService.takePhoto(view.getContext().getApplicationContext(), 1, 255, false); takePhoto(1, 255, false);
} }
}); });
this.binding.takeVideoBtn2.setOnClickListener(new View.OnClickListener() { this.binding.takeVideoBtn2.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
MicroPhotoService.takePhoto(view.getContext().getApplicationContext(), 2, 255, false); takePhoto(2, 255, false);
} }
}); });
this.binding.takeVideoBtn3.setOnClickListener(new View.OnClickListener() { this.binding.takeVideoBtn3.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
MicroPhotoService.takePhoto(view.getContext().getApplicationContext(), 3, 255, false); takePhoto(3, 255, false);
} }
}); });
this.binding.takeVideoBtn4.setOnClickListener(new View.OnClickListener() { this.binding.takeVideoBtn4.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
MicroPhotoService.takePhoto(view.getContext().getApplicationContext(), 4, 255, false); takePhoto(4, 255, false);
} }
}); });
@ -514,6 +514,10 @@ public class MainActivity extends AppCompatActivity {
} }
} }
private void takePhoto(int channel, int preset, boolean photoOrVideo) {
MicroPhotoService.takePhoto(this, channel, preset, photoOrVideo);
}
@Override @Override
protected void onStart() { protected void onStart() {
// call the superclass method first // call the superclass method first

@ -1,6 +1,5 @@
package com.xypower.mpapp; package com.xypower.mpapp;
import static java.lang.System.in;
import static java.lang.System.loadLibrary; import static java.lang.System.loadLibrary;
import android.app.AlarmManager; import android.app.AlarmManager;
@ -40,10 +39,8 @@ import android.os.PowerManager;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.SystemClock; import android.os.SystemClock;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.telephony.SignalStrength; import android.telephony.SignalStrength;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
@ -106,6 +103,7 @@ public class MicroPhotoService extends Service {
private static final String EXTRA_PARAM_PHOTO_OR_VIDEO = "PhotoOrVideo"; private static final String EXTRA_PARAM_PHOTO_OR_VIDEO = "PhotoOrVideo";
private static final String EXTRA_PARAM_SCHEDULES = "Schedules"; private static final String EXTRA_PARAM_SCHEDULES = "Schedules";
private static final String EXTRA_PARAM_SCHEDULE = "Schedule_"; private static final String EXTRA_PARAM_SCHEDULE = "Schedule_";
private static final String EXTRA_PARAM_TAKING_TIME = "TakingTime";
private static final String EXTRA_PARAM_TIME = "Time"; private static final String EXTRA_PARAM_TIME = "Time";
// private static String EXTRA_PARAM_FILENAME = "FileName"; // private static String EXTRA_PARAM_FILENAME = "FileName";
private static final String EXTRA_PARAM_TIMER_UID = "TimerUid"; private static final String EXTRA_PARAM_TIMER_UID = "TimerUid";
@ -357,7 +355,7 @@ public class MicroPhotoService extends Service {
long startTime = date.getTime() / 1000; long startTime = date.getTime() / 1000;
long baseTime = nowTs - startTime; long baseTime = nowTs - startTime;
mService.registerCaptureSchedule(startTime, baseTime); mService.updateCaptureSchedule(startTime, baseTime);
} else if (TextUtils.equals(ACTION_HEARTBEAT_MANUALLY, action)) { } else if (TextUtils.equals(ACTION_HEARTBEAT_MANUALLY, action)) {
Log.i(TAG, "HB Timer Fired ACTION=" + action); Log.i(TAG, "HB Timer Fired ACTION=" + action);
mService.sendHeartbeat(mService.mNativeHandle); mService.sendHeartbeat(mService.mNativeHandle);
@ -412,7 +410,7 @@ public class MicroPhotoService extends Service {
// alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeout, pendingIntent); // alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeout, pendingIntent);
} }
private static void registerPhotoTimer(Context context, int channel, int preset, long ts, long timeout, List<Long> schedules) { private static void registerPhotoTimer(Context context, int channel, int preset, long scheduleTime, long takingTime, long timeout, List<Long> schedules) {
// 创建延迟意图 // 创建延迟意图
Intent alarmIntent = new Intent(); Intent alarmIntent = new Intent();
@ -426,7 +424,8 @@ public class MicroPhotoService extends Service {
channelStr += "CH=" + ((val & 0XFF0000) >> 16) + "-PR=" + ((val & 0XFF00) >> 8) + " "; channelStr += "CH=" + ((val & 0XFF0000) >> 16) + "-PR=" + ((val & 0XFF00) >> 8) + " ";
} }
alarmIntent.putExtra(EXTRA_PARAM_TIME, ts); alarmIntent.putExtra(EXTRA_PARAM_TIME, scheduleTime);
alarmIntent.putExtra(EXTRA_PARAM_TAKING_TIME, takingTime);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
@ -441,12 +440,15 @@ public class MicroPhotoService extends Service {
} }
private void registerPhotoTimer(int channel, int preset, long ts, long timeout, List<Long> schedules) { private void registerPhotoTimer(int channel, int preset, long ts, long timeout, List<Long> schedules) {
registerPhotoTimer(this.getApplicationContext(), channel, preset, ts, timeout, schedules); registerPhotoTimer(this, channel, preset, ts, ts, timeout, schedules);
} }
public void startRecording(int cameraId, long videoId, int duration, int width, int height, int quality) { public void startRecording(int cameraId, long videoId, int duration, int width, int height, int quality) {
Context context = getApplicationContext(); Context context = getApplicationContext();
Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.xypower.mvapp"); Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.xypower.mvapp");
if (intent == null) {
return;
}
intent.putExtra("cameraId", cameraId); intent.putExtra("cameraId", cameraId);
intent.putExtra("videoId", videoId); intent.putExtra("videoId", videoId);
intent.putExtra("duration", duration); intent.putExtra("duration", duration);
@ -458,7 +460,7 @@ public class MicroPhotoService extends Service {
context.startActivity(intent); context.startActivity(intent);
} }
private boolean registerCaptureSchedule(long timeOfZeroPoint, long secondsOfToday) { protected boolean updateCaptureSchedule(long timeOfZeroPoint, long secondsOfToday) {
long[] photoTimeData = getPhotoTimeData(mNativeHandle); long[] photoTimeData = getPhotoTimeData(mNativeHandle);
if (photoTimeData == null) { if (photoTimeData == null) {
@ -485,6 +487,7 @@ public class MicroPhotoService extends Service {
} }
if ((ts - secondsOfToday) > maxDuration) { if ((ts - secondsOfToday) > maxDuration) {
day = 2; // For break two loops
break; break;
} }
@ -497,6 +500,7 @@ public class MicroPhotoService extends Service {
Log.i(TAG, "PhotoTimer Reg: CH=" + channel + " PR=" + preset); Log.i(TAG, "PhotoTimer Reg: CH=" + channel + " PR=" + preset);
} else if (ts > currentTs) { } else if (ts > currentTs) {
day = 2; // For break two loops
break; break;
} else { } else {
@ -536,7 +540,7 @@ public class MicroPhotoService extends Service {
schedules.add(Long.valueOf(val)); schedules.add(Long.valueOf(val));
registerPhotoTimer(context, channel, preset, 0, 0, schedules); registerPhotoTimer(context, channel, preset, 0, System.currentTimeMillis() / 1000, 100, schedules);
} }
public static void sendHeartbeat(Context context) { public static void sendHeartbeat(Context context) {
@ -691,7 +695,7 @@ public class MicroPhotoService extends Service {
long startTime = date.getTime() / 1000; long startTime = date.getTime() / 1000;
long baseTime = nowTs - startTime; long baseTime = nowTs - startTime;
service.registerCaptureSchedule(startTime, baseTime); service.updateCaptureSchedule(startTime, baseTime);
} }
} }
}; };

@ -21,13 +21,15 @@ public class JSONUtils {
public static JSONObject loadJson(String path) { public static JSONObject loadJson(String path) {
JSONObject jsonObject = null; JSONObject jsonObject = null;
FileInputStream fis = null;
InputStreamReader inputStreamReader = null; InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null; BufferedReader bufferedReader = null;
StringBuilder stringBuilder = null; StringBuilder stringBuilder = null;
try { try {
File appCfgFile = new File(path); File appCfgFile = new File(path);
if (appCfgFile.exists()) { if (appCfgFile.exists()) {
inputStreamReader = new InputStreamReader(new FileInputStream(appCfgFile), "UTF-8"); fis = new FileInputStream(appCfgFile);
inputStreamReader = new InputStreamReader(fis, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader); bufferedReader = new BufferedReader(inputStreamReader);
String line; String line;
stringBuilder = new StringBuilder(); stringBuilder = new StringBuilder();
@ -50,12 +52,21 @@ public class JSONUtils {
try { try {
bufferedReader.close(); bufferedReader.close();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace();
} }
} }
if (inputStreamReader != null) { if (inputStreamReader != null) {
try { try {
inputStreamReader.close(); inputStreamReader.close();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (Exception ex) {
ex.printStackTrace();
} }
} }
} }
@ -65,9 +76,11 @@ public class JSONUtils {
public static boolean saveJson(String path, JSONObject jsonObject) { public static boolean saveJson(String path, JSONObject jsonObject) {
FileOutputStream fos = null;
OutputStreamWriter outputStreamWriter = null; OutputStreamWriter outputStreamWriter = null;
try { try {
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File(path)), "UTF-8"); fos = new FileOutputStream(new File(path));
outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
outputStreamWriter.write(jsonObject.toString()); outputStreamWriter.write(jsonObject.toString());
return true; return true;
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
@ -82,6 +95,13 @@ public class JSONUtils {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
if (fos != null) {
try {
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
} }
return false; return false;

@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Environment; import android.os.Environment;
import android.text.TextUtils;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -43,6 +44,54 @@ public class MicroPhotoContext {
public int heartbeatDuration; public int heartbeatDuration;
} }
public static String readTextFile(String path) {
FileInputStream fis = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
StringBuilder stringBuilder = null;
try {
File appCfgFile = new File(path);
if (appCfgFile.exists()) {
fis = new FileInputStream(appCfgFile);
inputStreamReader = new InputStreamReader(fis, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return stringBuilder == null ? null : stringBuilder.toString();
}
public static String buildAppDir(Context contxt) { public static String buildAppDir(Context contxt) {
String path = Environment.getExternalStorageDirectory().getAbsolutePath(); String path = Environment.getExternalStorageDirectory().getAbsolutePath();
@ -80,22 +129,10 @@ public class MicroPhotoContext {
String appPath = buildAppDir(context); String appPath = buildAppDir(context);
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
StringBuilder stringBuilder = null;
try { try {
File appCfgFile = new File(appPath + "data/App.json"); String content = readTextFile(appPath + "data/App.json");
if (appCfgFile.exists()) {
inputStreamReader = new InputStreamReader(new FileInputStream(appCfgFile), "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() : new JSONObject(stringBuilder.toString()); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
appConfig.cmdid = jsonObject.optString("CMDID", ""); appConfig.cmdid = jsonObject.optString("CMDID", "");
appConfig.server = jsonObject.optString("Server", ""); appConfig.server = jsonObject.optString("Server", "");
appConfig.port = jsonObject.optInt("Port", 0); appConfig.port = jsonObject.optInt("Port", 0);
@ -108,27 +145,9 @@ public class MicroPhotoContext {
if (appConfig.protocol == 0) { if (appConfig.protocol == 0) {
appConfig.protocol = DEFAULT_PROTOCOL; appConfig.protocol = DEFAULT_PROTOCOL;
} }
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception ex) {
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception ex) {
}
}
} }
return appConfig; return appConfig;
} }
@ -138,22 +157,10 @@ public class MicroPhotoContext {
String appPath = buildMpAppDir(context); String appPath = buildMpAppDir(context);
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
StringBuilder stringBuilder = null;
try { try {
File appCfgFile = new File(appPath + "data/App.json"); String content = readTextFile(appPath + "data/App.json");
if (appCfgFile.exists()) {
inputStreamReader = new InputStreamReader(new FileInputStream(appCfgFile), "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() : new JSONObject(stringBuilder.toString()); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
appConfig.cmdid = jsonObject.optString("CMDID", ""); appConfig.cmdid = jsonObject.optString("CMDID", "");
appConfig.server = jsonObject.optString("Server", ""); appConfig.server = jsonObject.optString("Server", "");
appConfig.port = jsonObject.optInt("Port", 0); appConfig.port = jsonObject.optInt("Port", 0);
@ -166,25 +173,8 @@ public class MicroPhotoContext {
if (appConfig.protocol == 0) { if (appConfig.protocol == 0) {
appConfig.protocol = DEFAULT_PROTOCOL; appConfig.protocol = DEFAULT_PROTOCOL;
} }
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception ex) {
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception ex) {
}
}
} }
return appConfig; return appConfig;
@ -193,8 +183,7 @@ public class MicroPhotoContext {
public static void saveAppConfig(Context context, AppConfig appConfig) { public static void saveAppConfig(Context context, AppConfig appConfig) {
String appPath = buildAppDir(context); String appPath = buildAppDir(context);
InputStreamReader inputStreamReader = null; FileOutputStream fos = null;
BufferedReader bufferedReader = null;
OutputStreamWriter outputStreamWriter = null; OutputStreamWriter outputStreamWriter = null;
try { try {
@ -203,20 +192,9 @@ public class MicroPhotoContext {
dataPath.mkdirs(); dataPath.mkdirs();
} }
StringBuilder stringBuilder = new StringBuilder(); String content = readTextFile(appPath + "data/App.json");
File file = new File(dataPath, "App.json");
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 jsonObject = stringBuilder.length() > 0 ? (new JSONObject(stringBuilder.toString())) : (new JSONObject());
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
jsonObject.put("CMDID", appConfig.cmdid); jsonObject.put("CMDID", appConfig.cmdid);
jsonObject.put("Server", appConfig.server); jsonObject.put("Server", appConfig.server);
jsonObject.put("Port", appConfig.port); jsonObject.put("Port", appConfig.port);
@ -235,7 +213,8 @@ public class MicroPhotoContext {
jsonObject.remove("packetSize"); jsonObject.remove("packetSize");
} }
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File(appPath + "data/App.json")), "UTF-8"); fos = new FileOutputStream(new File(appPath + "data/App.json"));
outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
outputStreamWriter.write(jsonObject.toString()); outputStreamWriter.write(jsonObject.toString());
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
@ -244,24 +223,17 @@ public class MicroPhotoContext {
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (bufferedReader != null) { if (outputStreamWriter != null) {
try {
bufferedReader.close();
} catch (Exception ex) {
}
}
if (inputStreamReader != null) {
try { try {
inputStreamReader.close(); outputStreamWriter.close();
} catch (Exception ex) { } catch (Exception ex) {
} }
} }
if (outputStreamWriter != null) { if (fos != null) {
try { try {
outputStreamWriter.close(); fos.close();
} catch (Exception ex) { } catch (Exception ex) {
} }
} }
} }
} }
@ -269,48 +241,18 @@ public class MicroPhotoContext {
public static MasterConfig getMasterConfig(Context context) { public static MasterConfig getMasterConfig(Context context) {
MasterConfig masterConfig = new MasterConfig(); MasterConfig masterConfig = new MasterConfig();
String appPath = buildAppDir(context); String appPath = buildAppDir(context);
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
StringBuilder stringBuilder = null;
try { try {
File appCfgFile = new File(appPath + "data/Master.json"); String content = readTextFile(appPath + "data/Master.json");
if (appCfgFile.exists()) {
inputStreamReader = new InputStreamReader(new FileInputStream(appCfgFile), "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() : new JSONObject(stringBuilder.toString()); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
masterConfig.url = jsonObject.optString("url", ""); masterConfig.url = jsonObject.optString("url", "");
masterConfig.mntnMode = jsonObject.optInt("mntnMode", 0); masterConfig.mntnMode = jsonObject.optInt("mntnMode", 0);
masterConfig.quickHbMode = jsonObject.optInt("quickHbMode", 0); masterConfig.quickHbMode = jsonObject.optInt("quickHbMode", 0);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception ex) {
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception ex) {
}
}
} }
return masterConfig; return masterConfig;
@ -319,8 +261,7 @@ public class MicroPhotoContext {
public static void saveMasterConfig(Context context, MasterConfig masterConfig) { public static void saveMasterConfig(Context context, MasterConfig masterConfig) {
String appPath = buildAppDir(context); String appPath = buildAppDir(context);
InputStreamReader inputStreamReader = null; FileOutputStream fos = null;
BufferedReader bufferedReader = null;
OutputStreamWriter outputStreamWriter = null; OutputStreamWriter outputStreamWriter = null;
try { try {
@ -329,51 +270,32 @@ public class MicroPhotoContext {
dataPath.mkdirs(); dataPath.mkdirs();
} }
StringBuilder stringBuilder = new StringBuilder(); String content = readTextFile(appPath + "data/Master.json");
File file = new File(dataPath, "Master.json");
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 jsonObject = stringBuilder.length() > 0 ? (new JSONObject(stringBuilder.toString())) : (new JSONObject());
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
jsonObject.put("url", masterConfig.url); jsonObject.put("url", masterConfig.url);
jsonObject.put("Server", masterConfig.mntnMode); jsonObject.put("Server", masterConfig.mntnMode);
jsonObject.put("Port", masterConfig.quickHbMode); jsonObject.put("Port", masterConfig.quickHbMode);
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File(appPath + "data/App.json")), "UTF-8"); fos = new FileOutputStream(new File(appPath + "data/App.json"));
outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
outputStreamWriter.write(jsonObject.toString()); outputStreamWriter.write(jsonObject.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (bufferedReader != null) { if (outputStreamWriter != null) {
try {
bufferedReader.close();
} catch (Exception ex) {
}
}
if (inputStreamReader != null) {
try { try {
inputStreamReader.close(); outputStreamWriter.close();
} catch (Exception ex) { } catch (Exception ex) {
} }
} }
if (outputStreamWriter != null) { if (fos != null) {
try { try {
outputStreamWriter.close(); fos.close();
} catch (Exception ex) { } catch (Exception ex) {
} }
} }
} }
} }
@ -406,8 +328,7 @@ public class MicroPhotoContext {
public static void saveMpAppConfig(String path, AppConfig appConfig) { public static void saveMpAppConfig(String path, AppConfig appConfig) {
InputStreamReader inputStreamReader = null; FileOutputStream fos = null;
BufferedReader bufferedReader = null;
OutputStreamWriter outputStreamWriter = null; OutputStreamWriter outputStreamWriter = null;
try { try {
@ -416,20 +337,9 @@ public class MicroPhotoContext {
dataPath.mkdirs(); dataPath.mkdirs();
} }
StringBuilder stringBuilder = new StringBuilder(); String content = readTextFile(path + "data/App.json");
File file = new File(dataPath, "App.json");
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 jsonObject = stringBuilder.length() > 0 ? (new JSONObject(stringBuilder.toString())) : (new JSONObject());
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
jsonObject.put("CMDID", appConfig.cmdid); jsonObject.put("CMDID", appConfig.cmdid);
jsonObject.put("Server", appConfig.server); jsonObject.put("Server", appConfig.server);
jsonObject.put("Port", appConfig.port); jsonObject.put("Port", appConfig.port);
@ -448,33 +358,25 @@ public class MicroPhotoContext {
jsonObject.remove("packetSize"); jsonObject.remove("packetSize");
} }
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File(path + "data/App.json")), "UTF-8"); fos = new FileOutputStream(new File(path + "data/App.json"));
outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
outputStreamWriter.write(jsonObject.toString()); outputStreamWriter.write(jsonObject.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (bufferedReader != null) { if (outputStreamWriter != null) {
try {
bufferedReader.close();
} catch (Exception ex) {
}
}
if (inputStreamReader != null) {
try { try {
inputStreamReader.close(); outputStreamWriter.close();
} catch (Exception ex) { } catch (Exception ex) {
} }
} }
if (outputStreamWriter != null) { if (fos != null) {
try { try {
outputStreamWriter.close(); fos.close();
} catch (Exception ex) { } catch (Exception ex) {
} }
} }
} }
} }

@ -445,13 +445,15 @@ public class AppMaster {
private void buildParams(OutputStream output) { private void buildParams(OutputStream output) {
BufferedWriter bufferedWriter = null; BufferedWriter bufferedWriter = null;
OutputStreamWriter outputStreamWriter = null;
try { try {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("id", mCmdid); jsonObject.put("id", mCmdid);
jsonObject.put("i1Version", mService.getMpAppVersion()); jsonObject.put("i1Version", mService.getMpAppVersion());
jsonObject.put("maintainVersion", mService.getMasterAppVersion()); jsonObject.put("maintainVersion", mService.getMasterAppVersion());
bufferedWriter = new BufferedWriter(new OutputStreamWriter(output, "UTF-8")); outputStreamWriter = new OutputStreamWriter(output, "UTF-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(jsonObject.toString()); bufferedWriter.write(jsonObject.toString());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -463,6 +465,13 @@ public class AppMaster {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
try {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} }
} }

Loading…
Cancel
Save