Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 120 additions & 178 deletions app/src/main/java/ch/virt/smartphonemouse/MainActivity.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@

import android.content.SharedPreferences;

/**
* This class is used for restoring the settings to their factory defaults.
*/
public class DefaultSettings {

public static void check(SharedPreferences preferences){
/**
* Checks whether the preferences have once been populated.
* If not, they get initialized to the default settings.
*
* @param preferences preferences to check in
*/
public static void check(SharedPreferences preferences) {
if (!preferences.getBoolean("populated", false)) set(preferences);
}

public static void set(SharedPreferences preferences){
/**
* Overwrites the settings to the defaults
*
* @param preferences preferences to write in
*/
public static void set(SharedPreferences preferences) {
SharedPreferences.Editor edit = preferences.edit();

edit.putBoolean("populated", true);
Expand All @@ -22,7 +36,12 @@ public static void set(SharedPreferences preferences){
defaultCommunication(preferences);
}

private static void defaultInterface(SharedPreferences preferences){
/**
* Writes the default interface settings
*
* @param preferences preferences to write in
*/
private static void defaultInterface(SharedPreferences preferences) {
SharedPreferences.Editor edit = preferences.edit();

edit.putString("interfaceTheme", "dark");
Expand All @@ -48,7 +67,12 @@ private static void defaultInterface(SharedPreferences preferences){
edit.apply();
}

private static void defaultMovement(SharedPreferences preferences){
/**
* Writes the default movement settings
*
* @param preferences preferences to write in
*/
private static void defaultMovement(SharedPreferences preferences) {
SharedPreferences.Editor edit = preferences.edit();

edit.putFloat("movementSensitivity", 13);
Expand All @@ -59,7 +83,7 @@ private static void defaultMovement(SharedPreferences preferences){
edit.putInt("movementSamplingRealRate", 200);

edit.putInt("movementLowPassOrder", 1);
edit.putFloat("movementLowPassCutoff", 0.1f);
edit.putFloat("movementLowPassCutoff", 0.05f);

edit.putFloat("movementFreezerFreezingThreshold", 0.1f);
edit.putFloat("movementFreezerUnfreezingThreshold", 0.04f);
Expand All @@ -78,12 +102,16 @@ private static void defaultMovement(SharedPreferences preferences){
edit.apply();
}

private static void defaultCommunication(SharedPreferences preferences){
/**
* Writes the default communication settings
*
* @param preferences preferences to write in
*/
private static void defaultCommunication(SharedPreferences preferences) {
SharedPreferences.Editor edit = preferences.edit();

edit.putInt("communicationTransmissionRate", 100);

edit.apply();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;

import androidx.preference.PreferenceManager;

import java.util.Timer;
import java.util.TimerTask;

import ch.virt.smartphonemouse.helper.Listener;
import ch.virt.smartphonemouse.helper.MainContext;
import ch.virt.smartphonemouse.mouse.MovementHandler;

/**
* This class is used to measure and save the sampling rate of the inbuilt accelerometer.
*/
public class SamplingRateCalibrator implements SensorEventListener {

private static final int TEST_LENGTH = 5000;
Expand All @@ -29,34 +31,53 @@ public class SamplingRateCalibrator implements SensorEventListener {
private long delays;
private int amount;

private final MainContext main;
private final Context context;

public SamplingRateCalibrator(MainContext main) {
this.main = main;
/**
* Creates the calibrator.
*
* @param context context to use
*/
public SamplingRateCalibrator(Context context) {
this.context = context;

fetchSensor();
}

public void fetchSensor(){
manager = (SensorManager) main.getContext().getSystemService(Context.SENSOR_SERVICE);
/**
* Fetches the sensor from the system.
*/
private void fetchSensor() {
manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensor = manager.getDefaultSensor(MovementHandler.SENSOR_TYPE);
}

public void register(){
/**
* Registers itself as a listener.
*/
private void register() {
if (registered) return;
manager.registerListener(this, sensor, MovementHandler.SAMPLING_RATE);

registered = true;
}

public void unregister(){
/**
* Unregisters itself as a listener.
*/
private void unregister() {
if (!registered) return;
manager.unregisterListener(this, sensor);

registered = false;
}

public void calibrate(Listener doneListener){
/**
* Starts the measuring process.
*
* @param doneListener listener that is executed once the process has finished
*/
public void calibrate(DoneListener doneListener) {
register();
prepareTest();
running = true;
Expand All @@ -67,34 +88,46 @@ public void run() {
running = false;
unregister();

finishTest();
int rate = finishTest();

doneListener.called();
doneListener.done(rate);
}
}, TEST_LENGTH);

}

public void prepareTest(){
/**
* Initializes the variables for the process.
*/
private void prepareTest() {
lastTime = 0;
delays = 0;
amount = 0;
}

public void finishTest(){
/**
* Finishes the measuring process by processing the results and saving it into the preferences.
*/
private int finishTest() {
long averageDelay = delays / amount;
float averageDelaySecond = averageDelay * NANO_FULL_FACTOR;

int samplesPerSecond = Math.round(1f / averageDelaySecond);

SharedPreferences.Editor edit = main.getPreferences().edit();
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(context).edit();
edit.putBoolean("movementSamplingCalibrated", true);
edit.putInt("movementSamplingRealRate", samplesPerSecond);
edit.apply();

return samplesPerSecond;
}

public int getTestLength(){
/**
* Returns how long the measuring process approximately will go.
*
* @return length of the process in milliseconds
*/
public int getTestLength() {
return TEST_LENGTH;
}

Expand All @@ -116,6 +149,18 @@ public void onSensorChanged(SensorEvent event) {

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

/**
* This interface is a listener used for when the measuring process is done.
*/
public interface DoneListener {

/**
* Called when the process is done.
*
* @param samplingRate sampling rate that was measured
*/
void done(int samplingRate);
}
}

This file was deleted.

25 changes: 0 additions & 25 deletions app/src/main/java/ch/virt/smartphonemouse/helper/MainContext.java

This file was deleted.

This file was deleted.

Loading