mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-01-13 02:13:38 +00:00
Create BasePreferenceFragment
This commit is contained in:
parent
a189dec1c8
commit
a8cd9b3aa9
@ -0,0 +1,81 @@
|
|||||||
|
package com.topjohnwu.magisk.components;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
import com.topjohnwu.core.App;
|
||||||
|
import com.topjohnwu.core.utils.Topic;
|
||||||
|
import com.topjohnwu.magisk.R;
|
||||||
|
|
||||||
|
import androidx.preference.Preference;
|
||||||
|
import androidx.preference.PreferenceCategory;
|
||||||
|
import androidx.preference.PreferenceFragmentCompat;
|
||||||
|
import androidx.preference.PreferenceGroupAdapter;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
import androidx.preference.PreferenceViewHolder;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
public abstract class BasePreferenceFragment extends PreferenceFragmentCompat
|
||||||
|
implements SharedPreferences.OnSharedPreferenceChangeListener, Topic.AutoSubscriber {
|
||||||
|
|
||||||
|
public App app = App.self;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
|
View v = super.onCreateView(inflater, container, savedInstanceState);
|
||||||
|
app.prefs.registerOnSharedPreferenceChangeListener(this);
|
||||||
|
Topic.subscribe(this);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
app.prefs.unregisterOnSharedPreferenceChangeListener(this);
|
||||||
|
Topic.unsubscribe(this);
|
||||||
|
super.onDestroyView();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int[] getSubscribedTopics() {
|
||||||
|
return BaseActivity.EMPTY_INT_ARRAY;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
|
||||||
|
return new PreferenceGroupAdapter(preferenceScreen) {
|
||||||
|
@SuppressLint("RestrictedApi")
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(PreferenceViewHolder holder, int position) {
|
||||||
|
super.onBindViewHolder(holder, position);
|
||||||
|
Preference preference = getItem(position);
|
||||||
|
if (preference instanceof PreferenceCategory)
|
||||||
|
setZeroPaddingToLayoutChildren(holder.itemView);
|
||||||
|
else {
|
||||||
|
View iconFrame = holder.itemView.findViewById(R.id.icon_frame);
|
||||||
|
if (iconFrame != null) {
|
||||||
|
iconFrame.setVisibility(preference.getIcon() == null ? View.GONE : View.VISIBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setZeroPaddingToLayoutChildren(View view) {
|
||||||
|
if (!(view instanceof ViewGroup))
|
||||||
|
return;
|
||||||
|
ViewGroup viewGroup = (ViewGroup) view;
|
||||||
|
int childCount = viewGroup.getChildCount();
|
||||||
|
for (int i = 0; i < childCount; i++) {
|
||||||
|
setZeroPaddingToLayoutChildren(viewGroup.getChildAt(i));
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
|
||||||
|
viewGroup.setPaddingRelative(0, viewGroup.getPaddingTop(), viewGroup.getPaddingEnd(), viewGroup.getPaddingBottom());
|
||||||
|
else
|
||||||
|
viewGroup.setPadding(0, viewGroup.getPaddingTop(), viewGroup.getPaddingRight(), viewGroup.getPaddingBottom());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +1,13 @@
|
|||||||
package com.topjohnwu.magisk.fragments;
|
package com.topjohnwu.magisk.fragments;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.topjohnwu.core.App;
|
|
||||||
import com.topjohnwu.core.Config;
|
import com.topjohnwu.core.Config;
|
||||||
import com.topjohnwu.core.Const;
|
import com.topjohnwu.core.Const;
|
||||||
import com.topjohnwu.core.tasks.CheckUpdates;
|
import com.topjohnwu.core.tasks.CheckUpdates;
|
||||||
@ -19,6 +16,7 @@ import com.topjohnwu.core.utils.Topic;
|
|||||||
import com.topjohnwu.core.utils.Utils;
|
import com.topjohnwu.core.utils.Utils;
|
||||||
import com.topjohnwu.magisk.BuildConfig;
|
import com.topjohnwu.magisk.BuildConfig;
|
||||||
import com.topjohnwu.magisk.R;
|
import com.topjohnwu.magisk.R;
|
||||||
|
import com.topjohnwu.magisk.components.BasePreferenceFragment;
|
||||||
import com.topjohnwu.magisk.utils.AppUtils;
|
import com.topjohnwu.magisk.utils.AppUtils;
|
||||||
import com.topjohnwu.magisk.utils.DownloadApp;
|
import com.topjohnwu.magisk.utils.DownloadApp;
|
||||||
import com.topjohnwu.magisk.utils.FingerprintHelper;
|
import com.topjohnwu.magisk.utils.FingerprintHelper;
|
||||||
@ -33,18 +31,10 @@ import androidx.appcompat.app.AlertDialog;
|
|||||||
import androidx.preference.ListPreference;
|
import androidx.preference.ListPreference;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceCategory;
|
import androidx.preference.PreferenceCategory;
|
||||||
import androidx.preference.PreferenceFragmentCompat;
|
|
||||||
import androidx.preference.PreferenceGroupAdapter;
|
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
import androidx.preference.PreferenceViewHolder;
|
|
||||||
import androidx.preference.SwitchPreferenceCompat;
|
import androidx.preference.SwitchPreferenceCompat;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
|
||||||
|
|
||||||
public class SettingsFragment extends PreferenceFragmentCompat
|
public class SettingsFragment extends BasePreferenceFragment implements Topic.Subscriber {
|
||||||
implements SharedPreferences.OnSharedPreferenceChangeListener,
|
|
||||||
Topic.Subscriber, Topic.AutoSubscriber {
|
|
||||||
|
|
||||||
private App app = App.self;
|
|
||||||
|
|
||||||
private ListPreference updateChannel, autoRes, suNotification,
|
private ListPreference updateChannel, autoRes, suNotification,
|
||||||
requestTimeout, rootConfig, multiuserConfig, nsConfig;
|
requestTimeout, rootConfig, multiuserConfig, nsConfig;
|
||||||
@ -52,6 +42,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
@Override
|
@Override
|
||||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||||
setPreferencesFromResource(R.xml.app_settings, rootKey);
|
setPreferencesFromResource(R.xml.app_settings, rootKey);
|
||||||
|
requireActivity().setTitle(R.string.settings);
|
||||||
|
|
||||||
boolean showSuperuser = Utils.showSuperUser();
|
boolean showSuperuser = Utils.showSuperUser();
|
||||||
app.prefs.edit()
|
app.prefs.edit()
|
||||||
@ -176,21 +167,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
lp.setSummary(LocaleManager.locale.getDisplayName(LocaleManager.locale));
|
lp.setSummary(LocaleManager.locale.getDisplayName(LocaleManager.locale));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
||||||
app.prefs.registerOnSharedPreferenceChangeListener(this);
|
|
||||||
Topic.subscribe(this);
|
|
||||||
requireActivity().setTitle(R.string.settings);
|
|
||||||
return super.onCreateView(inflater, container, savedInstanceState);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroyView() {
|
|
||||||
app.prefs.unregisterOnSharedPreferenceChangeListener(this);
|
|
||||||
Topic.unsubscribe(this);
|
|
||||||
super.onDestroyView();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
|
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
@ -309,38 +285,4 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
public int[] getSubscribedTopics() {
|
public int[] getSubscribedTopics() {
|
||||||
return new int[] {Topic.LOCALE_FETCH_DONE};
|
return new int[] {Topic.LOCALE_FETCH_DONE};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
|
|
||||||
return new PreferenceGroupAdapter(preferenceScreen) {
|
|
||||||
@SuppressLint("RestrictedApi")
|
|
||||||
@Override
|
|
||||||
public void onBindViewHolder(PreferenceViewHolder holder, int position) {
|
|
||||||
super.onBindViewHolder(holder, position);
|
|
||||||
Preference preference = getItem(position);
|
|
||||||
if (preference instanceof PreferenceCategory)
|
|
||||||
setZeroPaddingToLayoutChildren(holder.itemView);
|
|
||||||
else {
|
|
||||||
View iconFrame = holder.itemView.findViewById(R.id.icon_frame);
|
|
||||||
if (iconFrame != null) {
|
|
||||||
iconFrame.setVisibility(preference.getIcon() == null ? View.GONE : View.VISIBLE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setZeroPaddingToLayoutChildren(View view) {
|
|
||||||
if (!(view instanceof ViewGroup))
|
|
||||||
return;
|
|
||||||
ViewGroup viewGroup = (ViewGroup) view;
|
|
||||||
int childCount = viewGroup.getChildCount();
|
|
||||||
for (int i = 0; i < childCount; i++) {
|
|
||||||
setZeroPaddingToLayoutChildren(viewGroup.getChildAt(i));
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
|
|
||||||
viewGroup.setPaddingRelative(0, viewGroup.getPaddingTop(), viewGroup.getPaddingEnd(), viewGroup.getPaddingBottom());
|
|
||||||
else
|
|
||||||
viewGroup.setPadding(0, viewGroup.getPaddingTop(), viewGroup.getPaddingRight(), viewGroup.getPaddingBottom());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user