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 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.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> postParams = new ArrayList<>(); postParams.add(new Pair("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> paramsList) { BufferedWriter bufferedWriter = null; try { StringBuilder stringBuilder = new StringBuilder(); for (Pair 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(); } }