android – What's the best way to check for permissions at runtime using MVP architecture?-ThrowExceptions

Exception or error:

I’m developing an android app in which I have to ask for permissions at runtime. I’m wondering about the best way to implement that using Model-View-Presenter architecture.

My initial thought was to have the presenter call a component responsible for permissions(say a PermissionHandler), and update view accordingly.

The issue is that the code to check for permissions is tightly coupled with the Activity class. Here are some of the methods involved that require an Activity or Context:

  • ContextCompat.checkSelfPermission()
  • ActivityCompat.shouldShowRequestPermissionRationale()
  • ActivityCompat.requestPermissions()
  • onRequestPermissionsResult()(callback)

This means I would have to pass an activity object to the presenter, which I didn’t like much because I’ve heard that keeping your presenter free from Android code is good for testing.

Due to that, I then thought about handling permissions at view level(in an activity), but then I guess this would hurt the purpose of leaving the view responsible only for UI updates, without business logic.

I’m not sure what would be the best approach to tackle that keeping the code as decoupled and maintainable as possible. Any ideas?

How to solve:

What I would do is:

The view will implement:

public Activity getViewActivity();

The presenter will implement:

public void requestPermissions();
public void onPermissionsResult();

Inside requestPermissions, the presenter will do: getViewActivity().checkSelfPermission; getViewActivity.requestPermissions(); etc.

The view will call inside the onRequestPermissionsResult callback to presenter.onPermissionsResult();

With this all the logic will be implemented inside the presenter.

In my opinion, your presenter is decoupled: it won’t depend on any view implementation (it will only depend on the view interface).

“I’ve heard that keeping your presenter free from Android code is good for testing.” I don’t understand this part. If the code is good, it can be tested without any problem.

###

If you still want to be able to mock permission access/requests, you still create something like a PermissionHandler, but only reference it inside your view class. For example –

Interface:

public interface PermissionsHandler {
    boolean checkHasPermission(AppCompatActivity activity, String permission);
    void requestPermission(AppCompatActivity activity, String[] permissions, int requestCode);
}

Production implementation:

public class PermissionsHandlerAndroid implements PermissionsHandler {
    @Override
    public boolean checkHasPermission(AppCompatActivity activity, String permission) {
        return ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
    }

    @Override
    public void requestPermission(AppCompatActivity activity, String[] permissions, int requestCode){
        ActivityCompat.requestPermissions(activity, permissions, requestCode);
    }
}

Mocked class (for example, to test and make sure your activity correctly handles onRequestPermissionsResult)

public class PermissionsHandlerMocked implements PermissionsHandler {
    @Override
    public boolean checkHasPermission(AppCompatActivity activity, String permission) {
        return false;
    }

    @Override
    public void requestPermission(AppCompatActivity activity, String[] permissions, int requestCode){
        int[] grantResults = new int[permissions.length];
        for (int i = 0; i < permissions.length; i++) {
            grantResults[i] = PackageManager.PERMISSION_GRANTED
        }
        activity.onRequestPermissionResult(requestCode, permissions, grantResults);
    }
}

Then in your activity:

PermissionsHandler permissionsHandler;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    permissionsHandler = Injection.providePermissionsHandler();
    //or however you choose to inject your production vs mocked handler. 
}

//method from your view interface, to be called by your presenter
@Override
void requestLocationPermission() {
    permissionsHandler.requestPermision((AppCompatActivity) this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE_LOCATION};
}

fobo66, you can always make the view implement much more generic methods like
checkLocationPermissionGranted() and requestLocationPermission(). Then your view implementation can reference the activity as needed, and your presenter never has to touch the activity reference.

###

Permissions requests and status are View(Fragment or Activity) responsibility due to depend of the user actions to make a request or grant permissions. I manage permissions with MVP as follows(Read external storage example):

My Contract

interface View {
    ...
    void requestReadPermission();
    boolean areReadPermissionGranted();
    void showPermissionError();
    void hidePermissionError();
    ...
}

interface Presenter {
    ...
    void setReadPermissions(boolean grantedPermissions);
    ...
}

interface Model {
    ...
}

My view implementation. (Fragment in this case but it can be Activity or whatever, the Presenter will only expect the response).

public class MyView extends Fragment implements Contract.View {

...
Contract.Presenter presenter;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    boolean grantedPermissions =  (grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED);
    presenter.setReadPermissions(grantedPermissions);
}



@Override
public void showPermissionError() {
    // Show not permission message
}

@Override
public void hidePermissionError() {
    // Hide not permission message
}

@Override
public void requestReadPermission() {
    this.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
@Override
public boolean areReadPermissionGranted() {
    return ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
...

And the Presenter implementation

public class MyPresenter implements Contract.Presenter {

...
Contract.View view;

public void doSomethingThatRequiresPermissions() {

    ...
        if ( !view.areReadPermissionGranted() ) {
            view.requestReadPermission();
            view.showPermissionError();
        } else {
            view.hidePermissionError();
            doSomethingWithPermissionsGranted();
        }
    ...
}

@Override
public void setReadPermissions(boolean grantedPermissions) {
    if( grantedPermissions ){
        view.hidePermissionError();
        doSomethingThatRequiresPermissions();

    } else {
        view.showPermissionError();
    }
}

public void doSomethingWithPermissionsGranted(){
    ...
}

Then you can make unit test like

Contract.View mockedView;
@Test
public void requestAlbumListWithoutPermissions() {
    when(mockedView.areReadPermissionGranted()).thenReturn(false);
    presenter.doSomethingWithPermissionsGranted();
    verify(mockedView).showPermissionError();
    verify(mockedView).requestReadPermission();
}

Leave a Reply

Your email address will not be published. Required fields are marked *