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 cd3389b commit 88b1ce8
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 31 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ allprojects {
task clean(type: Delete) {
delete rootProject.buildDir
}

ext{
BUILD_VERSION=29
}
7 changes: 3 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
version = "2.2.3.2"
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
compileSdkVersion BUILD_VERSION
resourcePrefix "versionchecklib"
defaultConfig {
minSdkVersion 14
targetSdkVersion 28
targetSdkVersion BUILD_VERSION
versionCode 1
versionName version
}
Expand All @@ -28,7 +27,7 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
implementation 'com.squareup.okhttp3:okhttp:4.3.1'
implementation 'org.greenrobot:eventbus:3.1.1'

}
Expand Down
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
Original file line number Diff line number Diff line change
@@ -1,41 +1,68 @@
package com.allenliu.versionchecklib.utils;

import android.content.Context;
import android.os.Environment;
import android.support.annotation.NonNull;

import java.io.File;



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 @@ -59,7 +59,7 @@ public DownloadBuilder() {

private void initialize() {
isSilentDownload = false;
downloadAPKPath = FileHelper.getDownloadApkCachePath();
// downloadAPKPath = FileHelper.getDownloadApkCachePath();
isForceRedownload = false;
isShowDownloadingDialog = true;
isShowNotification = true;
Expand Down Expand Up @@ -321,6 +321,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 @@ -329,6 +332,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 @@ -87,13 +87,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,12 +1,12 @@
apply plugin: 'com.android.application'
//apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 28
compileSdkVersion BUILD_VERSION
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.allenliu.sample"
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion BUILD_VERSION
versionCode 1
versionName "1.0"

Expand Down

0 comments on commit 88b1ce8

Please sign in to comment.