Skip to content

Commit a71768f

Browse files
committed
fix request external storage permission error on android Q
fix okhttp ssl factory error on android q
1 parent a9424f3 commit a71768f

File tree

9 files changed

+77
-37
lines changed

9 files changed

+77
-37
lines changed

library/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
apply plugin: 'com.android.library'
22
android {
3-
compileSdkVersion 28
4-
buildToolsVersion "28.0.3"
3+
compileSdkVersion 29
4+
buildToolsVersion "29.0.3"
55
resourcePrefix "versionchecklib"
66
defaultConfig {
77
minSdkVersion 14
8-
targetSdkVersion 28
8+
targetSdkVersion 29
99
versionCode 1
1010
versionName version
1111
}
@@ -25,7 +25,7 @@ android {
2525
dependencies {
2626
implementation fileTree(include: ['*.jar'], dir: 'libs')
2727
implementation 'androidx.appcompat:appcompat:1.1.0'
28-
implementation 'com.squareup.okhttp3:okhttp:3.14.2'
28+
implementation 'com.squareup.okhttp3:okhttp:4.3.1'
2929
implementation 'org.greenrobot:eventbus:3.1.1'
3030

3131
}

library/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<application
1414
android:allowBackup="true"
1515
android:supportsRtl="true"
16+
android:requestLegacyExternalStorage="true"
1617
android:usesCleartextTraffic="true">
1718
<activity
1819
android:name=".core.VersionDialogActivity"

library/src/main/java/com/allenliu/versionchecklib/core/http/AllenHttp.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class AllenHttp {
3636
public static OkHttpClient getHttpClient() {
3737
if (client == null) {
3838
OkHttpClient.Builder builder = new OkHttpClient.Builder();
39-
builder.sslSocketFactory(createSSLSocketFactory());
39+
builder.sslSocketFactory(createSSLSocketFactory(),new TrustAllCerts());
4040
builder.connectTimeout(15,TimeUnit.SECONDS);
4141
builder.hostnameVerifier(new TrustAllHostnameVerifier());
4242
client=builder.build();
@@ -182,7 +182,7 @@ private static <T extends Request.Builder> T assembleHeader(T builder, RequestVe
182182
}
183183
public static Request.Builder get(RequestVersionBuilder versionParams) {
184184
Request.Builder builder = new Request.Builder();
185-
builder = assembleHeader(builder, versionParams);
185+
assembleHeader(builder, versionParams);
186186
builder.url(assembleUrl(versionParams.getRequestUrl(), versionParams.getRequestParams()));
187187

188188
return builder;
@@ -191,7 +191,7 @@ public static Request.Builder get(RequestVersionBuilder versionParams) {
191191
public static Request.Builder post(RequestVersionBuilder versionParams) {
192192
FormBody formBody = getRequestParams(versionParams);
193193
Request.Builder builder = new Request.Builder();
194-
builder = assembleHeader(builder, versionParams);
194+
assembleHeader(builder, versionParams);
195195
builder.post(formBody).url(versionParams.getRequestUrl());
196196
return builder;
197197
}
@@ -201,7 +201,7 @@ public static Request.Builder postJson(RequestVersionBuilder versionParams) {
201201
String json = getRequestParamsJson(versionParams.getRequestParams());
202202
RequestBody body = RequestBody.create(JSON, json);
203203
Request.Builder builder = new Request.Builder();
204-
builder = assembleHeader(builder, versionParams);
204+
assembleHeader(builder, versionParams);
205205
builder.post(body).url(versionParams.getRequestUrl());
206206
return builder;
207207
}
Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,67 @@
11
package com.allenliu.versionchecklib.utils;
22

3+
import android.content.Context;
34
import android.os.Environment;
45

56
import java.io.File;
67

8+
import androidx.annotation.NonNull;
9+
710
public class FileHelper {
11+
@Deprecated
12+
public static String getDownloadApkCachePath() {
13+
14+
String appCachePath = null;
15+
16+
17+
if (checkSDCard()) {
18+
19+
appCachePath = Environment.getExternalStorageDirectory() + "/AllenVersionPath/";
20+
} else {
21+
appCachePath = Environment.getDataDirectory().getPath() + "/AllenVersionPath/";
22+
}
23+
File file = new File(appCachePath);
24+
if (!file.exists()) {
25+
file.mkdirs();
26+
}
27+
return appCachePath;
28+
}
829

9-
public static String getDownloadApkCachePath() {
30+
public static String getDownloadApkCachePath(Context context) {
31+
String appCachePath;
32+
if (checkSDCard()) {
33+
appCachePath = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/AllenVersionPath/";
1034

11-
String appCachePath = null;
35+
} else {
36+
appCachePath = context.getFilesDir().getAbsolutePath() + "/AllenVersionPath/";
1237

38+
}
1339

14-
if (checkSDCard()) {
15-
appCachePath = Environment.getExternalStorageDirectory() + "/AllenVersionPath/" ;
16-
} else {
17-
appCachePath = Environment.getDataDirectory().getPath() + "/AllenVersionPath/" ;
18-
}
19-
File file = new File(appCachePath);
20-
if (!file.exists()) {
21-
file.mkdirs();
22-
}
23-
return appCachePath;
24-
}
2540

41+
File file = new File(appCachePath);
42+
if (!file.exists()) {
43+
file.mkdirs();
44+
}
45+
return appCachePath;
46+
}
2647

2748

28-
/**
29-
*
30-
*/
31-
public static boolean checkSDCard() {
32-
boolean sdCardExist = Environment.getExternalStorageState().equals(
33-
Environment.MEDIA_MOUNTED);
49+
/**
50+
*
51+
*/
52+
private static boolean checkSDCard() {
3453

35-
return sdCardExist;
54+
return Environment.getExternalStorageState().equals(
55+
Environment.MEDIA_MOUNTED);
3656

37-
}
57+
}
3858

3959

60+
public static String dealDownloadPath(@NonNull String downloadAPKPath) {
61+
if (!downloadAPKPath.endsWith(File.separator)) {
62+
downloadAPKPath += File.separator;
63+
}
64+
return downloadAPKPath;
4065

66+
}
4167
}

library/src/main/java/com/allenliu/versionchecklib/v2/builder/DownloadBuilder.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.content.Context;
44
import android.content.pm.ApplicationInfo;
55
import android.content.pm.PackageManager;
6+
import android.os.Environment;
7+
68
import androidx.annotation.NonNull;
79

810
import com.allenliu.versionchecklib.callback.APKDownloadListener;
@@ -58,14 +60,14 @@ public DownloadBuilder() {
5860

5961
private void initialize() {
6062
isSilentDownload = false;
61-
downloadAPKPath = FileHelper.getDownloadApkCachePath();
63+
// downloadAPKPath = FileHelper.getDownloadApkCachePath();
6264
isForceRedownload = false;
6365
isShowDownloadingDialog = true;
6466
isShowNotification = true;
6567
isDirectDownload = false;
6668
isShowDownloadFailDialog = true;
6769
notificationBuilder = NotificationBuilder.create();
68-
runOnForegroundService=true;
70+
runOnForegroundService = true;
6971
}
7072

7173
public DownloadBuilder(RequestVersionBuilder requestVersionBuilder, UIData versionBundle) {
@@ -309,6 +311,7 @@ public void executeMission(Context context) {
309311
if (apkName == null) {
310312
apkName = context.getApplicationContext().getPackageName();
311313
}
314+
312315
if (notificationBuilder.getIcon() == 0) {
313316
final PackageManager pm = context.getPackageManager();
314317
final ApplicationInfo applicationInfo;
@@ -320,6 +323,9 @@ public void executeMission(Context context) {
320323
e.printStackTrace();
321324
}
322325
}
326+
//fix path permission
327+
setupDownloadPath(context);
328+
// downloadAPKPath=context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath() + "/";
323329
if (checkWhetherNeedRequestVersion()) {
324330
RequestVersionManager.getInstance().requestVersion(this, context.getApplicationContext());
325331
} else {
@@ -328,6 +334,13 @@ public void executeMission(Context context) {
328334

329335
}
330336

337+
private void setupDownloadPath(Context context) {
338+
if (downloadAPKPath == null) {
339+
downloadAPKPath = FileHelper.getDownloadApkCachePath(context);
340+
}
341+
downloadAPKPath = FileHelper.dealDownloadPath(downloadAPKPath);
342+
}
343+
331344
public void download(Context context) {
332345
VersionService.enqueueWork(context.getApplicationContext(), this);
333346
}

library/src/main/java/com/allenliu/versionchecklib/v2/builder/VersionCheckBinder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.ServiceConnection;
44
import android.os.Binder;
5+
import android.os.Environment;
56

67
import com.allenliu.versionchecklib.v2.ui.VersionService;
78

library/src/main/java/com/allenliu/versionchecklib/v2/ui/VersionService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
8888
if (!EventBus.getDefault().isRegistered(this)) {
8989
EventBus.getDefault().register(this);
9090
}
91-
9291
ALog.e("version service create");
9392
//https://issuetracker.google.com/issues/76112072
9493
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
9594
startForeground(NotificationHelper.NOTIFICATION_ID, NotificationHelper.createSimpleNotification(this));
9695
// init();
97-
return super.onStartCommand(intent, flags, startId);
96+
return START_REDELIVER_INTENT;
9897
}
9998

10099
@Override

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
apply plugin: 'com.android.application'
22
//apply plugin: 'me.tatarka.retrolambda'
33
android {
4-
compileSdkVersion 28
4+
compileSdkVersion 29
55
defaultConfig {
66
applicationId "com.allenliu.sample"
77
minSdkVersion 16
8-
targetSdkVersion 28
8+
targetSdkVersion 29
99
versionCode 1
1010
versionName "1.0"
1111

sample/src/main/java/com/allenliu/sample/v2/V2Activity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ public void onCancel() {
181181
builder.setCustomDownloadFailedListener(createCustomDownloadFailedDialog());
182182
break;
183183
}
184-
//自定义下载路径
185-
builder.setDownloadAPKPath(Environment.getExternalStorageDirectory() + "/ALLEN/AllenVersionPath2/");
184+
//自定义下载路径.在Android Q以上,请将路径设置为app内部路径,不要随便设置
185+
// builder.setDownloadAPKPath(Environment.getExternalStorageDirectory() + "/ALLEN/AllenVersionPath2");
186186
String address = etAddress.getText().toString();
187187
if (!"".equals(address))
188188
builder.setDownloadAPKPath(address);

0 commit comments

Comments
 (0)