Skip to content

Commit c22c70c

Browse files
committed
优化尝试静默安装,如果失败再尝试普通安装
1 parent 86f6c81 commit c22c70c

File tree

8 files changed

+182
-92
lines changed

8 files changed

+182
-92
lines changed

app/build.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#Build Properties
2-
#Thu Mar 12 10:39:47 CST 2020
2+
#Fri Apr 24 15:28:26 CST 2020
33
version_minor=0
44
version_build=0
5-
version_patch=6
5+
version_patch=9
6+
version_store=109
67
version_major=2
7-
version_store=106

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
<uses-permission android:name="android.permission.INTERNET" />
3636
<uses-permission android:name="android.permission.WAKE_LOCK" />
3737
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
38+
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
39+
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
3840

3941
<application
4042
android:name="com.ayst.romupgrade.App"

app/src/main/java/com/ayst/romupgrade/service/UpdateService.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import com.ayst.romupgrade.adapter.DownloadAdapter;
6060
import com.ayst.romupgrade.entity.InstallProgress;
6161
import com.ayst.romupgrade.entity.LocalPackage;
62-
import com.ayst.romupgrade.util.SilentInstall;
62+
import com.ayst.romupgrade.util.InstallUtil;
6363
import com.baidu.commonlib.interfaces.ICheckUpdateListener;
6464
import com.baidu.commonlib.interfaces.IDownloadListener;
6565
import com.baidu.commonlib.interfaces.IUpgradeListener;
@@ -725,9 +725,11 @@ private void installApp(final File file) {
725725
Observable.create(new ObservableOnSubscribe<Boolean>() {
726726
@Override
727727
public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
728-
boolean success = SilentInstall.install(UpdateService.this,
729-
file.getAbsolutePath());
730-
emitter.onNext(success);
728+
if (InstallUtil.installSilent(file.getAbsolutePath())) {
729+
emitter.onNext(true);
730+
} else {
731+
InstallUtil.install(UpdateService.this, file.getAbsolutePath());
732+
}
731733
}
732734
}).subscribeOn(Schedulers.io())
733735
.observeOn(AndroidSchedulers.mainThread())
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright(c) 2020 Bob Shen <[email protected]>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.ayst.romupgrade.util;
18+
19+
import android.app.AlertDialog;
20+
import android.app.Dialog;
21+
import android.content.Context;
22+
import android.content.DialogInterface;
23+
import android.content.Intent;
24+
import android.net.Uri;
25+
import android.os.Build;
26+
import android.provider.Settings;
27+
import android.util.Log;
28+
import android.view.WindowManager;
29+
30+
import androidx.annotation.RequiresApi;
31+
import androidx.core.content.FileProvider;
32+
33+
import com.ayst.romupgrade.R;
34+
35+
import java.io.BufferedReader;
36+
import java.io.DataOutputStream;
37+
import java.io.File;
38+
import java.io.IOException;
39+
import java.io.InputStreamReader;
40+
import java.nio.charset.Charset;
41+
42+
public class InstallUtil {
43+
private static final String TAG = "InstallUtil";
44+
private static final String AUTHORITY = "com.ayst.romupgrade.fileProvider";
45+
46+
public static void install(Context context, String path) {
47+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
48+
installO(context, path);
49+
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
50+
installN(context, path);
51+
} else {
52+
installOther(context, path);
53+
}
54+
}
55+
56+
/**
57+
* android1.x-6.x
58+
*
59+
* @param context Context
60+
* @param path Package
61+
*/
62+
private static void installOther(Context context, String path) {
63+
Intent install = new Intent(Intent.ACTION_VIEW);
64+
install.setDataAndType(Uri.parse("file://" + path),
65+
"application/vnd.android.package-archive");
66+
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
67+
context.startActivity(install);
68+
}
69+
70+
/**
71+
* android7.x
72+
*
73+
* @param context Context
74+
* @param path Package
75+
*/
76+
private static void installN(Context context, String path) {
77+
Uri apkUri = FileProvider.getUriForFile(context, AUTHORITY, new File(path));
78+
Intent install = new Intent(Intent.ACTION_VIEW);
79+
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
80+
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
81+
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
82+
context.startActivity(install);
83+
}
84+
85+
/**
86+
* android8.x
87+
*
88+
* @param context Context
89+
* @param path Package
90+
*/
91+
@RequiresApi(api = Build.VERSION_CODES.O)
92+
private static void installO(Context context, String path) {
93+
boolean isGranted = context.getPackageManager().canRequestPackageInstalls();
94+
if (isGranted) {
95+
installN(context, path);
96+
} else {
97+
Dialog dialog = new AlertDialog.Builder(context.getApplicationContext())
98+
.setTitle(R.string.upgrade_unknown_sources_package)
99+
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
100+
public void onClick(DialogInterface d, int w) {
101+
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
102+
context.startActivity(intent);
103+
}
104+
}).create();
105+
dialog.setCancelable(false);
106+
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
107+
dialog.show();
108+
}
109+
}
110+
111+
/**
112+
* Silent install
113+
*
114+
* @param path Package
115+
* @return true: success false: failed
116+
*/
117+
public static boolean installSilent(String path) {
118+
boolean result = false;
119+
BufferedReader es = null;
120+
DataOutputStream os = null;
121+
122+
try {
123+
Process process = Runtime.getRuntime().exec("su");
124+
os = new DataOutputStream(process.getOutputStream());
125+
126+
String command = "pm install -r " + path + "\n";
127+
os.write(command.getBytes(Charset.forName("utf-8")));
128+
os.flush();
129+
os.writeBytes("exit\n");
130+
os.flush();
131+
132+
process.waitFor();
133+
es = new BufferedReader(new InputStreamReader(process.getErrorStream()));
134+
135+
String line;
136+
StringBuilder builder = new StringBuilder();
137+
while ((line = es.readLine()) != null) {
138+
builder.append(line);
139+
}
140+
Log.d(TAG, "install msg is " + builder.toString());
141+
142+
/* Installation is considered a Failure if the result contains
143+
the Failure character, or a success if it is not.
144+
*/
145+
if (!builder.toString().contains("Failure")) {
146+
result = true;
147+
}
148+
} catch (Exception e) {
149+
Log.e(TAG, e.getMessage(), e);
150+
} finally {
151+
try {
152+
if (os != null) {
153+
os.close();
154+
}
155+
if (es != null) {
156+
es.close();
157+
}
158+
} catch (IOException e) {
159+
Log.e(TAG, e.getMessage(), e);
160+
}
161+
}
162+
163+
return result;
164+
}
165+
}

