Skip to content

Commit

Permalink
fix request external storage permission error on android Q
Browse files Browse the repository at this point in the history
fix okhttp ssl factory error on android q
  • Loading branch information
AlexLiuSheng committed Apr 13, 2020
1 parent a9424f3 commit a71768f
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 37 deletions.
8 changes: 4 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
compileSdkVersion 29
buildToolsVersion "29.0.3"
resourcePrefix "versionchecklib"
defaultConfig {
minSdkVersion 14
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName version
}
Expand All @@ -25,7 +25,7 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.squareup.okhttp3:okhttp:3.14.2'
implementation 'com.squareup.okhttp3:okhttp:4.3.1'
implementation 'org.greenrobot:eventbus:3.1.1'

}
1 change: 1 addition & 0 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<application
android:allowBackup="true"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true">
<activity
android:name=".core.VersionDialogActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AllenHttp {
public static OkHttpClient getHttpClient() {
if (client == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(createSSLSocketFactory());
builder.sslSocketFactory(createSSLSocketFactory(),new TrustAllCerts());
builder.connectTimeout(15,TimeUnit.SECONDS);
builder.hostnameVerifier(new TrustAllHostnameVerifier());
client=builder.build();
Expand Down Expand Up @@ -182,7 +182,7 @@ private static <T extends Request.Builder> T assembleHeader(T builder, RequestVe
}
public static Request.Builder get(RequestVersionBuilder versionParams) {
Request.Builder builder = new Request.Builder();
builder = assembleHeader(builder, versionParams);
assembleHeader(builder, versionParams);
builder.url(assembleUrl(versionParams.getRequestUrl(), versionParams.getRequestParams()));

return builder;
Expand All @@ -191,7 +191,7 @@ public static Request.Builder get(RequestVersionBuilder versionParams) {
public static Request.Builder post(RequestVersionBuilder versionParams) {
FormBody formBody = getRequestParams(versionParams);
Request.Builder builder = new Request.Builder();
builder = assembleHeader(builder, versionParams);
assembleHeader(builder, versionParams);
builder.post(formBody).url(versionParams.getRequestUrl());
return builder;
}
Expand All @@ -201,7 +201,7 @@ public static Request.Builder postJson(RequestVersionBuilder versionParams) {
String json = getRequestParamsJson(versionParams.getRequestParams());
RequestBody body = RequestBody.create(JSON, json);
Request.Builder builder = new Request.Builder();
builder = assembleHeader(builder, versionParams);
assembleHeader(builder, versionParams);
builder.post(body).url(versionParams.getRequestUrl());
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,67 @@
package com.allenliu.versionchecklib.utils;

import android.content.Context;
import android.os.Environment;

import java.io.File;

import androidx.annotation.NonNull;

public class FileHelper {
@Deprecated
public static String getDownloadApkCachePath() {

String appCachePath = null;


if (checkSDCard()) {

appCachePath = Environment.getExternalStorageDirectory() + "/AllenVersionPath/";
} else {
appCachePath = Environment.getDataDirectory().getPath() + "/AllenVersionPath/";
}
File file = new File(appCachePath);
if (!file.exists()) {
file.mkdirs();
}
return appCachePath;
}

public static String getDownloadApkCachePath() {
public static String getDownloadApkCachePath(Context context) {
String appCachePath;
if (checkSDCard()) {
appCachePath = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/AllenVersionPath/";

String appCachePath = null;
} else {
appCachePath = context.getFilesDir().getAbsolutePath() + "/AllenVersionPath/";

}

if (checkSDCard()) {
appCachePath = Environment.getExternalStorageDirectory() + "/AllenVersionPath/" ;
} else {
appCachePath = Environment.getDataDirectory().getPath() + "/AllenVersionPath/" ;
}
File file = new File(appCachePath);
if (!file.exists()) {
file.mkdirs();
}
return appCachePath;
}

File file = new File(appCachePath);
if (!file.exists()) {
file.mkdirs();
}
return appCachePath;
}


/**
*
*/
public static boolean checkSDCard() {
boolean sdCardExist = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
/**
*
*/
private static boolean checkSDCard() {

return sdCardExist;
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);

}
}


public static String dealDownloadPath(@NonNull String downloadAPKPath) {
if (!downloadAPKPath.endsWith(File.separator)) {
downloadAPKPath += File.separator;
}
return downloadAPKPath;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Environment;

import androidx.annotation.NonNull;

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

private void initialize() {
isSilentDownload = false;
downloadAPKPath = FileHelper.getDownloadApkCachePath();
// downloadAPKPath = FileHelper.getDownloadApkCachePath();
isForceRedownload = false;
isShowDownloadingDialog = true;
isShowNotification = true;
isDirectDownload = false;
isShowDownloadFailDialog = true;
notificationBuilder = NotificationBuilder.create();
runOnForegroundService=true;
runOnForegroundService = true;
}

public DownloadBuilder(RequestVersionBuilder requestVersionBuilder, UIData versionBundle) {
Expand Down Expand Up @@ -309,6 +311,7 @@ public void executeMission(Context context) {
if (apkName == null) {
apkName = context.getApplicationContext().getPackageName();
}

if (notificationBuilder.getIcon() == 0) {
final PackageManager pm = context.getPackageManager();
final ApplicationInfo applicationInfo;
Expand All @@ -320,6 +323,9 @@ public void executeMission(Context context) {
e.printStackTrace();
}
}
//fix path permission
setupDownloadPath(context);
// downloadAPKPath=context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath() + "/";
if (checkWhetherNeedRequestVersion()) {
RequestVersionManager.getInstance().requestVersion(this, context.getApplicationContext());
} else {
Expand All @@ -328,6 +334,13 @@ public void executeMission(Context context) {

}

private void setupDownloadPath(Context context) {
if (downloadAPKPath == null) {
downloadAPKPath = FileHelper.getDownloadApkCachePath(context);
}
downloadAPKPath = FileHelper.dealDownloadPath(downloadAPKPath);
}

public void download(Context context) {
VersionService.enqueueWork(context.getApplicationContext(), this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Environment;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}

ALog.e("version service create");
//https://issuetracker.google.com/issues/76112072
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startForeground(NotificationHelper.NOTIFICATION_ID, NotificationHelper.createSimpleNotification(this));
// init();
return super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'
//apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "com.allenliu.sample"
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"

Expand Down
4 changes: 2 additions & 2 deletions sample/src/main/java/com/allenliu/sample/v2/V2Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public void onCancel() {
builder.setCustomDownloadFailedListener(createCustomDownloadFailedDialog());
break;
}
//自定义下载路径
builder.setDownloadAPKPath(Environment.getExternalStorageDirectory() + "/ALLEN/AllenVersionPath2/");
//自定义下载路径.在Android Q以上,请将路径设置为app内部路径,不要随便设置
// builder.setDownloadAPKPath(Environment.getExternalStorageDirectory() + "/ALLEN/AllenVersionPath2");
String address = etAddress.getText().toString();
if (!"".equals(address))
builder.setDownloadAPKPath(address);
Expand Down

0 comments on commit a71768f

Please sign in to comment.