tbruyelle / RxPermissions

Android runtime permissions powered by RxJava2
Apache License 2.0
10.48k stars 1.31k forks source link

Not support FlutterActivity #276

Open Gennki opened 5 years ago

Gennki commented 5 years ago

My MainActivity extends the FlutterActivity, When I try to write val rxPermissions = RxPermissions(this) in my MainActivity, I find that the type of the parameter needs FragmentActivity, so I modified the code of FlutterActivityand let FlutterActivityextends the AppCompatActivity, but I found that subscribe does not work.


Here is my FlutterActivity code:

the FlutterActivity in flutter sdk extends Activity.

I just change Activity to AppCompatActivity.

package com.fandine.fandineflutter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import io.flutter.app.FlutterActivityDelegate;
import io.flutter.app.FlutterActivityDelegate.ViewFactory;
import io.flutter.app.FlutterActivityEvents;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.view.FlutterNativeView;
import io.flutter.view.FlutterView;
import io.flutter.view.FlutterView.Provider;

public class FlutterActivity extends AppCompatActivity implements Provider, PluginRegistry, ViewFactory {
    private final FlutterActivityDelegate delegate = new FlutterActivityDelegate(this, this);
    private final FlutterActivityEvents eventDelegate;
    private final Provider viewProvider;
    private final PluginRegistry pluginRegistry;

    public FlutterActivity() {
        this.eventDelegate = this.delegate;
        this.viewProvider = this.delegate;
        this.pluginRegistry = this.delegate;
    }

    public FlutterView getFlutterView() {
        return this.viewProvider.getFlutterView();
    }

    public FlutterView createFlutterView(Context context) {
        return null;
    }

    public FlutterNativeView createFlutterNativeView() {
        return null;
    }

    public boolean retainFlutterNativeView() {
        return false;
    }

    public final boolean hasPlugin(String key) {
        return this.pluginRegistry.hasPlugin(key);
    }

    public final <T> T valuePublishedByPlugin(String pluginKey) {
        return this.pluginRegistry.valuePublishedByPlugin(pluginKey);
    }

    public final Registrar registrarFor(String pluginKey) {
        return this.pluginRegistry.registrarFor(pluginKey);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.eventDelegate.onCreate(savedInstanceState);
    }

    protected void onStart() {
        super.onStart();
        this.eventDelegate.onStart();
    }

    protected void onResume() {
        super.onResume();
        this.eventDelegate.onResume();
    }

    protected void onDestroy() {
        this.eventDelegate.onDestroy();
        super.onDestroy();
    }

    public void onBackPressed() {
        if (!this.eventDelegate.onBackPressed()) {
            super.onBackPressed();
        }

    }

    protected void onStop() {
        this.eventDelegate.onStop();
        super.onStop();
    }

    protected void onPause() {
        super.onPause();
        this.eventDelegate.onPause();
    }

    protected void onPostResume() {
        super.onPostResume();
        this.eventDelegate.onPostResume();
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        this.eventDelegate.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (!this.eventDelegate.onActivityResult(requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        }

    }

    protected void onNewIntent(Intent intent) {
        this.eventDelegate.onNewIntent(intent);
    }

    public void onUserLeaveHint() {
        this.eventDelegate.onUserLeaveHint();
    }

    public void onTrimMemory(int level) {
        this.eventDelegate.onTrimMemory(level);
    }

    public void onLowMemory() {
        this.eventDelegate.onLowMemory();
    }

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        this.eventDelegate.onConfigurationChanged(newConfig);
    }
}

And here is my MainActicity code:

package com.fandine.fandineflutter

import android.Manifest
import android.os.Bundle
import android.util.Log
import com.tbruyelle.rxpermissions2.RxPermissions
import io.reactivex.disposables.CompositeDisposable

class MainActivity : FlutterActivity() {

    private val disposable = CompositeDisposable()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val rxPermissions = RxPermissions(this)
        disposable.add(rxPermissions
            .request(Manifest.permission.ACCESS_COARSE_LOCATION)
            .subscribe { granted ->
                Log.e("test", granted.toString())
            })
    }

    override fun onDestroy() {
        super.onDestroy()
        disposable.dispose()
    }
}
330676687 commented 5 years ago

upupup

mrArtCore commented 5 years ago

@330676687 Did you try to test without adding to disposables ?

Gennki commented 5 years ago

Yes, I tried it, but none of them worked. @mrArtCore

mrArtCore commented 5 years ago

This is weird. I suppose that something strange with Your FlatterActivity and eventDelegates. (I'm not familiar with Flatter) I would suggest you to log your onActivityResult() and onRequestPermissionResult() methods. Are they being invoked inside FlatterActivity ??? Also add logging to RxPermission chain doOnSubscribe() doFinally() etc. To be sure that subscription is really happening.

mrArtCore commented 5 years ago

@Gennki @330676687 any updates about issue ?

recvec commented 3 years ago

as an alternative, I'm using the FlutterFragmentActivity