app/src/main/java/com/ayst/romupgrade/util/SilentInstall.java

Lines changed: 0 additions & 85 deletions
This file was deleted.

app/src/main/res/values-zh/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<string name="retry">重试</string>
2222
<string name="confirm">确认</string>
2323
<string name="delete">删除</string>
24+
<string name="ok">确定</string>
2425

2526
<string name="upgrade_title">升级</string>
2627
<string name="upgrade_download">下载</string>
@@ -32,5 +33,6 @@
3233
<string name="upgrade_install_success">%1$s 安装成功!</string>
3334
<string name="upgrade_install_failed">%1$s 安装失败!</string>
3435
<string name="upgrade_invalid_package">"%1$s 无效的升级包!"</string>
36+
<string name="upgrade_unknown_sources_package">"安装应用需要打开未知来源权限,请去设置中开启权限!"</string>
3537

3638
</resources>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<string name="retry">Retry</string>
2222
<string name="confirm">OK</string>
2323
<string name="delete">Delete</string>
24+
<string name="ok">OK</string>
2425

2526
<string name="upgrade_title">Upgrade</string>
2627
<string name="upgrade_download">Download</string>
@@ -32,5 +33,6 @@
3233
<string name="upgrade_install_success">%1$s install success!</string>
3334
<string name="upgrade_install_failed">%1$s install failed!</string>
3435
<string name="upgrade_invalid_package">%1$s invalid package!</string>
36+
<string name="upgrade_unknown_sources_package">To install the application, you need to open the permissions of unknown source. Please go to Settings to open the permissions!</string>
3537

3638
</resources>

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ buildscript {
44

55
repositories {
66
google()
7+
mavenCentral()
78
jcenter()
89
}
910
dependencies {
@@ -18,6 +19,7 @@ buildscript {
1819
allprojects {
1920
repositories {
2021
google()
22+
mavenCentral()
2123
jcenter()
2224
maven {
2325
url "https://jitpack.io"

0 commit comments

Comments
 (0)