Skip to content
This repository was archived by the owner on Sep 29, 2019. It is now read-only.

Commit 07a4b48

Browse files
author
dadaewqq
committed
Add permission request
Try to fix some bugs
1 parent 9f8fb2a commit 07a4b48

File tree

7 files changed

+146
-21
lines changed

7 files changed

+146
-21
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
applicationId "com.modosa.apkinstaller"
77
minSdkVersion 23
88
targetSdkVersion 28
9-
versionCode 2
10-
versionName "2.0"
9+
versionCode 3
10+
versionName "3.0"
1111
}
1212
buildTypes {
1313
release {

app/src/main/AndroidManifest.xml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,46 @@
44

55
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
66
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
7+
78
<application
89
android:allowBackup="false"
910
android:icon="@drawable/ic_launcher"
1011
android:label="@string/app_name"
1112
android:noHistory="true">
13+
<activity
14+
android:name=".InitializeActivity"
15+
android:enabled="true"
16+
android:launchMode="singleTask"
17+
android:theme="@android:style/Theme.NoDisplay">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN" />
20+
21+
<category android:name="android.intent.category.LAUNCHER" />
22+
</intent-filter>
23+
</activity>
1224
<activity
1325
android:name=".MainActivity"
1426
android:excludeFromRecents="true"
15-
android:label="Install Apk With IceBox"
1627
android:launchMode="singleTask"
28+
android:enabled="true"
1729
android:theme="@android:style/Theme.NoDisplay">
1830
<intent-filter>
1931
<action android:name="android.intent.action.VIEW" />
2032
<action android:name="android.intent.action.INSTALL_PACKAGE" />
33+
2134
<category android:name="android.intent.category.DEFAULT" />
35+
2236
<data android:scheme="file" />
2337
<data android:scheme="content" />
2438
<data android:mimeType="application/vnd.android.package-archive" />
2539
</intent-filter>
2640
</activity>
41+
2742
<provider
2843
android:name="android.support.v4.content.FileProvider"
2944
android:authorities="${applicationId}.FILE_PROVIDER"
30-
android:grantUriPermissions="true"
31-
android:exported="false">
45+
android:exported="false"
46+
android:grantUriPermissions="true">
3247
<meta-data
3348
android:name="android.support.FILE_PROVIDER_PATHS"
3449
android:resource="@xml/file_paths" />

app/src/main/java/com/modosa/apkinstaller/ApkInfo.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ class ApkInfo {
66
private File apkFile;
77
private boolean isFakePath;
88

9-
void setApkFile(File apkFile) {
10-
this.apkFile = apkFile;
9+
void setFakePath() {
10+
isFakePath = true;
1111
}
1212

13-
void setFakePath(boolean fakePath) {
14-
isFakePath = fakePath;
15-
}
1613
boolean isFakePath() {
1714
return isFakePath;
1815
}
@@ -21,4 +18,8 @@ File getApkFile() {
2118
return apkFile;
2219
}
2320

21+
void setApkFile(File apkFile) {
22+
this.apkFile = apkFile;
23+
}
24+
2425
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.modosa.apkinstaller;
2+
3+
import android.Manifest;
4+
import android.app.Activity;
5+
import android.content.ComponentName;
6+
import android.content.Intent;
7+
import android.content.pm.PackageManager;
8+
import android.os.Bundle;
9+
import android.support.annotation.Nullable;
10+
import android.support.v4.app.ActivityCompat;
11+
import android.support.v4.content.ContextCompat;
12+
import android.widget.Toast;
13+
14+
import com.catchingnow.icebox.sdk_client.IceBox;
15+
16+
import io.reactivex.disposables.Disposable;
17+
18+
public class InitializeActivity extends Activity {
19+
private Disposable mSubscribe;
20+
21+
@Override
22+
protected void onCreate(Bundle savedInstanceState) {
23+
// Log.e("----tagg----","onCreate");
24+
super.onCreate(savedInstanceState);
25+
26+
PackageManager pm = getPackageManager();
27+
ComponentName comptName = new ComponentName(this, MainActivity.class);
28+
boolean isenabled = (pm.getComponentEnabledSetting(comptName) == (PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) || pm.getComponentEnabledSetting(comptName) == (PackageManager.COMPONENT_ENABLED_STATE_ENABLED));
29+
if (!isenabled) {
30+
pm.getComponentEnabledSetting(comptName);
31+
pm.setComponentEnabledSetting(comptName,
32+
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
33+
PackageManager.DONT_KILL_APP);
34+
// Log.e("--ComponentEnabled--", "MainActivity组件已启用");
35+
// Log.e("--isEnabled--", pm.getComponentEnabledSetting(comptName) + "");
36+
}
37+
this.requestPermission();
38+
}
39+
40+
@Override
41+
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
42+
super.onActivityResult(requestCode, resultCode, data);
43+
updatePermissionState();
44+
}
45+
46+
@Override
47+
protected void onResume() {
48+
super.onResume();
49+
// Log.e("----tagg----","onResume");
50+
updatePermissionState();
51+
}
52+
53+
@Override
54+
protected void onDestroy() {
55+
// Log.e("----tagg----","onDestroy");
56+
super.onDestroy();
57+
disposeSafety();
58+
}
59+
60+
private void requestPermission() {
61+
ActivityCompat.requestPermissions(this,
62+
new String[]{IceBox.SDK_PERMISSION, Manifest.permission.WRITE_EXTERNAL_STORAGE},
63+
0x233);
64+
}
65+
66+
private void updatePermissionState() {
67+
IceBox.SilentInstallSupport state = IceBox.querySupportSilentInstall(this);
68+
// Log.e("----state----",state+"");
69+
int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
70+
// Log.e("permission==",permission == PackageManager.PERMISSION_GRANTED ? "PERMISSION_GRANTED" : "PERMISSION_DENIED");
71+
if ((state + "").contains("SUPPORTED") && (permission == 0)) {
72+
Toast.makeText(this, getString(R.string.hide_icon), Toast.LENGTH_LONG).show();
73+
PackageManager p = getPackageManager();
74+
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
75+
}
76+
finish();
77+
}
78+
79+
private void disposeSafety() {
80+
if (mSubscribe != null && !mSubscribe.isDisposed()) mSubscribe.dispose();
81+
mSubscribe = null;
82+
}
83+
84+
}

app/src/main/java/com/modosa/apkinstaller/MainActivity.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
import android.util.AndroidRuntimeException;
1111
import android.util.Log;
1212
import android.widget.Toast;
13+
14+
import com.catchingnow.icebox.sdk_client.IceBox;
15+
1316
import java.io.File;
1417
import java.io.FileOutputStream;
1518
import java.io.IOException;
1619
import java.io.InputStream;
1720
import java.io.OutputStream;
1821

19-
import com.catchingnow.icebox.sdk_client.IceBox;
20-
2122
import io.reactivex.Single;
2223
import io.reactivex.android.schedulers.AndroidSchedulers;
2324
import io.reactivex.disposables.Disposable;
@@ -33,7 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
3334
if (getIntent().getData() != null) {
3435
apkCommander = new APKCommander(this, getIntent().getData(), this);
3536
} else {
36-
showToast("读取 APK 失败");
37+
showToast(getString(R.string.failed_read));
3738
}
3839
}
3940

@@ -51,8 +52,8 @@ private void installApp(ApkInfo mapkinfo, String path) {
5152
.subscribeOn(Schedulers.io())
5253
.observeOn(AndroidSchedulers.mainThread())
5354
.subscribe(success -> {
54-
Toast.makeText(this, success ? "安装成功" : "安装失败", Toast.LENGTH_SHORT).show();
55-
if(mapkinfo.isFakePath())
55+
Toast.makeText(this, success ? getString(R.string.success_install) : getString(R.string.failed_install), Toast.LENGTH_SHORT).show();
56+
if (mapkinfo.isFakePath())
5657
deleteSingleFile(mapkinfo.getApkFile().getPath());
5758
}, Throwable::printStackTrace);
5859
}
@@ -75,6 +76,7 @@ private void deleteSingleFile(String filePath$Name) {
7576
private void showToast(final String text) {
7677
runOnUiThread(() -> Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show());
7778
}
79+
7880
private void disposeSafety() {
7981
if (mSubscribe != null && !mSubscribe.isDisposed()) mSubscribe.dispose();
8082
mSubscribe = null;
@@ -94,13 +96,14 @@ public void onApkParsed() {
9496
public void onApkPreInstall(ApkInfo apkInfo) {
9597
showToast(getString(R.string.start_install, apkInfo.getApkFile().getPath()));
9698
}
99+
97100
class APKCommander {
98101

99102
private final Context context;
100103
private final Uri uri;
101-
private ApkInfo mApkInfo;
102104
private final ICommanderCallback callback;
103105
private final Handler handler;
106+
private ApkInfo mApkInfo;
104107

105108
APKCommander(Context context, Uri uri, ICommanderCallback commanderCallback) {
106109
this.context = context;
@@ -120,7 +123,7 @@ public void run() {
120123
super.run();
121124
handler.post(() -> callback.onApkPreInstall(mApkInfo));
122125
new Thread(() -> {
123-
handler.post(() -> installApp(mApkInfo,mApkInfo.getApkFile().getPath()));
126+
handler.post(() -> installApp(mApkInfo, mApkInfo.getApkFile().getPath()));
124127
finish();
125128
}).start();
126129
}
@@ -134,11 +137,15 @@ public void run() {
134137
handler.post(callback::onStartParseApk);
135138
mApkInfo = new ApkInfo();
136139
String apkSourcePath = ContentUriUtils.getPath(context, uri);
140+
141+
// Log.e("uri",uri+"");
137142
if (apkSourcePath == null) {
138-
mApkInfo.setFakePath(true);
143+
mApkInfo.setFakePath();
139144
File tempFile = new File(context.getExternalCacheDir(), System.currentTimeMillis() + ".apk");
140145
try {
146+
// Log.e("TAG",context.getContentResolver()+"");
141147
InputStream is = context.getContentResolver().openInputStream(uri);
148+
// Log.e("TAG", is+"");
142149
if (is != null) {
143150
OutputStream fos = new FileOutputStream(tempFile);
144151
byte[] buf = new byte[4096 * 1024];
@@ -154,9 +161,13 @@ public void run() {
154161
e.printStackTrace();
155162
}
156163
mApkInfo.setApkFile(tempFile);
164+
// Log.e("tempFile",mApkInfo.getApkFile().getPath()+"");
157165
} else {
158166
mApkInfo.setApkFile(new File(apkSourcePath));
159167
}
168+
169+
// Log.e("mApkInfo",mApkInfo.getApkFile().getPath()+"");
170+
160171
handler.post(callback::onApkParsed);
161172
} catch (Exception e) {
162173
handler.post(callback::onApkParsed);
@@ -167,4 +178,4 @@ public void run() {
167178
}
168179
}
169180
}
170-
}
181+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="app_name">Install Apk With IceBox</string>
4+
<string name="parsing">解析中…</string>
5+
<string name="start_install">开始安装 %s</string>
6+
<string name="hide_icon">权限已授予,将隐藏桌面图标</string>
7+
<string name="failed_read">读取apk失败</string>
8+
<string name="success_install">安装成功</string>
9+
<string name="failed_install">安装失败</string>
10+
</resources>
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<string name="app_name">Install Apk With IceBox</string>
4-
<string name="parsing">解析中…</string>
5-
<string name="start_install">开始安装 %s</string>
4+
<string name="parsing">Parsing…</string>
5+
<string name="start_install">Start installing %s</string>
6+
<string name="hide_icon">Permission granted,Desktop icon will be hidden</string>
7+
<string name="failed_read">Reading apk failed</string>
8+
<string name="success_install">Successful</string>
9+
<string name="failed_install">Failed</string>
610
</resources>

0 commit comments

Comments
 (0)