2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Christian
|
2017-03-10 14:53:19 +01:00
|
|
|
Fischer, Daniele Gobbetti, Lem Dulfo
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2015-05-07 01:30:40 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
2017-07-31 22:49:05 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2015-05-07 01:30:40 +02:00
|
|
|
import android.os.Bundle;
|
2016-04-25 23:51:58 +02:00
|
|
|
import android.preference.EditTextPreference;
|
2015-05-07 01:30:40 +02:00
|
|
|
import android.preference.ListPreference;
|
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceManager;
|
2016-04-25 23:51:58 +02:00
|
|
|
import android.text.InputType;
|
2015-05-07 01:30:40 +02:00
|
|
|
import android.view.MenuItem;
|
|
|
|
|
2020-08-26 04:34:41 +02:00
|
|
|
import androidx.core.app.NavUtils;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2017-07-31 22:49:05 +02:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2016-04-14 15:34:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2021-12-05 10:32:35 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
|
2017-07-31 22:49:05 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
2016-04-14 15:34:53 +02:00
|
|
|
|
2015-05-10 21:12:10 +02:00
|
|
|
/**
|
|
|
|
* A settings activity with support for preferences directly displaying their value.
|
|
|
|
* If you combine such preferences with a custom OnPreferenceChangeListener, you have
|
|
|
|
* to set that listener in #onCreate, *not* in #onPostCreate, otherwise the value will
|
|
|
|
* not be displayed.
|
2023-07-27 00:08:13 +02:00
|
|
|
*
|
|
|
|
* @deprecated use AbstractSettingsActivityV2
|
2015-05-10 21:12:10 +02:00
|
|
|
*/
|
2023-07-27 00:08:13 +02:00
|
|
|
@Deprecated
|
2017-09-03 01:02:31 +02:00
|
|
|
public abstract class AbstractSettingsActivity extends AppCompatPreferenceActivity implements GBActivity {
|
2015-05-07 01:30:40 +02:00
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AbstractSettingsActivity.class);
|
2015-05-07 01:30:40 +02:00
|
|
|
|
2017-09-07 23:26:24 +02:00
|
|
|
private boolean isLanguageInvalid = false;
|
|
|
|
|
2017-07-31 22:49:05 +02:00
|
|
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
switch (action) {
|
|
|
|
case GBApplication.ACTION_LANGUAGE_CHANGE:
|
2017-09-03 01:02:31 +02:00
|
|
|
setLanguage(GBApplication.getLanguage(), true);
|
2017-07-31 22:49:05 +02:00
|
|
|
break;
|
|
|
|
case GBApplication.ACTION_QUIT:
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-05-07 01:30:40 +02:00
|
|
|
/**
|
|
|
|
* A preference value change listener that updates the preference's summary
|
|
|
|
* to reflect its new value.
|
|
|
|
*/
|
2015-05-10 21:12:10 +02:00
|
|
|
private static class SimpleSetSummaryOnChangeListener implements Preference.OnPreferenceChangeListener {
|
2015-05-07 01:30:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object value) {
|
2016-04-25 23:51:58 +02:00
|
|
|
if (preference instanceof EditTextPreference) {
|
2016-07-05 23:14:48 +02:00
|
|
|
if ((((EditTextPreference) preference).getEditText().getKeyListener().getInputType() & InputType.TYPE_CLASS_NUMBER) != 0) {
|
2016-04-25 23:51:58 +02:00
|
|
|
if ("".equals(String.valueOf(value))) {
|
|
|
|
// reject empty numeric input
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-10 21:12:10 +02:00
|
|
|
updateSummary(preference, value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateSummary(Preference preference, Object value) {
|
2016-04-02 16:08:36 +02:00
|
|
|
String stringValue = String.valueOf(value);
|
2015-05-07 01:30:40 +02:00
|
|
|
|
|
|
|
if (preference instanceof ListPreference) {
|
|
|
|
// For list preferences, look up the correct display value in
|
|
|
|
// the preference's 'entries' list.
|
|
|
|
ListPreference listPreference = (ListPreference) preference;
|
|
|
|
int index = listPreference.findIndexOfValue(stringValue);
|
|
|
|
|
|
|
|
// Set the summary to reflect the new value.
|
|
|
|
preference.setSummary(
|
|
|
|
index >= 0
|
|
|
|
? listPreference.getEntries()[index]
|
|
|
|
: null);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// For all other preferences, set the summary to the value's
|
|
|
|
// simple string representation.
|
|
|
|
preference.setSummary(stringValue);
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 20:09:35 +02:00
|
|
|
}
|
2015-05-07 01:30:40 +02:00
|
|
|
|
2015-05-10 21:12:10 +02:00
|
|
|
private static class ExtraSetSummaryOnChangeListener extends SimpleSetSummaryOnChangeListener {
|
2016-04-09 05:41:31 +02:00
|
|
|
private final Preference.OnPreferenceChangeListener prefChangeListener;
|
2015-05-10 21:12:10 +02:00
|
|
|
|
2016-04-09 05:41:31 +02:00
|
|
|
public ExtraSetSummaryOnChangeListener(Preference.OnPreferenceChangeListener prefChangeListener) {
|
|
|
|
this.prefChangeListener = prefChangeListener;
|
2015-05-10 21:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object value) {
|
2016-04-09 05:41:31 +02:00
|
|
|
boolean result = prefChangeListener.onPreferenceChange(preference, value);
|
2015-05-10 21:12:10 +02:00
|
|
|
if (result) {
|
|
|
|
return super.onPreferenceChange(preference, value);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private static final SimpleSetSummaryOnChangeListener sBindPreferenceSummaryToValueListener = new SimpleSetSummaryOnChangeListener();
|
2015-05-10 21:12:10 +02:00
|
|
|
|
2016-04-09 05:41:31 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2017-09-03 01:02:31 +02:00
|
|
|
AbstractGBActivity.init(this);
|
2017-07-31 22:49:05 +02:00
|
|
|
|
|
|
|
IntentFilter filterLocal = new IntentFilter();
|
|
|
|
filterLocal.addAction(GBApplication.ACTION_QUIT);
|
|
|
|
filterLocal.addAction(GBApplication.ACTION_LANGUAGE_CHANGE);
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal);
|
|
|
|
|
2016-04-09 05:41:31 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
}
|
|
|
|
|
2015-05-10 21:12:10 +02:00
|
|
|
@Override
|
|
|
|
protected void onPostCreate(Bundle savedInstanceState) {
|
|
|
|
super.onPostCreate(savedInstanceState);
|
|
|
|
|
|
|
|
for (String prefKey : getPreferenceKeysWithSummary()) {
|
|
|
|
final Preference pref = findPreference(prefKey);
|
|
|
|
if (pref != null) {
|
|
|
|
bindPreferenceSummaryToValue(pref);
|
|
|
|
} else {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.error("Unknown preference key: " + prefKey + ", unable to display value.");
|
2015-05-10 21:12:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:26:24 +02:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (isLanguageInvalid) {
|
|
|
|
isLanguageInvalid = false;
|
|
|
|
recreate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 22:49:05 +02:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2015-05-10 21:12:10 +02:00
|
|
|
/**
|
|
|
|
* Subclasses should reimplement this to return the keys of those
|
|
|
|
* preferences which should print its values as a summary below the
|
|
|
|
* preference name.
|
|
|
|
*/
|
|
|
|
protected String[] getPreferenceKeysWithSummary() {
|
|
|
|
return new String[0];
|
|
|
|
}
|
|
|
|
|
2015-05-07 01:30:40 +02:00
|
|
|
/**
|
|
|
|
* Binds a preference's summary to its value. More specifically, when the
|
|
|
|
* preference's value is changed, its summary (line of text below the
|
|
|
|
* preference title) is updated to reflect the value. The summary is also
|
|
|
|
* immediately updated upon calling this method. The exact display format is
|
|
|
|
* dependent on the type of preference.
|
|
|
|
*
|
|
|
|
* @see #sBindPreferenceSummaryToValueListener
|
|
|
|
*/
|
|
|
|
private static void bindPreferenceSummaryToValue(Preference preference) {
|
|
|
|
// Set the listener to watch for value changes.
|
2020-08-26 04:28:03 +02:00
|
|
|
SimpleSetSummaryOnChangeListener listener;
|
2015-05-10 21:12:10 +02:00
|
|
|
Preference.OnPreferenceChangeListener existingListener = preference.getOnPreferenceChangeListener();
|
|
|
|
if (existingListener != null) {
|
|
|
|
listener = new ExtraSetSummaryOnChangeListener(existingListener);
|
|
|
|
} else {
|
|
|
|
listener = sBindPreferenceSummaryToValueListener;
|
|
|
|
}
|
|
|
|
preference.setOnPreferenceChangeListener(listener);
|
2015-05-07 01:30:40 +02:00
|
|
|
|
2015-05-10 21:12:10 +02:00
|
|
|
// Trigger the listener immediately with the preference's current value.
|
2015-09-07 12:06:56 +02:00
|
|
|
try {
|
|
|
|
listener.updateSummary(preference,
|
|
|
|
PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(preference.getContext())
|
|
|
|
.getString(preference.getKey(), ""));
|
|
|
|
} catch (ClassCastException cce) {
|
|
|
|
//the preference is not a string, use the provided summary
|
|
|
|
//TODO: it shows true/false instead of the xml summary
|
|
|
|
listener.updateSummary(preference, preference.getSummary());
|
|
|
|
}
|
2015-05-07 01:30:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2017-07-31 22:49:05 +02:00
|
|
|
|
2017-09-07 23:26:24 +02:00
|
|
|
public void setLanguage(Locale language, boolean invalidateLanguage) {
|
|
|
|
if (invalidateLanguage) {
|
|
|
|
isLanguageInvalid = true;
|
|
|
|
}
|
|
|
|
AndroidUtils.setLanguage(this, language);
|
2017-07-31 22:49:05 +02:00
|
|
|
}
|
2021-05-27 23:11:00 +02:00
|
|
|
|
|
|
|
protected void addPreferenceHandlerFor(final String preferenceKey) {
|
|
|
|
Preference pref = findPreference(preferenceKey);
|
|
|
|
if (pref != null) {
|
|
|
|
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newVal) {
|
|
|
|
GBApplication.deviceService().onSendConfiguration(preferenceKey);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
LOG.warn("Could not find preference " + preferenceKey);
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 01:30:40 +02:00
|
|
|
}
|