优化配置文件的初始化

把日志显示移到独立的activity
rtmpsuck
Matthew 5 months ago
parent 25e465df3b
commit 680b763ff6

@ -99,7 +99,10 @@
android:supportsRtl="true"
android:theme="@style/Theme.MicroPhoto"
tools:targetApi="28">
<activity
android:name=".LogActivity"
android:exported="false"
android:screenOrientation="landscape" />
<activity
android:name=".video.RawActivity"
android:exported="false"

@ -0,0 +1,191 @@
package com.xypower.mpapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.FileObserver;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.MenuItem;
import com.xypower.common.MicroPhotoContext;
import com.xypower.mpapp.databinding.ActivityLogBinding;
import com.xypower.mpapp.utils.RandomReader;
import java.io.File;
public class LogActivity extends AppCompatActivity {
public static final String TAG = "MPLOG";
public static final int MSG_WHAT_LOG_OBSERVER = MicroPhotoService.MSG_WHAT_MAX + 10;
public static final int MAX_LOG_LINES = 480;
public static final int MIN_LOG_LINES = 120;
private ActivityLogBinding binding;
private Handler mHandler = null;
private LogFileObserver mLogFileObserver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityLogBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
binding.logs.setText("");
binding.logs.setMovementMethod(ScrollingMovementMethod.getInstance());
binding.logs.setScrollbarFadingEnabled(false);
mHandler = new Handler(Looper.myLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what) {
case MSG_WHAT_LOG_OBSERVER:
{
byte[] bytes = (byte[])msg.obj;
int bytesRead = msg.arg1;
String log = null;
try {
log = new String(bytes, 0, bytesRead, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
if (log != null) {
binding.logs.append(log);
int offset = binding.logs.getLineCount() * binding.logs.getLineHeight();
if (offset > binding.logs.getHeight()) {
binding.logs.scrollTo(0, offset - binding.logs.getHeight() + binding.logs.getLineHeight());
}
}
}
break;
}
}
};
}
@Override
protected void onResume() {
// call the superclass method first
super.onResume();
try {
String logFilePath = MicroPhotoContext.buildAppDir(this.getApplicationContext());
logFilePath += "logs";
File file = new File(logFilePath);
if (!file.exists()) {
file.mkdirs();
}
logFilePath += "/log.txt";
file = new File(logFilePath);
if (!file.exists()) {
file.createNewFile();
}
mLogFileObserver = new LogFileObserver(logFilePath);
mLogFileObserver.startWatching();
Log.i(TAG, "Log Observer Started");
int lines = binding.logs.getLineCount();
if (lines > MAX_LOG_LINES) {
int excessLineNumber = lines - MIN_LOG_LINES;
int eolIndex = -1;
CharSequence charSequence = binding.logs.getText();
for (int i = 0; i < excessLineNumber; i++) {
do {
eolIndex++;
} while (eolIndex < charSequence.length() && charSequence.charAt(eolIndex) != '\n');
}
if (eolIndex < charSequence.length()) {
binding.logs.getEditableText().delete(0, eolIndex + 1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onPause() {
// call the superclass method first
super.onPause();
try {
if (mLogFileObserver != null) {
mLogFileObserver.stopWatching();
mLogFileObserver = null;
Log.i(TAG, "Log Observer Stopped");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// todo: goto back activity from here
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private class LogFileObserver extends FileObserver {
private long mOffset = 0;
private String mPath = null;
public LogFileObserver(String path) {
super(path, FileObserver.MODIFY | FileObserver.CREATE);
mPath = path;
File file = new File(path);
if (file.exists()) {
mOffset = file.length();
}
}
@Override
public void onEvent(int event, String s) {
int e = event & FileObserver.ALL_EVENTS;
if (e == FileObserver.MODIFY) {
File file = new File(mPath);
long newOffset = file.length();
if (newOffset > mOffset) {
RandomReader reader = new RandomReader(mPath, mOffset);
byte[] bytes = new byte[(int)(newOffset - mOffset)];
int bytesRead = reader.read(bytes);
mOffset += bytesRead;
Message msg = Message.obtain();
msg.what = MSG_WHAT_LOG_OBSERVER;
msg.obj = bytes;
msg.arg1 = bytesRead;
mHandler.sendMessage(msg);
}
} else if (e == FileObserver.CREATE) {
mOffset = 0;
}
}
}
}

@ -50,60 +50,12 @@ public class MainActivity extends AppCompatActivity {
public static final String TAG = "MPLOG";
private static int MY_PERMISSIONS_REQUEST_FOREGROUND_SERVICE = 100;
public static final int MSG_WHAT_LOG_OBSERVER = MicroPhotoService.MSG_WHAT_MAX + 10;
// Used to load the 'microphoto' library on application startup.
public static final int MAX_LOG_LINES = 480;
public static final int MIN_LOG_LINES = 120;
private ActivityMainBinding binding;
private Handler mHandler = null;
private Messenger mMessenger = null;
private LogFileObserver mLogFileObserver = null;
private class LogFileObserver extends FileObserver {
private long mOffset = 0;
private String mPath = null;
public LogFileObserver(String path) {
super(path, FileObserver.MODIFY | FileObserver.CREATE);
mPath = path;
File file = new File(path);
if (file.exists()) {
mOffset = file.length();
}
}
@Override
public void onEvent(int event, String s) {
int e = event & FileObserver.ALL_EVENTS;
if (e == FileObserver.MODIFY) {
File file = new File(mPath);
long newOffset = file.length();
if (newOffset > mOffset) {
RandomReader reader = new RandomReader(mPath, mOffset);
byte[] bytes = new byte[(int)(newOffset - mOffset)];
int bytesRead = reader.read(bytes);
mOffset += bytesRead;
Message msg = Message.obtain();
msg.what = MSG_WHAT_LOG_OBSERVER;
msg.obj = bytes;
msg.arg1 = bytesRead;
mHandler.sendMessage(msg);
}
} else if (e == FileObserver.CREATE) {
mOffset = 0;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -128,39 +80,45 @@ public class MainActivity extends AppCompatActivity {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
actionBar.setTitle(actionBar.getTitle().toString() + " v" + MicroPhotoContext.getVersionName(getApplicationContext()) + " " + sdf.format(date));
binding.logs.setText("");
binding.logs.setMovementMethod(ScrollingMovementMethod.getInstance());
binding.logs.setScrollbarFadingEnabled(false);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mHandler = new Handler(Looper.myLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what) {
case MSG_WHAT_LOG_OBSERVER:
{
byte[] bytes = (byte[])msg.obj;
int bytesRead = msg.arg1;
String log = null;
initListener();
Context appContext = getApplicationContext();
String appPath = MicroPhotoContext.buildMpAppDir(appContext);
File appPathFile = new File(appPath);
if (!appPathFile.exists()) {
try {
log = new String(bytes, 0, bytesRead, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
appPathFile.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
}
if (log != null) {
binding.logs.append(log);
int offset = binding.logs.getLineCount() * binding.logs.getLineHeight();
if (offset > binding.logs.getHeight()) {
binding.logs.scrollTo(0, offset - binding.logs.getHeight() + binding.logs.getLineHeight());
}
if (!MicroPhotoContext.hasMpAppConfig(appContext)) {
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();
}
}
break;
try {
mpdataFile.renameTo(dataFile);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
};
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Intent intent = getIntent();
final int noDelay = intent.getIntExtra("noDelay", 0);
@ -175,7 +133,9 @@ public class MainActivity extends AppCompatActivity {
Log.d(TAG, "MainActivity: reboot=" + rebootFlag + " noDelay=" + noDelay);
final MicroPhotoContext.AppConfig appConfig = getAppConfig();
final MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(appContext);
if (TextUtils.isEmpty(appConfig.cmdid)) {
appConfig.cmdid = MicroPhotoService.getSerialNumber();
binding.cmdid.setText(appConfig.cmdid);
@ -206,6 +166,45 @@ 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)) {
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()) {
binding.btnStartServ.performClick();
}
}
}
};
Handler handler = new Handler();
handler.postDelayed(runnable, 500);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
protected void initListener() {
this.binding.btnStartServ.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -226,17 +225,10 @@ public class MainActivity extends AppCompatActivity {
// return;
}
binding.logs.setText("");
MicroPhotoContext.AppConfig curAppConfig = retrieveAndSaveAppConfig();
// TakeAndThrowPhoto(2, 0xFF);
try {
// Thread.sleep(20);
} catch (Exception ex) {
ex.printStackTrace();
}
Context appContext = getApplicationContext();
MicroPhotoContext.AppConfig curAppConfig = MicroPhotoContext.getMpAppConfig(appContext);
startMicroPhotoService(MainActivity.this.getApplicationContext(), appConfig, mMessenger);
startMicroPhotoService(appContext, curAppConfig, mMessenger);
binding.btnStartServ.setEnabled(false);
binding.btnStopServ.setEnabled(true);
@ -356,30 +348,13 @@ public class MainActivity extends AppCompatActivity {
}
});
if (MicroPhotoService.isRunning) {
Intent intent2 = new Intent(MainActivity.this, MicroPhotoService.class);
try {
stopService(intent2);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Runnable runnable = new Runnable() {
binding.btnLogs.setOnClickListener(new View.OnClickListener() {
@Override
public void run() {
if (!MicroPhotoService.isRunning && !TextUtils.isEmpty(appConfig.cmdid) && !TextUtils.isEmpty(appConfig.server) && appConfig.port != 0) {
if (binding.btnStartServ.isEnabled()) {
binding.btnStartServ.performClick();
}
}
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, LogActivity.class);
startActivity(intent);
}
};
mHandler.postDelayed(runnable, noDelay != 0 ? 1000 : 5000);
binding.btnStartServ.setEnabled(!MicroPhotoService.isRunning);
binding.btnStopServ.setEnabled(MicroPhotoService.isRunning);
});
binding.btnSendHb.setOnClickListener(new View.OnClickListener() {
@Override
@ -443,17 +418,13 @@ public class MainActivity extends AppCompatActivity {
}
};
mHandler.postDelayed(runnable, 1500);
Handler handler = new Handler();
handler.postDelayed(runnable, 1500);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public static void startMicroPhotoService(Context context, MicroPhotoContext.AppConfig curAppConfig, Messenger messenger) {
if (TextUtils.isEmpty(curAppConfig.cmdid) || TextUtils.isEmpty(curAppConfig.server) || curAppConfig.port == 0) {
@ -524,63 +495,6 @@ public class MainActivity extends AppCompatActivity {
MicroPhotoService.takePhoto(channel, preset, true, configFile.getAbsolutePath(), photoFile.getAbsolutePath());
}
@Override
protected void onResume() {
// call the superclass method first
super.onResume();
try {
String logFilePath = MicroPhotoContext.buildAppDir(this.getApplicationContext());
logFilePath += "logs";
File file = new File(logFilePath);
if (!file.exists()) {
file.mkdirs();
}
logFilePath += "/log.txt";
file = new File(logFilePath);
if (!file.exists()) {
file.createNewFile();
}
mLogFileObserver = new LogFileObserver(logFilePath);
mLogFileObserver.startWatching();
Log.i(TAG, "Log Observer Started");
int lines = binding.logs.getLineCount();
if (lines > MAX_LOG_LINES) {
int excessLineNumber = lines - MIN_LOG_LINES;
int eolIndex = -1;
CharSequence charSequence = binding.logs.getText();
for (int i = 0; i < excessLineNumber; i++) {
do {
eolIndex++;
} while (eolIndex < charSequence.length() && charSequence.charAt(eolIndex) != '\n');
}
if (eolIndex < charSequence.length()) {
binding.logs.getEditableText().delete(0, eolIndex + 1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onPause() {
// call the superclass method first
super.onPause();
try {
if (mLogFileObserver != null) {
mLogFileObserver.stopWatching();
mLogFileObserver = null;
Log.i(TAG, "Log Observer Stopped");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private MicroPhotoContext.AppConfig retrieveAndSaveAppConfig() {
MicroPhotoContext.AppConfig appConfig = new MicroPhotoContext.AppConfig();

@ -204,6 +204,16 @@
app:layout_constraintStart_toEndOf="@+id/btnSaveCfg"
app:layout_constraintTop_toTopOf="@+id/btnStartServ" />
<Button
android:id="@+id/btnLogs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:minWidth="@dimen/activity_btn_min_width"
android:minHeight="@dimen/activity_btn_min_height"
android:text="日志"
app:layout_constraintStart_toEndOf="@+id/btnChannels"
app:layout_constraintTop_toTopOf="@+id/btnStartServ" />
<Button
android:id="@+id/btnTakePhoto"
@ -352,32 +362,4 @@
app:layout_constraintStart_toStartOf="@+id/btnCameraInfo"
app:layout_constraintTop_toTopOf="@+id/btnCameraInfo" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/leftBarrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="textViewCmdId,cmdid,protocol,networkProtocol,encryptions,btnSaveCfg,btnTakePhoto4,btnChannels,network,takeVideoBtn4"
tools:layout_editor_absoluteX="46dp" />
<TextView
android:id="@+id/logs"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:padding="4dp"
android:background="@drawable/textview_border"
android:orientation="horizontal"
android:lineSpacingMultiplier="1.25"
android:scrollbars="vertical"
android:singleLine="false"
android:text="Logs"
android:textColor="@color/black"
android:textIsSelectable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/leftBarrier"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".LogActivity">
<TextView
android:id="@+id/logs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:padding="4dp"
android:background="@drawable/textview_border"
android:orientation="horizontal"
android:lineSpacingMultiplier="1.25"
android:scrollbars="vertical"
android:singleLine="false"
android:text="Logs"
android:textColor="@color/black"
android:textIsSelectable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -211,6 +211,16 @@
android:layout_marginTop="8dp"
android:text="通道设置"
app:layout_constraintStart_toEndOf="@+id/simchange2"
app:layout_constraintTop_toTopOf="@+id/btnStartServ" />
<Button
android:id="@+id/btnLogs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="8dp"
android:text="日志"
app:layout_constraintStart_toEndOf="@+id/btnChannels"
app:layout_constraintTop_toBottomOf="@+id/btnStartServ" />
<Button
@ -343,32 +353,6 @@
app:layout_constraintStart_toStartOf="@+id/btnCameraInfo"
app:layout_constraintTop_toTopOf="@+id/btnCameraInfo" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/leftBarrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="textViewCmdId,cmdid,protocol,networkProtocol,encryptions,btnSaveCfg,btnTakePhoto4,btnChannels"
tools:layout_editor_absoluteX="46dp" />
<TextView
android:id="@+id/logs"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:orientation="horizontal"
android:padding="4dp"
android:lineSpacingMultiplier="1.25"
android:scrollbars="vertical"
android:text="Logs"
android:textColor="@color/black"
android:singleLine="false"
android:textIsSelectable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/btnTakePhoto4"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -127,7 +127,6 @@ public class FilesUtils {
return intValue;
}
public static void writeTextFile(String path, String content) {
FileOutputStream fileOutputStream = null;
try {

@ -1,6 +1,8 @@
package com.xypower.common;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
@ -224,6 +226,14 @@ public class MicroPhotoContext {
return path;
}
public static boolean hasMpAppConfig(Context context) {
boolean existed = true;
String appPath = MicroPhotoContext.buildMpAppDir(context);
File appPathFile = new File(appPath);
File appConfigFile = new File(appPathFile, "data/App.json");
return appConfigFile.exists();
}
public static AppConfig getMpAppConfig(Context context) {
String appPath = buildMpAppDir(context);
@ -379,6 +389,20 @@ public class MicroPhotoContext {
restartApp(context, PACKAGE_NAME_MPAPP, reason);
}
public static void restartMpApp(Context context, String reason, long delayedTimeMs) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME_MPAPP);
int noDelay = 1;
intent.putExtra("noDelay", noDelay);
if (!TextUtils.isEmpty(reason)) {
intent.putExtra("reason", reason);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, 0);
AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delayedTimeMs, restartIntent);
}
public static void restartApp(Context context, String packageName, String reason) {
/*
Context context = MicroPhotoService.this.getApplicationContext();

@ -154,7 +154,6 @@ public class MainActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item);
}
private void loadConfigInfo() {
Context context = getApplicationContext();
MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(context);

@ -27,7 +27,6 @@ import android.widget.RemoteViews;
import android.widget.Toast;
import androidx.core.app.NotificationCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.dev.devapi.api.SysApi;
import com.xypower.common.FileDownloader;
@ -45,14 +44,12 @@ import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.channels.FileLock;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.concurrent.atomic.AtomicInteger;;
@ -389,10 +386,7 @@ public class MpMasterService extends Service {
public boolean shouldSyncTime() { return mSyncTime; }
public void startMpApp() {
if (true) {
return;
}
public void detectMpAppAlive() {
try {
final Context context = getApplicationContext();
long ts = System.currentTimeMillis();
@ -457,17 +451,9 @@ public class MpMasterService extends Service {
" Will restart MpApp in " + Long.toString(mDelayedRestartMpTime / 1000) + " seconds";
logger.warning(msg);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent();
alarmIntent.setAction(ACTION_MP_RESTART);
alarmIntent.putExtra("reason", msg);
int uniqueReqCode = reqCode.getAndIncrement();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueReqCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, ts + mDelayedRestartMpTime, pendingIntent);
MicroPhotoContext.restartMpApp(context, msg, mDelayedRestartMpTime);
mTimeToStartMpApp = ts;
mTimeToStartMpApp = ts + mDelayedRestartMpTime / 1000;
}
}
} catch (Exception ex) {
@ -657,7 +643,7 @@ public class MpMasterService extends Service {
if (!keepAlive) {
mService.startMaster(false);
}
mService.startMpApp();
mService.detectMpAppAlive();
} else if (TextUtils.equals(MicroPhotoContext.ACTION_HEARTBEAT_MP, action)) {
@ -675,7 +661,7 @@ public class MpMasterService extends Service {
if (!mService.mSeparateNetwork && (!mService.mMntnMode)) {
mService.startMaster(true);
}
mService.startMpApp();
mService.detectMpAppAlive();
} else if (TextUtils.equals(ACTION_UPDATE_CONFIGS, action)) {
int restart = intent.getIntExtra("restart", 0);
mService.logger.info("Update Config Fired ACTION=" + action + " restart=" + restart);
@ -867,7 +853,7 @@ public class MpMasterService extends Service {
mHander.postDelayed(new Runnable() {
@Override
public void run() {
startMpApp();
detectMpAppAlive();
}
}, 5000);
@ -1244,19 +1230,20 @@ public class MpMasterService extends Service {
public static boolean initMpAppConfigurations(final Context context) {
Runnable runnable = new Runnable() {
@Override
public void run() {
boolean existed = true;
String destPath = MicroPhotoContext.buildMpAppDir(context);
File destPathFile = new File(destPath);
if (destPathFile.exists()) {
File dataPath = new File(destPathFile, "data");
if (dataPath.exists()) {
File file = new File(destPathFile, "data/App.json");
File file = new File(destPathFile, "App.json");
if (file.exists()) {
return;
return false;
} else {
existed = false;
}
} else {
existed = false;
try {
dataPath.mkdirs();
} catch (Exception ex) {
@ -1264,14 +1251,38 @@ public class MpMasterService extends Service {
}
}
} else {
existed = false;
File dataPath = new File(destPathFile, "data");
try {
destPathFile.mkdirs();
dataPath.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
}
}
copyAssetsDir(context, "mpapp", destPath);
if (existed) {
return false;
}
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (Exception ex) {
}
File tmpDestPath = new File(MicroPhotoContext.buildMasterAppDir(context));
tmpDestPath = new File(tmpDestPath, "mpdata");
if (tmpDestPath.exists()) {
try {
tmpDestPath.delete();
} catch (Exception ex) {
ex.printStackTrace();
}
}
copyAssetsDir(context, "mpapp/data", tmpDestPath.getAbsolutePath());
MicroPhotoContext.restartMpApp(context, "FIRST Config Init");
}
};
@ -1279,26 +1290,27 @@ public class MpMasterService extends Service {
Thread th = new Thread(runnable);
th.start();
return false;
return true;
}
public static boolean initMpMasterConfigurations(final Context context) {
String destPath = MicroPhotoContext.buildMasterAppDir(context);
File destPathFile = new File(destPath);
if (destPathFile.exists()) {
File file = new File(destPathFile, "data/Master.json");
File dataPath = new File(destPathFile, "data");
File file = new File(dataPath, "Master.json");
if (file.exists()) {
return false;
}
}
if (!dataPath.exists()) {
try {
destPathFile.mkdirs();
dataPath.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
}
}
copyAssetsDir(context, "mpmst", destPath);
copyAssetsDir(context, "mpmst", dataPath.getAbsolutePath());
return true;
}

Loading…
Cancel
Save