package com.basic.security.utils; import android.os.Environment; import com.alfeye.a1io.DeviceControl; import com.basic.security.base.BaseApplication; import com.google.gson.Gson; import java.io.File; import java.net.URL; import java.util.Map; import java.util.Scanner; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class UpdateApk { static ExecutorService singleThreadExecutorService = Executors.newSingleThreadExecutor(); // public static boolean urlExists(String URLName) { // try { // HttpURLConnection.setFollowRedirects(false); // HttpURLConnection con = // (HttpURLConnection) new URL(URLName).openConnection(); // con.setRequestMethod("HEAD"); // return (con.getResponseCode() == HttpURLConnection.HTTP_OK); // } catch (Exception e) { // e.printStackTrace(); // return false; // } // } // public static boolean downloadApk(String urlStr, String apkName) { // try { // URL url = new URL(urlStr); // HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // urlConnection.setRequestMethod("GET"); // urlConnection.setDoOutput(true); // urlConnection.connect(); // // File file = new File(apkName); // // FileOutputStream fileOutput = new FileOutputStream(file); // InputStream inputStream = urlConnection.getInputStream(); // // byte[] buffer = new byte[1024]; // int bufferLength = 0; // // while ((bufferLength = inputStream.read(buffer)) > 0) { // fileOutput.write(buffer, 0, bufferLength); // } // fileOutput.close(); // return true; // } catch (Exception e) { // System.out.println("UpdateApk.downloadapk"); // return false; // } // } // public static void checkUpgrade() { // try { // String dirName = Environment.getExternalStorageDirectory() + "/download/"; // File dir = new File(dirName); // dir.mkdirs(); // File apkFile = new File(dir, Constants.updateApkFileName); // // String apkUrl = Constants.updateApkUrl + Constants.updateApkFileName; // if (urlExists(apkUrl)) { // if (downloadApk(apkUrl, apkFile.getAbsolutePath())) { // installApk(apkFile.getAbsolutePath()); // } // } // } catch (Exception e1) { // System.out.println("UpdateApk.run "); // } // } // public static void checkUpdateInLoop() { // singleThreadExecutorService.execute(new Runnable() { // @Override // public void run() { // while (true) { // try { // try { // String dirName = Environment.getExternalStorageDirectory() + "/download/"; // File dir = new File(dirName); // dir.mkdirs(); // File apkFile = new File(dir, Constants.updateApkFileName); // // String apkUrl = Constants.updateApkUrl + Constants.updateApkFileName; // if (urlExists(apkUrl)) { // if (downloadApk(apkUrl, apkFile.getAbsolutePath())) { // installApk(apkFile.getAbsolutePath()); // } // } // } catch (Exception e1) { // System.out.println("UpdateApk.run "); // } // } catch (Exception e) { // e.printStackTrace(); // } // SystemClock.sleep(10 * 1000); // } // } // }); // // } // public static void installApk(String apkPath) { // Intent intent = new Intent(Intent.ACTION_VIEW); // System.out.println("UpdateApk.installApk " + apkPath); // File file = new File(apkPath); // Uri apkURI = FileProvider.getUriForFile(BaseApplication.getApplication(),BuildConfig.APPLICATION_ID + ".fileProvider", file); // intent.setDataAndType(apkURI, "application/vnd.android.package-archive"); // intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // BaseApplication.getApplication().activity.startActivity(intent); // } public static void getApk() { try { String out = new Scanner(new URL(Constants.getApkUrl).openStream(), "UTF-8").useDelimiter("\\A").next(); Gson gson = new Gson(); Map map = gson.fromJson(out, Map.class); if ("true".equals(map.get("hasNewVersion"))) { System.out.println("UpdateApk.getApk 开始下载"); String newApkName = (String) map.get("newApkName"); String newApkSize = (String) map.get("newApkSize"); String newApkNameUrl = Constants.getApkUrl.substring(0, Constants.getApkUrl.lastIndexOf("/")) + "/" + newApkName; String dirName = Environment.getExternalStorageDirectory() + "/download/"; new File(dirName).mkdirs(); File downloadedFile = new File(dirName, newApkName); org.apache.commons.io.FileUtils.copyURLToFile(new URL(newApkNameUrl), downloadedFile); if (Integer.parseInt(newApkSize) == downloadedFile.length()) { if (Constants.useAlf) { DeviceControl.startInstallApk( BaseApplication.getApplication(), "com.basic.security", downloadedFile.getAbsolutePath(), true, true ); } } System.out.println("UpdateApk.getApk 安装完成"); } } catch (Exception e) { e.printStackTrace(); } } }