Skip to content

Commit

Permalink
Usability improvements in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
anselm94 committed Nov 20, 2017
1 parent ceb003d commit 5c20013
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

import in.blogspot.anselmbros.torchie.R;
import in.blogspot.anselmbros.torchie.main.manager.device.output.vibrator.Vibrator;
import in.blogspot.anselmbros.torchie.service.TorchieQuick;
import in.blogspot.anselmbros.torchie.ui.activity.MainActivity;
import in.blogspot.anselmbros.torchie.ui.widget.settings.CheckBoxDialogPreference;
import in.blogspot.anselmbros.torchie.utils.SettingsUtils;

import static android.content.Context.SENSOR_SERVICE;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

/**
Expand All @@ -47,6 +53,12 @@ public void onCreate(Bundle savedInstanceState) {
PreferenceManager.getDefaultSharedPreferences(this.getActivity().getApplicationContext()).registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onResume() {
super.onResume();
this.ableSettings();
}

@Override
public void onDestroy() {
PreferenceManager.getDefaultSharedPreferences(this.getActivity().getApplicationContext()).unregisterOnSharedPreferenceChangeListener(this);
Expand Down Expand Up @@ -74,4 +86,26 @@ public void restartApp(Context context) {
}
Runtime.getRuntime().exit(0);
}

private void ableSettings() {
SensorManager mSensorManager = (SensorManager) this.getActivity().getSystemService(SENSOR_SERVICE);
Sensor proximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

boolean isTorchieServiceEnabled = TorchieQuick.getInstance() != null;
boolean isProximitySensorAvailable = (proximitySensor != null);

this.ableCheckboxPref((CheckBoxPreference) getPreferenceScreen().findPreference("pref_screen_on"), isTorchieServiceEnabled);
this.ableCheckboxPref((CheckBoxPreference) getPreferenceScreen().findPreference("pref_screen_lock"), isTorchieServiceEnabled);
this.ableListPref((CheckBoxDialogPreference) getPreferenceScreen().findPreference("pref_screen_off_timeout"), isTorchieServiceEnabled);
this.ableCheckboxPref((CheckBoxPreference) getPreferenceScreen().findPreference("pref_proximity"), isTorchieServiceEnabled && isProximitySensorAvailable);
}

private void ableCheckboxPref(CheckBoxPreference checkBoxPreference, boolean enable) {
checkBoxPreference.setChecked(enable);
checkBoxPreference.setEnabled(enable);
}

private void ableListPref(CheckBoxDialogPreference checkBoxDialogPreference, boolean enable) {
checkBoxDialogPreference.setEnabled(enable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public CheckBoxDialogPreference(Context context) {
protected void onBindView(View view) {
super.onBindView(view);
int value = Integer.parseInt(this.getValue());
CheckBox checkBox = (CheckBox) view.findViewById(R.id.chkbx_pref);
checkBox.setChecked(value == -1 || value > 0);
CheckBox checkBox = view.findViewById(R.id.chkbx_pref);
checkBox.setChecked((value == -1 || value > 0) && isEnabled());
}
}

0 comments on commit 5c20013

Please sign in to comment.