blob: b4236eab78b8a37cdce40c9bc52ff79d91b813a3 [file] [log] [blame]
Maarten Derksdb352f32016-07-15 17:32:05 +02001package com.fairphone.psensor;
2
Maarten Derksdb352f32016-07-15 17:32:05 +02003import android.app.Activity;
Borjan Tchakaloff089f6712016-08-25 11:11:51 +02004import android.app.DialogFragment;
5import android.content.ComponentName;
Maarten Derks23505702016-08-03 13:12:29 +02006import android.content.ContentValues;
Maarten Derksdb352f32016-07-15 17:32:05 +02007import android.content.Context;
Borjan Tchakaloff089f6712016-08-25 11:11:51 +02008import android.content.Intent;
Maarten Derks23505702016-08-03 13:12:29 +02009import android.content.pm.PackageInfo;
10import android.content.pm.PackageManager;
11import android.database.sqlite.SQLiteDatabase;
Maarten Derksdb352f32016-07-15 17:32:05 +020012import android.os.Bundle;
13import android.os.Handler;
14import android.os.PowerManager;
Maarten Derksdb352f32016-07-15 17:32:05 +020015import android.util.Log;
Dirk Vogt707feff2016-07-21 12:01:03 +020016import android.view.LayoutInflater;
Maarten Derksdb352f32016-07-15 17:32:05 +020017import android.view.View;
18import android.widget.Button;
19import android.widget.TextView;
Dirk Vogt707feff2016-07-21 12:01:03 +020020import android.widget.ViewFlipper;
Maarten Derksdb352f32016-07-15 17:32:05 +020021
Maarten Derks23505702016-08-03 13:12:29 +020022import com.fairphone.psensor.CalibrationContract.CalibrationData;
Borjan Tchakaloff089f6712016-08-25 11:11:51 +020023import com.fairphone.psensor.fragments.IncompatibleDeviceDialog;
Borjan Tchakalofffd382222016-08-27 16:39:45 +020024import com.fairphone.psensor.helpers.CalibrationStatusHelper;
25import com.fairphone.psensor.helpers.ProximitySensorHelper;
Maarten Derks23505702016-08-03 13:12:29 +020026
Borjan Tchakaloff13920272016-08-04 14:57:22 +020027import java.util.Locale;
Maarten Derksdb352f32016-07-15 17:32:05 +020028
29/**
Borjan Tchakaloffb0446df2016-08-25 17:45:53 +020030 * Activity to start the calibration process.<br>
31 * <br>
32 * The calibration steps are:
33 * <ol>
34 * <li>Ask for a blocked sensor and read the (blocked) value.</li>
35 * <li>Ask for a non-blocked sensor and read the (non-blocked) value.</li>
36 * <li>Compute a new calibration (near and far threshold as well as the offset compensation) and persist it into the
37 * memory.</li>
38 * </ol>
39 * <br>
Borjan Tchakaloff84d67872016-08-25 19:12:43 +020040 * The offset compensation is -was- 0 out of factory and could cause issues because certain devices require a higher
41 * compensation.<br>
42 * <br>
Borjan Tchakaloffb0446df2016-08-25 17:45:53 +020043 * The dynamic offset compensation is computed from the non-blocked value read at step 2.<br>
Borjan Tchakaloff79891ca2016-10-18 17:26:14 +020044 * The rules and heuristic are as follow:
Borjan Tchakaloffb0446df2016-08-25 17:45:53 +020045 * <ol>
46 * <li>The read value is reduced by approx. 32 (sensor units) for each offset compensation increment (from the
47 * specification).</li>
Borjan Tchakaloff79891ca2016-10-18 17:26:14 +020048 * <li>According to the vendor, the value read must be above 0 when non-blocked and as close to is as possible, we
49 * use the integer part of <code>floor("value read"/32)</code> value and decrement it by 1..</li>
50 * <li>By doing so, we take into consideration the current state that might be (and likely is) not perfectly clean.
51 * A cleaner state will result in lower values read, and we do not wish to have values &lt; 0 read. The non-blocked
52 * value then belongs to [32;63] in the current conditions.</li>
Borjan Tchakaloffb0446df2016-08-25 17:45:53 +020053 * <li>If the value read is already 0, we lower the persisted offset by 2 to reach a similar non-blocked range than
54 * above.</li>
55 * <li>The proximity sensor offset compensation belongs to [{@link ProximitySensorConfiguration#MIN_OFFSET_COMPENSATION}, {@link ProximitySensorConfiguration#MAX_OFFSET_COMPENSATION}].</li>
56 * </ol>
Maarten Derksdb352f32016-07-15 17:32:05 +020057 */
Borjan Tchakaloff089f6712016-08-25 11:11:51 +020058public class CalibrationActivity extends Activity implements IncompatibleDeviceDialog.IncompatibleDeviceDialogListener {
Maarten Derksdb352f32016-07-15 17:32:05 +020059 private static final String TAG = CalibrationActivity.class.getSimpleName();
60
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +020061 /* Calibration step status */
62 private static final int STEP_CURRENT = 0;
63 private static final int STEP_IN_PROGRESS = 1;
64 private static final int STEP_ERROR = 2;
65 private static final int STEP_OK = 3;
Maarten Derksdb352f32016-07-15 17:32:05 +020066
Borjan Tchakaloff13920272016-08-04 14:57:22 +020067 /**
Borjan Tchakaloff13920272016-08-04 14:57:22 +020068 * Value to compute the near threshold from the blocked value (in sensor units).
69 */
70 public static final int NEAR_THRESHOLD_FROM_BLOCKED_VALUE = 30;
71 /**
72 * Value to compute the far threshold from the near threshold (in sensor units).
73 */
74 public static final int FAR_THRESHOLD_FROM_NEAR_THRESHOLD = 30;
75 /**
76 * Minimal accepted value for the blocked value (in sensor units).
77 */
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +020078 public static final int BLOCKED_MINIMAL_VALUE = 235;
Borjan Tchakaloff13920272016-08-04 14:57:22 +020079 /**
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +020080 * Maximal accepted value for the non-blocked value in relation to the read blocked value (in sensor units).
Borjan Tchakaloff13920272016-08-04 14:57:22 +020081 */
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +020082 public static final int NON_BLOCKED_MAXIMAL_VALUE_FROM_BLOCKED_VALUE = 5;
83 /**
84 * Delay to emulate a long calibration (in ms).
85 */
86 public static final int CALIBRATION_DELAY_MS = 3000;
Borjan Tchakaloff13920272016-08-04 14:57:22 +020087
Borjan Tchakaloff85869b72016-08-31 19:17:43 +020088 private boolean mAbortActivity;
89
Borjan Tchakaloff3e60e162016-08-04 20:46:27 +020090 private ProximitySensorConfiguration mPersistedConfiguration;
91 private ProximitySensorConfiguration mCalibratedConfiguration;
Borjan Tchakaloff13920272016-08-04 14:57:22 +020092
Borjan Tchakaloffb0446df2016-08-25 17:45:53 +020093 private int mBlockedValue;
94 private int mNonBlockedValue;
95
Maarten Derksdb352f32016-07-15 17:32:05 +020096 private Handler mHandler;
Borjan Tchakaloff13920272016-08-04 14:57:22 +020097
98 private ViewFlipper mFlipper;
99 private View mViewStep1;
100 private View mViewStep2;
101 private View mViewStep3;
Maarten Derksdb352f32016-07-15 17:32:05 +0200102
Borjan Tchakalofffd382222016-08-27 16:39:45 +0200103 private final View.OnClickListener actionReboot = new View.OnClickListener() {
104 @Override
105 public void onClick(View v) {
106 final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
107 powerManager.reboot(null);
108 }
109
110 };
111
Maarten Derksdb352f32016-07-15 17:32:05 +0200112 @Override
113 protected void onCreate(Bundle savedInstanceState) {
114 super.onCreate(savedInstanceState);
Dirk Vogt707feff2016-07-21 12:01:03 +0200115
Borjan Tchakaloff13920272016-08-04 14:57:22 +0200116 mHandler = new Handler();
Dirk Vogt707feff2016-07-21 12:01:03 +0200117
Maarten Derksdb352f32016-07-15 17:32:05 +0200118 setContentView(R.layout.activity_calibration);
119
Borjan Tchakaloff089f6712016-08-25 11:11:51 +0200120 if (!ProximitySensorHelper.canReadProximitySensorValue()) {
121 Log.w(TAG, "Proximity sensor value not read-able, aborting.");
122
Borjan Tchakaloff85869b72016-08-31 19:17:43 +0200123 mAbortActivity = true;
Borjan Tchakaloff089f6712016-08-25 11:11:51 +0200124 showIncompatibleDeviceDialog();
Borjan Tchakaloffe4e58f12016-08-26 16:39:43 +0200125 } else if (!ProximitySensorConfiguration.canReadFromAndPersistToMemory()) {
126 Log.w(TAG, "Proximity sensor configuration not accessible (R/W), aborting.");
127
Borjan Tchakaloff85869b72016-08-31 19:17:43 +0200128 mAbortActivity = true;
Borjan Tchakaloffe4e58f12016-08-26 16:39:43 +0200129 showIncompatibleDeviceDialog();
Borjan Tchakaloff089f6712016-08-25 11:11:51 +0200130 } else {
Borjan Tchakaloff85869b72016-08-31 19:17:43 +0200131 mAbortActivity = false;
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200132 init();
Borjan Tchakaloff089f6712016-08-25 11:11:51 +0200133 }
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200134 }
Borjan Tchakaloff089f6712016-08-25 11:11:51 +0200135
Borjan Tchakalofffd382222016-08-27 16:39:45 +0200136 @Override
137 protected void onResume() {
138 super.onResume();
139
Borjan Tchakaloff85869b72016-08-31 19:17:43 +0200140 if (mAbortActivity) {
141 return;
142 }
143
Borjan Tchakalofffd382222016-08-27 16:39:45 +0200144 if (CalibrationStatusHelper.isCalibrationPending(this)) {
145 updateCalibrationStepView(mViewStep3, STEP_CURRENT, R.string.step_3, R.string.msg_calibration_success, -1, actionReboot, R.string.reboot);
146 if (mFlipper.getDisplayedChild() != 2) {
147 mFlipper.setDisplayedChild(2);
148 }
149 } else {
150 reset();
151 }
152 }
153
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200154 private void init() {
Dirk Vogt707feff2016-07-21 12:01:03 +0200155 mFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
Dirk Vogt707feff2016-07-21 12:01:03 +0200156 mFlipper.setInAnimation(this, R.anim.slide_in_from_left);
157 mFlipper.setOutAnimation(this, R.anim.slide_out_to_right);
158
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200159 final LayoutInflater inflater = LayoutInflater.from(this);
160 mViewStep1 = inflater.inflate(R.layout.view_calibration_step, mFlipper, false);
161 mViewStep2 = inflater.inflate(R.layout.view_calibration_step, mFlipper, false);
162 mViewStep3 = inflater.inflate(R.layout.view_calibration_step, mFlipper, false);
Dirk Vogt707feff2016-07-21 12:01:03 +0200163
164 mFlipper.addView(mViewStep1);
165 mFlipper.addView(mViewStep2);
166 mFlipper.addView(mViewStep3);
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200167 }
Dirk Vogt707feff2016-07-21 12:01:03 +0200168
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200169 private void reset() {
170 mPersistedConfiguration = ProximitySensorConfiguration.readFromMemory();
171 mCalibratedConfiguration = new ProximitySensorConfiguration();
Dirk Vogt707feff2016-07-21 12:01:03 +0200172
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200173 updateCalibrationStepView(mViewStep1, STEP_CURRENT, R.string.step_1, R.string.msg_block, -1, new View.OnClickListener() {
Maarten Derksdb352f32016-07-15 17:32:05 +0200174 @Override
175 public void onClick(View v) {
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200176 doReadBlockedValue();
Maarten Derksdb352f32016-07-15 17:32:05 +0200177 }
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200178 }, R.string.next);
Borjan Tchakaloff13920272016-08-04 14:57:22 +0200179
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200180 updateCalibrationStepView(mViewStep2, STEP_CURRENT, R.string.step_2, R.string.msg_unblock, -1, new View.OnClickListener() {
Maarten Derksdb352f32016-07-15 17:32:05 +0200181 @Override
182 public void onClick(View v) {
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200183 doReadNonBlockedValue();
Maarten Derksdb352f32016-07-15 17:32:05 +0200184 }
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200185 }, R.string.next);
Dirk Vogt707feff2016-07-21 12:01:03 +0200186
Borjan Tchakalofffd382222016-08-27 16:39:45 +0200187 updateCalibrationStepView(mViewStep3, STEP_CURRENT, R.string.step_3, R.string.msg_cal, -1, actionReboot, R.string.reboot);
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200188
189 mFlipper.setDisplayedChild(0);
Maarten Derksdb352f32016-07-15 17:32:05 +0200190 }
191
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200192 private void updateCalibrationStepView(View stepView, int viewStatus, int title, int instructions, int errorNotice, View.OnClickListener action, int actionLabel) {
193 final TextView viewTitle = (TextView) stepView.findViewById(R.id.current_step);
194 final TextView viewInstructions = (TextView) stepView.findViewById(R.id.instructions);
195 final TextView viewErrorNotice = (TextView) stepView.findViewById(R.id.error_notice);
196 final View viewInProgress = stepView.findViewById(R.id.progress_bar);
197 final Button buttonAction = (Button) stepView.findViewById(R.id.button);
Maarten Derksdb352f32016-07-15 17:32:05 +0200198
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200199 switch (viewStatus) {
200 case STEP_CURRENT:
201 viewErrorNotice.setVisibility(View.GONE);
202 viewInProgress.setVisibility(View.GONE);
203
204 buttonAction.setEnabled(true);
Maarten Derksdb352f32016-07-15 17:32:05 +0200205 break;
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200206
207 case STEP_IN_PROGRESS:
208 viewErrorNotice.setVisibility(View.GONE);
209 viewInProgress.setVisibility(View.VISIBLE);
210
211 buttonAction.setEnabled(false);
Maarten Derksdb352f32016-07-15 17:32:05 +0200212 break;
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200213
214 case STEP_ERROR:
215 viewErrorNotice.setVisibility(View.VISIBLE);
216 viewInProgress.setVisibility(View.GONE);
217
218 buttonAction.setEnabled(true);
Maarten Derksdb352f32016-07-15 17:32:05 +0200219 break;
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200220
221 case STEP_OK:
222 viewErrorNotice.setVisibility(View.GONE);
223 viewInProgress.setVisibility(View.GONE);
224
225 buttonAction.setEnabled(false);
Maarten Derksdb352f32016-07-15 17:32:05 +0200226 break;
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200227
Maarten Derksdb352f32016-07-15 17:32:05 +0200228 default:
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200229 Log.wtf(TAG, "Unknown calibration step reached: " + viewStatus);
230 }
231
232 if (title != -1) {
233 viewTitle.setText(title);
234 }
235
236 if (instructions != -1) {
237 viewInstructions.setText(instructions);
238 }
239
240 if (errorNotice != -1) {
241 viewErrorNotice.setText(errorNotice);
242 }
243
244 if (action != null) {
245 buttonAction.setOnClickListener(action);
246 }
247
248 if (actionLabel != -1) {
249 buttonAction.setText(actionLabel);
Maarten Derksdb352f32016-07-15 17:32:05 +0200250 }
251 }
252
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200253 private void updateCalibrationStepView(View stepView, int viewStatus, int instructions) {
254 updateCalibrationStepView(stepView, viewStatus, -1, instructions, -1, null, -1);
Maarten Derksdb352f32016-07-15 17:32:05 +0200255 }
256
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200257 private void updateCalibrationStepView(View stepView, int viewStatus, int instructions, int errorNotice) {
258 updateCalibrationStepView(stepView, viewStatus, -1, instructions, errorNotice, null, -1);
259 }
260
261 private void updateCalibrationStepView(View stepView, int viewStatus, int instructions, int errorNotice, View.OnClickListener action, int actionLabel) {
262 updateCalibrationStepView(stepView, viewStatus, -1, instructions, errorNotice, action, actionLabel);
263 }
264
265 private void doReadBlockedValue() {
266 updateCalibrationStepView(mViewStep1, STEP_IN_PROGRESS, R.string.msg_reading);
267
Dirk Vogt707feff2016-07-21 12:01:03 +0200268 new Thread(new Runnable() {
Maarten Derksdb352f32016-07-15 17:32:05 +0200269 @Override
270 public void run() {
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200271 final int value = ProximitySensorHelper.read(BLOCKED_MINIMAL_VALUE, ProximitySensorHelper.READ_MAX_LIMIT);
Dirk Vogt707feff2016-07-21 12:01:03 +0200272
273 mHandler.post(new Runnable() {
274 @Override
275 public void run() {
Borjan Tchakaloff13920272016-08-04 14:57:22 +0200276 Log.d(TAG, " blocked value = " + String.format(Locale.ENGLISH, "%3d", value));
Borjan Tchakalofffae148b2016-08-03 12:53:02 +0200277
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200278 doSaveBlockedValue(value);
Dirk Vogt707feff2016-07-21 12:01:03 +0200279 }
280 });
Maarten Derksdb352f32016-07-15 17:32:05 +0200281 }
Dirk Vogt707feff2016-07-21 12:01:03 +0200282 }).start();
Maarten Derksdb352f32016-07-15 17:32:05 +0200283 }
284
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200285 private void doSaveBlockedValue(int value) {
286 if (value >= 0) {
287 mBlockedValue = value;
Borjan Tchakaloffb0446df2016-08-25 17:45:53 +0200288
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200289 updateCalibrationStepView(mViewStep1, STEP_OK, R.string.msg_step_success);
290 mFlipper.setDisplayedChild(1);
291 } else {
292 updateCalibrationStepView(mViewStep1, STEP_ERROR, R.string.msg_block, R.string.msg_fail_block);
293 mFlipper.setDisplayedChild(0);
294 }
Maarten Derksdb352f32016-07-15 17:32:05 +0200295 }
296
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200297 private void doReadNonBlockedValue() {
298 updateCalibrationStepView(mViewStep2, STEP_IN_PROGRESS, R.string.msg_reading);
Maarten Derksdb352f32016-07-15 17:32:05 +0200299
Dirk Vogt707feff2016-07-21 12:01:03 +0200300 new Thread(new Runnable() {
Maarten Derksdb352f32016-07-15 17:32:05 +0200301 @Override
302 public void run() {
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200303 final int value = ProximitySensorHelper.read(ProximitySensorHelper.READ_MIN_LIMIT, (mBlockedValue - NON_BLOCKED_MAXIMAL_VALUE_FROM_BLOCKED_VALUE));
Maarten Derksdb352f32016-07-15 17:32:05 +0200304
Borjan Tchakaloffe952b322016-08-02 18:49:59 +0200305 mHandler.post(new Runnable() {
306 @Override
307 public void run() {
Borjan Tchakaloff13920272016-08-04 14:57:22 +0200308 Log.d(TAG, "non-blocked value = " + String.format(Locale.ENGLISH, "%3d", value));
309
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200310 doSaveNonBlockedValue(value);
Borjan Tchakaloffe952b322016-08-02 18:49:59 +0200311 }
312 });
Maarten Derksdb352f32016-07-15 17:32:05 +0200313 }
Dirk Vogt707feff2016-07-21 12:01:03 +0200314 }).start();
Maarten Derksdb352f32016-07-15 17:32:05 +0200315 }
316
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200317 private void doSaveNonBlockedValue(int value) {
318 if (value >= 0) {
319 mNonBlockedValue = value;
320
321 updateCalibrationStepView(mViewStep2, STEP_OK, R.string.msg_step_success);
322 mFlipper.setDisplayedChild(2);
323
324 doCalibrate();
325 } else {
326 updateCalibrationStepView(mViewStep2, STEP_ERROR, R.string.msg_unblock, R.string.msg_fail_unlock);
327 mFlipper.setDisplayedChild(1);
328 }
329 }
330
331 public void doCalibrate() {
332 updateCalibrationStepView(mViewStep3, STEP_IN_PROGRESS, R.string.msg_cal);
333
334 mCalibratedConfiguration.nearThreshold = mBlockedValue - NEAR_THRESHOLD_FROM_BLOCKED_VALUE;
335 mCalibratedConfiguration.farThreshold = mCalibratedConfiguration.nearThreshold - FAR_THRESHOLD_FROM_NEAR_THRESHOLD;
336
337 if (mNonBlockedValue == 0) {
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200338 mCalibratedConfiguration.offsetCompensation = Math.min(Math.max(mPersistedConfiguration.offsetCompensation - 2, ProximitySensorConfiguration.MIN_OFFSET_COMPENSATION), ProximitySensorConfiguration.MAX_OFFSET_COMPENSATION);
339 Log.d(TAG, "New offset based on current offset only");
340 } else {
341 mCalibratedConfiguration.offsetCompensation = Math.min(Math.max(mPersistedConfiguration.offsetCompensation + (int)Math.floor(mNonBlockedValue / 32) - 1, ProximitySensorConfiguration.MIN_OFFSET_COMPENSATION), ProximitySensorConfiguration.MAX_OFFSET_COMPENSATION);
342 Log.d(TAG, "New offset based on unblock value and current offset");
343 }
344
345 if (mCalibratedConfiguration.persistToMemory()) {
346 storeCalibrationData();
Borjan Tchakalofffd382222016-08-27 16:39:45 +0200347 CalibrationStatusHelper.setCalibrationSuccessful(this);
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200348
349 // wait a bit because the calibration is otherwise too fast
350 new Thread(new Runnable() {
351 @Override
352 public void run() {
353 try {
354 Thread.sleep(CALIBRATION_DELAY_MS);
355 } catch (InterruptedException e) {
356 // Log but ignore interruption.
357 Log.e(TAG, e.getMessage());
358 }
359
360 mHandler.post(new Runnable() {
361 @Override
362 public void run() {
363 updateCalibrationStepView(mViewStep3, STEP_CURRENT, R.string.msg_calibration_success);
364 mFlipper.setDisplayedChild(2);
365 }
366 });
367 }
368 }).start();
369 } else {
370 updateCalibrationStepView(mViewStep3, STEP_ERROR, R.string.msg_cal, R.string.msg_fail_write_sns, new View.OnClickListener() {
371 @Override
372 public void onClick(View v) {
373 reset();
374 startFairphoneUpdaterActivity();
375 }
376 }, R.string.go_to_updater);
377 }
378
379 }
380
Dirk Vogt707feff2016-07-21 12:01:03 +0200381 @Override
382 protected void onPause() {
383 UpdateFinalizerService.startActionCheckCalibrationPending(this);
384 super.onPause();
385 }
386
Maarten Derks23505702016-08-03 13:12:29 +0200387 private void storeCalibrationData() {
388 CalibrationDbHelper mDbHelper = new CalibrationDbHelper(this);
389
390 // Gets the data repository in write mode
391 SQLiteDatabase db = mDbHelper.getWritableDatabase();
392
393 // Create a new map of values, where column names are the keys
394 ContentValues values = new ContentValues();
Borjan Tchakaloff3e60e162016-08-04 20:46:27 +0200395 values.put(CalibrationData.COLUMN_NAME_PREVIOUS_NEAR, mPersistedConfiguration.nearThreshold);
396 values.put(CalibrationData.COLUMN_NAME_PREVIOUS_FAR, mPersistedConfiguration.farThreshold);
397 values.put(CalibrationData.COLUMN_NAME_PREVIOUS_OFFSET, mPersistedConfiguration.offsetCompensation);
398 values.put(CalibrationData.COLUMN_NAME_NEAR, mCalibratedConfiguration.nearThreshold);
399 values.put(CalibrationData.COLUMN_NAME_FAR, mCalibratedConfiguration.farThreshold);
400 values.put(CalibrationData.COLUMN_NAME_OFFSET, mCalibratedConfiguration.offsetCompensation);
Maarten Derks23505702016-08-03 13:12:29 +0200401
402 PackageInfo pInfo = null;
403 try {
404 pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
405 } catch (PackageManager.NameNotFoundException e) {
Borjan Tchakaloff13920272016-08-04 14:57:22 +0200406 Log.wtf(TAG, e);
Maarten Derks23505702016-08-03 13:12:29 +0200407 }
Maarten Derks23505702016-08-03 13:12:29 +0200408
Borjan Tchakaloff7a4e3df2016-08-30 16:59:51 +0200409 if (pInfo == null) {
410 Log.wtf(TAG, "Could not retrieve PackageInfo instance.");
411 } else {
412 int verCode = pInfo.versionCode;
413 values.put(CalibrationData.COLUMN_NAME_APP_VERSION, verCode);
Maarten Derks23505702016-08-03 13:12:29 +0200414
Borjan Tchakaloff7a4e3df2016-08-30 16:59:51 +0200415 // Insert the new row, returning the primary key value of the new row
416 if (db.insert(CalibrationData.TABLE_NAME, null, values) == -1) {
417 Log.wtf(TAG, "Could not insert calibration data into database.");
418 }
419 }
Maarten Derks23505702016-08-03 13:12:29 +0200420 }
421
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200422 private void startFairphoneUpdaterActivity() {
423 final Intent intent = new Intent();
424 intent.setComponent(new ComponentName(getString(R.string.package_fairphone_updater), getString(R.string.activity_fairphone_updater_check_for_updates)));
425 intent.setFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME | Intent.FLAG_ACTIVITY_NEW_TASK);
426 startActivity(intent);
427 }
428
Borjan Tchakaloff089f6712016-08-25 11:11:51 +0200429 private void showIncompatibleDeviceDialog() {
430 final DialogFragment dialog = new IncompatibleDeviceDialog();
431 dialog.show(getFragmentManager(), getString(R.string.fragment_tag_incompatible_device_dialog));
432 }
433
434 @Override
435 public void onIncompatibleDeviceDialogPositiveAction(DialogFragment dialog) {
Borjan Tchakaloff426f4b62016-08-27 16:02:01 +0200436 startFairphoneUpdaterActivity();
Borjan Tchakaloff089f6712016-08-25 11:11:51 +0200437 }
438
439 @Override
440 public void onIncompatibleDeviceDialogNegativeAction(DialogFragment dialog) {
441 // fall-through
442 }
443
444 @Override
445 public void onDismissIncompatibleDeviceDialog(DialogFragment dialog) {
446 finish();
447 }
Maarten Derksdb352f32016-07-15 17:32:05 +0200448}