mirror of https://github.com/iSoron/uhabits.git
parent
d97f94075d
commit
b88b3a683d
@ -1,337 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* This file is part of Loop Habit Tracker.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker 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.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker 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.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License along
|
|
||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.isoron.uhabits.preferences;
|
|
||||||
|
|
||||||
import android.content.*;
|
|
||||||
import android.preference.*;
|
|
||||||
|
|
||||||
import org.isoron.androidbase.*;
|
|
||||||
import org.isoron.uhabits.*;
|
|
||||||
import org.isoron.uhabits.core.*;
|
|
||||||
import org.isoron.uhabits.core.models.*;
|
|
||||||
import org.isoron.uhabits.core.preferences.*;
|
|
||||||
import org.isoron.uhabits.core.ui.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import javax.inject.*;
|
|
||||||
|
|
||||||
@AppScope
|
|
||||||
public class AndroidPreferences
|
|
||||||
implements SharedPreferences.OnSharedPreferenceChangeListener, Preferences
|
|
||||||
{
|
|
||||||
private final Context context;
|
|
||||||
|
|
||||||
private SharedPreferences prefs;
|
|
||||||
|
|
||||||
private Boolean shouldReverseCheckmarks = null;
|
|
||||||
|
|
||||||
private LinkedList<Listener> listeners;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public AndroidPreferences(@AppContext Context context)
|
|
||||||
{
|
|
||||||
this.context = context;
|
|
||||||
listeners = new LinkedList<>();
|
|
||||||
|
|
||||||
prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
||||||
prefs.registerOnSharedPreferenceChangeListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addListener(Listener listener)
|
|
||||||
{
|
|
||||||
listeners.add(listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getDefaultHabitColor(int fallbackColor)
|
|
||||||
{
|
|
||||||
return prefs.getInt("pref_default_habit_palette_color", fallbackColor);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HabitList.Order getDefaultOrder()
|
|
||||||
{
|
|
||||||
String name = prefs.getString("pref_default_order", "BY_POSITION");
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return HabitList.Order.valueOf(name);
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException e)
|
|
||||||
{
|
|
||||||
setDefaultOrder(HabitList.Order.BY_POSITION);
|
|
||||||
return HabitList.Order.BY_POSITION;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reset()
|
|
||||||
{
|
|
||||||
if(!HabitsApplication.isTestMode()) throw new IllegalStateException(
|
|
||||||
"this method can only be used while testing");
|
|
||||||
prefs.edit().clear().commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDefaultOrder(HabitList.Order order)
|
|
||||||
{
|
|
||||||
prefs.edit().putString("pref_default_order", order.name()).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getDefaultScoreSpinnerPosition()
|
|
||||||
{
|
|
||||||
int defaultScoreInterval = prefs.getInt("pref_score_view_interval", 1);
|
|
||||||
if (defaultScoreInterval > 5 || defaultScoreInterval < 0)
|
|
||||||
defaultScoreInterval = 1;
|
|
||||||
return defaultScoreInterval;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDefaultScoreSpinnerPosition(int position)
|
|
||||||
{
|
|
||||||
prefs.edit().putInt("pref_score_view_interval", position).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the number of the last hint shown to the user.
|
|
||||||
*
|
|
||||||
* @return number of last hint shown
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getLastHintNumber()
|
|
||||||
{
|
|
||||||
return prefs.getInt("last_hint_number", -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the time when the last hint was shown to the user.
|
|
||||||
*
|
|
||||||
* @return timestamp of the day the last hint was shown
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public long getLastHintTimestamp()
|
|
||||||
{
|
|
||||||
return prefs.getLong("last_hint_timestamp", -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getLastSync()
|
|
||||||
{
|
|
||||||
return prefs.getLong("last_sync", 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLastSync(long timestamp)
|
|
||||||
{
|
|
||||||
prefs.edit().putLong("last_sync", timestamp).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean getShowArchived()
|
|
||||||
{
|
|
||||||
return prefs.getBoolean("pref_show_archived", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setShowArchived(boolean showArchived)
|
|
||||||
{
|
|
||||||
prefs.edit().putBoolean("pref_show_archived", showArchived).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean getShowCompleted()
|
|
||||||
{
|
|
||||||
return prefs.getBoolean("pref_show_completed", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setShowCompleted(boolean showCompleted)
|
|
||||||
{
|
|
||||||
prefs.edit().putBoolean("pref_show_completed", showCompleted).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getSnoozeInterval()
|
|
||||||
{
|
|
||||||
return Long.parseLong(prefs.getString("pref_snooze_interval", "15"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSyncAddress()
|
|
||||||
{
|
|
||||||
return prefs.getString("pref_sync_address", "https://sync.loophabits.org:4000");
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSyncClientId()
|
|
||||||
{
|
|
||||||
String id = prefs.getString("pref_sync_client_id", "");
|
|
||||||
if (!id.isEmpty()) return id;
|
|
||||||
|
|
||||||
id = UUID.randomUUID().toString();
|
|
||||||
prefs.edit().putString("pref_sync_client_id", id).apply();
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSyncKey()
|
|
||||||
{
|
|
||||||
return prefs.getString("pref_sync_key", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getTheme()
|
|
||||||
{
|
|
||||||
return prefs.getInt("pref_theme", ThemeSwitcher.THEME_LIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setTheme(int theme)
|
|
||||||
{
|
|
||||||
prefs.edit().putInt("pref_theme", theme).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void incrementLaunchCount()
|
|
||||||
{
|
|
||||||
int count = prefs.getInt("launch_count", 0);
|
|
||||||
prefs.edit().putInt("launch_count", count + 1).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initialize()
|
|
||||||
{
|
|
||||||
PreferenceManager.setDefaultValues(context, R.xml.preferences, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDeveloper()
|
|
||||||
{
|
|
||||||
return prefs.getBoolean("pref_developer", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setDeveloper(boolean isDeveloper)
|
|
||||||
{
|
|
||||||
prefs.edit().putBoolean("pref_developer", isDeveloper).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isFirstRun()
|
|
||||||
{
|
|
||||||
return prefs.getBoolean("pref_first_run", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setFirstRun(boolean isFirstRun)
|
|
||||||
{
|
|
||||||
prefs.edit().putBoolean("pref_first_run", isFirstRun).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isNumericalHabitsFeatureEnabled()
|
|
||||||
{
|
|
||||||
return prefs.getBoolean("pref_feature_numerical_habits", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isPureBlackEnabled()
|
|
||||||
{
|
|
||||||
return prefs.getBoolean("pref_pure_black", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isShortToggleEnabled()
|
|
||||||
{
|
|
||||||
return prefs.getBoolean("pref_short_toggle", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSyncFeatureEnabled()
|
|
||||||
{
|
|
||||||
return prefs.getBoolean("pref_feature_sync", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
|
|
||||||
String key)
|
|
||||||
{
|
|
||||||
if (key.equals("pref_checkmark_reverse_order"))
|
|
||||||
{
|
|
||||||
shouldReverseCheckmarks = null;
|
|
||||||
for (Listener l : listeners) l.onCheckmarkOrderChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key.equals("pref_sticky_notifications"))
|
|
||||||
for (Listener l : listeners) l.onNotificationsChanged();
|
|
||||||
|
|
||||||
if (key.equals("pref_feature_sync"))
|
|
||||||
for (Listener l : listeners) l.onSyncFeatureChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeListener(Listener listener)
|
|
||||||
{
|
|
||||||
listeners.remove(listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDefaultHabitColor(int color)
|
|
||||||
{
|
|
||||||
prefs.edit().putInt("pref_default_habit_palette_color", color).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setShouldReverseCheckmarks(boolean reverse)
|
|
||||||
{
|
|
||||||
shouldReverseCheckmarks = null;
|
|
||||||
prefs
|
|
||||||
.edit()
|
|
||||||
.putBoolean("pref_checkmark_reverse_order", reverse)
|
|
||||||
.apply();
|
|
||||||
|
|
||||||
for (Listener l : listeners) l.onCheckmarkOrderChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean shouldMakeNotificationsSticky()
|
|
||||||
{
|
|
||||||
return prefs.getBoolean("pref_sticky_notifications", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean shouldReverseCheckmarks()
|
|
||||||
{
|
|
||||||
if (shouldReverseCheckmarks == null) shouldReverseCheckmarks =
|
|
||||||
prefs.getBoolean("pref_checkmark_reverse_order", false);
|
|
||||||
|
|
||||||
return shouldReverseCheckmarks;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateLastAppVersion()
|
|
||||||
{
|
|
||||||
prefs.edit().putInt("last_version", BuildConfig.VERSION_CODE).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the last hint shown to the user, and the time that it was shown.
|
|
||||||
*
|
|
||||||
* @param number number of the last hint shown
|
|
||||||
* @param timestamp timestamp for the day the last hint was shown
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void updateLastHint(int number, long timestamp)
|
|
||||||
{
|
|
||||||
prefs
|
|
||||||
.edit()
|
|
||||||
.putInt("last_hint_number", number)
|
|
||||||
.putLong("last_hint_timestamp", timestamp)
|
|
||||||
.apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface Listener
|
|
||||||
{
|
|
||||||
default void onCheckmarkOrderChanged() {}
|
|
||||||
|
|
||||||
default void onNotificationsChanged() {}
|
|
||||||
|
|
||||||
default void onSyncFeatureChanged() {}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,139 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.preferences;
|
||||||
|
|
||||||
|
import android.content.*;
|
||||||
|
import android.preference.*;
|
||||||
|
import android.support.annotation.*;
|
||||||
|
|
||||||
|
import org.isoron.androidbase.*;
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.core.*;
|
||||||
|
import org.isoron.uhabits.core.preferences.*;
|
||||||
|
|
||||||
|
import javax.inject.*;
|
||||||
|
|
||||||
|
@AppScope
|
||||||
|
public class SharedPreferencesStorage
|
||||||
|
implements SharedPreferences.OnSharedPreferenceChangeListener,
|
||||||
|
Preferences.Storage
|
||||||
|
{
|
||||||
|
@NonNull
|
||||||
|
private SharedPreferences sharedPrefs;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Preferences preferences;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public SharedPreferencesStorage(@AppContext Context context)
|
||||||
|
{
|
||||||
|
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
sharedPrefs.registerOnSharedPreferenceChangeListener(this);
|
||||||
|
PreferenceManager.setDefaultValues(context, R.xml.preferences, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear()
|
||||||
|
{
|
||||||
|
sharedPrefs.edit().clear().apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(String key, boolean defValue)
|
||||||
|
{
|
||||||
|
return sharedPrefs.getBoolean(key, defValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInt(String key, int defValue)
|
||||||
|
{
|
||||||
|
return sharedPrefs.getInt(key, defValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getLong(String key, int defValue)
|
||||||
|
{
|
||||||
|
return sharedPrefs.getLong(key, defValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getString(String key, String defValue)
|
||||||
|
{
|
||||||
|
return sharedPrefs.getString(key, defValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttached(Preferences preferences)
|
||||||
|
{
|
||||||
|
this.preferences = preferences;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
|
||||||
|
String key)
|
||||||
|
{
|
||||||
|
if(preferences == null) return;
|
||||||
|
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
|
||||||
|
switch (key)
|
||||||
|
{
|
||||||
|
case "pref_checkmark_reverse_order":
|
||||||
|
preferences.setShouldReverseCheckmarks(getBoolean(key, false));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "pref_sticky_notifications":
|
||||||
|
preferences.setNotificationsSticky(getBoolean(key, false));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "pref_feature_sync":
|
||||||
|
preferences.setSyncEnabled(getBoolean(key, false));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putBoolean(String key, boolean value)
|
||||||
|
{
|
||||||
|
sharedPrefs.edit().putBoolean(key, value).apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putInt(String key, int value)
|
||||||
|
{
|
||||||
|
sharedPrefs.edit().putInt(key, value).apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putLong(String key, long value)
|
||||||
|
{
|
||||||
|
sharedPrefs.edit().putLong(key, value).apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putString(String key, String value)
|
||||||
|
{
|
||||||
|
sharedPrefs.edit().putString(key, value).apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(String key)
|
||||||
|
{
|
||||||
|
sharedPrefs.edit().remove(key).apply();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue