Skip to content

Commit

Permalink
Merge pull request #136 from journeyapps/fix/torch-control
Browse files Browse the repository at this point in the history
Save torch state
  • Loading branch information
rkistner committed Feb 6, 2016
2 parents d0876fb + ed3eddd commit a7ee170
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sample/src/main/java/example/zxing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void scanToolbar(View view) {
}

public void scanCustomScanner(View view) {
new IntentIntegrator(this).setCaptureActivity(CustomScannerActivity.class).initiateScan();
new IntentIntegrator(this).setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class).initiateScan();
}

public void scanMarginScanner(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
Expand Down Expand Up @@ -122,6 +124,8 @@ public interface StateListener {
// must be smaller than 0.5;
private double marginFraction = 0.1d;

private boolean torchOn = false;

@TargetApi(14)
private TextureView.SurfaceTextureListener surfaceTextureListener() {
// Cannot initialize automatically, since we may be API < 14
Expand Down Expand Up @@ -363,6 +367,7 @@ private void calculateFrames() {
* @param on true to turn on the torch
*/
public void setTorch(boolean on) {
torchOn = on;
if (cameraInstance != null) {
cameraInstance.setTorch(on);
}
Expand All @@ -375,6 +380,9 @@ private void containerSized(Size containerSize) {
displayConfiguration = new DisplayConfiguration(getDisplayRotation(), containerSize);
cameraInstance.setDisplayConfiguration(displayConfiguration);
cameraInstance.configureCamera();
if(torchOn) {
cameraInstance.setTorch(torchOn);
}
}
}
}
Expand Down Expand Up @@ -718,4 +726,27 @@ protected Rect calculateFramingRect(Rect container, Rect surface) {
}
return intersection;
}

@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();

Bundle myState = new Bundle();
myState.putParcelable("super", superState);
myState.putBoolean("torch", torchOn);
return myState;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
if(!(state instanceof Bundle)) {
super.onRestoreInstanceState(state);
return;
}
Bundle myState = (Bundle)state;
Parcelable superState = myState.getParcelable("super");
super.onRestoreInstanceState(superState);
boolean torch = myState.getBoolean("torch");
setTorch(torch);
}
}

0 comments on commit a7ee170

Please sign in to comment.