Skip to content

Commit

Permalink
camera switch fixes for #238
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Jan 20, 2018
1 parent 3dfa691 commit 88432ed
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 17 deletions.
35 changes: 31 additions & 4 deletions src/main/java/org/havenapp/main/MonitorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import org.havenapp.main.service.MonitorService;
import org.havenapp.main.ui.AccelConfigureActivity;
import org.havenapp.main.ui.CameraConfigureActivity;
import org.havenapp.main.ui.CameraFragment;
import org.havenapp.main.ui.MicrophoneConfigureActivity;

Expand All @@ -56,6 +57,11 @@ public class MonitorActivity extends AppCompatActivity implements TimePickerDial
private boolean mIsInitializedLayout = false;
private boolean mOnTimerTicking = false;

private final static int REQUEST_CAMERA = 999;
private final static int REQUEST_TIMER = 1000;

private CameraFragment mFragmentCamera;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -148,7 +154,7 @@ public void onClick(View v) {
findViewById(R.id.btnCameraSwitch).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchCamera();
configCamera();
}
});

Expand All @@ -159,21 +165,29 @@ public void onClick(View v) {
}
});

mFragmentCamera = ((CameraFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_camera));

mIsInitializedLayout = true;
}

private void switchCamera() {
private void configCamera() {

mFragmentCamera.stopCamera();
startActivityForResult(new Intent(this, CameraConfigureActivity.class),REQUEST_CAMERA);
/**
String camera = preferences.getCamera();
if (camera.equals(PreferenceManager.FRONT))
preferences.setCamera(PreferenceManager.BACK);
else if (camera.equals(PreferenceManager.BACK))
preferences.setCamera(PreferenceManager.FRONT);
((CameraFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_camera)).resetCamera();
**/

}



private void updateTimerValue(int val) {
preferences.setTimerDelay(val);
int valM = val * 1000;
Expand Down Expand Up @@ -212,7 +226,7 @@ private void showSettings() {
if (cTimer != null) {
cTimer.cancel();
cTimer = null;
startActivityForResult(i, 9999);
startActivityForResult(i, REQUEST_TIMER);

} else {
startActivity(i);
Expand All @@ -224,9 +238,22 @@ private void showSettings() {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 9999) {
if (requestCode == REQUEST_TIMER) {
initTimer();
}
else if (requestCode == REQUEST_CAMERA)
{
mFragmentCamera.resetCamera();
}
}

@Override
protected void onDestroy() {
if (!mIsMonitoring)
{
mFragmentCamera.stopCamera();
}
super.onDestroy();
}

private void initTimer() {
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/org/havenapp/main/sensors/motion/Preview.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ public class Preview extends SurfaceView implements SurfaceHolder.Callback {
* Messenger used to signal motion to the alert service
*/
private Messenger serviceMessenger = null;

private ServiceConnection mConnection = new ServiceConnection() {

private SurfaceHolder mHolder;
private Camera camera;
private Context context;
private MotionAsyncTask task;

private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className,
IBinder service) {
Expand All @@ -102,12 +107,6 @@ public void onServiceDisconnected(ComponentName arg0) {
}
};


private SurfaceHolder mHolder;
private Camera camera;
private Context context;
private MotionAsyncTask task;

public Preview (Context context) {
super(context);
this.context = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void onClick(View v) {
public void onValueChanged(int oldValue, int newValue) {
mFragment.setMotionSensitivity(newValue);
mPrefManager.setCameraSensitivity(newValue);
setResult(RESULT_OK);
}
});
mIsInitializedLayout = true;
Expand All @@ -96,7 +97,7 @@ else if (camera.equals(PreferenceManager.BACK))
mPrefManager.setCamera(PreferenceManager.FRONT);

((CameraFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_camera)).resetCamera();

setResult(RESULT_OK);
}


Expand All @@ -105,17 +106,20 @@ else if (camera.equals(PreferenceManager.BACK))
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mFragment.stopCamera();
finish();
break;
}
return true;
}


/**
* When user closes the activity
*/
@Override
public void onBackPressed() {
mFragment.stopCamera();
finish();
}

Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/havenapp/main/ui/CameraFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,18 @@ public void onResume() {
initCamera ();
}

public void stopCamera ()
{
if (preview != null) {
preview.stopCamera();
preview = null;
}
}

public void resetCamera ()
{
stopCamera();
((FrameLayout) getActivity().findViewById(R.id.preview)).removeAllViews();
preview = null;
initCamera();
}

Expand All @@ -87,6 +95,10 @@ public void onProcess(Bitmap oldBitmap, Bitmap newBitmap, Bitmap rawBitmap,
boolean motionDetected) {
int rotation = 0;
boolean reflex = false;

if (preview == null)
return;

if (preview.getCameraFacing() == Camera.CameraInfo.CAMERA_FACING_BACK) {
rotation = 90;
} else {
Expand All @@ -105,8 +117,6 @@ public void onProcess(Bitmap oldBitmap, Bitmap newBitmap, Bitmap rawBitmap,
@Override
public void onDestroy() {
super.onDestroy();

preview.stopCamera();
}

public void onSensorChanged(SensorEvent event) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
<string name="notification_time_dialog">Enter time (minutes) to limit notifications. \'0\' to send every notification.</string>
<string name="minutes">minutes(s)</string>
<string name="keep_watch">Keep Watch!</string>
<string name="camera_sensitivity_tip">Select camera and use the slider to adjust motion detection sensitivity</string>
<string name="camera_sensitivity_tip">Switch camera or use the slider to adjust motion detection sensitivity</string>
<string name="disable_battery_opt_title">Disable Battery Optimizations</string>
<string name="disable_battery_opt_summary">Allow app to run when screen is off</string>

Expand Down

0 comments on commit 88432ed

Please sign in to comment.