Skip to content

Commit

Permalink
bug fixed, Android O
Browse files Browse the repository at this point in the history
  • Loading branch information
a-voyager committed Dec 17, 2018
1 parent 34ae642 commit 564d65e
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 45 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ AutoInstaller installer = new AutoInstaller.Builder(this)
.build();
```

## Updates
1. fixed on Android O
2. support override install


## Support me


Expand Down
5 changes: 5 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ AutoInstaller installer = new AutoInstaller.Builder(this)
.build();
```

## 更新
1. 适配 Android O
2. 修复高版本覆盖安装特性,感谢@雪松



## 赞助作者

Expand Down
13 changes: 6 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
compileSdkVersion 27

defaultConfig {

minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
Expand All @@ -20,8 +19,8 @@ android {
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25+'
compile project(':installerlibrary')
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation project(':installerlibrary')
}
11 changes: 8 additions & 3 deletions app/src/main/java/top/wuhaojie/installer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public void onClick(View view) {
/* 方案一: 默认安装器 */
/* 方案一: 默认安装器 */
AutoInstaller installer = AutoInstaller.getDefault(MainActivity.this);
// installer.install(APK_FILE_PATH);
installer.installFromUrl(APK_URL);
installer.install(APK_FILE_PATH);
// installer.installFromUrl(APK_URL);
installer.setOnStateChangedListener(new AutoInstaller.OnStateChangedListener() {
@Override
public void onStart() {
Expand All @@ -51,6 +51,11 @@ public void onComplete() {
public void onNeed2OpenService() {
Toast.makeText(MainActivity.this, "请打开辅助功能服务", Toast.LENGTH_SHORT).show();
}

@Override
public void needPermission() {
Toast.makeText(MainActivity.this, "需要申请存储空间权限", Toast.LENGTH_SHORT).show();
}
});


Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.android.tools.build:gradle:3.2.1'
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 All @@ -15,6 +16,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Oct 18 10:33:38 CST 2016
#Fri Dec 14 10:34:02 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
13 changes: 6 additions & 7 deletions installerlibrary/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

group='com.github.a-voyager'
group = 'com.github.a-voyager'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
compileSdkVersion 27

defaultConfig {
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
Expand All @@ -22,7 +21,7 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25+'
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:27.1.1'
}
22 changes: 13 additions & 9 deletions installerlibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="top.wuhaojie.installerlibrary">
package="top.wuhaojie.installerlibrary">

<!-- 辅助功能权限 -->
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

<application
android:allowBackup="true"
Expand All @@ -23,11 +27,11 @@
android:exported="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"/>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config"/>
android:resource="@xml/accessibility_service_config" />
</service>
<!-- 文件访问 -->
<provider
Expand All @@ -37,7 +41,7 @@
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
android:resource="@xml/file_paths" />
</provider>

</application>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package top.wuhaojie.installerlibrary;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
Expand Down Expand Up @@ -37,7 +39,10 @@
*/
public class AutoInstaller extends Handler {

private static final String TAG = "AutoInstaller";
private static final String TAG = AutoInstaller.class.getSimpleName();
private static final int REQUEST_CODE_PERMISSION_STORAGE = 100;


private static volatile AutoInstaller mAutoInstaller;
private Context mContext;
private String mTempPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Download";
Expand Down Expand Up @@ -72,6 +77,8 @@ public interface OnStateChangedListener {
void onComplete();

void onNeed2OpenService();

void needPermission();
}

private OnStateChangedListener mOnStateChangedListener;
Expand Down Expand Up @@ -109,6 +116,7 @@ private boolean installUseRoot(String filePath) {
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
result = false;
} finally {
try {
if (outputStream != null) {
Expand All @@ -127,12 +135,32 @@ private boolean installUseRoot(String filePath) {
}

private void installUseAS(String filePath) {
// 存储空间
if (permissionDenied()) {
sendEmptyMessage(4);
return;
}

// 允许安装应用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean b = mContext.getPackageManager().canRequestPackageInstalls();
if (!b) {
sendEmptyMessage(4);
return;
}
}

File file = new File(filePath);
if (!file.exists()) {
Log.e(TAG, "apk file not exists, path: " + filePath);
return;
}
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
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);
mContext.grantUriPermission(mContext.getPackageName(), contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(uri, "application/vnd.android.package-archive");
Expand All @@ -145,6 +173,23 @@ private void installUseAS(String filePath) {
}
}

private boolean permissionDenied() {
if (Build.VERSION.SDK_INT >= 23) {
String[] permissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

for (String str : permissions) {
if (mContext.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
return true;
}
}
}

return false;
}

private void toAccessibilityService() {
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
mContext.startActivity(intent);
Expand Down Expand Up @@ -200,8 +245,9 @@ public void run() {

switch (mMode) {
case BOTH:
if (!Utils.checkRooted() || !installUseRoot(filePath))
if (!Utils.checkRooted() || !installUseRoot(filePath)) {
installUseAS(filePath);
}
break;
case ROOT_ONLY:
installUseRoot(filePath);
Expand Down Expand Up @@ -232,6 +278,11 @@ public void handleMessage(Message msg) {
if (mOnStateChangedListener != null)
mOnStateChangedListener.onNeed2OpenService();
break;
case 4:
if (mOnStateChangedListener != null) {
mOnStateChangedListener.needPermission();
}
break;

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package top.wuhaojie.installerlibrary;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
Expand All @@ -13,20 +15,41 @@
*/
public class InstallAccessibilityService extends android.accessibilityservice.AccessibilityService {

private static final String TAG = InstallAccessibilityService.class.getSimpleName();

private Map<Integer, Boolean> handledMap = new HashMap<>();

private Handler mHandler = new Handler(Looper.getMainLooper());

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d(TAG, "onAccessibilityEvent: " + event.toString());

if (!String.valueOf(event.getPackageName()).contains("packageinstaller")) {
//不写完整包名,是因为某些手机(如小米)安装器包名是自定义的
return;
}

AccessibilityNodeInfo nodeInfo = event.getSource();
if (nodeInfo != null) {
int eventType = event.getEventType();
if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED ||
eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
if (handledMap.get(event.getWindowId()) == null) {
boolean handled = iterateNodesAndHandle(nodeInfo);
if (handled) {
handledMap.put(event.getWindowId(), true);
}
if (nodeInfo == null) {
Log.i(TAG, "eventNode: null, 重新获取eventNode...");
performGlobalAction(GLOBAL_ACTION_RECENTS); // 打开最近页面
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
performGlobalAction(GLOBAL_ACTION_BACK); // 返回安装页面
}
}, 320);
return;
}

int eventType = event.getEventType();
if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED ||
eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
if (handledMap.get(event.getWindowId()) == null) {
boolean handled = iterateNodesAndHandle(nodeInfo);
if (handled) {
handledMap.put(event.getWindowId(), true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="true"
android:description="@string/accessibility_service_description"
android:packageNames="com.android.packageinstaller"/>
android:description="@string/accessibility_service_description" />
7 changes: 6 additions & 1 deletion installerlibrary/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="download" path="Download"/>
<!--<external-path-->
<!--name="download"-->
<!--path="Download" />-->
<external-path
name="ext_root"
path="/" />
</paths>

0 comments on commit 564d65e

Please sign in to comment.