Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 1 | package com.fairphone.psensor; |
| 2 | |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 3 | import android.app.Activity; |
Borjan Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 4 | import android.app.DialogFragment; |
| 5 | import android.content.ComponentName; |
Maarten Derks | 2350570 | 2016-08-03 13:12:29 +0200 | [diff] [blame] | 6 | import android.content.ContentValues; |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 7 | import android.content.Context; |
Borjan Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 8 | import android.content.Intent; |
Maarten Derks | 2350570 | 2016-08-03 13:12:29 +0200 | [diff] [blame] | 9 | import android.content.pm.PackageInfo; |
| 10 | import android.content.pm.PackageManager; |
| 11 | import android.database.sqlite.SQLiteDatabase; |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 12 | import android.os.Bundle; |
| 13 | import android.os.Handler; |
| 14 | import android.os.PowerManager; |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 15 | import android.util.Log; |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 16 | import android.view.LayoutInflater; |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 17 | import android.view.View; |
| 18 | import android.widget.Button; |
| 19 | import android.widget.TextView; |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 20 | import android.widget.ViewFlipper; |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 21 | |
Maarten Derks | 2350570 | 2016-08-03 13:12:29 +0200 | [diff] [blame] | 22 | import com.fairphone.psensor.CalibrationContract.CalibrationData; |
Borjan Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 23 | import com.fairphone.psensor.fragments.IncompatibleDeviceDialog; |
Borjan Tchakaloff | fd38222 | 2016-08-27 16:39:45 +0200 | [diff] [blame] | 24 | import com.fairphone.psensor.helpers.CalibrationStatusHelper; |
| 25 | import com.fairphone.psensor.helpers.ProximitySensorHelper; |
Maarten Derks | 2350570 | 2016-08-03 13:12:29 +0200 | [diff] [blame] | 26 | |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 27 | import java.util.Locale; |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 28 | |
| 29 | /** |
Borjan Tchakaloff | b0446df | 2016-08-25 17:45:53 +0200 | [diff] [blame] | 30 | * 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 Tchakaloff | 84d6787 | 2016-08-25 19:12:43 +0200 | [diff] [blame] | 40 | * 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 Tchakaloff | b0446df | 2016-08-25 17:45:53 +0200 | [diff] [blame] | 43 | * The dynamic offset compensation is computed from the non-blocked value read at step 2.<br> |
Borjan Tchakaloff | 79891ca | 2016-10-18 17:26:14 +0200 | [diff] [blame] | 44 | * The rules and heuristic are as follow: |
Borjan Tchakaloff | b0446df | 2016-08-25 17:45:53 +0200 | [diff] [blame] | 45 | * <ol> |
| 46 | * <li>The read value is reduced by approx. 32 (sensor units) for each offset compensation increment (from the |
| 47 | * specification).</li> |
Borjan Tchakaloff | 79891ca | 2016-10-18 17:26:14 +0200 | [diff] [blame] | 48 | * <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 < 0 read. The non-blocked |
| 52 | * value then belongs to [32;63] in the current conditions.</li> |
Borjan Tchakaloff | b0446df | 2016-08-25 17:45:53 +0200 | [diff] [blame] | 53 | * <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 Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 57 | */ |
Borjan Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 58 | public class CalibrationActivity extends Activity implements IncompatibleDeviceDialog.IncompatibleDeviceDialogListener { |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 59 | private static final String TAG = CalibrationActivity.class.getSimpleName(); |
| 60 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 61 | /* 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 Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 66 | |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 67 | /** |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 68 | * 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 Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 78 | public static final int BLOCKED_MINIMAL_VALUE = 235; |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 79 | /** |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 80 | * Maximal accepted value for the non-blocked value in relation to the read blocked value (in sensor units). |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 81 | */ |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 82 | 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 Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 87 | |
Borjan Tchakaloff | 85869b7 | 2016-08-31 19:17:43 +0200 | [diff] [blame] | 88 | private boolean mAbortActivity; |
| 89 | |
Borjan Tchakaloff | 3e60e16 | 2016-08-04 20:46:27 +0200 | [diff] [blame] | 90 | private ProximitySensorConfiguration mPersistedConfiguration; |
| 91 | private ProximitySensorConfiguration mCalibratedConfiguration; |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 92 | |
Borjan Tchakaloff | b0446df | 2016-08-25 17:45:53 +0200 | [diff] [blame] | 93 | private int mBlockedValue; |
| 94 | private int mNonBlockedValue; |
| 95 | |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 96 | private Handler mHandler; |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 97 | |
| 98 | private ViewFlipper mFlipper; |
| 99 | private View mViewStep1; |
| 100 | private View mViewStep2; |
| 101 | private View mViewStep3; |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 102 | |
Borjan Tchakaloff | fd38222 | 2016-08-27 16:39:45 +0200 | [diff] [blame] | 103 | 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 Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 112 | @Override |
| 113 | protected void onCreate(Bundle savedInstanceState) { |
| 114 | super.onCreate(savedInstanceState); |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 115 | |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 116 | mHandler = new Handler(); |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 117 | |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 118 | setContentView(R.layout.activity_calibration); |
| 119 | |
Borjan Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 120 | if (!ProximitySensorHelper.canReadProximitySensorValue()) { |
| 121 | Log.w(TAG, "Proximity sensor value not read-able, aborting."); |
| 122 | |
Borjan Tchakaloff | 85869b7 | 2016-08-31 19:17:43 +0200 | [diff] [blame] | 123 | mAbortActivity = true; |
Borjan Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 124 | showIncompatibleDeviceDialog(); |
Borjan Tchakaloff | e4e58f1 | 2016-08-26 16:39:43 +0200 | [diff] [blame] | 125 | } else if (!ProximitySensorConfiguration.canReadFromAndPersistToMemory()) { |
| 126 | Log.w(TAG, "Proximity sensor configuration not accessible (R/W), aborting."); |
| 127 | |
Borjan Tchakaloff | 85869b7 | 2016-08-31 19:17:43 +0200 | [diff] [blame] | 128 | mAbortActivity = true; |
Borjan Tchakaloff | e4e58f1 | 2016-08-26 16:39:43 +0200 | [diff] [blame] | 129 | showIncompatibleDeviceDialog(); |
Borjan Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 130 | } else { |
Borjan Tchakaloff | 85869b7 | 2016-08-31 19:17:43 +0200 | [diff] [blame] | 131 | mAbortActivity = false; |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 132 | init(); |
Borjan Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 133 | } |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 134 | } |
Borjan Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 135 | |
Borjan Tchakaloff | fd38222 | 2016-08-27 16:39:45 +0200 | [diff] [blame] | 136 | @Override |
| 137 | protected void onResume() { |
| 138 | super.onResume(); |
| 139 | |
Borjan Tchakaloff | 85869b7 | 2016-08-31 19:17:43 +0200 | [diff] [blame] | 140 | if (mAbortActivity) { |
| 141 | return; |
| 142 | } |
| 143 | |
Borjan Tchakaloff | fd38222 | 2016-08-27 16:39:45 +0200 | [diff] [blame] | 144 | 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 Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 154 | private void init() { |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 155 | mFlipper = (ViewFlipper) findViewById(R.id.viewFlipper); |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 156 | mFlipper.setInAnimation(this, R.anim.slide_in_from_left); |
| 157 | mFlipper.setOutAnimation(this, R.anim.slide_out_to_right); |
| 158 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 159 | 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 Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 163 | |
| 164 | mFlipper.addView(mViewStep1); |
| 165 | mFlipper.addView(mViewStep2); |
| 166 | mFlipper.addView(mViewStep3); |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 167 | } |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 168 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 169 | private void reset() { |
| 170 | mPersistedConfiguration = ProximitySensorConfiguration.readFromMemory(); |
| 171 | mCalibratedConfiguration = new ProximitySensorConfiguration(); |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 172 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 173 | updateCalibrationStepView(mViewStep1, STEP_CURRENT, R.string.step_1, R.string.msg_block, -1, new View.OnClickListener() { |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 174 | @Override |
| 175 | public void onClick(View v) { |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 176 | doReadBlockedValue(); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 177 | } |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 178 | }, R.string.next); |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 179 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 180 | updateCalibrationStepView(mViewStep2, STEP_CURRENT, R.string.step_2, R.string.msg_unblock, -1, new View.OnClickListener() { |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 181 | @Override |
| 182 | public void onClick(View v) { |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 183 | doReadNonBlockedValue(); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 184 | } |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 185 | }, R.string.next); |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 186 | |
Borjan Tchakaloff | fd38222 | 2016-08-27 16:39:45 +0200 | [diff] [blame] | 187 | updateCalibrationStepView(mViewStep3, STEP_CURRENT, R.string.step_3, R.string.msg_cal, -1, actionReboot, R.string.reboot); |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 188 | |
| 189 | mFlipper.setDisplayedChild(0); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 192 | 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 Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 198 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 199 | switch (viewStatus) { |
| 200 | case STEP_CURRENT: |
| 201 | viewErrorNotice.setVisibility(View.GONE); |
| 202 | viewInProgress.setVisibility(View.GONE); |
| 203 | |
| 204 | buttonAction.setEnabled(true); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 205 | break; |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 206 | |
| 207 | case STEP_IN_PROGRESS: |
| 208 | viewErrorNotice.setVisibility(View.GONE); |
| 209 | viewInProgress.setVisibility(View.VISIBLE); |
| 210 | |
| 211 | buttonAction.setEnabled(false); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 212 | break; |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 213 | |
| 214 | case STEP_ERROR: |
| 215 | viewErrorNotice.setVisibility(View.VISIBLE); |
| 216 | viewInProgress.setVisibility(View.GONE); |
| 217 | |
| 218 | buttonAction.setEnabled(true); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 219 | break; |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 220 | |
| 221 | case STEP_OK: |
| 222 | viewErrorNotice.setVisibility(View.GONE); |
| 223 | viewInProgress.setVisibility(View.GONE); |
| 224 | |
| 225 | buttonAction.setEnabled(false); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 226 | break; |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 227 | |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 228 | default: |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 229 | 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 Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 253 | private void updateCalibrationStepView(View stepView, int viewStatus, int instructions) { |
| 254 | updateCalibrationStepView(stepView, viewStatus, -1, instructions, -1, null, -1); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 257 | 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 Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 268 | new Thread(new Runnable() { |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 269 | @Override |
| 270 | public void run() { |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 271 | final int value = ProximitySensorHelper.read(BLOCKED_MINIMAL_VALUE, ProximitySensorHelper.READ_MAX_LIMIT); |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 272 | |
| 273 | mHandler.post(new Runnable() { |
| 274 | @Override |
| 275 | public void run() { |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 276 | Log.d(TAG, " blocked value = " + String.format(Locale.ENGLISH, "%3d", value)); |
Borjan Tchakaloff | fae148b | 2016-08-03 12:53:02 +0200 | [diff] [blame] | 277 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 278 | doSaveBlockedValue(value); |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 279 | } |
| 280 | }); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 281 | } |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 282 | }).start(); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 285 | private void doSaveBlockedValue(int value) { |
| 286 | if (value >= 0) { |
| 287 | mBlockedValue = value; |
Borjan Tchakaloff | b0446df | 2016-08-25 17:45:53 +0200 | [diff] [blame] | 288 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 289 | 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 Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 297 | private void doReadNonBlockedValue() { |
| 298 | updateCalibrationStepView(mViewStep2, STEP_IN_PROGRESS, R.string.msg_reading); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 299 | |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 300 | new Thread(new Runnable() { |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 301 | @Override |
| 302 | public void run() { |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 303 | final int value = ProximitySensorHelper.read(ProximitySensorHelper.READ_MIN_LIMIT, (mBlockedValue - NON_BLOCKED_MAXIMAL_VALUE_FROM_BLOCKED_VALUE)); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 304 | |
Borjan Tchakaloff | e952b32 | 2016-08-02 18:49:59 +0200 | [diff] [blame] | 305 | mHandler.post(new Runnable() { |
| 306 | @Override |
| 307 | public void run() { |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 308 | Log.d(TAG, "non-blocked value = " + String.format(Locale.ENGLISH, "%3d", value)); |
| 309 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 310 | doSaveNonBlockedValue(value); |
Borjan Tchakaloff | e952b32 | 2016-08-02 18:49:59 +0200 | [diff] [blame] | 311 | } |
| 312 | }); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 313 | } |
Dirk Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 314 | }).start(); |
Maarten Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 315 | } |
| 316 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 317 | 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 Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 338 | 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 Tchakaloff | fd38222 | 2016-08-27 16:39:45 +0200 | [diff] [blame] | 347 | CalibrationStatusHelper.setCalibrationSuccessful(this); |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 348 | |
| 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 Vogt | 707feff | 2016-07-21 12:01:03 +0200 | [diff] [blame] | 381 | @Override |
| 382 | protected void onPause() { |
| 383 | UpdateFinalizerService.startActionCheckCalibrationPending(this); |
| 384 | super.onPause(); |
| 385 | } |
| 386 | |
Maarten Derks | 2350570 | 2016-08-03 13:12:29 +0200 | [diff] [blame] | 387 | 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 Tchakaloff | 3e60e16 | 2016-08-04 20:46:27 +0200 | [diff] [blame] | 395 | 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 Derks | 2350570 | 2016-08-03 13:12:29 +0200 | [diff] [blame] | 401 | |
| 402 | PackageInfo pInfo = null; |
| 403 | try { |
| 404 | pInfo = getPackageManager().getPackageInfo(getPackageName(), 0); |
| 405 | } catch (PackageManager.NameNotFoundException e) { |
Borjan Tchakaloff | 1392027 | 2016-08-04 14:57:22 +0200 | [diff] [blame] | 406 | Log.wtf(TAG, e); |
Maarten Derks | 2350570 | 2016-08-03 13:12:29 +0200 | [diff] [blame] | 407 | } |
Maarten Derks | 2350570 | 2016-08-03 13:12:29 +0200 | [diff] [blame] | 408 | |
Borjan Tchakaloff | 7a4e3df | 2016-08-30 16:59:51 +0200 | [diff] [blame] | 409 | 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 Derks | 2350570 | 2016-08-03 13:12:29 +0200 | [diff] [blame] | 414 | |
Borjan Tchakaloff | 7a4e3df | 2016-08-30 16:59:51 +0200 | [diff] [blame] | 415 | // 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 Derks | 2350570 | 2016-08-03 13:12:29 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Borjan Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 422 | 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 Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 429 | 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 Tchakaloff | 426f4b6 | 2016-08-27 16:02:01 +0200 | [diff] [blame] | 436 | startFairphoneUpdaterActivity(); |
Borjan Tchakaloff | 089f671 | 2016-08-25 11:11:51 +0200 | [diff] [blame] | 437 | } |
| 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 Derks | db352f3 | 2016-07-15 17:32:05 +0200 | [diff] [blame] | 448 | } |