Skip to content

Commit 88432ed

Browse files
committed
camera switch fixes for #238
1 parent 3dfa691 commit 88432ed

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

src/main/java/org/havenapp/main/MonitorActivity.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import org.havenapp.main.service.MonitorService;
3737
import org.havenapp.main.ui.AccelConfigureActivity;
38+
import org.havenapp.main.ui.CameraConfigureActivity;
3839
import org.havenapp.main.ui.CameraFragment;
3940
import org.havenapp.main.ui.MicrophoneConfigureActivity;
4041

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

60+
private final static int REQUEST_CAMERA = 999;
61+
private final static int REQUEST_TIMER = 1000;
62+
63+
private CameraFragment mFragmentCamera;
64+
5965
@Override
6066
protected void onCreate(Bundle savedInstanceState) {
6167
super.onCreate(savedInstanceState);
@@ -148,7 +154,7 @@ public void onClick(View v) {
148154
findViewById(R.id.btnCameraSwitch).setOnClickListener(new View.OnClickListener() {
149155
@Override
150156
public void onClick(View v) {
151-
switchCamera();
157+
configCamera();
152158
}
153159
});
154160

@@ -159,21 +165,29 @@ public void onClick(View v) {
159165
}
160166
});
161167

168+
mFragmentCamera = ((CameraFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_camera));
169+
162170
mIsInitializedLayout = true;
163171
}
164172

165-
private void switchCamera() {
173+
private void configCamera() {
166174

175+
mFragmentCamera.stopCamera();
176+
startActivityForResult(new Intent(this, CameraConfigureActivity.class),REQUEST_CAMERA);
177+
/**
167178
String camera = preferences.getCamera();
168179
if (camera.equals(PreferenceManager.FRONT))
169180
preferences.setCamera(PreferenceManager.BACK);
170181
else if (camera.equals(PreferenceManager.BACK))
171182
preferences.setCamera(PreferenceManager.FRONT);
172183
173184
((CameraFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_camera)).resetCamera();
185+
**/
174186

175187
}
176188

189+
190+
177191
private void updateTimerValue(int val) {
178192
preferences.setTimerDelay(val);
179193
int valM = val * 1000;
@@ -212,7 +226,7 @@ private void showSettings() {
212226
if (cTimer != null) {
213227
cTimer.cancel();
214228
cTimer = null;
215-
startActivityForResult(i, 9999);
229+
startActivityForResult(i, REQUEST_TIMER);
216230

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

227-
if (requestCode == 9999) {
241+
if (requestCode == REQUEST_TIMER) {
228242
initTimer();
229243
}
244+
else if (requestCode == REQUEST_CAMERA)
245+
{
246+
mFragmentCamera.resetCamera();
247+
}
248+
}
249+
250+
@Override
251+
protected void onDestroy() {
252+
if (!mIsMonitoring)
253+
{
254+
mFragmentCamera.stopCamera();
255+
}
256+
super.onDestroy();
230257
}
231258

232259
private void initTimer() {

src/main/java/org/havenapp/main/sensors/motion/Preview.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ public class Preview extends SurfaceView implements SurfaceHolder.Callback {
8686
* Messenger used to signal motion to the alert service
8787
*/
8888
private Messenger serviceMessenger = null;
89-
90-
private ServiceConnection mConnection = new ServiceConnection() {
89+
90+
private SurfaceHolder mHolder;
91+
private Camera camera;
92+
private Context context;
93+
private MotionAsyncTask task;
94+
95+
private ServiceConnection mConnection = new ServiceConnection() {
9196

9297
public void onServiceConnected(ComponentName className,
9398
IBinder service) {
@@ -102,12 +107,6 @@ public void onServiceDisconnected(ComponentName arg0) {
102107
}
103108
};
104109

105-
106-
private SurfaceHolder mHolder;
107-
private Camera camera;
108-
private Context context;
109-
private MotionAsyncTask task;
110-
111110
public Preview (Context context) {
112111
super(context);
113112
this.context = context;

src/main/java/org/havenapp/main/ui/CameraConfigureActivity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public void onClick(View v) {
8282
public void onValueChanged(int oldValue, int newValue) {
8383
mFragment.setMotionSensitivity(newValue);
8484
mPrefManager.setCameraSensitivity(newValue);
85+
setResult(RESULT_OK);
8586
}
8687
});
8788
mIsInitializedLayout = true;
@@ -96,7 +97,7 @@ else if (camera.equals(PreferenceManager.BACK))
9697
mPrefManager.setCamera(PreferenceManager.FRONT);
9798

9899
((CameraFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_camera)).resetCamera();
99-
100+
setResult(RESULT_OK);
100101
}
101102

102103

@@ -105,17 +106,20 @@ else if (camera.equals(PreferenceManager.BACK))
105106
public boolean onOptionsItemSelected(MenuItem item) {
106107
switch (item.getItemId()) {
107108
case android.R.id.home:
109+
mFragment.stopCamera();
108110
finish();
109111
break;
110112
}
111113
return true;
112114
}
113115

116+
114117
/**
115118
* When user closes the activity
116119
*/
117120
@Override
118121
public void onBackPressed() {
122+
mFragment.stopCamera();
119123
finish();
120124
}
121125

src/main/java/org/havenapp/main/ui/CameraFragment.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,18 @@ public void onResume() {
5959
initCamera ();
6060
}
6161

62+
public void stopCamera ()
63+
{
64+
if (preview != null) {
65+
preview.stopCamera();
66+
preview = null;
67+
}
68+
}
69+
6270
public void resetCamera ()
6371
{
72+
stopCamera();
6473
((FrameLayout) getActivity().findViewById(R.id.preview)).removeAllViews();
65-
preview = null;
6674
initCamera();
6775
}
6876

@@ -87,6 +95,10 @@ public void onProcess(Bitmap oldBitmap, Bitmap newBitmap, Bitmap rawBitmap,
8795
boolean motionDetected) {
8896
int rotation = 0;
8997
boolean reflex = false;
98+
99+
if (preview == null)
100+
return;
101+
90102
if (preview.getCameraFacing() == Camera.CameraInfo.CAMERA_FACING_BACK) {
91103
rotation = 90;
92104
} else {
@@ -105,8 +117,6 @@ public void onProcess(Bitmap oldBitmap, Bitmap newBitmap, Bitmap rawBitmap,
105117
@Override
106118
public void onDestroy() {
107119
super.onDestroy();
108-
109-
preview.stopCamera();
110120
}
111121

112122
public void onSensorChanged(SensorEvent event) {

src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
<string name="notification_time_dialog">Enter time (minutes) to limit notifications. \'0\' to send every notification.</string>
136136
<string name="minutes">minutes(s)</string>
137137
<string name="keep_watch">Keep Watch!</string>
138-
<string name="camera_sensitivity_tip">Select camera and use the slider to adjust motion detection sensitivity</string>
138+
<string name="camera_sensitivity_tip">Switch camera or use the slider to adjust motion detection sensitivity</string>
139139
<string name="disable_battery_opt_title">Disable Battery Optimizations</string>
140140
<string name="disable_battery_opt_summary">Allow app to run when screen is off</string>
141141

0 commit comments

Comments
 (0)