2017-02-05 18:34:55 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* This file is a part of Tusky.
|
2017-02-05 18:34:55 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* This program 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.
|
2017-02-05 18:34:55 +11:00
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-04-10 10:12:31 +10:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
2017-02-05 18:34:55 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2017-02-05 18:34:55 +11:00
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
2017-02-17 05:52:55 +11:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2017-02-05 18:34:55 +11:00
|
|
|
import android.os.Bundle;
|
2017-02-17 05:52:55 +11:00
|
|
|
import android.preference.PreferenceManager;
|
2017-02-05 18:34:55 +11:00
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.fragment.PreferencesFragment;
|
|
|
|
|
2017-03-10 05:31:15 +11:00
|
|
|
public class PreferencesActivity extends BaseActivity
|
2017-02-17 05:52:55 +11:00
|
|
|
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
|
|
|
private boolean themeSwitched;
|
|
|
|
|
2017-02-05 18:34:55 +11:00
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2017-02-17 05:52:55 +11:00
|
|
|
if (savedInstanceState != null) {
|
|
|
|
themeSwitched = savedInstanceState.getBoolean("themeSwitched");
|
|
|
|
} else {
|
|
|
|
Bundle extras = getIntent().getExtras();
|
2017-02-23 06:13:51 +11:00
|
|
|
themeSwitched = extras != null && extras.getBoolean("themeSwitched");
|
2017-02-17 05:52:55 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
if (preferences.getBoolean("lightTheme", false)) {
|
|
|
|
setTheme(R.style.AppTheme_Light);
|
|
|
|
}
|
|
|
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
|
2017-02-05 18:34:55 +11:00
|
|
|
getFragmentManager().beginTransaction()
|
|
|
|
.replace(android.R.id.content, new PreferencesFragment())
|
|
|
|
.commit();
|
|
|
|
}
|
2017-02-17 05:52:55 +11:00
|
|
|
|
|
|
|
private void saveInstanceState(Bundle outState) {
|
|
|
|
outState.putBoolean("themeSwitched", themeSwitched);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
saveInstanceState(outState);
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
|
|
|
if (key.equals("lightTheme")) {
|
|
|
|
themeSwitched = true;
|
|
|
|
// recreate() could be used instead, but it doesn't have an animation B).
|
|
|
|
Intent intent = getIntent();
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
Bundle savedInstanceState = new Bundle();
|
|
|
|
saveInstanceState(savedInstanceState);
|
|
|
|
intent.putExtras(savedInstanceState);
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
|
|
|
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
|
2017-03-16 09:45:59 +11:00
|
|
|
} else if (key.equals("notificationsEnabled")) {
|
|
|
|
boolean notificationsEnabled = sharedPreferences.getBoolean("notificationsEnabled", true);
|
|
|
|
|
|
|
|
if (notificationsEnabled) {
|
|
|
|
enablePushNotifications();
|
|
|
|
} else {
|
|
|
|
disablePushNotifications();
|
|
|
|
}
|
2017-02-17 05:52:55 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
/* Switching themes won't actually change the theme of activities on the back stack.
|
|
|
|
* Either the back stack activities need to all be recreated, or do the easier thing, which
|
|
|
|
* is hijack the back button press and use it to launch a new MainActivity and clear the
|
|
|
|
* back stack. */
|
|
|
|
if (themeSwitched) {
|
|
|
|
Intent intent = new Intent(this, MainActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
|
|
startActivity(intent);
|
|
|
|
} else {
|
|
|
|
super.onBackPressed();
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 18:34:55 +11:00
|
|
|
}
|