From 59a767fcc65f960216cf17ea5c997d51291b5521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 28 Feb 2020 12:00:25 +0800 Subject: [PATCH] remove rxpermissions with context compat instead --- doric-android/devkit/build.gradle | 1 - .../pub/doric/devkit/ui/DoricDevActivity.java | 44 ++++++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/doric-android/devkit/build.gradle b/doric-android/devkit/build.gradle index c610fbd3..aac9151c 100644 --- a/doric-android/devkit/build.gradle +++ b/doric-android/devkit/build.gradle @@ -33,7 +33,6 @@ dependencies { implementation 'com.squareup.okhttp3:okhttp:4.2.2' implementation 'com.google.code.gson:gson:2.8.6' implementation 'cn.bingoogolapple:bga-qrcode-zbar:1.3.7' - implementation 'com.github.tbruyelle:rxpermissions:0.10.2' implementation "io.reactivex.rxjava2:rxjava:2.2.15" api 'org.greenrobot:eventbus:3.1.1' implementation 'com.lahm.library:easy-protector-release:1.1.1' diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java index 8a41d6a1..d5e4fc2f 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java @@ -2,24 +2,25 @@ package pub.doric.devkit.ui; import android.Manifest; import android.content.Intent; +import android.content.pm.PackageManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; import com.google.gson.JsonObject; -import com.tbruyelle.rxpermissions2.RxPermissions; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; -import io.reactivex.disposables.Disposable; -import io.reactivex.functions.Consumer; import pub.doric.DoricContext; import pub.doric.DoricContextManager; import pub.doric.devkit.DevKit; @@ -42,22 +43,33 @@ public class DoricDevActivity extends AppCompatActivity { setContentView(R.layout.layout_debug_context); initViews(); } else { - if (DevKit.isRunningInEmulator) { + if (!DevKit.isRunningInEmulator) { DevKit.ip = "10.0.2.2"; DevKit.getInstance().connectDevKit("ws://" + DevKit.ip + ":7777"); } else { - final RxPermissions rxPermissions = new RxPermissions(this); - Disposable disposable = rxPermissions - .request(Manifest.permission.CAMERA) - .subscribe(new Consumer() { - @Override - public void accept(Boolean grant) throws Exception { - if (grant) { - Intent intent = new Intent(DoricDevActivity.this, ScanQRCodeActivity.class); - startActivity(intent); - } - } - }); + if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) + != PackageManager.PERMISSION_GRANTED) { + if (ActivityCompat.shouldShowRequestPermissionRationale(this, + Manifest.permission.CAMERA)) { + } else { + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.CAMERA,}, 1); + } + } + } + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + if (requestCode == 1) { + for (int i = 0; i < permissions.length; i++) { + if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { + Intent intent = new Intent(DoricDevActivity.this, ScanQRCodeActivity.class); + startActivity(intent); + } } } }