Skip to content

Commit

Permalink
fixed for Android N
Browse files Browse the repository at this point in the history
  • Loading branch information
a-voyager committed Dec 8, 2017
1 parent fa4b437 commit 29b22bc
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 17 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}
Expand All @@ -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')
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions installerlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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+'
}
10 changes: 10 additions & 0 deletions installerlibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config"/>
</service>
<!-- 文件访问 -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="top.wuhaojie.installerlibrary.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>

</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions installerlibrary/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="download" path="Download"/>
</paths>

0 comments on commit 29b22bc

Please sign in to comment.