Skip to content
Permalink
Browse files

Add Feature: silent uninstall

  • Loading branch information
heruoxin committed Nov 7, 2018
1 parent 6c46833 commit 5942bc32663ce50816f2640887da5f90c1d69346
@@ -6,7 +6,7 @@

## 使用方法

#### 依赖
### 依赖

添加依赖,在 app 下的 build.gradle 文件中:

@@ -20,21 +20,23 @@ repositories {
dependencies {
......
......
implementation 'com.catchingnow.icebox:SDK:1.0.2'
implementation 'com.catchingnow.icebox:SDK:1.0.5'
}
```

#### 请求权限
### 请求权限

在 Android 6.0+ 设备上,冰箱 SDK 的部分接口需要先请求权限再使用
在 Android 6.0+ 设备上,冰箱 SDK 的部分接口(冻结/安装 APK)需要先请求权限再使用

```java
if (ContextCompat.checkSelfPermission(this, IceBox.PERMISSION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{IceBox.PERMISSION}, 0x233);
}
```

#### 调用接口
### 调用接口

#### 冻结解冻相关接口

##### 查询工作模式

@@ -62,4 +64,44 @@ IceBox.getAppEnabledSetting(applicationInfo);
IceBox.setAppEnabledSettings(getContext(), enable, PACKAGE_NAME...);
```

#### 静默安装卸载相关接口

当冰箱运行在设备管理员(俗称免 Root)模式下时,可对外提供静默安装和卸载 APK 的接口。

##### 查询是否支持

```java
IceBox.querySupportSilentInstall(context);
```

返回为一个枚举类型

```java
public enum SilentInstallSupport {
SUPPORTED,
NOT_INSTALLED, //未安装冰箱 IceBox;
UPDATE_REQUIRED, //冰箱 IceBox 版本过低;
SYSTEM_NOT_SUPPORTED, //当前系统版本过低,不支持静默安装;
NOT_DEVICE_OWNER, //冰箱 IceBox 不是设备管理员;
PERMISSION_REQUIRED, //当前 App 未取得权限,请发起标准的 Android 权限请求;
}
```

##### 安装/卸载 APK

在冰箱支持的前提下,可以调用实现静默安装卸载,无需用户确认。方法均为同步,直到安装完成或失败后才会返回。

安装成功后通知栏会由系统发送通知提示,同时冰箱的 SDK 日志页面中也会留存记录。

```java
// 安装
IceBox.installPackage(context, uriToApk);
// 卸载
IceBox.uninstallPackage(context, packageName);
```


更详细的代码示例可见 demo app。
@@ -33,7 +33,9 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mBinding.setPath("/sdcard/1.apk");
mBinding.btnInstall.setOnClickListener(v -> installApk(mBinding.getPath()));
mBinding.setPackagename("com.some.app");
mBinding.btnInstall.setOnClickListener(v -> installApp(mBinding.getPath()));
mBinding.btnUninstall.setOnClickListener(v -> uninstallApp(mBinding.getPackagename()));
mBinding.btnRequest.setOnClickListener(v -> requestPermission());
}

@@ -52,7 +54,7 @@ protected void onResume() {
@Override
protected void onDestroy() {
super.onDestroy();
if (mSubscribe != null && !mSubscribe.isDisposed()) mSubscribe.dispose();
disposeSafety();
}

private void requestPermission() {
@@ -62,16 +64,17 @@ private void requestPermission() {
}

private void updatePermissionState() {
IceBox.SilentInstallState state = IceBox.querySupportSilentInstall(this);
IceBox.SilentInstallSupport state = IceBox.querySupportSilentInstall(this);
mBinding.setIceboxState(state.toString());

int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
mBinding.setSdcardState(permission == PackageManager.PERMISSION_GRANTED ? "PERMISSION_GRANTED" : "PERMISSION_DENIED");
}

private void installApk(String path) {
private void installApp(String path) {
String authority = getPackageName() + ".FILE_PROVIDER";
Uri uri = FileProvider.getUriForFile(this, authority, new File(path));
disposeSafety();
mSubscribe = Single.fromCallable(() -> IceBox.installPackage(this, uri))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
@@ -80,4 +83,19 @@ private void installApk(String path) {
}, Throwable::printStackTrace);
}

private void uninstallApp(String packageName) {
disposeSafety();
mSubscribe = Single.fromCallable(() -> IceBox.uninstallPackage(this, packageName))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(success -> {
Toast.makeText(this, packageName + (success ? " 卸载成功" : " 卸载失败"), Toast.LENGTH_SHORT).show();
}, Throwable::printStackTrace);
}

private void disposeSafety() {
if (mSubscribe != null && !mSubscribe.isDisposed()) mSubscribe.dispose();
mSubscribe = null;
}

}
@@ -4,15 +4,19 @@
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="path"
type="String"/>

<variable
name="icebox_state"
type="String"/>
<variable
name="sdcard_state"
type="String"/>
<variable
name="path"
type="String"/>
<variable
name="packagename"
type="String"/>

</data>

@@ -27,8 +31,9 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{@string/permission_state(icebox_state, sdcard_state)}"
android:layout_marginBottom="32dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/input"
app:layout_constraintBottom_toTopOf="@id/input_path"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn_request"
app:layout_constraintHorizontal_chainStyle="spread"
@@ -47,7 +52,7 @@
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/input"
android:id="@+id/input_path"
tools:text="/sdcard/1.apk"
android:text="@={path}"
android:maxLines="3"
@@ -66,11 +71,33 @@
android:text="Install"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/input"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@id/input_path"
app:layout_constraintBottom_toTopOf="@id/input_pn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/input_pn"
tools:text="com.some.app"
android:text="@={packagename}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_install"
app:layout_constraintBottom_toTopOf="@id/btn_uninstall"
android:layout_marginTop="72dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>

<Button
android:id="@+id/btn_uninstall"
android:text="@string/btn_uninstall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/input_pn"/>

</android.support.constraint.ConstraintLayout>
</layout>
@@ -2,4 +2,5 @@
<string name="app_name">Install app</string>
<string name="request_permission">Request</string>
<string name="permission_state">IceBox SDK:\n%1$s\n\nWRITE_EXTERNAL_STORAGE:\n%2$s</string>
<string name="btn_uninstall">Uninstall</string>
</resources>
@@ -7,11 +7,10 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 28
versionCode 3
versionName "1.0.3"
versionCode 5
versionName "1.0.5"

}

buildTypes {
release {
minifyEnabled false
@@ -24,9 +23,9 @@ android {
}
}

version = "1.0.3"
version = "1.0.5"
group = 'com.catchingnow.icebox'
androidArtifact { // 2
androidArtifact {
artifactId = "SDK"
}
publish {
@@ -4,6 +4,8 @@
import android.content.Context;
import android.content.Intent;

import com.catchingnow.icebox.sdk_client.StateReceiver;

class AuthorizeUtil {

private static PendingIntent authorizePendingIntent = null;

0 comments on commit 5942bc3

Please sign in to comment.
You can’t perform that action at this time.