Skip to content
Permalink
Browse files

Add silent install demo

  • Loading branch information
heruoxin committed Nov 7, 2018
1 parent d79a2d9 commit 6c46833b82dce0058f23e9dd40715f3cf7ce7eef
@@ -8,8 +8,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.novoda:bintray-release:0.5.0'

classpath "guru.stefma.bintrayrelease:bintrayrelease:1.1.1"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
compileSdkVersion 28
defaultConfig {
applicationId "com.catchingnow.iceboxsdk"
minSdkVersion 17
targetSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -29,7 +29,8 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(":sdk-client")

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'

api 'io.reactivex.rxjava2:rxjava:2.1.7'

This file was deleted.

@@ -1,4 +1,4 @@
#Mon Nov 05 23:52:18 CST 2018
#Wed Nov 07 12:16:55 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
@@ -38,6 +38,7 @@ dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'io.reactivex.rxjava2:rxjava:2.1.7'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.android.support:support-annotations:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
@@ -1,8 +1,14 @@
package com.catchingnow.installapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
@@ -28,23 +34,50 @@ protected void onCreate(Bundle savedInstanceState) {
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mBinding.setPath("/sdcard/1.apk");
mBinding.btnInstall.setOnClickListener(v -> installApk(mBinding.getPath()));
mBinding.btnRequest.setOnClickListener(v -> requestPermission());
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
updatePermissionState();
}

@Override
protected void onResume() {
super.onResume();
updatePermissionState();
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mSubscribe != null && !mSubscribe.isDisposed()) mSubscribe.dispose();
}

private void requestPermission() {
ActivityCompat.requestPermissions(this,
new String[]{IceBox.SDK_PERMISSION, Manifest.permission.WRITE_EXTERNAL_STORAGE},
0x233);
}

private void updatePermissionState() {
IceBox.SilentInstallState 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) {
String authority = getPackageName() + ".FILE_PROVIDER";
Uri uri = FileProvider.getUriForFile(this, authority, new File(path));
mSubscribe = Single.fromCallable(() -> IceBox.silentInstallPackage(this, uri))
mSubscribe = Single.fromCallable(() -> IceBox.installPackage(this, uri))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(success -> {
Toast.makeText(this, success ? "安装成功" : "安装失败", Toast.LENGTH_SHORT).show();
}, Throwable::printStackTrace);
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mSubscribe != null && !mSubscribe.isDisposed()) mSubscribe.dispose();
}
}
@@ -7,24 +7,55 @@
<variable
name="path"
type="String"/>
<variable
name="icebox_state"
type="String"/>
<variable
name="sdcard_state"
type="String"/>

</data>

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{@string/permission_state(icebox_state, sdcard_state)}"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/input"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn_request"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintVertical_bias="0.3"
app:layout_constraintVertical_chainStyle="packed"
tools:text="PERMISSION_GRANTED"/>

<Button
android:id="@+id/btn_request"
android:text="@string/request_permission"
app:layout_constraintTop_toTopOf="@id/state"
app:layout_constraintBottom_toBottomOf="@id/state"
app:layout_constraintLeft_toRightOf="@id/state"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/input"
tools:text="/sdcard/1.apk"
android:text="@={path}"
android:maxLines="3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@id/state"
app:layout_constraintBottom_toTopOf="@id/btn_install"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintVertical_bias="0.4"
android:layout_marginTop="32dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
@@ -1,3 +1,5 @@
<resources>
<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>
</resources>

0 comments on commit 6c46833

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