NetworkTraffic: Cleanup and align to the new implementation.

* Also port "Show single traffic and allow custom font size [2/2]"
* Thanks to neobuddy for showing how the code can be slimmed down:
  https://github.com/crdroidandroid/android_packages_apps_crDroidSettings/commit/cab6d64

Signed-off-by: spezi77 <spezi7713@gmx.net>
Signed-off-by: Hưng Phan <phandinhhungvp2001@gmail.com>
This commit is contained in:
spezi77
2022-02-28 19:12:58 +01:00
committed by Hưng Phan
parent 8118c376c5
commit 4c5cdef705
5 changed files with 173 additions and 218 deletions

View File

@@ -408,7 +408,7 @@
<item>3</item>
</string-array>
<!-- Network traffic location -->
<!-- Network Traffic -->
<string-array name="network_traffic_location_entries" translatable="false">
<item>@string/network_traffic_disabled</item>
<item>@string/network_traffic_statusbar</item>
@@ -420,11 +420,11 @@
<item>1</item>
<item>2</item>
</string-array>
<string-array name="network_traffic_mode_entries" translatable="false">
<string-array name="network_traffic_mode_entries" translatable="false">
<item>@string/network_traffic_dynamic</item>
<item>@string/network_traffic_download</item>
<item>@string/network_traffic_upload</item>
<item>@string/network_traffic_download</item>
</string-array>
<string-array name="network_traffic_mode_values" translatable="false">
@@ -432,4 +432,16 @@
<item>1</item>
<item>2</item>
</string-array>
<string-array name="network_traffic_units_entries" translatable="false">
<item>@string/network_traffic_units_bits</item>
<item>@string/network_traffic_units_bytes</item>
</string-array>
<string-array name="network_traffic_units_values" translatable="false">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>

View File

@@ -508,10 +508,24 @@
<string name="network_traffic_statusbar">Status bar</string>
<string name="network_traffic_qs_header">QS header</string>
<string name="network_traffic_refresh_interval_title">Refresh interval</string>
<string name="network_traffic_refresh_interval_summary">Duration after which speed is remeasured</string>
<string name="network_traffic_mode_title">Display mode</string>
<string name="network_traffic_dynamic">Dynamic</string>
<string name="network_traffic_download">Download</string>
<string name="network_traffic_upload">Upload</string>
<string name="network_traffic_font_size">Single net font size</string>
<string name="network_traffic_autohide">Auto hide</string>
<string name="network_traffic_autohide_summary">Hide the traffic monitor when inactive</string>
<string name="network_traffic_autohide_kilobits">Kb/s</string>
<string name="network_traffic_autohide_kilobytes">KB/s</string>
<string name="network_traffic_units_title">Traffic measurement units</string>
<string name="network_traffic_units_bits">Bits per second (b/s)</string>
<string name="network_traffic_units_kilobits">Kilobits per second (kb/s)</string>
<string name="network_traffic_units_megabits">Megabits per second (Mb/s)</string>
<string name="network_traffic_units_bytes">Bytes per second (B/s)</string>
<string name="network_traffic_units_kilobytes">Kilobytes per second (kB/s)</string>
<string name="network_traffic_units_megabytes">Megabytes per second (MB/s)</string>
<string name="network_traffic_hidearrow">Hide arrows</string>
<string name="network_traffic_hidearrow_summary">Hide the network traffic indicator arrows</string>
</resources>

View File

@@ -1,62 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2020-2022 The CherishOS Project
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (C) 2020-2022 The CherishOS Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
android:title="@string/traffic_title">
<ListPreference
<PreferenceScreen
android:title="@string/traffic_title"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">
<com.cherish.settings.preferences.SystemSettingListPreference
android:key="network_traffic_location"
android:title="@string/network_traffic_location_title"
android:summary="%s"
android:entries="@array/network_traffic_location_entries"
android:entryValues="@array/network_traffic_location_values"
android:defaultValue="0"
android:persistent="false" />
android:defaultValue="0" />
<com.cherish.settings.preferences.CustomSeekBarPreference
<com.cherish.settings.preferences.SystemSettingSwitchPreference
android:key="network_traffic_autohide"
android:title="@string/network_traffic_autohide"
android:summary="@string/network_traffic_autohide_summary"
android:defaultValue="true" />
<com.cherish.settings.preferences.SystemSettingSeekBarPreference
android:key="network_traffic_autohide_threshold"
android:title="@string/network_traffic_autohide_threshold_title"
android:max="10"
android:defaultValue="0"
android:max="10000"
settings:min="0"
settings:units="KB/s"
android:defaultValue="0" />
settings:units="@string/network_traffic_autohide_kilobytes"
settings:interval="10"
android:dependency="network_traffic_autohide" />
<com.cherish.settings.preferences.SystemSettingListPreference
android:key="network_traffic_mode"
android:title="@string/network_traffic_mode_title"
android:entries="@array/network_traffic_mode_entries"
android:entryValues="@array/network_traffic_mode_values"
android:summary="%s"
android:defaultValue="0" />
<com.cherish.settings.preferences.SystemSettingSeekBarPreference
android:key="network_traffic_refresh_interval"
android:title="@string/network_traffic_refresh_interval_title"
android:summary="@string/network_traffic_refresh_interval_summary"
android:defaultValue="2"
android:max="10"
settings:min="1"
settings:units="sec"
settings:units="@string/unit_seconds" />
<com.cherish.settings.preferences.SystemSettingListPreference
android:key="network_traffic_units"
android:title="@string/network_traffic_units_title"
android:entries="@array/network_traffic_units_entries"
android:entryValues="@array/network_traffic_units_values"
android:summary="%s"
android:defaultValue="1" />
<com.cherish.settings.preferences.CustomSeekBarPreference
<com.cherish.settings.preferences.SystemSettingSwitchPreference
android:key="network_traffic_hidearrow"
android:title="@string/network_traffic_hidearrow"
android:summary="@string/network_traffic_hidearrow_summary" />
<com.cherish.settings.preferences.SystemSettingSeekBarPreference
android:key="network_traffic_font_size"
android:title="@string/network_traffic_font_size"
android:max="20"
android:min="12"
android:max="24"
settings:min="14"
settings:defaultValue="18"
settings:units="dp" />
</PreferenceScreen>
</PreferenceScreen>

View File

@@ -0,0 +1,102 @@
/*
* Copyright (C) 2021-2022 The PixelDust Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cherish.settings.fragments;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.SwitchPreference;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.cherish.settings.preferences.CustomSeekBarPreference;
public class Traffic extends SettingsPreferenceFragment implements OnPreferenceChangeListener {
private CustomSeekBarPreference mNetTrafficSize;
private CustomSeekBarPreference mNetTrafficAutohideThreshold;
private CustomSeekBarPreference mNetTrafficRefreshInterval;
private ListPreference mNetTrafficLocation;
private ListPreference mNetTrafficMode;
private ListPreference mNetTrafficUnits;
private SwitchPreference mNetTrafficAutohide;
private SwitchPreference mNetTrafficHideArrow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.cherish_settings_traffic);
final ContentResolver resolver = getActivity().getContentResolver();
mNetTrafficSize = (CustomSeekBarPreference)
findPreference(Settings.System.NETWORK_TRAFFIC_FONT_SIZE);
mNetTrafficAutohideThreshold = (CustomSeekBarPreference)
findPreference(Settings.System.NETWORK_TRAFFIC_AUTOHIDE_THRESHOLD);
mNetTrafficRefreshInterval = (CustomSeekBarPreference)
findPreference(Settings.System.NETWORK_TRAFFIC_REFRESH_INTERVAL);
mNetTrafficLocation = (ListPreference)
findPreference(Settings.System.NETWORK_TRAFFIC_LOCATION);
mNetTrafficLocation.setOnPreferenceChangeListener(this);
mNetTrafficMode = (ListPreference)
findPreference(Settings.System.NETWORK_TRAFFIC_MODE);
mNetTrafficAutohide = (SwitchPreference)
findPreference(Settings.System.NETWORK_TRAFFIC_AUTOHIDE);
mNetTrafficUnits = (ListPreference)
findPreference(Settings.System.NETWORK_TRAFFIC_UNITS);
mNetTrafficHideArrow = (SwitchPreference)
findPreference(Settings.System.NETWORK_TRAFFIC_HIDEARROW);
int location = Settings.System.getIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_LOCATION, 0, UserHandle.USER_CURRENT);
updateEnabledStates(location);
}
@Override
public int getMetricsCategory() {
return MetricsEvent.CHERISH_SETTINGS;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == mNetTrafficLocation) {
int location = Integer.valueOf((String) newValue);
updateEnabledStates(location);
return true;
}
return false;
}
private void updateEnabledStates(int location) {
final boolean enabled = location != 0;
mNetTrafficSize.setEnabled(enabled);
mNetTrafficMode.setEnabled(enabled);
mNetTrafficAutohide.setEnabled(enabled);
mNetTrafficAutohideThreshold.setEnabled(enabled);
mNetTrafficHideArrow.setEnabled(enabled);
mNetTrafficRefreshInterval.setEnabled(enabled);
mNetTrafficUnits.setEnabled(enabled);
}
}

View File

@@ -1,192 +0,0 @@
/*
* Copyright (C) 2020-2022 The CherishOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cherish.settings.fragments;
import android.app.ActivityManagerNative;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.IWindowManager;
import android.view.View;
import android.view.WindowManagerGlobal;
import androidx.preference.ListPreference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.cherish.settings.preferences.CustomSeekBarPreference;
import com.cherish.settings.preferences.SystemSettingSwitchPreference;
import com.cherish.settings.preferences.SystemSettingSeekBarPreference;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class Traffic extends SettingsPreferenceFragment implements OnPreferenceChangeListener {
private static final String NETWORK_TRAFFIC_AUTOHIDE_THRESHOLD = "network_traffic_autohide_threshold";
private static final String NETWORK_TRAFFIC_LOCATION = "network_traffic_location";
private static final String NETWORK_TRAFFIC_REFRESH_INTERVAL = "network_traffic_refresh_interval";
private static final String NETWORK_TRAFFIC_FONT_SIZE = "network_traffic_font_size";
private CustomSeekBarPreference mThreshold;
private SystemSettingSeekBarPreference mInterval;
private ListPreference mNetTrafficLocation;
private CustomSeekBarPreference mNetTrafficSize;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.cherish_settings_traffic);
final ContentResolver resolver = getActivity().getContentResolver();
final PreferenceScreen prefSet = getPreferenceScreen();
// Network traffic location
mNetTrafficLocation = (ListPreference) findPreference(NETWORK_TRAFFIC_LOCATION);
int location = Settings.System.getIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_LOCATION, 0, UserHandle.USER_CURRENT);
mNetTrafficLocation.setOnPreferenceChangeListener(this);
int value = Settings.System.getIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_AUTOHIDE_THRESHOLD, 1, UserHandle.USER_CURRENT);
mThreshold = (CustomSeekBarPreference) findPreference(NETWORK_TRAFFIC_AUTOHIDE_THRESHOLD);
mThreshold.setValue(value);
mThreshold.setOnPreferenceChangeListener(this);
int val = Settings.System.getIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_REFRESH_INTERVAL, 1, UserHandle.USER_CURRENT);
mInterval = (SystemSettingSeekBarPreference) findPreference(NETWORK_TRAFFIC_REFRESH_INTERVAL);
mInterval.setValue(val);
mInterval.setOnPreferenceChangeListener(this);
int fontSize = Settings.System.getInt(resolver,
Settings.System.NETWORK_TRAFFIC_FONT_SIZE, 18);
mNetTrafficSize = (CustomSeekBarPreference) findPreference(NETWORK_TRAFFIC_FONT_SIZE);
mNetTrafficSize.setValue(fontSize / 1);
mNetTrafficSize.setOnPreferenceChangeListener(this);
int netMonitorEnabled = Settings.System.getIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_STATE, 0, UserHandle.USER_CURRENT);
if (netMonitorEnabled == 1) {
mNetTrafficLocation.setValue(String.valueOf(location+1));
updateTrafficLocation(location+1);
} else {
mNetTrafficLocation.setValue("0");
updateTrafficLocation(0);
}
mNetTrafficLocation.setSummary(mNetTrafficLocation.getEntry());
}
@Override
public int getMetricsCategory() {
return MetricsEvent.CHERISH_SETTINGS;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final ContentResolver resolver = getActivity().getContentResolver();
if (preference == mNetTrafficLocation) {
int location = Integer.valueOf((String) newValue);
int index = mNetTrafficLocation.findIndexOfValue((String) newValue);
mNetTrafficLocation.setSummary(mNetTrafficLocation.getEntries()[index]);
if (location > 0) {
// Convert the selected location mode from our list {0,1,2} and store it to "view location" setting: 0=sb; 1=expanded sb
Settings.System.putIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_LOCATION, location-1, UserHandle.USER_CURRENT);
// And also enable the net monitor
Settings.System.putIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_STATE, 1, UserHandle.USER_CURRENT);
} else { // Disable net monitor completely
Settings.System.putIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_STATE, 0, UserHandle.USER_CURRENT);
}
updateTrafficLocation(location);
return true;
} else if (preference == mThreshold) {
int val = (Integer) newValue;
Settings.System.putIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_AUTOHIDE_THRESHOLD, val,
UserHandle.USER_CURRENT);
return true;
} else if (preference == mInterval) {
int val = (Integer) newValue;
Settings.System.putIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_REFRESH_INTERVAL, val,
UserHandle.USER_CURRENT);
return true;
} else if (preference == mNetTrafficSize) {
int fontSize = ((Integer)objValue).intValue();
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.NETWORK_TRAFFIC_FONT_SIZE, fontSize);
return true;
}
return false;
}
public void updateTrafficLocation(int location) {
switch(location){
case 0:
mThreshold.setEnabled(false);
mInterval.setEnabled(false);
mNetTrafficSize.setEnabled(false);
break;
case 1:
mThreshold.setEnabled(true);
mInterval.setEnabled(true);
mNetTrafficSize.setEnabled(true);
break;
case 2:
mThreshold.setEnabled(true);
mInterval.setEnabled(true);
mNetTrafficSize.setEnabled(false);
break;
default:
break;
}
}
/**
* For Search.
*/
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.cherish_settings_traffic);
}