From 29b22bcda078762664f287d4b9ff1e7abc3c34e2 Mon Sep 17 00:00:00 2001 From: wuhaojie Date: Fri, 8 Dec 2017 11:36:16 +0800 Subject: [PATCH] fixed for Android N --- app/build.gradle | 8 +-- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- installerlibrary/build.gradle | 8 +-- installerlibrary/src/main/AndroidManifest.xml | 10 ++++ .../installerlibrary/AutoInstaller.java | 59 +++++++++++++++++-- .../InstallAccessibilityService.java | 9 ++- .../src/main/res/xml/file_paths.xml | 4 ++ 8 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 installerlibrary/src/main/res/xml/file_paths.xml diff --git a/app/build.gradle b/app/build.gradle index eed6a89..8e2ee4d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 24 - buildToolsVersion "24.0.0" + compileSdkVersion 25 + buildToolsVersion "25.0.0" defaultConfig { minSdkVersion 16 - targetSdkVersion 24 + targetSdkVersion 25 versionCode 1 versionName "1.0" } @@ -22,6 +22,6 @@ android { dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:24.0.0' + compile 'com.android.support:appcompat-v7:25+' compile project(':installerlibrary') } diff --git a/build.gradle b/build.gradle index b048ef7..bd2b09f 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.1' + classpath 'com.android.tools.build:gradle:2.3.2' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f06ce25..55940f0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/installerlibrary/build.gradle b/installerlibrary/build.gradle index 8b68b0d..ec94f1f 100644 --- a/installerlibrary/build.gradle +++ b/installerlibrary/build.gradle @@ -4,12 +4,12 @@ apply plugin: 'com.github.dcendents.android-maven' group='com.github.a-voyager' android { - compileSdkVersion 24 - buildToolsVersion "24.0.0" + compileSdkVersion 25 + buildToolsVersion "25.0.0" defaultConfig { minSdkVersion 16 - targetSdkVersion 24 + targetSdkVersion 25 versionCode 1 versionName "1.0" } @@ -24,5 +24,5 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:24.0.0' + compile 'com.android.support:appcompat-v7:25+' } diff --git a/installerlibrary/src/main/AndroidManifest.xml b/installerlibrary/src/main/AndroidManifest.xml index 5e7c1d0..1b4fcde 100644 --- a/installerlibrary/src/main/AndroidManifest.xml +++ b/installerlibrary/src/main/AndroidManifest.xml @@ -29,6 +29,16 @@ android:name="android.accessibilityservice" android:resource="@xml/accessibility_service_config"/> + + + + diff --git a/installerlibrary/src/main/java/top/wuhaojie/installerlibrary/AutoInstaller.java b/installerlibrary/src/main/java/top/wuhaojie/installerlibrary/AutoInstaller.java index eb28270..2814d5f 100644 --- a/installerlibrary/src/main/java/top/wuhaojie/installerlibrary/AutoInstaller.java +++ b/installerlibrary/src/main/java/top/wuhaojie/installerlibrary/AutoInstaller.java @@ -3,10 +3,12 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.os.Build; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.provider.Settings; +import android.support.v4.content.FileProvider; import android.text.TextUtils; import android.util.Log; @@ -19,6 +21,14 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.security.SecureRandom; +import java.security.cert.X509Certificate; + +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; import top.wuhaojie.installerlibrary.utils.Utils; @@ -30,7 +40,7 @@ public class AutoInstaller extends Handler { private static final String TAG = "AutoInstaller"; private static volatile AutoInstaller mAutoInstaller; private Context mContext; - private String mTempPath = Environment.getExternalStorageDirectory().getAbsolutePath(); + private String mTempPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Download"; public enum MODE { ROOT_ONLY, @@ -117,9 +127,17 @@ private boolean installUseRoot(String filePath) { } private void installUseAS(String filePath) { - Uri uri = Uri.fromFile(new File(filePath)); + File file = new File(filePath); + Uri uri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setDataAndType(uri, "application/vnd.android.package-archive"); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileProvider", file); + intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); + } else { + intent.setDataAndType(uri, "application/vnd.android.package-archive"); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + } mContext.startActivity(intent); if (!isAccessibilitySettingsOn(mContext)) { toAccessibilityService(); @@ -247,8 +265,15 @@ private File downLoadFile(String httpUrl) { try { URL url = new URL(httpUrl); connection = (HttpURLConnection) url.openConnection(); - connection.setConnectTimeout(10 * 1000); - connection.setReadTimeout(10 * 1000); + if (connection instanceof HttpsURLConnection) { + SSLContext sslContext = getSLLContext(); + if (sslContext != null) { + SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); + ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory); + } + } + connection.setConnectTimeout(60 * 1000); + connection.setReadTimeout(60 * 1000); connection.connect(); inputStream = connection.getInputStream(); outputStream = new FileOutputStream(file); @@ -275,6 +300,30 @@ private File downLoadFile(String httpUrl) { return file; } + private SSLContext getSLLContext() { + SSLContext sslContext = null; + try { + sslContext = SSLContext.getInstance("TLS"); + sslContext.init(null, new TrustManager[]{new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }}, new SecureRandom()); + } catch (Exception e) { + e.printStackTrace(); + } + return sslContext; + } + public static class Builder { private MODE mode = MODE.BOTH; diff --git a/installerlibrary/src/main/java/top/wuhaojie/installerlibrary/InstallAccessibilityService.java b/installerlibrary/src/main/java/top/wuhaojie/installerlibrary/InstallAccessibilityService.java index 3b78db4..7d75a01 100644 --- a/installerlibrary/src/main/java/top/wuhaojie/installerlibrary/InstallAccessibilityService.java +++ b/installerlibrary/src/main/java/top/wuhaojie/installerlibrary/InstallAccessibilityService.java @@ -1,5 +1,6 @@ package top.wuhaojie.installerlibrary; +import android.text.TextUtils; import android.util.Log; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; @@ -37,9 +38,13 @@ private boolean iterateNodesAndHandle(AccessibilityNodeInfo nodeInfo) { if ("android.widget.Button".equals(nodeInfo.getClassName())) { String nodeContent = nodeInfo.getText().toString(); Log.d("TAG", "content is " + nodeContent); - if ("安装".equals(nodeContent) + if (!TextUtils.isEmpty(nodeContent) + && ("安装".equals(nodeContent) + || "install".equals(nodeContent.toLowerCase()) + || "done".equals(nodeContent.toLowerCase()) || "完成".equals(nodeContent) - || "确定".equals(nodeContent)) { + || "确定".equals(nodeContent) + )) { nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK); return true; } diff --git a/installerlibrary/src/main/res/xml/file_paths.xml b/installerlibrary/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000..f84c377 --- /dev/null +++ b/installerlibrary/src/main/res/xml/file_paths.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file