Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Commit

Permalink
Value for 'work cycles' is remembered between app runs. Close #74
Browse files Browse the repository at this point in the history
  • Loading branch information
dturner committed Sep 7, 2017
1 parent 26efb7f commit 1bd6391
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.media.AudioManager;
Expand All @@ -44,7 +45,9 @@ public class MainActivity extends AppCompatActivity {
private static final int VARIABLE_LOAD_LOW_DURATION = 2000;
private static final int VARIABLE_LOAD_HIGH_DURATION = 2000;
public static final int MAXIMUM_WORK_CYCLES = 500000;
private static final int WORK_CYCLE_STEPS = 100;
private static final int SEEKBAR_STEPS = 100;
private static final float WORK_CYCLES_PER_STEP = MAXIMUM_WORK_CYCLES / SEEKBAR_STEPS;
private static final String PREFERENCES_KEY_WORK_CYCLES = "work_cycles";

private static int workCycles = 0;

Expand All @@ -55,6 +58,7 @@ public class MainActivity extends AppCompatActivity {
private TextView mDeviceInfoText, mWorkCyclesText;
private AudioTrack mAudioTrack;
private VariableLoadGenerator mLoadThread;
private SharedPreferences mSettings;

// Native methods
private static native void native_createEngine(int apiLevel);
Expand All @@ -77,6 +81,10 @@ protected void onCreate(Bundle savedInstanceState) {
// Lock to portrait to avoid onCreate being called more than once
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

// Load any previously saved values
mSettings = getPreferences(MODE_PRIVATE);
workCycles = mSettings.getInt(PREFERENCES_KEY_WORK_CYCLES, workCycles);

initDeviceInfoUI();
initPerformanceConfigurationUI();

Expand All @@ -88,6 +96,16 @@ protected void onCreate(Bundle savedInstanceState) {

// Update the UI when there are underruns
initUnderrunUpdater();

setWorkCycles(workCycles);
}

@Override
protected void onStop(){
super.onStop();
SharedPreferences.Editor editor = mSettings.edit();
editor.putInt(PREFERENCES_KEY_WORK_CYCLES, workCycles);
editor.apply();
}

private void initDeviceInfoUI(){
Expand Down Expand Up @@ -210,10 +228,12 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
mWorkCyclesText = (TextView) findViewById(R.id.workCyclesText);

SeekBar workCyclesSeekBar = (SeekBar) findViewById(R.id.workCycles);
workCyclesSeekBar.setProgress((int)(workCycles / WORK_CYCLES_PER_STEP));

workCyclesSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
workCycles = progress * (MAXIMUM_WORK_CYCLES / WORK_CYCLE_STEPS);
workCycles = (int)(progress * WORK_CYCLES_PER_STEP);
setWorkCycles(workCycles);
}

Expand Down

0 comments on commit 1bd6391

Please sign in to comment.