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-10-30 20:41:59 +11:00
|
|
|
import android.support.annotation.StringRes;
|
|
|
|
import android.support.annotation.XmlRes;
|
|
|
|
import android.support.v7.app.ActionBar;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.view.MenuItem;
|
2017-02-05 18:34:55 +11:00
|
|
|
|
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 {
|
2017-10-30 20:41:59 +11:00
|
|
|
|
2017-02-17 05:52:55 +11:00
|
|
|
private boolean themeSwitched;
|
2017-10-30 20:41:59 +11:00
|
|
|
private @XmlRes int currentPreferences;
|
|
|
|
private @StringRes int currentTitle;
|
2017-02-17 05:52:55 +11:00
|
|
|
|
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);
|
|
|
|
}
|
2017-10-30 20:41:59 +11:00
|
|
|
|
|
|
|
setContentView(R.layout.activity_preferences);
|
|
|
|
|
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setDisplayShowHomeEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-17 05:52:55 +11:00
|
|
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
|
2017-10-30 20:41:59 +11:00
|
|
|
if(savedInstanceState == null) {
|
|
|
|
currentPreferences = R.xml.preferences;
|
|
|
|
currentTitle = R.string.action_view_preferences;
|
|
|
|
} else {
|
|
|
|
currentPreferences = savedInstanceState.getInt("preferences");
|
|
|
|
currentTitle = savedInstanceState.getInt("title");
|
|
|
|
}
|
|
|
|
showFragment(currentPreferences, currentTitle);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showFragment(@XmlRes int preferenceId, @StringRes int title) {
|
|
|
|
|
|
|
|
//TODO: cache the Fragments so they can be reused
|
2017-02-05 18:34:55 +11:00
|
|
|
getFragmentManager().beginTransaction()
|
2017-10-30 20:41:59 +11:00
|
|
|
.replace(R.id.fragment_container, PreferencesFragment.newInstance(preferenceId))
|
2017-02-05 18:34:55 +11:00
|
|
|
.commit();
|
2017-10-19 07:18:07 +11:00
|
|
|
|
2017-10-30 20:41:59 +11:00
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setTitle(title);
|
|
|
|
}
|
2017-10-19 07:18:07 +11:00
|
|
|
|
2017-10-30 20:41:59 +11:00
|
|
|
currentPreferences = preferenceId;
|
|
|
|
currentTitle = title;
|
2017-02-05 18:34:55 +11:00
|
|
|
}
|
2017-02-17 05:52:55 +11:00
|
|
|
|
|
|
|
private void saveInstanceState(Bundle outState) {
|
|
|
|
outState.putBoolean("themeSwitched", themeSwitched);
|
2017-10-30 20:41:59 +11:00
|
|
|
outState.putInt("preferences", currentPreferences);
|
|
|
|
outState.putInt("title", currentTitle);
|
2017-02-17 05:52:55 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
saveInstanceState(outState);
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
2017-07-09 10:59:48 +10:00
|
|
|
switch (key) {
|
|
|
|
case "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);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "notificationsEnabled": {
|
|
|
|
boolean enabled = sharedPreferences.getBoolean("notificationsEnabled", true);
|
|
|
|
if (enabled) {
|
|
|
|
enablePushNotifications();
|
|
|
|
} else {
|
|
|
|
disablePushNotifications();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "pullNotificationCheckInterval": {
|
|
|
|
String s = sharedPreferences.getString("pullNotificationCheckInterval", "15");
|
|
|
|
long minutes = Long.valueOf(s);
|
|
|
|
setPullNotificationCheckInterval(minutes);
|
|
|
|
break;
|
2017-03-16 09:45:59 +11:00
|
|
|
}
|
2017-02-17 05:52:55 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
2017-10-30 20:41:59 +11:00
|
|
|
//if we are not on the top level, show the top level. Else exit the activity
|
|
|
|
if(currentPreferences != R.xml.preferences) {
|
|
|
|
showFragment(R.xml.preferences, R.string.action_view_preferences);
|
|
|
|
|
|
|
|
} else {
|
2017-02-17 05:52:55 +11:00
|
|
|
/* 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. */
|
2017-10-30 20:41:59 +11:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home: {
|
|
|
|
onBackPressed();
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-17 05:52:55 +11:00
|
|
|
}
|
2017-10-30 20:41:59 +11:00
|
|
|
return super.onOptionsItemSelected(item);
|
2017-02-17 05:52:55 +11:00
|
|
|
}
|
2017-02-05 18:34:55 +11:00
|
|
|
}
|