2017-02-05 18:34:55 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is part of Tusky.
|
|
|
|
*
|
|
|
|
* Tusky 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
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;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
|
2017-02-17 05:52:55 +11:00
|
|
|
public class PreferencesActivity extends AppCompatActivity
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|