Skip to content

Commit

Permalink
Sample app use last refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Mar 18, 2016
1 parent 740578c commit d4ac138
Showing 1 changed file with 21 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,69 +13,43 @@

import java.io.IOException;

import rx.Observable;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "RxPermissionsSample";

private RxPermissions mRxPermissions;
private Camera mCamera;
private SurfaceView mSurfaceView;

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

mRxPermissions = RxPermissions.getInstance(this);
mRxPermissions.setLogging(true);
RxPermissions.getInstance(this).setLogging(true);

setContentView(R.layout.act_main);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);

Observable.just(null)
RxView.clicks(findViewById(R.id.enableCamera))
.compose(RxPermissions.ensure(this, Manifest.permission.CAMERA))
.subscribe(granted -> {
Log.i(TAG, "Received result " + granted);
if (granted) {
Toast.makeText(MainActivity.this,
"Permission granted, enable the camera",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"Permission denied, can't enable the camera",
Toast.LENGTH_SHORT).show();
}
});

// If the permission request is triggered by a particular event
// (a click for instance) we need to pass that information threw
// an Observable.
// com.jakewharton.rxbinding.view.RxView does exactly what we need.
Observable<Object> trigger = RxView.clicks(findViewById(R.id.enableCamera));

// trigger
// .compose(RxPermissions.ensure(this, Manifest.permission.CAMERA))
// .subscribe(granted -> {
// Log.i(TAG, " TRIGGER Received result " + granted);
// if (granted) {
// releaseCamera();
// mCamera = Camera.open(0);
// try {
// mCamera.setPreviewDisplay(mSurfaceView.getHolder());
// mCamera.startPreview();
// } catch (IOException e) {
// Log.e(TAG, "Error while trying to display the camera preview", e);
// }
// } else {
// Toast.makeText(MainActivity.this,
// "Permission denied, can't enable the camera",
// Toast.LENGTH_SHORT).show();
// }
// },
// t -> Log.e(TAG, "onError", t),
// () -> Log.i(TAG, "OnComplete")
// );
Log.i(TAG, " TRIGGER Received result " + granted);
if (granted) {
releaseCamera();
mCamera = Camera.open(0);
try {
mCamera.setPreviewDisplay(mSurfaceView.getHolder());
mCamera.startPreview();
} catch (IOException e) {
Log.e(TAG, "Error while trying to display the camera preview", e);
}
} else {
Toast.makeText(MainActivity.this,
"Permission denied, can't enable the camera",
Toast.LENGTH_SHORT).show();
}
},
t -> Log.e(TAG, "onError", t),
() -> Log.i(TAG, "OnComplete")
);
}

@Override
Expand Down

0 comments on commit d4ac138

Please sign in to comment.