Skip to content

Instantly share code, notes, and snippets.

@prvn2004
Last active April 30, 2023 10:10
Show Gist options
  • Save prvn2004/1a2e35ca5cc70bad00b4ec1f91a5a86c to your computer and use it in GitHub Desktop.
Save prvn2004/1a2e35ca5cc70bad00b4ec1f91a5a86c to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Location Privacy">
<EditTextPreference
android:key="accuracy"
android:title="Accuracy"
android:summary="Set location accuracy in meters"
android:inputType="numberDecimal"
android:maxLength="3"
android:defaultValue="100"
android:max="100"/>
<EditTextPreference
android:key="interval"
android:title="Interval"
android:summary="Set location update interval in seconds"
android:inputType="number"
android:maxLength="3"
android:defaultValue="0"
/>
</PreferenceCategory>
</PreferenceScreen>
/**
* Copyright (C) 2013 - 2021 the enviroCar community
* <p>
* This file is part of the enviroCar app.
* <p>
* The enviroCar app is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* The enviroCar app is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License along
* with the enviroCar app. If not, see http://www.gnu.org/licenses/.
*/
package org.envirocar.app.views.settings;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.preference.CheckBoxPreference;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import org.envirocar.app.R;
import org.envirocar.app.handler.ApplicationSettings;
import org.envirocar.app.recording.RecordingType;
import org.envirocar.app.views.settings.custom.AutoConnectIntervalPreference;
import org.envirocar.app.views.settings.custom.GPSConnectionDurationPreference;
import org.envirocar.app.views.settings.custom.GPSTrimDurationPreference;
import org.envirocar.app.views.settings.custom.SamplingRatePreference;
import org.envirocar.app.views.settings.custom.TimePickerPreferenceDialog;
import de.fh.muenster.locationprivacytoolkit.LocationPrivacyConfig;
import de.fh.muenster.locationprivacytoolkit.LocationPrivacyConfigKey;
import de.fh.muenster.locationprivacytoolkit.LocationPrivacyToolkit;
/**
* @author dewall
*/
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// add the settingsfragment
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.activity_settings_content, new LocationPrivacySettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragmentCompat {
private Preference automaticRecording;
private Preference searchInterval;
private Preference enableGPSMode;
private Preference gpsTrimDuration;
private Preference gpsAutoRecording;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.settings);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// find all preferences
this.automaticRecording = findPreference(getString(R.string.prefkey_automatic_recording));
this.searchInterval = findPreference(getString(R.string.prefkey_search_interval));
this.enableGPSMode = findPreference(getString(R.string.prefkey_enable_gps_based_track_recording));
this.gpsTrimDuration = findPreference(getString(R.string.prefkey_track_trim_duration));
this.gpsAutoRecording = findPreference(getString(R.string.prefkey_gps_mode_ar));
// set initial state
this.searchInterval.setVisible(((CheckBoxPreference) automaticRecording).isChecked());
this.gpsTrimDuration.setVisible(((CheckBoxPreference) enableGPSMode).isChecked());
this.gpsAutoRecording.setVisible(((CheckBoxPreference) enableGPSMode).isChecked());
// set preference change listener
this.automaticRecording.setOnPreferenceChangeListener((preference, newValue) -> {
searchInterval.setVisible((boolean) newValue);
return true;
});
this.enableGPSMode.setOnPreferenceChangeListener(((preference, newValue) -> {
if (!(boolean) newValue) {
ApplicationSettings.setSelectedRecordingType(getContext(), RecordingType.OBD_ADAPTER_BASED);
}
gpsTrimDuration.setVisible((boolean) newValue);
gpsAutoRecording.setVisible((boolean) newValue);
return true;
}));
}
@Override
public void onDisplayPreferenceDialog(Preference preference) {
DialogFragment fragment = null;
if (preference instanceof AutoConnectIntervalPreference) {
fragment = TimePickerPreferenceDialog.newInstance(preference.getKey());
} else if (preference instanceof SamplingRatePreference) {
fragment = SamplingRatePreference.Dialog.newInstance(preference.getKey());
} else if (preference instanceof GPSTrimDurationPreference) {
fragment = TimePickerPreferenceDialog.newInstance(preference.getKey());
} else if (preference instanceof GPSConnectionDurationPreference) {
fragment = TimePickerPreferenceDialog.newInstance(preference.getKey());
}
if (fragment != null) {
fragment.setTargetFragment(this, 0);
fragment.show(this.getFragmentManager(), "android.support.v7.preference.PreferenceFragment.DIALOG");
} else {
super.onDisplayPreferenceDialog(preference);
}
}
}
public static class LocationPrivacySettingsFragment extends PreferenceFragmentCompat {
private LocationPrivacyConfig locationPrivacyConfig;
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) {
case "accuracy": {
String accuracy = sharedPreferences.getString(key, "");
locationPrivacyConfig.setPrivacyConfig(LocationPrivacyConfigKey.accuracy, Integer.parseInt(accuracy));
int maxDist = locationPrivacyConfig.getPrivacyConfig(LocationPrivacyConfigKey.accuracy);
Log.d("log_detection", "config_accuracy : " + String.valueOf(maxDist));
break;
}
case "interval": {
String interval = sharedPreferences.getString(key, "");
locationPrivacyConfig.setPrivacyConfig(LocationPrivacyConfigKey.interval, Integer.parseInt(interval));
break;
}
}
}
};
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.location_privacy_preferences, rootKey);
locationPrivacyConfig = new LocationPrivacyConfig(requireContext());
}
@Override
public void onResume() {
super.onResume();
SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
sharedPreferences.registerOnSharedPreferenceChangeListener(listener);
}
@Override
public void onPause() {
super.onPause();
SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment