From f51ceac69d4368bc62bd4bfa27cfd7076552df37 Mon Sep 17 00:00:00 2001 From: Administrator <981964879@qq.com> Date: Wed, 18 Mar 2020 18:40:06 +0800 Subject: [PATCH] =?UTF-8?q?update:2019.03.18=20fix:=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=A1=8C=E9=9D=A2=EF=BC=8C=E5=BA=94=E7=94=A8=E5=B8=82=E5=9C=BA?= =?UTF-8?q?=EF=BC=8Cupdatetools=E9=9D=99=E9=BB=98=E5=AE=89=E8=A3=85?= =?UTF-8?q?=E3=80=82=E5=A2=9E=E5=8A=A0=E4=BA=AE=E5=B1=8F=E5=90=8E=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E6=9B=B4=E6=96=B0=20add:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 4 +- app/src/main/AndroidManifest.xml | 18 +- app/src/main/assets/aria_config.xml | 161 ++++++++++++++++++ .../main/java/com/info/sn/MainActivity.java | 17 +- .../info/sn/network/api/HTTPInterface.java | 53 +++++- .../com/info/sn/service/BootReceiver.java | 16 +- .../info/sn/service/MyDownloadService.java | 102 +++++++++-- .../main/java/com/info/sn/utils/ApkUtils.java | 44 +++++ 8 files changed, 385 insertions(+), 30 deletions(-) create mode 100644 app/src/main/assets/aria_config.xml diff --git a/app/build.gradle b/app/build.gradle index 58bebe2..f39f805 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,8 +15,8 @@ android { applicationId "com.info.sn" minSdkVersion 23 targetSdkVersion 28 - versionCode 15 - versionName "1.1.5" + versionCode 16 + versionName "1.1.6" multiDexEnabled true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" ndk { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6ca27fc..ef0c23d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,12 +1,14 @@ + package="com.info.sn"> + + + + - @@ -170,6 +172,16 @@ android:exported="true"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/info/sn/MainActivity.java b/app/src/main/java/com/info/sn/MainActivity.java index c491ebb..286c837 100644 --- a/app/src/main/java/com/info/sn/MainActivity.java +++ b/app/src/main/java/com/info/sn/MainActivity.java @@ -94,6 +94,7 @@ public class MainActivity extends AppCompatActivity { initView(); initData(); HTTPInterface.checkDevicesInfo(handler); + HTTPInterface.checkUpdateByself(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE)); startService(new Intent(MainActivity.this, MyDownloadService.class)); } @@ -135,7 +136,7 @@ public class MainActivity extends AppCompatActivity { if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) { mHits = new long[COUNTS];//重新初始化数组 Toast.makeText(this, "正在检查更新", Toast.LENGTH_SHORT).show(); - HTTPInterface.checkUpdateByPackage(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE)); + HTTPInterface.checkUpdateByself(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE)); } } @@ -275,21 +276,27 @@ public class MainActivity extends AppCompatActivity { SPUtils.put(MainActivity.this, "isLogined", 2); setImageAndText(imageView, "未经验证的设备,请联系客服"); break; - case 200: + case 300: Bundle bundle = (Bundle) msg.obj; getFile(bundle); break; case -200: ToastUtil.show("已是最新版本"); break; + + case -300: + ToastUtil.show("网络连接失败,检查网络连接"); + break; } } }; private void getFile(final Bundle bundle) { + String url = bundle.getString("url"); + final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/"); path.mkdirs(); - final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/Update" + bundle.getString("versionCode") + ".apk"); + final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/" + url.substring(url.lastIndexOf("/") + 1)); if (file.exists() && file.isFile()) { AlertDialog.Builder builder = new AlertDialog.Builder(this) @@ -309,8 +316,8 @@ public class MainActivity extends AppCompatActivity { ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失 ad.show(); } else { - OkGo.get(bundle.getString("url")) - .execute(new FileCallback("Sninfo/Update" + bundle.getString("versionCode") + ".apk") { + OkGo.get(url) + .execute(new FileCallback("Sninfo/" + url.substring(url.lastIndexOf("/") + 1)) { @Override public void onSuccess(final Response response) { // Settings.System.putString(getApplicationContext().getContentResolver(), "qch_app_forbid", "com.baidu.video"); diff --git a/app/src/main/java/com/info/sn/network/api/HTTPInterface.java b/app/src/main/java/com/info/sn/network/api/HTTPInterface.java index 107f5f2..40b6bdc 100644 --- a/app/src/main/java/com/info/sn/network/api/HTTPInterface.java +++ b/app/src/main/java/com/info/sn/network/api/HTTPInterface.java @@ -143,20 +143,65 @@ public class HTTPInterface { message.obj = bundle; } handler.sendMessage(message); - Log.e("checkUpdateByPackage", msg); + Log.e("checkUpdateSN", msg); } else { handler.sendEmptyMessage(-200); - Log.e("checkUpdateByPackage", msg); + Log.e("checkUpdateSN", msg); } } catch (Exception e) { - Log.e("checkUpdateByPackage", e.getMessage()); + Log.e("checkUpdateSN", e.getMessage()); } } @Override public void onError(Response response) { super.onError(response); - Log.e("checkUpdateByPackage", response.getException().toString()); + handler.sendEmptyMessage(-300); + Log.e("checkUpdateSN", response.getException().toString()); + } + }); + } + synchronized public static void checkUpdateByself(final Handler handler, String packageName, String versionCode) { + OkGo.post(UrlPath.GET_APP_UPDATE) + .params("code", versionCode) + .params("package", packageName) + .execute(new StringCallback() { + @Override + public void onSuccess(Response response) { + try { + JSONObject body = JSON.parseObject(response.body()); + int code = body.getInteger("code"); + String msg = body.getString("msg"); + if (code == 200) { + Message message = new Message(); + JSONObject data = JSON.parseObject(body.getString("data")); + if (data != null) { + String url = data.getString("downloadurl"); + String newversion = data.getString("newversion"); + String content = data.getString("content"); + message.what = 300; + Bundle bundle = new Bundle(); + bundle.putString("url", url); + bundle.putString("versionCode", newversion); + bundle.putString("content", content); + message.obj = bundle; + } + handler.sendMessage(message); + Log.e("checkUpdateSN", msg); + } else { + handler.sendEmptyMessage(-200); + Log.e("checkUpdateSN", msg); + } + } catch (Exception e) { + Log.e("checkUpdateSN", e.getMessage()); + } + } + + @Override + public void onError(Response response) { + super.onError(response); + handler.sendEmptyMessage(-300); + Log.e("checkUpdateSN", response.getException().toString()); } }); } diff --git a/app/src/main/java/com/info/sn/service/BootReceiver.java b/app/src/main/java/com/info/sn/service/BootReceiver.java index 4f4994a..c58b907 100644 --- a/app/src/main/java/com/info/sn/service/BootReceiver.java +++ b/app/src/main/java/com/info/sn/service/BootReceiver.java @@ -11,12 +11,24 @@ public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - Log.e("BootReceiver", intent.getAction()); - if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { + Log.e("SNBootReceiver", intent.getAction()); + if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED") + || intent.getAction().equals("android.intent.action.BATTERY_CHANGED") + || intent.getAction().equals("android.intent.action.BATTERY_LEVEL_CHANGED") + || intent.getAction().equals("android.intent.action.BATTERY_LOW") + || intent.getAction().equals("android.intent.action.BATTERY_OKAY") + || intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED") + || intent.getAction().equals("android.intent.action.ACTION_POWER_DISCONNECTED") + || intent.getAction().equals("android.intent.action.DATE_CHANGED") + || intent.getAction().equals("android.intent.action.TIME_TICK") + || intent.getAction().equals("android.intent.action.USER_PRESENT") + || intent.getAction().equals("android.intent.action.ACTION_SCREEN_ON") + || intent.getAction().equals("android.intent.action.ACTION_SCREEN_OFF")) { // Intent i = new Intent(context, InitJpushServer.class); // context.startService(i); context.startService(new Intent(context, StepService.class)); context.startService(new Intent(context, GuardService.class)); + context.startService(new Intent(context, MyDownloadService.class)); } } diff --git a/app/src/main/java/com/info/sn/service/MyDownloadService.java b/app/src/main/java/com/info/sn/service/MyDownloadService.java index fd1db7f..8ff0a8f 100644 --- a/app/src/main/java/com/info/sn/service/MyDownloadService.java +++ b/app/src/main/java/com/info/sn/service/MyDownloadService.java @@ -36,24 +36,24 @@ public class MyDownloadService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { flags = START_STICKY; - return super.onStartCommand(intent, flags, startId); - } - - synchronized private void CheckUpdate() { - HTTPInterface.checkUpdateByPackage(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE)); - } - - @Override - public void onCreate() { - super.onCreate(); startService(new Intent(this, StepService.class)); startService(new Intent(this, GuardService.class)); CheckUpdate(); CheckUpdateByPackageName("com.appstore.uiui", handler); //静默升级应用市场 + CheckUpdateByPackageName("com.android.uiuios", handler); + //静默升级桌面 CheckUpdateByPackageName("com.uiuios.updatetools", handler); //静默升级更新工具 HTTPInterface.getAllAppPackageName(handler); + return super.onStartCommand(intent, flags, startId); + } + + + @Override + public void onCreate() { + super.onCreate(); + // new Thread(new Runnable() { // @Override @@ -72,8 +72,11 @@ public class MyDownloadService extends Service { // }).start(); } + synchronized private void CheckUpdate() { + HTTPInterface.checkUpdateByself(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE)); + } + synchronized private void CheckUpdateByPackageName(String packageName, Handler handlers) { - //检测设备信息的更新 PackageManager pm = getPackageManager(); PackageInfo packageInfo = null; try { @@ -124,14 +127,85 @@ public class MyDownloadService extends Service { break; case 202: break; + case 300: + Bundle b = (Bundle) msg.obj; + update(b); + break; } } }; private void getFile(final Bundle bundle) { + String url = bundle.getString("url"); + final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/"); path.mkdirs(); - final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/Update" + bundle.getString("versionCode") + ".apk"); + final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/" + url.substring(url.lastIndexOf("/") + 1)); + + if (file.exists() && file.isFile()) { +// AlertDialog.Builder builder = new AlertDialog.Builder(this) +// .setTitle("软件更新") +// .setIcon(R.mipmap.ic_launcher) +// .setCancelable(false) +// .setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content")) +// .setPositiveButton("确定", new DialogInterface.OnClickListener() { +// @Override +// public void onClick(DialogInterface dialogInterface, int i) { + ApkUtils.installApp(MyDownloadService.this, file.getAbsolutePath()); +// dialogInterface.dismiss(); +// } +// }); +// AlertDialog ad = builder.create(); +// ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); +// ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失 +// ad.show(); + } else { + OkGo.get(url) + .execute(new FileCallback("Sninfo/" + url.substring(url.lastIndexOf("/") + 1)) { + @Override + public void onSuccess(final Response response) { +// Settings.System.putString(getApplicationContext().getContentResolver(), "qch_app_forbid", "com.baidu.video"); +// ApkUtils.installApkInSilence(response.body().getAbsolutePath(), Launcher.this.getPackageName()); +// AlertDialog.Builder builder = new AlertDialog.Builder(MyDownloadService.this) +// .setTitle("软件更新") +// .setIcon(R.mipmap.ic_launcher) +// .setCancelable(false) +// .setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content")) +// .setPositiveButton("确定", new DialogInterface.OnClickListener() { +// @Override +// public void onClick(DialogInterface dialogInterface, int i) { + ApkUtils.installApp(MyDownloadService.this, response.body().getAbsolutePath()); +// dialogInterface.dismiss(); +// } +// }); +// AlertDialog ad = builder.create(); +// ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); +// ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失 +// ad.show(); + Log.e("getFile", "download file successful,now installing"); + } + + @Override + public void onError(Response response) { + super.onError(response); + Log.e("getFile", "File download Failure" + response.getException()); + } + + @Override + public void downloadProgress(Progress progress) { + super.downloadProgress(progress); + Log.e("getFile", "已下载:" + progress.currentSize + ",总大小:" + progress.totalSize + ",进度:" + progress.fraction + ",当前网速:" + progress.speed); + } + }); + } + } + + private void update(final Bundle bundle) { + String url = bundle.getString("url"); + + final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/"); + path.mkdirs(); + final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/" + url.substring(url.lastIndexOf("/") + 1)); if (file.exists() && file.isFile()) { AlertDialog.Builder builder = new AlertDialog.Builder(this) @@ -151,8 +225,8 @@ public class MyDownloadService extends Service { ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失 ad.show(); } else { - OkGo.get(bundle.getString("url")) - .execute(new FileCallback("Sninfo/Update" + bundle.getString("versionCode") + ".apk") { + OkGo.get(url) + .execute(new FileCallback("Sninfo/" + url.substring(url.lastIndexOf("/") + 1)) { @Override public void onSuccess(final Response response) { // Settings.System.putString(getApplicationContext().getContentResolver(), "qch_app_forbid", "com.baidu.video"); diff --git a/app/src/main/java/com/info/sn/utils/ApkUtils.java b/app/src/main/java/com/info/sn/utils/ApkUtils.java index 6ebe7ab..8faef72 100644 --- a/app/src/main/java/com/info/sn/utils/ApkUtils.java +++ b/app/src/main/java/com/info/sn/utils/ApkUtils.java @@ -16,6 +16,7 @@ import android.widget.Toast; import androidx.core.content.FileProvider; +import com.info.sn.BuildConfig; import com.info.sn.R; import java.io.BufferedReader; @@ -335,6 +336,49 @@ public class ApkUtils { e.printStackTrace(); } } + public static boolean installApp(Context context, String apkPath) { + ToastUtil.show("正在安装应用..."); + Process process = null; + BufferedReader successResult = null; + BufferedReader errorResult = null; + StringBuilder successMsg = new StringBuilder(); + StringBuilder errorMsg = new StringBuilder(); + try { + process = new ProcessBuilder("pm", "install", "-i", BuildConfig.APPLICATION_ID, "--user", "0", apkPath).start(); + successResult = new BufferedReader(new InputStreamReader(process.getInputStream())); + errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String s; + while ((s = successResult.readLine()) != null) { + successMsg.append(s); + } + while ((s = errorResult.readLine()) != null) { + errorMsg.append(s); + } + } catch (Exception e) { + + } finally { + try { + if (successResult != null) { + successResult.close(); + } + if (errorResult != null) { + errorResult.close(); + } + } catch (Exception e) { + + } + if (process != null) { + process.destroy(); + } + } + Log.e("result", "" + errorMsg.toString()); + //如果含有“success”认为安装成功 + Log.e("installApp", successMsg.toString()); +// if (!successMsg.toString().equalsIgnoreCase("success")) { +// ApkUtils.install(context, new File(apkPath)); +// } + return successMsg.toString().equalsIgnoreCase("success"); + } public static void deleteApkInSilence(String packageName) { Class pmService;