diff --git a/parts/res/layout/popup_calibration_warning.xml b/parts/res/layout/popup_calibration_warning.xml new file mode 100644 index 0000000..c807d60 --- /dev/null +++ b/parts/res/layout/popup_calibration_warning.xml @@ -0,0 +1,28 @@ + + + + + + diff --git a/parts/res/values/strings.xml b/parts/res/values/strings.xml index 239e8a4..e2f017b 100644 --- a/parts/res/values/strings.xml +++ b/parts/res/values/strings.xml @@ -28,6 +28,9 @@ Cabin door Calibration Calibrate the popup camera motor + Motor calibration + Unnecessary use of calibration may cause permanent damage. It is recommended to calibrate the popup camera motor only in case of it not moving properly or it being in incorrect position! + Do not show again Warning diff --git a/parts/src/org/lineageos/settings/popupcamera/PopupCameraSettingsFragment.java b/parts/src/org/lineageos/settings/popupcamera/PopupCameraSettingsFragment.java index 37e5fd8..12dca95 100644 --- a/parts/src/org/lineageos/settings/popupcamera/PopupCameraSettingsFragment.java +++ b/parts/src/org/lineageos/settings/popupcamera/PopupCameraSettingsFragment.java @@ -16,8 +16,16 @@ package org.lineageos.settings.popupcamera; +import android.app.AlertDialog; +import android.app.Activity; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.SharedPreferences; import android.os.Bundle; import android.view.MenuItem; +import android.view.View; +import android.widget.CompoundButton; +import android.widget.CheckBox; import androidx.preference.Preference; import androidx.preference.Preference.OnPreferenceChangeListener; @@ -59,9 +67,48 @@ public class PopupCameraSettingsFragment extends PreferenceFragment @Override public boolean onPreferenceClick(Preference preference) { if (MOTOR_CALIBRATION_KEY.equals(preference.getKey())) { - mPopupCameraService.calibrateMotor(); + SharedPreferences prefs = getActivity().getSharedPreferences( + MOTOR_CALIBRATION_KEY, Activity.MODE_PRIVATE); + if (!prefs.getBoolean("popup_calibration_warning_hidden", false)) { + MotorCalibrationWarningDialog fragment = new MotorCalibrationWarningDialog(); + fragment.show(getFragmentManager(), "motor_calibration_warning_dialog"); + } else { + mPopupCameraService.calibrateMotor(); + } return true; } return false; } + + private class MotorCalibrationWarningDialog extends DialogFragment { + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + View view = getActivity().getLayoutInflater().inflate( + R.layout.popup_calibration_warning, null); + CheckBox hideDialog = (CheckBox) view.findViewById(R.id.popup_calibration_warning_hide); + + hideDialog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { + getActivity() + .getSharedPreferences(MOTOR_CALIBRATION_KEY, Activity.MODE_PRIVATE) + .edit() + .putBoolean("popup_calibration_warning_hidden", isChecked) + .commit(); + } + }); + + return new AlertDialog.Builder(getActivity()) + .setTitle(R.string.popup_calibration_warning_title) + .setMessage(R.string.popup_calibration_warning_text) + .setView(view) + .setPositiveButton(R.string.popup_camera_calibrate_now, + (dialog, which) -> { + mPopupCameraService.calibrateMotor(); + dialog.cancel(); + }) + .setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.cancel()) + .create(); + } + } }