Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Context should be updated when we create Dexter instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
handrenliang committed Jan 20, 2017
1 parent a2f194d commit 04cf8aa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion dexter/src/main/java/com/karumi/dexter/Dexter.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ private static void initialize(Context context) {
if (instance == null) {
AndroidPermissionService androidPermissionService = new AndroidPermissionService();
IntentProvider intentProvider = new IntentProvider();
instance = new DexterInstance(context, androidPermissionService, intentProvider);
instance = new DexterInstance(androidPermissionService, intentProvider);
}
instance.setContext(context);
}

/**
Expand Down
9 changes: 6 additions & 3 deletions dexter/src/main/java/com/karumi/dexter/DexterInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class DexterInstance {
private static final MultiplePermissionsListener EMPTY_LISTENER =
new EmptyMultiplePermissionsListener();

private final WeakReference<Context> context;
private WeakReference<Context> context;
private final AndroidPermissionService androidPermissionService;
private final IntentProvider intentProvider;
private final Collection<String> pendingPermissions;
Expand All @@ -58,9 +58,8 @@ final class DexterInstance {
private Activity activity;
private MultiplePermissionsListener listener = EMPTY_LISTENER;

DexterInstance(Context context, AndroidPermissionService androidPermissionService,
DexterInstance(AndroidPermissionService androidPermissionService,
IntentProvider intentProvider) {
this.context = new WeakReference<>(context);
this.androidPermissionService = androidPermissionService;
this.intentProvider = intentProvider;
this.pendingPermissions = new TreeSet<>();
Expand All @@ -70,6 +69,10 @@ final class DexterInstance {
this.isShowingNativeDialog = new AtomicBoolean();
}

void setContext(Context context) {
this.context = new WeakReference<>(context);
}

/**
* Checks the state of a specific permission reporting it when ready to the listener.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import com.karumi.dexter.RetryCheckPermissionOnDeniedPermissionListener.CheckPermissionAction;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import com.karumi.dexter.listener.single.PermissionListener;
import java.util.Collections;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Collections;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
Expand Down Expand Up @@ -63,7 +66,8 @@
Context mockApplicationContext = mock(Context.class);
when(context.getApplicationContext()).thenReturn(mockApplicationContext);
asyncExecutor = new AsyncExecutor();
dexter = new DexterInstance(context, androidPermissionService, intentProvider);
dexter = new DexterInstance(androidPermissionService, intentProvider);
dexter.setContext(context);
}

@Test(expected = DexterException.class) public void onNoPermissionCheckedThenThrowException() {
Expand Down

0 comments on commit 04cf8aa

Please sign in to comment.