增加升级功能

serial
Matthew 2 years ago
parent 95d729b163
commit 56253ff351

@ -0,0 +1,204 @@
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.io.UnsupportedEncodingException;
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 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() {
mWakelock.release();
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();
}
}
}).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(MicroPhotoService.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();
}
}

@ -0,0 +1,30 @@
package com.xypower.mpapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootBroadcastReceiver extends BroadcastReceiver {
private static final String ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
// Log.e("接收广播", "onReceive: ");
// Log.e("接收广播", "onReceive: " + intent.getAction());
//开机启动
if (ACTION.equals(intent.getAction())) {
// Log.e("接收广播", "onReceive: 启动了。。。");
Intent mainIntent = new Intent(context, MainActivity.class);
mainIntent.putExtra("reboot", 1);
/**
* Intent.FLAG_ACTIVITY_NEW_TASK
* Intent.FLAG_ACTIVITY_CLEAR_TOP
*/
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent);
// context.startService(mainIntent);
}
}
}

@ -15,11 +15,12 @@ import java.net.Socket;
import java.net.URL;
public class FileDownloader {
protected void download(String urlString, String filePath) {
protected boolean download(String urlString, String filePath) {
File downloadFile = null;
if (TextUtils.isEmpty(urlString))
return;
return false;
HttpURLConnection connection = null;
boolean res = false;
try {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
URL url = new URL(urlString);
@ -47,6 +48,7 @@ public class FileDownloader {
if (os instanceof FileOutputStream) {
((FileOutputStream) os).getFD().sync();
}
res = true;
} finally {
closeSilently(os);
closeSilently(is);
@ -62,6 +64,8 @@ public class FileDownloader {
if (connection != null)
connection.disconnect();
}
return res;
}
public static final void closeSilently(Object closeable) {

@ -28,6 +28,7 @@ import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import com.dev.devapi.api.SysApi;
import com.dowse.camera.client.DSCameraManager;
import com.xypower.mpapp.databinding.ActivityMainBinding;
//import com.xinyingpower.microphoto.request.INettyMessageListener;
@ -94,6 +95,12 @@ public class MainActivity extends AppCompatActivity {
Log.d(TAG, "Screen Size: " + width + " x " + height);
Intent intent = getIntent();
int rebootFlag = intent.getIntExtra("reboot", 0);
if (rebootFlag == 1) {
// SysApi.enableAirPlane(MainActivity.this, true);
}
String cmdid = "0123456789ABCDEFG";
String server = "47.96.238.157";
Integer port = new Integer(6891);

@ -75,6 +75,8 @@ public class MicroPhotoService extends Service {
public static final int CONNECTED = 10;
public static final int NOT_CONNECTED = 0;
}
private String mCmdid = "";
private NotificationManager mNotificationManager;
private final Map<String, PowerManager.WakeLock> mWakeLocks = new HashMap<>();
private int mHeartbeatDuration = 0; // 5m: 5 * 60 * 1000
@ -149,6 +151,11 @@ public class MicroPhotoService extends Service {
super.onDestroy();
}
public String getCmdid() {
return mCmdid;
}
public static class AlarmReceiver extends BroadcastReceiver {
private MicroPhotoService mService;
public AlarmReceiver() {
@ -163,6 +170,14 @@ public class MicroPhotoService extends Service {
Log.i(TAG, "HB Timer Fired ACTION=" + action);
mService.sendHeartbeat(mService.mHandler);
mService.registerHeartbeatTimer();
String cmdid = mService.getCmdid();
if (!TextUtils.isEmpty(cmdid)) {
AppMaster appMaster = new AppMaster(mService, cmdid);
appMaster.start();
}
} else if (TextUtils.equals(ACTION_TAKE_PHOTO, action)) {
long ts = intent.getLongExtra(EXTRA_PARAM_TIME, 0);
int cnt = intent.getIntExtra(EXTRA_PARAM_SCHEDULES, 0);
@ -452,6 +467,7 @@ public class MicroPhotoService extends Service {
// Start the locker receiver
if (mHandler !=0) {
mCmdid = cmdid;
Date date = new Date();
long nowTs = date.getTime() / 1000;
date.setHours(0);
@ -462,6 +478,9 @@ public class MicroPhotoService extends Service {
registerCaptureSchedule(startTime, baseTime);
// AppMaster appMaster = new AppMaster(this, cmdid);
// appMaster.start();
}
// registerPhotoTimer();

@ -0,0 +1,4 @@
package com.xypower.mpapp;
public class Upgrader {
}

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
Loading…
Cancel
Save