2016-07-29 14:00:28 +02:00
|
|
|
/*
|
|
|
|
This file is part of the Diaspora for Android.
|
|
|
|
Diaspora for Android 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.
|
|
|
|
Diaspora for Android 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 the Diaspora for Android.
|
|
|
|
If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-06-09 22:21:49 +02:00
|
|
|
package com.github.dfa.diaspora_android.activity;
|
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.app.AlarmManager;
|
2016-06-18 21:45:23 +02:00
|
|
|
import android.app.AlertDialog;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
2016-06-18 21:45:23 +02:00
|
|
|
import android.content.DialogInterface;
|
2016-06-16 21:18:42 +02:00
|
|
|
import android.content.Intent;
|
2016-06-09 22:21:49 +02:00
|
|
|
import android.content.SharedPreferences;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.os.Build;
|
2016-06-09 22:21:49 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.EditTextPreference;
|
|
|
|
import android.preference.ListPreference;
|
|
|
|
import android.preference.Preference;
|
2016-08-13 19:54:16 +02:00
|
|
|
import android.preference.PreferenceFragment;
|
2016-06-18 15:53:49 +02:00
|
|
|
import android.preference.PreferenceScreen;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.support.design.widget.AppBarLayout;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.view.LayoutInflater;
|
2016-09-05 18:15:29 +02:00
|
|
|
import android.view.MenuItem;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.Window;
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
import android.widget.TextView;
|
2016-06-09 22:21:49 +02:00
|
|
|
|
2016-06-11 00:28:26 +02:00
|
|
|
import com.github.dfa.diaspora_android.App;
|
2016-06-09 22:21:49 +02:00
|
|
|
import com.github.dfa.diaspora_android.R;
|
2016-09-19 01:10:29 +02:00
|
|
|
import com.github.dfa.diaspora_android.data.AppSettings;
|
2016-10-13 16:56:31 +02:00
|
|
|
import com.github.dfa.diaspora_android.ui.IntellihideToolbarActivityListener;
|
2016-09-19 01:10:29 +02:00
|
|
|
import com.github.dfa.diaspora_android.util.AppLog;
|
2016-10-07 02:43:12 +02:00
|
|
|
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
|
2016-10-14 00:44:23 +02:00
|
|
|
import com.github.dfa.diaspora_android.util.ProxyHandler;
|
|
|
|
import com.github.dfa.diaspora_android.util.theming.ColorPalette;
|
2016-10-13 16:56:31 +02:00
|
|
|
import com.github.dfa.diaspora_android.util.theming.ThemeHelper;
|
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
import uz.shift.colorpicker.LineColorPicker;
|
|
|
|
import uz.shift.colorpicker.OnColorChangedListener;
|
2016-06-09 22:21:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author vanitas
|
|
|
|
*/
|
2016-10-13 16:56:31 +02:00
|
|
|
public class SettingsActivity extends ThemedActivity implements IntellihideToolbarActivityListener {
|
|
|
|
@BindView(R.id.settings__appbar)
|
|
|
|
protected AppBarLayout appBarLayout;
|
|
|
|
|
|
|
|
@BindView(R.id.settings__toolbar)
|
|
|
|
protected Toolbar toolbar;
|
|
|
|
|
|
|
|
private ProxyHandler.ProxySettings oldProxySettings;
|
2016-06-16 21:18:42 +02:00
|
|
|
|
|
|
|
|
2016-06-09 22:21:49 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2016-10-13 16:56:31 +02:00
|
|
|
setContentView(R.layout.settings__activity);
|
|
|
|
ButterKnife.bind(this);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24px));
|
|
|
|
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
SettingsActivity.this.onBackPressed();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
oldProxySettings = getAppSettings().getProxySettings();
|
|
|
|
getFragmentManager().beginTransaction().replace(R.id.settings__fragment_container, new SettingsFragment()).commit();
|
2016-06-11 00:28:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 18:15:29 +02:00
|
|
|
@Override
|
2016-09-19 01:10:29 +02:00
|
|
|
public boolean onOptionsItemSelected(MenuItem menuItem) {
|
2016-09-05 18:15:29 +02:00
|
|
|
switch (menuItem.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
onBackPressed();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(menuItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
@Override
|
|
|
|
protected void applyColorToViews() {
|
|
|
|
ThemeHelper.updateToolbarColor(toolbar);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void enableToolbarHiding() {
|
|
|
|
AppLog.d(this, "Enable Intellihide");
|
|
|
|
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
|
|
|
|
//scroll|enterAlways|snap
|
|
|
|
params.setScrollFlags(toolbarDefaultScrollFlags);
|
|
|
|
appBarLayout.setExpanded(true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void disableToolbarHiding() {
|
|
|
|
AppLog.d(this, "Disable Intellihide");
|
|
|
|
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
|
|
|
|
params.setScrollFlags(0); // clear all scroll flags
|
|
|
|
appBarLayout.setExpanded(true, true);
|
2016-06-09 22:21:49 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 19:54:16 +02:00
|
|
|
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
|
|
|
private SharedPreferences sharedPreferences;
|
2016-06-09 22:21:49 +02:00
|
|
|
|
2016-08-13 19:54:16 +02:00
|
|
|
public void onCreate(Bundle savedInstances) {
|
|
|
|
super.onCreate(savedInstances);
|
|
|
|
getPreferenceManager().setSharedPreferencesName("app");
|
|
|
|
addPreferencesFromResource(R.xml.preferences);
|
|
|
|
sharedPreferences = getPreferenceScreen().getSharedPreferences();
|
|
|
|
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
setPreferenceSummaries();
|
|
|
|
sharedPreferences.edit().putBoolean(getString(R.string.pref_key__proxy_was_enabled),
|
2016-10-13 16:56:31 +02:00
|
|
|
sharedPreferences.getBoolean(getString(R.string.pref_key__http_proxy_enabled), false)).apply();
|
2016-08-02 21:32:42 +02:00
|
|
|
}
|
2016-08-13 19:54:16 +02:00
|
|
|
|
|
|
|
private void setPreferenceSummaries() {
|
|
|
|
String[] editTextKeys = new String[]{
|
2016-10-13 16:56:31 +02:00
|
|
|
getString(R.string.pref_key__http_proxy_host), getString(R.string.pref_key__http_proxy_port)
|
2016-08-13 19:54:16 +02:00
|
|
|
};
|
|
|
|
for (String key : editTextKeys) {
|
|
|
|
EditTextPreference p = (EditTextPreference) findPreference(key);
|
|
|
|
p.setSummary(p.getText());
|
|
|
|
}
|
2016-06-09 22:21:49 +02:00
|
|
|
}
|
2016-08-13 19:54:16 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
|
|
|
updatePreference(findPreference(key));
|
2016-10-14 12:32:55 +02:00
|
|
|
if(isAdded()) {
|
|
|
|
if (key.equals(getString(R.string.pref_key__intellihide_toolbars))) {
|
|
|
|
if (sharedPreferences.getBoolean(getString(R.string.pref_key__intellihide_toolbars), false)) {
|
|
|
|
((SettingsActivity) getActivity()).enableToolbarHiding();
|
|
|
|
} else {
|
|
|
|
((SettingsActivity) getActivity()).disableToolbarHiding();
|
|
|
|
}
|
2016-10-13 16:56:31 +02:00
|
|
|
}
|
2016-08-14 18:23:23 +02:00
|
|
|
}
|
2016-06-09 22:21:49 +02:00
|
|
|
}
|
2016-06-18 15:53:49 +02:00
|
|
|
|
2016-08-13 19:54:16 +02:00
|
|
|
private void updatePreference(Preference preference) {
|
|
|
|
if (preference == null) {
|
|
|
|
return;
|
2016-08-02 21:32:42 +02:00
|
|
|
}
|
2016-08-13 19:54:16 +02:00
|
|
|
if (preference instanceof EditTextPreference) {
|
|
|
|
EditTextPreference textPref = (EditTextPreference) preference;
|
|
|
|
textPref.setSummary(textPref.getText());
|
|
|
|
return;
|
2016-08-02 21:32:42 +02:00
|
|
|
}
|
2016-08-13 19:54:16 +02:00
|
|
|
if (preference instanceof ListPreference) {
|
|
|
|
ListPreference listPref = (ListPreference) preference;
|
|
|
|
listPref.setSummary(listPref.getEntry());
|
2016-08-02 21:32:42 +02:00
|
|
|
}
|
2016-08-13 19:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
|
2016-10-07 02:43:12 +02:00
|
|
|
App app = ((App) getActivity().getApplication());
|
2016-10-13 16:56:31 +02:00
|
|
|
AppSettings appSettings = app.getSettings();
|
2016-10-14 00:44:23 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
2016-10-13 16:56:31 +02:00
|
|
|
if (preference instanceof PreferenceScreen && ((PreferenceScreen) preference).getDialog() != null) {
|
|
|
|
Window window = ((PreferenceScreen) preference).getDialog().getWindow();
|
|
|
|
if (window != null) {
|
|
|
|
window.setStatusBarColor(ThemeHelper.getPrimaryDarkColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 19:54:16 +02:00
|
|
|
Intent intent = new Intent(getActivity(), MainActivity.class);
|
2016-10-07 02:43:12 +02:00
|
|
|
DiasporaUrlHelper diasporaUrlHelper = new DiasporaUrlHelper(app.getSettings());
|
2016-10-13 16:56:31 +02:00
|
|
|
|
2016-08-13 19:54:16 +02:00
|
|
|
switch (preference.getTitleRes()) {
|
2016-10-13 16:56:31 +02:00
|
|
|
case R.string.pref_title__primary_color: {
|
|
|
|
showColorPickerDialog(1);
|
|
|
|
intent = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R.string.pref_title__accent_color: {
|
|
|
|
showColorPickerDialog(2);
|
|
|
|
intent = null;
|
|
|
|
break;
|
|
|
|
}
|
2016-08-13 19:54:16 +02:00
|
|
|
case R.string.pref_title__personal_settings: {
|
|
|
|
intent.setAction(MainActivity.ACTION_OPEN_URL);
|
2016-10-07 02:43:12 +02:00
|
|
|
intent.putExtra(MainActivity.URL_MESSAGE, diasporaUrlHelper.getPersonalSettingsUrl());
|
2016-08-13 19:54:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R.string.pref_title__manage_tags: {
|
|
|
|
intent.setAction(MainActivity.ACTION_OPEN_URL);
|
2016-10-07 02:43:12 +02:00
|
|
|
intent.putExtra(MainActivity.URL_MESSAGE, diasporaUrlHelper.getManageTagsUrl());
|
2016-08-13 19:54:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R.string.pref_title__manage_contacts: {
|
|
|
|
intent.setAction(MainActivity.ACTION_OPEN_URL);
|
2016-10-07 02:43:12 +02:00
|
|
|
intent.putExtra(MainActivity.URL_MESSAGE, diasporaUrlHelper.getManageContactsUrl());
|
2016-08-13 19:54:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R.string.pref_title__change_account: {
|
|
|
|
new AlertDialog.Builder(getActivity())
|
|
|
|
.setTitle(getString(R.string.confirmation))
|
|
|
|
.setMessage(getString(R.string.pref_warning__change_account))
|
|
|
|
.setNegativeButton(android.R.string.no, null)
|
|
|
|
.setPositiveButton(android.R.string.yes,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int id) {
|
|
|
|
Intent intent = new Intent(getActivity(), MainActivity.class);
|
|
|
|
intent.setAction(MainActivity.ACTION_CHANGE_ACCOUNT);
|
|
|
|
startActivity(intent);
|
|
|
|
getActivity().finish();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-13 16:56:31 +02:00
|
|
|
case R.string.pref_title__http_proxy_load_tor_preset: {
|
|
|
|
((EditTextPreference) findPreference(getString(R.string.pref_key__http_proxy_host))).setText("127.0.0.1");
|
|
|
|
((EditTextPreference) findPreference(getString(R.string.pref_key__http_proxy_port))).setText("8118");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:44:23 +02:00
|
|
|
case R.string.pref_title__clear_cache: {
|
2016-10-03 18:28:08 +02:00
|
|
|
intent.setAction(MainActivity.ACTION_CLEAR_CACHE);
|
|
|
|
break;
|
|
|
|
}
|
2016-09-19 01:10:29 +02:00
|
|
|
|
2016-08-13 19:54:16 +02:00
|
|
|
default: {
|
|
|
|
intent = null;
|
|
|
|
break;
|
|
|
|
}
|
2016-08-02 21:32:42 +02:00
|
|
|
}
|
2016-08-13 19:54:16 +02:00
|
|
|
if (intent != null) {
|
|
|
|
startActivity(intent);
|
|
|
|
getActivity().finish();
|
|
|
|
return true;
|
2016-08-02 21:32:42 +02:00
|
|
|
}
|
2016-08-13 19:54:16 +02:00
|
|
|
return super.onPreferenceTreeClick(screen, preference);
|
2016-07-26 22:47:11 +02:00
|
|
|
}
|
2016-10-13 16:56:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a colorPicker Dialog
|
2016-10-14 00:44:23 +02:00
|
|
|
*
|
2016-10-13 16:56:31 +02:00
|
|
|
* @param type 1 -> Primary Color, 2 -> Accent Color
|
|
|
|
*/
|
|
|
|
public void showColorPickerDialog(final int type) {
|
2016-10-14 00:44:23 +02:00
|
|
|
final AppSettings appSettings = ((App) getActivity().getApplication()).getSettings();
|
2016-10-13 16:56:31 +02:00
|
|
|
final Context context = getActivity();
|
|
|
|
|
|
|
|
//Inflate dialog layout
|
|
|
|
LayoutInflater inflater = getActivity().getLayoutInflater();
|
|
|
|
View dialogLayout = inflater.inflate(R.layout.color_picker__dialog, null);
|
|
|
|
final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
|
|
|
|
builder.setView(dialogLayout);
|
|
|
|
|
|
|
|
final FrameLayout titleBackground = (FrameLayout) dialogLayout.findViewById(R.id.color_picker_dialog__title_background);
|
|
|
|
final TextView title = (TextView) dialogLayout.findViewById(R.id.color_picker_dialog__title);
|
|
|
|
final LineColorPicker base = (LineColorPicker) dialogLayout.findViewById(R.id.color_picker_dialog__base_picker);
|
|
|
|
final LineColorPicker shade = (LineColorPicker) dialogLayout.findViewById(R.id.color_picker_dialog__shade_picker);
|
|
|
|
|
|
|
|
title.setText(type == 1 ? R.string.pref_title__primary_color : R.string.pref_title__accent_color);
|
|
|
|
title.setTextColor(getResources().getColor(R.color.white));
|
|
|
|
final int[] current = (type == 1 ? appSettings.getPrimaryColorSettings() : appSettings.getAccentColorSettings());
|
|
|
|
base.setColors((type == 1 ? ColorPalette.getBaseColors(context) : ColorPalette.getAccentColors(context)));
|
|
|
|
base.setSelectedColor(current[0]);
|
|
|
|
shade.setColors(ColorPalette.getColors(context, current[0]));
|
|
|
|
shade.setSelectedColor(current[1]);
|
|
|
|
titleBackground.setBackgroundColor(shade.getColor());
|
|
|
|
base.setOnColorChangedListener(new OnColorChangedListener() {
|
|
|
|
@Override
|
|
|
|
public void onColorChanged(int i) {
|
2016-10-14 00:44:23 +02:00
|
|
|
AppLog.d(this, "Selected Base color changed: " + i);
|
2016-10-13 16:56:31 +02:00
|
|
|
shade.setColors(ColorPalette.getColors(context, i));
|
|
|
|
titleBackground.setBackgroundColor(i);
|
2016-10-14 00:44:23 +02:00
|
|
|
if (i == current[0]) {
|
2016-10-13 16:56:31 +02:00
|
|
|
shade.setSelectedColor(current[1]);
|
|
|
|
titleBackground.setBackgroundColor(shade.getColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
shade.setOnColorChangedListener(new OnColorChangedListener() {
|
|
|
|
@Override
|
|
|
|
public void onColorChanged(int i) {
|
|
|
|
titleBackground.setBackgroundColor(i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//Build dialog
|
|
|
|
builder
|
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
2016-10-14 00:44:23 +02:00
|
|
|
if (type == 1) {
|
2016-10-13 16:56:31 +02:00
|
|
|
appSettings.setPrimaryColorSettings(base.getColor(), shade.getColor());
|
2016-10-14 00:44:23 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
2016-10-13 16:56:31 +02:00
|
|
|
getActivity().getWindow().setStatusBarColor(ThemeHelper.getPrimaryDarkColor());
|
|
|
|
}
|
2016-10-14 00:44:23 +02:00
|
|
|
((ThemedActivity) getActivity()).applyColorToViews();
|
2016-10-13 16:56:31 +02:00
|
|
|
} else {
|
|
|
|
appSettings.setAccentColorSettings(base.getColor(), shade.getColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).show();
|
|
|
|
}
|
2016-06-18 15:53:49 +02:00
|
|
|
}
|
2016-08-07 02:28:37 +02:00
|
|
|
|
2016-09-19 01:10:29 +02:00
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
|
|
|
|
// Reset logging
|
|
|
|
AppSettings settings = new AppSettings(getApplicationContext());
|
|
|
|
AppLog.setLoggingEnabled(settings.isLoggingEnabled());
|
|
|
|
AppLog.setLoggingSpamEnabled(settings.isLoggingSpamEnabled());
|
|
|
|
}
|
|
|
|
|
2016-08-07 02:28:37 +02:00
|
|
|
@Override
|
|
|
|
protected void onStop() {
|
2016-10-13 16:56:31 +02:00
|
|
|
ProxyHandler.ProxySettings newProxySettings = getAppSettings().getProxySettings();
|
2016-10-14 00:44:23 +02:00
|
|
|
if (!oldProxySettings.equals(newProxySettings)) {
|
2016-10-13 16:56:31 +02:00
|
|
|
AppLog.d(this, "ProxySettings changed.");
|
|
|
|
//Proxy on-off? => Restart app
|
2016-10-14 00:44:23 +02:00
|
|
|
if (oldProxySettings.isEnabled() && !newProxySettings.isEnabled()) {
|
2016-10-13 16:56:31 +02:00
|
|
|
Intent restartActivity = new Intent(SettingsActivity.this, MainActivity.class);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(SettingsActivity.this, 12374, restartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
|
AlarmManager mgr = (AlarmManager) SettingsActivity.this.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent);
|
|
|
|
System.exit(0);
|
|
|
|
} //Proxy changed? => Update
|
|
|
|
else {
|
|
|
|
ProxyHandler.getInstance().updateProxySettings(this);
|
|
|
|
}
|
2016-08-07 02:28:37 +02:00
|
|
|
}
|
2016-10-13 16:56:31 +02:00
|
|
|
super.onStop();
|
2016-08-07 02:28:37 +02:00
|
|
|
}
|
2016-07-29 14:00:28 +02:00
|
|
|
}
|