Skip to content

Instantly share code, notes, and snippets.

@andaag
Last active October 14, 2015 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andaag/cb3e3ded2d8d21f02388 to your computer and use it in GitHub Desktop.
Save andaag/cb3e3ded2d8d21f02388 to your computer and use it in GitHub Desktop.
RxPermissions.getInstance(this, new HandleOutOfActivityObject(viewIdOfResultView))
.request(Manifest.permission.CAMERA)
.subscribe(granted -> {
if (granted) resultView.permissionGranted();
}
resultView {
public void permissionGranted();
}
// This object you'd want to save to state if the request fails and you need to leave the activity:
class HandleOutOfActivityObject {
int viewId;
public void resolve() {
findViewById(viewId).permissionGranted();
}
}
public class RxActivityDelegate {
private static final String KEY_OBJECT = "rxActivityController.object";
private static final String KEY_OBJECTTYPE = "rxActivityController.object.type";
private static final String KEY_REQUESTCODE = "rxActivityController.requestCode";
public static void setResponse(Activity activity, int requestCode, RxResponseHandler rxResponseHandler) {
Intent intent = activity.getIntent();
intent.putExtra(KEY_OBJECT, JsonUtils.toBytes(rxResponseHandler));
intent.putExtra(KEY_OBJECTTYPE, rxResponseHandler.getClass().getName());
intent.putExtra(KEY_REQUESTCODE, requestCode);
}
public static void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (activity.getIntent().hasExtra(KEY_REQUESTCODE)) {
int ourRequestCode = activity.getIntent().getIntExtra(KEY_REQUESTCODE, Integer.MIN_VALUE);
if (ourRequestCode == requestCode) {
try {
Class<Object> objectType = (Class<Object>) Class.forName(activity.getIntent().getStringExtra(KEY_OBJECTTYPE));
RxResponseHandler responseHandler = JsonUtils.getSmileReader(objectType).readValue(activity.getIntent().getByteArrayExtra(KEY_OBJECT));
responseHandler.onActivityResult(activity, requestCode, resultCode, data);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new IllegalStateException(e);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new IllegalStateException(e);
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException(e);
}
}
}
}
public interface RxResponseHandler {
void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment