55import android .os .Bundle ;
66import android .preference .PreferenceManager ;
77import android .view .View ;
8+ import android .widget .RelativeLayout ;
89
910import java .util .Arrays ;
11+ import java .util .Objects ;
12+ import java .util .Set ;
13+ import java .util .stream .Collectors ;
14+ import java .util .stream .Stream ;
1015
1116import androidx .appcompat .app .ActionBar ;
1217import androidx .appcompat .app .AppCompatActivity ;
1318import androidx .preference .ListPreference ;
1419import androidx .preference .Preference ;
20+ import androidx .preference .PreferenceCategory ;
1521import androidx .preference .PreferenceFragmentCompat ;
1622import androidx .preference .PreferenceGroupAdapter ;
1723import androidx .preference .PreferenceScreen ;
@@ -38,6 +44,7 @@ protected void onCreate(Bundle savedInstanceState) {
3844
3945
4046 public static class MySettingsFragment extends PreferenceFragmentCompat {
47+
4148 @ Override //去除前面的空白
4249 protected RecyclerView .Adapter onCreateAdapter (PreferenceScreen preferenceScreen ) {
4350 return new PreferenceGroupAdapter (preferenceScreen ) {
@@ -58,36 +65,75 @@ public void onBindViewHolder(PreferenceViewHolder holder, int position) {
5865 public void onCreatePreferences (Bundle savedInstanceState , String rootKey ) {
5966 Context context = getPreferenceManager ().getContext ();
6067 PreferenceScreen screen = getPreferenceManager ().createPreferenceScreen (context );
68+ screen .setIconSpaceReserved (false );
6169
62- ListPreference quality = new ListPreference (context );
63- quality .setKey ("quality" );
64- quality .setEntries (VideoQuality .getEntries ());
65- quality .setEntryValues (VideoQuality .getEntries ());
66- quality .setTitle ("视频清晰度" );
67- quality .setSummary (PreferenceManager .getDefaultSharedPreferences (quality .getContext ()).getString (quality .getKey (), VideoQuality ._2 .description ));
68- quality .setValue ((String ) quality .getSummary ());
69- quality .setOnPreferenceChangeListener ((preference , newValue ) -> {
70- preference .setSummary (newValue .toString ());
71- videoQuality = VideoQuality .list [Arrays .asList (VideoQuality .getEntries ()).indexOf (newValue .toString ())];
70+ PreferenceCategory downCategory = new PreferenceCategory (context ) {
71+ @ Override
72+ public void onBindViewHolder (PreferenceViewHolder holder ) {
73+ super .onBindViewHolder (holder );
74+ holder .itemView .setLayoutParams (new RelativeLayout .LayoutParams (RelativeLayout .LayoutParams .MATCH_PARENT , RelativeLayout .LayoutParams .WRAP_CONTENT ));
75+ }
76+ };
77+ downCategory .setTitle ("下载设置" );
78+
79+ ListPreference qualityList = new ListPreference (context );
80+ qualityList .setNegativeButtonText ("" );
81+ qualityList .setKey ("quality" );
82+ qualityList .setEntries (VideoQuality .getEntries ());
83+ qualityList .setEntryValues (VideoQuality .getEntries ());
84+ qualityList .setTitle ("视频清晰度" );
85+ qualityList .setSummary (PreferenceManager .getDefaultSharedPreferences (context ).getString (qualityList .getKey (), VideoQuality ._2 .description ));
86+ qualityList .setValue ((String ) qualityList .getSummary ());
87+ qualityList .setOnPreferenceChangeListener ((preference , newValue ) -> {
88+ preference .setSummary ((String ) newValue );
89+ videoQuality = Stream .of (VideoQuality .values ()).filter (item -> Objects .equals (item .description , newValue )).findFirst ().get ();
90+ // videoQuality = VideoQuality.values()[Arrays.asList(VideoQuality.getEntries()).indexOf(newValue.toString())];
7291 return true ;
7392 });
93+
7494 SwitchPreference clientDownloadSwitch = new SwitchPreference (context );
7595 clientDownloadSwitch .setSummaryOn ("B站" );
7696 clientDownloadSwitch .setSummaryOff ("系统" );
7797 clientDownloadSwitch .setKey ("clientDown" );
78- clientDownloadSwitch .setChecked (PreferenceManager .getDefaultSharedPreferences (clientDownloadSwitch . getContext () ).getBoolean (clientDownloadSwitch .getKey (), true ));
98+ clientDownloadSwitch .setChecked (PreferenceManager .getDefaultSharedPreferences (context ).getBoolean (clientDownloadSwitch .getKey (), true ));
7999 clientDownloadSwitch .setTitle ("下载方式" );
80100 clientDownloadSwitch .setOnPreferenceChangeListener ((preference , newValue ) -> {
81101 clientDownload = (boolean ) newValue ;
82102 return true ;
83103 });
84104
85- screen .addPreference (quality );
86- screen .addPreference (clientDownloadSwitch );
87- setPreferenceScreen (screen );
105+ PreferenceCategory paraCategory = new PreferenceCategory (context ) {
106+ @ Override
107+ public void onBindViewHolder (PreferenceViewHolder holder ) {
108+ super .onBindViewHolder (holder );
109+ holder .itemView .setLayoutParams (new RelativeLayout .LayoutParams (RelativeLayout .LayoutParams .MATCH_PARENT , RelativeLayout .LayoutParams .WRAP_CONTENT ));
110+ }
111+ };
112+ paraCategory .setTitle ("参数设置" );
88113
89- }
114+ ListPreference clientTypeList = new ListPreference (context );
115+ clientTypeList .setNegativeButtonText ("" );
116+ clientTypeList .setKey ("clientType" );
117+ clientTypeList .setEntries (ClientType .getEntries ());
118+ Set <String > packageNames = context .getPackageManager ().getInstalledApplications (0 ).stream ().map (item -> item .packageName ).collect (Collectors .toSet ());
119+ clientTypeList .setEntries (Stream .of (ClientType .values ()).map (cT -> cT .packageName ).filter (item -> !packageNames .add (item )).toArray (String []::new ));
120+ clientTypeList .setEntryValues (clientTypeList .getEntries ());
121+ clientTypeList .setTitle ("客户端类型" );
122+ clientTypeList .setSummary (PreferenceManager .getDefaultSharedPreferences (context ).getString (clientTypeList .getKey (), ClientType .release .packageName ));
123+ clientTypeList .setValue ((String ) clientTypeList .getSummary ());
124+ clientTypeList .setOnPreferenceChangeListener ((preference , newValue ) -> {
125+ preference .setSummary (newValue .toString ());
126+ clientType = ClientType .values ()[Arrays .asList (ClientType .getEntries ()).indexOf ((String ) newValue )];
127+ return true ;
128+ });
90129
130+ screen .addPreference (downCategory );
131+ downCategory .addPreference (qualityList );
132+ downCategory .addPreference (clientDownloadSwitch );
133+ screen .addPreference (paraCategory );
134+ paraCategory .addPreference (clientTypeList );
135+ setPreferenceScreen (screen );
136+ }
91137 }
92138
93139}
0 commit comments