Cherish: Network Traffic [2/2]

Signed-off-by: Hưng Phan <phandinhhungvp2001@gmail.com>
This commit is contained in:
Jon Haus
2021-11-17 17:27:23 +05:30
committed by Hưng Phan
parent 1185608377
commit bc63f10c90
4 changed files with 170 additions and 0 deletions

View File

@@ -498,5 +498,11 @@
<string name="status_bar_battery_style_circle">Circle</string>
<string name="status_bar_battery_style_dotted_circle">Dotted сircle</string>
<string name="status_bar_battery_style_text">Text</string>
<!-- Network Traffic -->
<string name="traffic_title">Traffic indicators</string>
<string name="traffic_summary">Customize the traffic indicators</string>
<string name="network_traffic_state_title">Show network traffic</string>
<string name="network_traffic_autohide_threshold_title">Auto-hide threshold</string>
</resources>

View File

@@ -35,6 +35,12 @@
android:summary="@string/status_bar_clock_date_summary"
android:fragment="com.cherish.settings.fragments.ClockDateSettings" />
<Preference
android:key="traffic"
android:fragment="com.cherish.settings.fragments.Traffic"
android:title="@string/traffic_title"
android:summary="@string/traffic_summary" />
<com.cherish.settings.preferences.SystemSettingSwitchPreference
android:key="data_disabled_icon"
android:title="@string/data_disabled_icon_title"

View File

@@ -0,0 +1,35 @@
<?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">
<com.cherish.settings.preferences.SystemSettingSwitchPreference
android:key="network_traffic_state"
android:title="@string/network_traffic_state_title"
android:defaultValue="false" />
<com.cherish.settings.preferences.CustomSeekBarPreference
android:key="network_traffic_autohide_threshold"
android:title="@string/network_traffic_autohide_threshold_title"
android:max="10"
settings:min="0"
settings:units="KB/s"
android:defaultValue="0" />
</PreferenceScreen>

View File

@@ -0,0 +1,123 @@
/*
* 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;
@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_STATE = "network_traffic_state";
private SystemSettingSwitchPreference mNetMonitor;
private CustomSeekBarPreference mThreshold;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.cherish_settings_traffic);
final ContentResolver resolver = getActivity().getContentResolver();
final PreferenceScreen prefSet = getPreferenceScreen();
boolean isNetMonitorEnabled = Settings.System.getIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_STATE, 0, UserHandle.USER_CURRENT) == 1;
mNetMonitor = (SystemSettingSwitchPreference) findPreference(NETWORK_TRAFFIC_STATE);
mNetMonitor.setChecked(isNetMonitorEnabled);
mNetMonitor.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);
mThreshold.setEnabled(isNetMonitorEnabled);
}
@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 == mNetMonitor) {
boolean value = (Boolean) newValue;
Settings.System.putIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_STATE, value ? 1 : 0,
UserHandle.USER_CURRENT);
mNetMonitor.setChecked(value);
mThreshold.setEnabled(value);
return true;
} else if (preference == mThreshold) {
int val = (Integer) newValue;
Settings.System.putIntForUser(getContentResolver(),
Settings.System.NETWORK_TRAFFIC_AUTOHIDE_THRESHOLD, val,
UserHandle.USER_CURRENT);
return true;
}
return false;
}
/**
* For Search.
*/
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.cherish_settings_traffic);
}