Skip to content

Commit

Permalink
Version 0.3.0
Browse files Browse the repository at this point in the history
- Refactoring of services and threading frameworks
- Bound Service
  • Loading branch information
francescogabbrielli committed Sep 25, 2018
1 parent 5025d64 commit 7e17129
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--intent-filter>
<action android:name="android.intent.action.VIEW"/>
</intent-filter-->
</activity>
<activity
android:name=".SettingsActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void handleStart(final Bundle extras) {
}

public void log(final String filename, final int type, final byte[] data) {
Log.v(TAG, "Logging to "+filename+" type "+type);
// Log.v(TAG, "Logging to "+filename+" type "+type);
handler.post(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -154,7 +154,7 @@ public void run() {
}
break;
}
Log.d(TAG, "Logged to "+filename+", type "+type);
// Log.d(TAG, "Logged to "+filename+", type "+type);
} catch(Exception exc) {
Log.e(TAG, "Logging error", exc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,35 @@
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.Environment;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.widget.CompoundButton;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity implements
CompoundButton.OnCheckedChangeListener,
SurfaceHolder.Callback,
ICamera {

Expand All @@ -55,6 +62,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Expand All @@ -63,10 +72,6 @@ protected void onCreate(Bundle savedInstanceState) {
new SensorReader((SensorManager) getSystemService(SENSOR_SERVICE), prefs),
this);

final ToggleButton b = findViewById(R.id.btn_rec);
// b.setChecked(prefs.getBoolean(Util.PREF_RECORDING, false));
b.setOnCheckedChangeListener(this);

//start the service
startService(new Intent(this, LoggingService.class));

Expand Down Expand Up @@ -132,6 +137,10 @@ protected void onResume() {
protected void onPause() {
Log.d(TAG, "onPause");
recorder.stop();
hidePrepareAnimation();
hideBlinkingAnimation();
if (animExec!=null)
animExec.shutdown();
super.onPause();
}

Expand All @@ -151,23 +160,97 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

private boolean recording;
private long lastPressed;

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
recorder.start(this);
} else {
recorder.stop();
// restart preview ???
if (cameraHandlerThread != null)
cameraHandlerThread.restart();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (SystemClock.elapsedRealtime()-lastPressed>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);
}


//<editor-fold desc="Permissions">
// ----------------------------------- PERMISSIONS MANAGEMENT ----------------------------------
//

/**
* Checks if the app has the required permissions, as per current setttings.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 17 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@

<include layout="@layout/content_main" />

<ToggleButton
android:id="@+id/btn_rec"
<ImageView
android:id="@+id/img_record"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="bottom|center"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:textOn=""
android:textOff=""
android:background="@drawable/btn_rec" />
android:src="@drawable/btn_rec"
android:visibility="invisible"
/>

<TextView
android:id="@+id/anim_prepare"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@android:color/white"
android:textSize="100dp"
android:textStyle="bold"
android:textAlignment="center"
android:gravity="center"
/>

</android.support.design.widget.CoordinatorLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<string name="title_activity_settings">Settings</string>

<string name="app_folder">SensorLogger</string>
<string name="gif_recording">Recording starts in 3 seconds</string>

<!-- Strings related to Settings -->

Expand Down

0 comments on commit 7e17129

Please sign in to comment.