2121package eu .basicairdata .clinometer ;
2222
2323import android .content .Context ;
24+ import android .content .DialogInterface ;
2425import android .content .SharedPreferences ;
2526import android .hardware .Sensor ;
2627import android .hardware .SensorEvent ;
3132import android .os .Bundle ;
3233import android .os .Vibrator ;
3334import android .util .Log ;
35+ import android .view .MenuItem ;
3436import android .view .View ;
3537import android .view .WindowManager ;
3638import android .widget .ImageView ;
3739import android .widget .ProgressBar ;
3840import android .widget .TextView ;
3941
42+ import androidx .appcompat .app .AlertDialog ;
4043import androidx .appcompat .app .AppCompatActivity ;
4144import androidx .appcompat .widget .AppCompatButton ;
4245import androidx .preference .PreferenceManager ;
4346
47+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_CALIBRATION_ANGLE_0 ;
48+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_CALIBRATION_ANGLE_1 ;
49+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_CALIBRATION_ANGLE_2 ;
50+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_CALIBRATION_GAIN_0 ;
51+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_CALIBRATION_GAIN_1 ;
52+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_CALIBRATION_GAIN_2 ;
53+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_CALIBRATION_OFFSET_0 ;
54+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_CALIBRATION_OFFSET_1 ;
55+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_CALIBRATION_OFFSET_2 ;
56+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_CALIBRATION_TIME ;
57+ import static eu .basicairdata .clinometer .ClinometerApplication .KEY_PREF_KEEP_SCREEN_ON ;
58+
4459
4560public class CalibrationActivity extends AppCompatActivity implements SensorEventListener {
4661
@@ -64,6 +79,7 @@ public class CalibrationActivity extends AppCompatActivity implements SensorEven
6479private AppCompatButton buttonNext ;
6580private ProgressBar progressBar ;
6681private ImageView imageViewMain ;
82+ private ImageView imageViewCalibrationIcon ;
6783private TextView textViewStepDescription ;
6884private TextView textViewLastCalibration ;
6985private TextView textViewProgress ;
@@ -84,7 +100,7 @@ public class CalibrationActivity extends AppCompatActivity implements SensorEven
84100private static final int STEP_6_CAL =11 ;// Calibrating... Don't move the device
85101private static final int STEP_7 =12 ;// Step 7 of 7 Press next and lay face down
86102private static final int STEP_7_CAL =13 ;// Calibrating... Don't move the device
87- private static final int STEP_COMPLETED =14 ;// Calibration completed (Message Box)
103+ private static final int STEP_COMPLETED =14 ;// Calibration completed, performs calculations and shows results
88104
89105private static final float STANDARD_GRAVITY =9.807f ;
90106
@@ -110,12 +126,16 @@ protected void onCreate(Bundle savedInstanceState) {
110126textViewLastCalibration =findViewById (R .id .id_textview_last_calibration );
111127textViewProgress =findViewById (R .id .id_textview_progress );
112128imageViewMain =findViewById (R .id .id_imageViewMain );
129+ imageViewCalibrationIcon =findViewById (R .id .id_imageViewCalibrationIcon );
113130
114131buttonNext .setOnClickListener (new View .OnClickListener () {
115132@ Override
116133public void onClick (View view ) {
117- currentStep ++;
118- startStep ();
134+ if (currentStep ==STEP_COMPLETED )finish ();
135+ else {
136+ currentStep ++;
137+ startStep ();
138+ }
119139 }
120140 });
121141
@@ -125,16 +145,16 @@ public void onClick(View view) {
125145
126146SharedPreferences preferences =PreferenceManager .getDefaultSharedPreferences (this );
127147
128- if (preferences .contains ("prefCalibrationAngle0" )) {
148+ if (preferences .contains (KEY_PREF_CALIBRATION_ANGLE_0 )) {
129149textViewLastCalibration .setText (getString (R .string .calibration_active_calibration ) +"\n "
130150 +getString (R .string .calibration_active_calibration_gains )
131- +String .format (" = %1.3f; %1.3f; %1.3f" ,preferences .getFloat ("prefCalibrationGain0" ,0 ),preferences .getFloat ("prefCalibrationGain1" ,0 ),preferences .getFloat ("prefCalibrationGain2" ,0 ))
151+ +String .format (" = %1.3f; %1.3f; %1.3f" ,preferences .getFloat (KEY_PREF_CALIBRATION_GAIN_0 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_GAIN_1 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_GAIN_2 ,0 ))
132152 +"\n "
133153 +getString (R .string .calibration_active_calibration_offsets )
134- +String .format (" = %1.3f; %1.3f; %1.3f" ,preferences .getFloat ("prefCalibrationOffset0" ,0 ),preferences .getFloat ("prefCalibrationOffset1" ,0 ),preferences .getFloat ("prefCalibrationOffset2" ,0 ))
154+ +String .format (" = %1.3f; %1.3f; %1.3f" ,preferences .getFloat (KEY_PREF_CALIBRATION_OFFSET_0 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_OFFSET_1 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_OFFSET_2 ,0 ))
135155 +"\n "
136156 +getString (R .string .calibration_active_calibration_angles )
137- +String .format (" = %1.2f°; %1.2f°; %1.2f°" ,preferences .getFloat ("prefCalibrationAngle0" ,0 ),preferences .getFloat ("prefCalibrationAngle1" ,0 ),preferences .getFloat ("prefCalibrationAngle2" ,0 ))
157+ +String .format (" = %1.2f°; %1.2f°; %1.2f°" ,preferences .getFloat (KEY_PREF_CALIBRATION_ANGLE_2 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_ANGLE_1 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_ANGLE_0 ,0 ))
138158 );
139159 }
140160
@@ -143,16 +163,62 @@ public void onClick(View view) {
143163
144164
145165@ Override
146- protected void onResume () {
147- super .onResume ();
148- getWindow ().addFlags (WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON );
166+ public boolean onOptionsItemSelected (MenuItem item )
167+ {
168+ // Handle action bar item clicks here. The action bar will
169+ // automatically handle clicks on the Home/Up button, so long
170+ // as you specify a parent activity in AndroidManifest.xml.
171+ switch (item .getItemId ()) {
172+ case android .R .id .home :
173+ onBackPressed ();
174+ return true ;
175+ default :
176+ return super .onOptionsItemSelected (item );
177+ }
178+ }
149179
150- if ((int ) (currentStep /2 ) *2 !=currentStep )currentStep --;
151- Log .d ("CalibrationActivity" ,"CurrentStep = " +currentStep );
152180
153- startStep ();
181+ @ Override
182+ public void onBackPressed ()
183+ {
184+ Log .d ("CalibrationActivity" ,"onBackPressed on Step " +currentStep );
185+ if ((currentStep !=STEP_1 ) && (currentStep <STEP_COMPLETED )) {
186+ AlertDialog .Builder builder =new AlertDialog .Builder (this );
187+ builder .setMessage (getResources ().getString (R .string .calibration_abort ));
188+ //builder.setIcon(android.R.drawable.ic_menu_info_details);
189+ builder .setPositiveButton (R .string .yes ,new DialogInterface .OnClickListener () {
190+ public void onClick (DialogInterface dialog ,int id ) {
191+ dialog .dismiss ();
192+ finish ();
193+ }
194+ });
195+ builder .setNegativeButton (R .string .no ,new DialogInterface .OnClickListener () {
196+ public void onClick (DialogInterface dialog ,int id ) {
197+ dialog .dismiss ();
198+ }
199+ });
200+ AlertDialog dialog =builder .create ();
201+ dialog .show ();
202+ }else {
203+ finish ();
204+ //super.onBackPressed(); // optional depending on your needs
205+ }
206+ }
154207
155- //mSensorManager.registerListener(this, mRotationSensor, ACCELEROMETER_UPDATE_INTERVAL_MICROS);
208+
209+ @ Override
210+ protected void onResume () {
211+ super .onResume ();
212+
213+ if (PreferenceManager .getDefaultSharedPreferences (this ).getBoolean (KEY_PREF_KEEP_SCREEN_ON ,true ))getWindow ().addFlags (WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON );
214+ else getWindow ().clearFlags (WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON );
215+
216+ if (currentStep <STEP_COMPLETED ) {
217+ // If the app was calibrating when the onPause is called, the app goes back 1 step (the previous screen)
218+ if ((int ) (currentStep /2 ) *2 !=currentStep )currentStep --;
219+ Log .d ("CalibrationActivity" ,"CurrentStep = " +currentStep );
220+ startStep ();
221+ }
156222 }
157223
158224
@@ -238,7 +304,20 @@ private void startStep() {
238304mSensorManager .registerListener (this ,mRotationSensor ,ACCELEROMETER_UPDATE_INTERVAL_MICROS );
239305break ;
240306case STEP_COMPLETED :
307+
308+ // Write raw step Values into Preferences
309+
310+ SharedPreferences preferences =PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
311+ SharedPreferences .Editor editorRaw =preferences .edit ();
312+ for (int i =0 ;i <7 ;i ++) {
313+ editorRaw .putFloat ("prefCalibrationRawMean_0_" +i ,mean [0 ][i ]);
314+ editorRaw .putFloat ("prefCalibrationRawMean_1_" +i ,mean [1 ][i ]);
315+ editorRaw .putFloat ("prefCalibrationRawMean_2_" +i ,mean [2 ][i ]);
316+ }
317+ editorRaw .commit ();
318+
241319// Calculations
320+
242321Log .d ("Clinometer" ,"-- MEAN NOT CORRECTED ------------------------------------------------------" );
243322for (int i =0 ;i <7 ;i ++) {
244323Log .d ("Clinometer" ,String .format ("mean[ ][" +i +"] = %+1.4f %+1.4f %+1.4f" ,mean [0 ][i ],mean [1 ][i ],mean [2 ][i ]));
@@ -292,29 +371,45 @@ private void startStep() {
292371
293372calibrationAngle [2 ] = (angle [0 ][0 ] +angle [0 ][1 ])/2 ;// angle 0 = X axis
294373calibrationAngle [1 ] = -(angle [1 ][0 ] +angle [1 ][1 ])/2 ;// angle 1 = Y axis
295- calibrationAngle [0 ] = -(angle [1 ][3 ]+ angle [1 ][2 ])/2 ;// angle 2 = Z axis
374+ calibrationAngle [0 ] = -(angle [1 ][3 ]- angle [1 ][2 ])/2 ;// angle 2 = Z axis
296375
297376Log .d ("Clinometer" ,"-- CALIBRATION ANGLES ------------------------------------------------------" );
298- Log .d ("Clinometer" ,String .format ("Cal.Angles = %+1.4f° %+1.4f° %+1.4f°" ,calibrationAngle [0 ],calibrationAngle [1 ],calibrationAngle [2 ]));
299-
377+ Log .d ("Clinometer" ,String .format ("Cal.Angles = %+1.4f° %+1.4f° %+1.4f°" ,calibrationAngle [2 ],calibrationAngle [1 ],calibrationAngle [0 ]));
300378Log .d ("Clinometer" ,"----------------------------------------------------------------------------" );
301379
302380// Write Calibration Angles into Preferences
303- SharedPreferences preferences = PreferenceManager . getDefaultSharedPreferences ( getApplicationContext ());
381+
304382SharedPreferences .Editor editor =preferences .edit ();
305- editor .putFloat ("prefCalibrationAngle0" ,calibrationAngle [0 ]);
306- editor .putFloat ("prefCalibrationAngle1" ,calibrationAngle [1 ]);
307- editor .putFloat ("prefCalibrationAngle2" ,calibrationAngle [2 ]);
308- editor .putFloat ("prefCalibrationGain0" ,calibrationGain [0 ]);
309- editor .putFloat ("prefCalibrationGain1" ,calibrationGain [1 ]);
310- editor .putFloat ("prefCalibrationGain2" ,calibrationGain [2 ]);
311- editor .putFloat ("prefCalibrationOffset0" ,calibrationOffset [0 ]);
312- editor .putFloat ("prefCalibrationOffset1" ,calibrationOffset [1 ]);
313- editor .putFloat ("prefCalibrationOffset2" ,calibrationOffset [2 ]);
314- editor .putLong ("prefCalibrationTime" ,System .currentTimeMillis ());
383+ editor .putFloat (KEY_PREF_CALIBRATION_ANGLE_0 ,calibrationAngle [0 ]);
384+ editor .putFloat (KEY_PREF_CALIBRATION_ANGLE_1 ,calibrationAngle [1 ]);
385+ editor .putFloat (KEY_PREF_CALIBRATION_ANGLE_2 ,calibrationAngle [2 ]);
386+ editor .putFloat (KEY_PREF_CALIBRATION_GAIN_0 ,calibrationGain [0 ]);
387+ editor .putFloat (KEY_PREF_CALIBRATION_GAIN_1 ,calibrationGain [1 ]);
388+ editor .putFloat (KEY_PREF_CALIBRATION_GAIN_2 ,calibrationGain [2 ]);
389+ editor .putFloat (KEY_PREF_CALIBRATION_OFFSET_0 ,calibrationOffset [0 ]);
390+ editor .putFloat (KEY_PREF_CALIBRATION_OFFSET_1 ,calibrationOffset [1 ]);
391+ editor .putFloat (KEY_PREF_CALIBRATION_OFFSET_2 ,calibrationOffset [2 ]);
392+ editor .putLong (KEY_PREF_CALIBRATION_TIME ,System .currentTimeMillis ());
315393editor .commit ();
316394
317- finish ();
395+ progressBar .setVisibility (View .INVISIBLE );
396+ textViewProgress .setVisibility (View .INVISIBLE );
397+ imageViewMain .setImageBitmap (null );
398+ textViewStepDescription .setText (R .string .dialog_calibration_completed );
399+ textViewLastCalibration .setText (getString (R .string .calibration_active_calibration ) +"\n "
400+ +getString (R .string .calibration_active_calibration_gains )
401+ +String .format (" = %1.3f; %1.3f; %1.3f" ,preferences .getFloat (KEY_PREF_CALIBRATION_GAIN_0 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_GAIN_1 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_GAIN_2 ,0 ))
402+ +"\n "
403+ +getString (R .string .calibration_active_calibration_offsets )
404+ +String .format (" = %1.3f; %1.3f; %1.3f" ,preferences .getFloat (KEY_PREF_CALIBRATION_OFFSET_0 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_OFFSET_1 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_OFFSET_2 ,0 ))
405+ +"\n "
406+ +getString (R .string .calibration_active_calibration_angles )
407+ +String .format (" = %1.2f°; %1.2f°; %1.2f°" ,preferences .getFloat (KEY_PREF_CALIBRATION_ANGLE_2 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_ANGLE_1 ,0 ),preferences .getFloat (KEY_PREF_CALIBRATION_ANGLE_0 ,0 ))
408+ );
409+ textViewLastCalibration .setVisibility (View .VISIBLE );
410+ buttonNext .setVisibility (View .VISIBLE );
411+ buttonNext .setText (R .string .close );
412+ imageViewCalibrationIcon .setVisibility (View .VISIBLE );
318413 }
319414 }
320415