1717package org .havenapp .main ;
1818
1919import android .Manifest ;
20+ import android .animation .ValueAnimator ;
2021import android .app .PictureInPictureParams ;
22+ import android .content .BroadcastReceiver ;
23+ import android .content .Context ;
2124import android .content .Intent ;
25+ import android .content .IntentFilter ;
2226import android .content .pm .PackageManager ;
2327import android .content .res .Configuration ;
28+ import android .graphics .Color ;
2429import android .os .Build ;
2530import android .os .Bundle ;
2631import android .os .CountDownTimer ;
2732import android .os .Environment ;
33+ import android .os .Handler ;
34+ import android .os .Message ;
35+ import android .text .TextUtils ;
2836import android .util .Log ;
2937import android .view .View ;
3038import android .view .WindowManager ;
39+ import android .view .animation .Animation ;
40+ import android .view .animation .AnimationUtils ;
3141import android .widget .Button ;
3242import android .widget .TextView ;
43+ import android .widget .Toast ;
3344
3445import com .wdullaer .materialdatetimepicker .time .TimePickerDialog ;
3546
47+ import org .havenapp .main .model .EventTrigger ;
3648import org .havenapp .main .service .MonitorService ;
3749import org .havenapp .main .ui .AccelConfigureActivity ;
3850import org .havenapp .main .ui .CameraConfigureActivity ;
4658import androidx .appcompat .app .AppCompatActivity ;
4759import androidx .core .app .ActivityCompat ;
4860import androidx .core .content .ContextCompat ;
61+ import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
4962
5063import static org .havenapp .main .Utils .getTimerText ;
5164
@@ -66,6 +79,67 @@ public class MonitorActivity extends AppCompatActivity implements TimePickerDial
6679
6780 private CameraFragment mFragmentCamera ;
6881
82+ private View mBtnCamera , mBtnMic , mBtnAccel ;
83+ private Animation mAnimShake ;
84+ private TextView txtStatus ;
85+
86+ private int lastEventType = -1 ;
87+
88+ /**
89+ * Handler used to update back the UI after motion detection
90+ */
91+ private final Handler handler = new Handler ()
92+ {
93+ @ Override
94+ public void handleMessage (Message msg ) {
95+ super .handleMessage (msg );
96+
97+ if (mIsMonitoring ) {
98+
99+ String message = null ;
100+
101+ if (msg .what == EventTrigger .CAMERA ) {
102+ mBtnCamera .startAnimation (mAnimShake );
103+ message = getString (R .string .motion_detected );
104+
105+ } else if (msg .what == EventTrigger .POWER ) {
106+ message = getString (R .string .power_detected );
107+
108+ } else if (msg .what == EventTrigger .MICROPHONE ) {
109+ mBtnMic .startAnimation (mAnimShake );
110+ message = getString (R .string .sound_detected );
111+
112+
113+ } else if (msg .what == EventTrigger .ACCELEROMETER || msg .what == EventTrigger .BUMP ) {
114+ mBtnAccel .startAnimation (mAnimShake );
115+ message = getString (R .string .device_move_detected );
116+
117+ } else if (msg .what == EventTrigger .LIGHT ) {
118+ message = getString (R .string .status_light );
119+
120+ }
121+
122+ if (lastEventType != msg .what ) {
123+ if (!TextUtils .isEmpty (message ))
124+ txtStatus .setText (message );
125+ }
126+
127+ lastEventType = msg .what ;
128+ }
129+ }
130+ };
131+
132+ BroadcastReceiver receiver = new BroadcastReceiver () {
133+ @ Override
134+ public void onReceive (Context context , Intent intent ) {
135+
136+ int eventType = intent .getIntExtra ("type" ,-1 );
137+ boolean detected = intent .getBooleanExtra ("detected" ,true );
138+ if (detected )
139+ handler .sendEmptyMessage (eventType );
140+ }
141+ };
142+
69143 @ Override
70144 protected void onCreate (Bundle savedInstanceState ) {
71145 super .onCreate (savedInstanceState );
@@ -107,70 +181,52 @@ private void initSetupLayout() {
107181 int timeM = preferences .getTimerDelay () * 1000 ;
108182
109183 txtTimer .setText (getTimerText (timeM ));
110- txtTimer .setOnClickListener (new View .OnClickListener () {
111- @ Override
112- public void onClick (View v ) {
113- if (cTimer == null )
114- showTimeDelayDialog ();
184+ txtTimer .setOnClickListener (v -> {
185+ if (cTimer == null )
186+ showTimeDelayDialog ();
115187
116- }
117188 });
118- findViewById (R .id .timer_text_title ).setOnClickListener (new View .OnClickListener () {
119- @ Override
120- public void onClick (View v ) {
121- if (cTimer == null )
122- showTimeDelayDialog ();
189+ findViewById (R .id .timer_text_title ).setOnClickListener (v -> {
190+ if (cTimer == null )
191+ showTimeDelayDialog ();
123192
124- }
125193 });
126194
127- findViewById (R .id .btnStartLater ).setOnClickListener (new View .OnClickListener () {
128- @ Override
129- public void onClick (View v ) {
130- doCancel ();
131- }
132- });
195+ findViewById (R .id .btnStartLater ).setOnClickListener (v -> doCancel ());
133196
134- findViewById (R .id .btnStartNow ).setOnClickListener (new View .OnClickListener () {
135- @ Override
136- public void onClick (View v ) {
137- ((Button ) findViewById (R .id .btnStartLater )).setText (R .string .action_cancel );
138- findViewById (R .id .btnStartNow ).setVisibility (View .INVISIBLE );
139- findViewById (R .id .timer_text_title ).setVisibility (View .INVISIBLE );
140- initTimer ();
141- }
197+ findViewById (R .id .btnStartNow ).setOnClickListener (v -> {
198+ ((Button ) findViewById (R .id .btnStartLater )).setText (R .string .action_cancel );
199+ findViewById (R .id .btnStartNow ).setVisibility (View .INVISIBLE );
200+ findViewById (R .id .timer_text_title ).setVisibility (View .INVISIBLE );
201+ initTimer ();
142202 });
143203
144- findViewById (R .id .btnAccelSettings ). setOnClickListener ( new View . OnClickListener () {
145- @ Override
146- public void onClick ( View v ) {
204+ mBtnAccel = findViewById (R .id .btnAccelSettings );
205+ mBtnAccel . setOnClickListener ( v -> {
206+ if (! mIsMonitoring )
147207 startActivity (new Intent (MonitorActivity .this , AccelConfigureActivity .class ));
148- }
149208 });
150209
151- findViewById (R .id .btnMicSettings ). setOnClickListener ( new View . OnClickListener () {
152- @ Override
153- public void onClick ( View v ) {
210+ mBtnMic = findViewById (R .id .btnMicSettings );
211+ mBtnMic . setOnClickListener ( v -> {
212+ if (! mIsMonitoring )
154213 startActivity (new Intent (MonitorActivity .this , MicrophoneConfigureActivity .class ));
155- }
156214 });
157215
158- findViewById (R .id .btnCameraSwitch ). setOnClickListener ( new View . OnClickListener () {
159- @ Override
160- public void onClick ( View v ) {
216+ mBtnCamera = findViewById (R .id .btnCameraSwitch );
217+ mBtnCamera . setOnClickListener ( v -> {
218+ if (! mIsMonitoring )
161219 configCamera ();
162- }
163220 });
164221
165- findViewById (R .id .btnSettings ).setOnClickListener (new View .OnClickListener () {
166- @ Override
167- public void onClick (View v ) {
168- showSettings ();
169- }
170- });
222+ findViewById (R .id .btnSettings ).setOnClickListener (v -> showSettings ());
171223
172224 mFragmentCamera = ((CameraFragment ) getSupportFragmentManager ().findFragmentById (R .id .fragment_camera ));
173225
226+ txtStatus = findViewById (R .id .txtStatus );
227+
228+ mAnimShake = AnimationUtils .loadAnimation (this , R .anim .shake );
229+
174230 mIsInitializedLayout = true ;
175231 }
176232
@@ -361,6 +417,18 @@ public void onResume() {
361417 int totalMilliseconds = preferences .getTimerDelay () * 1000 ;
362418 txtTimer .setText (getTimerText (totalMilliseconds ));
363419 }
420+
421+ IntentFilter filter = new IntentFilter ();
422+ filter .addAction ("event" );
423+ LocalBroadcastManager .getInstance (this ).registerReceiver (receiver ,filter );
424+
425+ }
426+
427+ @ Override
428+ protected void onPause () {
429+ super .onPause ();
430+ LocalBroadcastManager .getInstance (this ).unregisterReceiver (receiver );
431+
364432 }
365433
366434 @ Override
@@ -404,4 +472,5 @@ public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int seco
404472 int delaySeconds = second + minute * 60 + hourOfDay * 60 * 60 ;
405473 updateTimerValue (delaySeconds );
406474 }
475+
407476}
0 commit comments