display the flash icon only when its really available
This commit is contained in:
parent
faa72cf25c
commit
dff62f66d9
|
@ -19,11 +19,13 @@ import android.widget.ImageView;
|
|||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.simplemobiletools.camera.Preview.PreviewListener;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements SensorEventListener {
|
||||
public class MainActivity extends AppCompatActivity implements SensorEventListener, PreviewListener {
|
||||
@BindView(R.id.viewHolder) RelativeLayout viewHolder;
|
||||
@BindView(R.id.toggle_camera) ImageView toggleCameraBtn;
|
||||
@BindView(R.id.toggle_flash) ImageView toggleFlashBtn;
|
||||
|
@ -49,7 +51,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||
setContentView(R.layout.activity_main);
|
||||
ButterKnife.bind(this);
|
||||
currCamera = Camera.CameraInfo.CAMERA_FACING_BACK;
|
||||
preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView));
|
||||
preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView), this);
|
||||
preview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||
viewHolder.addView(preview);
|
||||
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
|
||||
|
@ -77,9 +79,8 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||
public void toggleFlash() {
|
||||
if (isFlashEnabled) {
|
||||
disableFlash();
|
||||
} else if (preview.enableFlash()) {
|
||||
isFlashEnabled = preview.enableFlash();
|
||||
toggleFlashBtn.setImageResource(R.mipmap.flash_on);
|
||||
} else {
|
||||
enableFlash();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -89,6 +90,12 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||
toggleFlashBtn.setImageResource(R.mipmap.flash_off);
|
||||
}
|
||||
|
||||
private void enableFlash() {
|
||||
preview.enableFlash();
|
||||
isFlashEnabled = true;
|
||||
toggleFlashBtn.setImageResource(R.mipmap.flash_on);
|
||||
}
|
||||
|
||||
@OnClick(R.id.shutter)
|
||||
public void shutterPressed() {
|
||||
if (isPhoto) {
|
||||
|
@ -215,4 +222,14 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFlashAvailable(boolean available) {
|
||||
if (available) {
|
||||
toggleFlashBtn.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
toggleFlashBtn.setVisibility(View.INVISIBLE);
|
||||
disableFlash();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
private static int currCameraId;
|
||||
private static boolean isFlashEnabled;
|
||||
private static Camera.Parameters parameters;
|
||||
|
||||
private static PreviewListener callback;
|
||||
private static MediaRecorder recorder;
|
||||
private static boolean isRecording;
|
||||
private static boolean isVideoMode;
|
||||
|
@ -45,10 +45,11 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
super(context);
|
||||
}
|
||||
|
||||
public Preview(Activity act, SurfaceView sv) {
|
||||
public Preview(Activity act, SurfaceView sv, PreviewListener cb) {
|
||||
super(act);
|
||||
|
||||
activity = act;
|
||||
callback = cb;
|
||||
surfaceView = sv;
|
||||
surfaceHolder = surfaceView.getHolder();
|
||||
surfaceHolder.addCallback(this);
|
||||
|
@ -95,6 +96,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
}
|
||||
setupPreview();
|
||||
}
|
||||
|
||||
callback.setFlashAvailable(Utils.hasFlash(camera));
|
||||
}
|
||||
|
||||
if (isVideoMode)
|
||||
|
@ -321,10 +324,6 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
}
|
||||
|
||||
public boolean enableFlash() {
|
||||
if (!Utils.hasFlash(camera)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
isFlashEnabled = true;
|
||||
return true;
|
||||
}
|
||||
|
@ -397,4 +396,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
|
||||
isRecording = false;
|
||||
}
|
||||
|
||||
public interface PreviewListener {
|
||||
void setFlashAvailable(boolean available);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue