Skip to content

Commit

Permalink
check for physical activity permission only on SKD29+
Browse files Browse the repository at this point in the history
handle preference display correctly
enabled actions after permission request result received
  • Loading branch information
sleep-yearning committed Jul 12, 2021
1 parent 3e31dde commit 75cea05
Showing 1 changed file with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

Expand Down Expand Up @@ -76,6 +75,8 @@ public class PreferencesActivity extends AppCompatPreferenceActivity {
static int REQUEST_LOCATION = 1;
static int REQUEST_ACTIVITY = 3;

private GeneralPreferenceFragment generalPreferenceFragment;

/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
Expand Down Expand Up @@ -236,12 +237,18 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
if (requestCode == REQUEST_ACTIVITY){
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//TODO switch step counting (call function which is also called on general pref switching)
if (generalPreferenceFragment !=null){
generalPreferenceFragment.saveStepsAndRestartService();
generalPreferenceFragment.checkHardwareStepUse(true);
}
} else {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(getString(R.string.pref_use_step_hardware), false);
editor.apply();
if (generalPreferenceFragment !=null){
generalPreferenceFragment.checkHardwareStepUse(false);
}
}
}

Expand All @@ -268,6 +275,8 @@ public void onCreate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);

((PreferencesActivity) getActivity()).generalPreferenceFragment = this;

// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
Expand All @@ -294,17 +303,6 @@ public void onCreate(Bundle savedInstanceState) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
sharedPref.registerOnSharedPreferenceChangeListener(this);

/*
if (AndroidVersionHelper.supportsStepDetector(getActivity().getPackageManager()) && sharedPref.getBoolean(getString(R.string.pref_use_step_hardware), false) ) {
// hide accelerometer threshold if hardware detection is used.
PreferenceScreen screen = getPreferenceScreen();
ListPreference accelerometerThresholdPref = (ListPreference) findPreference(getString(R.string.pref_accelerometer_threshold));
EditTextPreference accelerometerStepsThresholdPref = (EditTextPreference) findPreference(getString(R.string.pref_accelerometer_steps_threshold));
screen.removePreference(accelerometerThresholdPref);
screen.removePreference(accelerometerStepsThresholdPref);
}
*/

stepCounterEnabledPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
Expand All @@ -325,15 +323,21 @@ public boolean onPreferenceClick(Preference preference) {
if(enable) {

if (AndroidVersionHelper.supportsStepDetector(getActivity().getApplicationContext().getPackageManager())) {
if(verifyActivityPermissions(getActivity())){
return true;

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if(verifyActivityPermissions(getActivity())) {
checkHardwareStepUse(true);
}
}
return true;

} else {
Toast.makeText(getActivity(), R.string.pref_use_step_hardware_not_available, Toast.LENGTH_SHORT).show();
checkHardwareStepUse(false);
return false;
}
}

checkHardwareStepUse(false);
return false;
}
});
Expand All @@ -350,6 +354,11 @@ public boolean onPreferenceClick(Preference preference) {
});
}

private void checkHardwareStepUse(Boolean checked) {
final SwitchPreference hardwarePreference = (SwitchPreference) findPreference(getString(R.string.pref_use_step_hardware));
if (hardwarePreference!=null) hardwarePreference.setChecked(checked);
}

private void saveStepsAndRestartService() {
final Context context = getActivity().getApplicationContext();

Expand Down

0 comments on commit 75cea05

Please sign in to comment.