From 7e171292d73e8c10fb88cb84722912f4042daab5 Mon Sep 17 00:00:00 2001 From: Francesco Gabbrielli Date: Tue, 25 Sep 2018 20:01:50 +0200 Subject: [PATCH] Version 0.3.0 - Refactoring of services and threading frameworks - Bound Service --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 3 + .../apps/sensorlogger/LoggingService.java | 4 +- .../apps/sensorlogger/MainActivity.java | 117 +++++++++++++++--- .../apps/sensorlogger/Recorder.java | 7 +- app/src/main/res/layout/activity_main.xml | 23 +++- app/src/main/res/values/strings.xml | 1 + 7 files changed, 128 insertions(+), 29 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ed91946..db36eb7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 26 - buildToolsVersion '27.0.3' + buildToolsVersion '28.0.2' defaultConfig { applicationId "it.francescogabbrielli.apps.sensorlogger" minSdkVersion 21 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 39685a3..52d05d0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -32,6 +32,9 @@ + 1000) + if (keyCode==KeyEvent.KEYCODE_VOLUME_DOWN || keyCode==KeyEvent.KEYCODE_VOLUME_UP ) { + lastPressed = SystemClock.elapsedRealtime(); + AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE); + if (!recording) { + recording = true; + recorder.start(this); + mgr.playSoundEffect(AudioManager.FX_KEY_CLICK); + showPrepareAnimation(); + } else { + recording = false; + recorder.stop(); + mgr.playSoundEffect(AudioManager.FX_KEY_CLICK); + hidePrepareAnimation(); + hideBlinkingAnimation(); + // restart preview ??? + if (cameraHandlerThread != null) + cameraHandlerThread.restart(); + } + return true; + } + return super.onKeyDown(keyCode,event); + } + + private ScheduledExecutorService animExec; + private ScheduledFuture prepareFuture, recordingFuture; + + private void showPrepareAnimation() { + if (animExec==null) + animExec = Executors.newSingleThreadScheduledExecutor(); + prepareFuture = animExec.schedule(new Runnable() { + @Override + public void run() { + runOnUiThread(new Runnable() { + @Override + public void run() { + TextView t = findViewById(R.id.anim_prepare); + int val = 4; + try { val = Integer.parseInt(t.getText().toString()); } catch(Exception e) {} + if (--val>0) { + t.setText(String.valueOf(val)); + showPrepareAnimation(); + } else { + hidePrepareAnimation(); + showBlinkingAnimation(); + } + } + }); + } + }, 1, TimeUnit.SECONDS); + } + private void hidePrepareAnimation() { + if (prepareFuture !=null) + prepareFuture.cancel(true); + TextView t = findViewById(R.id.anim_prepare); + t.setText(""); + } + + private void showBlinkingAnimation() { + if (animExec==null) + animExec = Executors.newSingleThreadScheduledExecutor(); + recordingFuture = animExec.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + runOnUiThread(new Runnable() { + @Override + public void run() { + ImageView i = findViewById(R.id.img_record); + i.setVisibility(i.getVisibility()==View.VISIBLE ? View.INVISIBLE : View.VISIBLE); + } + }); + } + }, 0, 500, TimeUnit.MILLISECONDS); + + } + private void hideBlinkingAnimation() { + if (recordingFuture!=null) + recordingFuture.cancel(true); + ImageView i = findViewById(R.id.img_record); + i.setVisibility(View.INVISIBLE); } // // ----------------------------------- PERMISSIONS MANAGEMENT ---------------------------------- // - /** * Checks if the app has the required permissions, as per current setttings. *

diff --git a/app/src/main/java/it/francescogabbrielli/apps/sensorlogger/Recorder.java b/app/src/main/java/it/francescogabbrielli/apps/sensorlogger/Recorder.java index 84489f0..2ebd0fd 100644 --- a/app/src/main/java/it/francescogabbrielli/apps/sensorlogger/Recorder.java +++ b/app/src/main/java/it/francescogabbrielli/apps/sensorlogger/Recorder.java @@ -98,7 +98,7 @@ private void logSensors(int timestamp) { if (buffer.length()>0) buffer.deleteCharAt(buffer.length()-1); - Log.v(TAG, "Sensor reading: "+buffer); +// Log.v(TAG, "Sensor reading: "+buffer); buffer.append('\n'); @@ -111,7 +111,7 @@ private void logSensors(int timestamp) { } private void scheduleJob(String filename, byte[] data, int type) { - Log.v(TAG, "Scheduling "+filename+", type"+type); +// Log.v(TAG, "Scheduling "+filename+", type"+type); // Bundle extras = new Bundle(); // extras.putString(Util.EXTRA_FILENAME, filename); // extras.putInt(Util.EXTRA_TYPE, type); @@ -153,7 +153,8 @@ public void stop() { schedule.cancel(true); schedule = null; reader.stop(); - scheduleJob(null,null, ILogTarget.CLOSE); + if (started) + scheduleJob(null,null, ILogTarget.CLOSE); // unbinding context.unbindService(this); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c25043e..59eb27d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -22,14 +22,25 @@ - + android:src="@drawable/btn_rec" + android:visibility="invisible" + /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4408c15..726253f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,6 +3,7 @@ Settings SensorLogger + Recording starts in 3 seconds