From d2e09bb74651c124fbc3d4bba741a0a99f833831 Mon Sep 17 00:00:00 2001 From: ThirtyDegreesRay Date: Tue, 2 Jan 2018 15:27:02 +0800 Subject: [PATCH] use rx permissions --- app/build.gradle | 7 +---- .../openhub/http/Downloader.java | 31 ++++++++----------- gradle.properties | 3 +- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0f6e5dcd..59672b6f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -142,12 +142,7 @@ dependencies { compile "com.google.dagger:dagger:${DAGGER_VERSION}" annotationProcessor "com.google.dagger:dagger-compiler:${DAGGER_VERSION}" - //Android6.0以上权限获取工具 - //PermissionsDispatcher provides a simple annotation-based API to handle runtime permissions in Android Marshmallow. -// androidApt: 'com.neenbedankt.gradle.plugins:android-apt:1.8', -// compile "com.github.hotchemi:permissionsdispatcher:${PERMISSIONS_DISPATCHER_VERSION}" -// annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:${PERMISSIONS_DISPATCHER_VERSION}" - compile "com.yanzhenjie:permission:${AND_PERMISSION_VERSION}" + compile "com.tbruyelle.rxpermissions:rxpermissions:${RX_PERMISSION_VERSION}" compile "com.github.bumptech.glide:glide:${GLIDE_VERSION}" annotationProcessor "com.github.bumptech.glide:compiler:${GLIDE_VERSION}" diff --git a/app/src/main/java/com/thirtydegreesray/openhub/http/Downloader.java b/app/src/main/java/com/thirtydegreesray/openhub/http/Downloader.java index 6d1e228d..27c2a485 100644 --- a/app/src/main/java/com/thirtydegreesray/openhub/http/Downloader.java +++ b/app/src/main/java/com/thirtydegreesray/openhub/http/Downloader.java @@ -1,5 +1,6 @@ package com.thirtydegreesray.openhub.http; +import android.Manifest; import android.app.DownloadManager; import android.content.BroadcastReceiver; import android.content.Context; @@ -9,18 +10,14 @@ import android.database.sqlite.SQLiteException; import android.net.Uri; import android.os.Environment; -import android.support.annotation.NonNull; import android.widget.Toast; import com.orhanobut.logger.Logger; +import com.tbruyelle.rxpermissions.RxPermissions; import com.thirtydegreesray.openhub.R; +import com.thirtydegreesray.openhub.ui.activity.base.BaseActivity; import com.thirtydegreesray.openhub.util.AppUtils; import com.thirtydegreesray.openhub.util.StringUtils; -import com.yanzhenjie.permission.AndPermission; -import com.yanzhenjie.permission.Permission; -import com.yanzhenjie.permission.PermissionListener; - -import java.util.List; import es.dmoral.toasty.Toasty; @@ -62,22 +59,20 @@ public void start(String url, String fileName) { AppUtils.showDownloadServiceSetting(mContext); return ; } - - AndPermission.with(mContext) - .permission(Permission.STORAGE) - .callback(new PermissionListener() { - @Override - public void onSucceed(int requestCode, @NonNull List grantPermissions) { + if(BaseActivity.getCurActivity() == null){ + Toasty.error(mContext, mContext.getString(R.string.download_failed), Toast.LENGTH_SHORT).show(); + return; + } + new RxPermissions(BaseActivity.getCurActivity()) + .request(Manifest.permission.WRITE_EXTERNAL_STORAGE) + .subscribe(granted -> { + if (granted) { start(); - } - - @Override - public void onFailed(int requestCode, @NonNull List deniedPermissions) { + } else { Toasty.error(mContext, mContext.getString(R.string.permission_storage_denied), Toast.LENGTH_LONG).show(); } - }) - .start(); + }); }catch (Exception e){ Toasty.error(mContext, e.getMessage()).show(); } diff --git a/gradle.properties b/gradle.properties index 5c2353df..d3714088 100644 --- a/gradle.properties +++ b/gradle.properties @@ -37,8 +37,7 @@ FILE_DOWNLOADER_VERSION=0.3.1 RXJAVA_VERSION=1.1.0 RETROFIT_VERSION=2.1.0 DAGGER_VERSION=2.11 -#PERMISSIONS_DISPATCHER_VERSION=2.1.3 -AND_PERMISSION_VERSION=1.1.0 +RX_PERMISSION_VERSION=0.9.4@aar RETROLAMBDA_VERSION=2.3.0 OKHTTP_VERSION=3.6.0 FASTJSON_VERSION=1.1.46.android