2016-09-21 21:55:20 +00:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
2016-09-25 05:16:28 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2016-09-21 21:55:20 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.CheckBoxPreference;
|
2016-09-25 05:16:28 +00:00
|
|
|
import android.preference.ListPreference;
|
2016-09-21 21:55:20 +00:00
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceFragment;
|
2016-09-25 05:16:28 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2016-09-21 21:55:20 +00:00
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2016-09-25 05:16:28 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Logger;
|
2016-09-21 21:55:20 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
|
|
|
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
2016-09-25 06:16:08 +00:00
|
|
|
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
2016-09-21 21:55:20 +00:00
|
|
|
private CheckBoxPreference quickTilePreference;
|
2016-09-25 05:16:28 +00:00
|
|
|
private ListPreference themePreference;
|
2016-09-25 06:16:08 +00:00
|
|
|
|
|
|
|
public SettingsFragment() {
|
|
|
|
}
|
2016-09-21 21:55:20 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
addPreferencesFromResource(R.xml.uisettings);
|
2016-09-26 18:16:11 +00:00
|
|
|
PreferenceManager.setDefaultValues(getActivity(), R.xml.uisettings, false);
|
2016-09-21 21:55:20 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 21:22:11 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
getActivity().setTitle("Settings");
|
2016-09-25 05:16:28 +00:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(getActivity()).unregisterOnSharedPreferenceChangeListener(this);
|
|
|
|
|
2016-09-23 21:22:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 21:55:20 +00:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
View view = super.onCreateView(inflater, container, savedInstanceState);
|
|
|
|
ButterKnife.bind(this, view);
|
|
|
|
quickTilePreference = (CheckBoxPreference) findPreference("enable_quicktile");
|
2016-09-25 05:16:28 +00:00
|
|
|
themePreference = (ListPreference) findPreference("theme");
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);
|
2016-09-24 15:09:56 +00:00
|
|
|
CheckBoxPreference keepRootOffPreference = (CheckBoxPreference) findPreference("keep_root_off");
|
|
|
|
CheckBoxPreference hideRootNotificationPreference = (CheckBoxPreference) findPreference("hide_root_notification");
|
2016-09-25 05:16:28 +00:00
|
|
|
themePreference.setSummary(themePreference.getValue());
|
2016-09-21 21:55:20 +00:00
|
|
|
if (Utils.magiskVersion == -1) {
|
|
|
|
quickTilePreference.setEnabled(false);
|
|
|
|
keepRootOffPreference.setEnabled(false);
|
|
|
|
hideRootNotificationPreference.setEnabled(false);
|
|
|
|
} else {
|
|
|
|
quickTilePreference.setEnabled(true);
|
|
|
|
keepRootOffPreference.setEnabled(true);
|
|
|
|
hideRootNotificationPreference.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Preference.OnPreferenceClickListener preferenceClickListener = preference -> {
|
|
|
|
if (preference == quickTilePreference) {
|
|
|
|
boolean isChecked = quickTilePreference.isChecked();
|
2016-09-23 21:22:11 +00:00
|
|
|
if (isChecked) {
|
|
|
|
Utils.installTile(getActivity());
|
|
|
|
} else {
|
|
|
|
Utils.uninstallTile(getActivity());
|
|
|
|
}
|
2016-09-21 21:55:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
quickTilePreference.setOnPreferenceClickListener(preferenceClickListener);
|
|
|
|
// calculate margins
|
|
|
|
int horizontalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
|
|
|
|
int verticalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
|
|
|
|
TypedValue tv = new TypedValue();
|
|
|
|
int actionBarHeight = 130;
|
|
|
|
if (getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
|
|
|
|
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
|
|
|
|
}
|
|
|
|
|
|
|
|
view.setPadding(horizontalMargin, actionBarHeight, horizontalMargin, verticalMargin);
|
|
|
|
|
|
|
|
return view;
|
2016-09-25 05:16:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
|
|
|
Logger.dh("Settings: NewValue is " + key);
|
|
|
|
|
|
|
|
if (key.equals("theme")) {
|
2016-09-25 06:16:08 +00:00
|
|
|
String pref = sharedPreferences.getString(key, "");
|
2016-09-25 05:16:28 +00:00
|
|
|
|
|
|
|
themePreference.setSummary(pref);
|
|
|
|
if (pref.equals("Dark")) {
|
|
|
|
getActivity().getApplication().setTheme(R.style.AppTheme_dh);
|
|
|
|
} else {
|
|
|
|
getActivity().getApplication().setTheme(R.style.AppTheme);
|
|
|
|
}
|
|
|
|
Intent intent = new Intent(getActivity(), MainActivity.class);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2016-09-25 06:16:08 +00:00
|
|
|
intent.putExtra("Relaunch", "Settings");
|
2016-09-25 05:16:28 +00:00
|
|
|
startActivity(intent);
|
|
|
|
|
|
|
|
Logger.dh("SettingsFragment: theme is " + pref);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-21 21:55:20 +00:00
|
|
|
}
|
|
|
|
}
|