This repository has been archived on 2025-09-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
android_packages_apps_Cherish/src/com/cherish/settings/fragments/Traffic.java
2021-07-04 01:43:31 +07:00

232 lines
10 KiB
Java

/*
* Copyright (C) 2017 AospExtended ROM 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.Context;
import android.content.ContentResolver;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.RemoteException;
import android.os.ServiceManager;
import androidx.preference.Preference;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import androidx.preference.Preference.OnPreferenceChangeListener;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManagerGlobal;
import android.view.IWindowManager;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
import android.provider.SearchIndexableResource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import android.text.TextUtils;
import android.view.View;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.Utils;
import com.cherish.settings.preferences.CustomSeekBarPreference;
import com.cherish.settings.preferences.SystemSettingSwitchPreference;
import com.cherish.settings.preferences.SystemSettingListPreference;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class Traffic extends SettingsPreferenceFragment implements OnPreferenceChangeListener {
private static final String NETWORK_TRAFFIC_FONT_SIZE = "network_traffic_font_size";
private ListPreference mNetTrafficLocation;
private ListPreference mNetTrafficType;
private ListPreference mNetTrafficLayout;
private CustomSeekBarPreference mNetTrafficSize;
private CustomSeekBarPreference mThreshold;
private SystemSettingSwitchPreference mShowArrows;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.traffic);
final ContentResolver resolver = getActivity().getContentResolver();
final PreferenceScreen prefSet = getPreferenceScreen();
int NetTrafficSize = Settings.System.getInt(resolver,
Settings.System.NETWORK_TRAFFIC_FONT_SIZE, 42);
mNetTrafficSize = (CustomSeekBarPreference) findPreference(NETWORK_TRAFFIC_FONT_SIZE);
mNetTrafficSize.setValue(NetTrafficSize / 1);
mNetTrafficSize.setOnPreferenceChangeListener(this);
int type = Settings.System.getIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_TYPE, 0, UserHandle.USER_CURRENT);
mNetTrafficType = (ListPreference) findPreference("network_traffic_type");
mNetTrafficType.setValue(String.valueOf(type));
mNetTrafficType.setSummary(mNetTrafficType.getEntry());
mNetTrafficType.setOnPreferenceChangeListener(this);
int netlayout = Settings.System.getIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_LAYOUT, 0, UserHandle.USER_CURRENT);
mNetTrafficLayout = (ListPreference) findPreference("network_traffic_layout");
mNetTrafficLayout.setValue(String.valueOf(netlayout));
mNetTrafficLayout.setSummary(mNetTrafficLayout.getEntry());
mNetTrafficLayout.setOnPreferenceChangeListener(this);
mNetTrafficLocation = (ListPreference) findPreference("network_traffic_location");
int location = Settings.System.getIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_VIEW_LOCATION, 0, UserHandle.USER_CURRENT);
mNetTrafficLocation.setOnPreferenceChangeListener(this);
int trafvalue = Settings.System.getIntForUser(resolver,
Settings.System.NETWORK_TRAFFIC_AUTOHIDE_THRESHOLD, 1, UserHandle.USER_CURRENT);
mThreshold = (CustomSeekBarPreference) findPreference("network_traffic_autohide_threshold");
mThreshold.setValue(trafvalue);
mThreshold.setOnPreferenceChangeListener(this);
mShowArrows = (SystemSettingSwitchPreference) findPreference("network_traffic_arrow");
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 objValue) {
if (preference == mNetTrafficLocation) {
int location = Integer.valueOf((String) objValue);
int index = mNetTrafficLocation.findIndexOfValue((String) objValue);
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(getActivity().getContentResolver(),
Settings.System.NETWORK_TRAFFIC_VIEW_LOCATION, location-1, UserHandle.USER_CURRENT);
// And also enable the net monitor
Settings.System.putIntForUser(getActivity().getContentResolver(),
Settings.System.NETWORK_TRAFFIC_STATE, 1, UserHandle.USER_CURRENT);
updateTrafficLocation(location+1);
} else { // Disable net monitor completely
Settings.System.putIntForUser(getActivity().getContentResolver(),
Settings.System.NETWORK_TRAFFIC_STATE, 0, UserHandle.USER_CURRENT);
updateTrafficLocation(location);
}
return true;
} else if (preference == mNetTrafficLayout) {
int val = Integer.valueOf((String) objValue);
Settings.System.putIntForUser(getContentResolver(),
Settings.System.NETWORK_TRAFFIC_LAYOUT, val,
UserHandle.USER_CURRENT);
int index = mNetTrafficLayout.findIndexOfValue((String) objValue);
mNetTrafficLayout.setSummary(mNetTrafficLayout.getEntries()[index]);
return true;
} else if (preference == mThreshold) {
int val = (Integer) objValue;
Settings.System.putIntForUser(getContentResolver(),
Settings.System.NETWORK_TRAFFIC_AUTOHIDE_THRESHOLD, val,
UserHandle.USER_CURRENT);
return true;
} else if (preference == mNetTrafficType) {
int val = Integer.valueOf((String) objValue);
Settings.System.putIntForUser(getContentResolver(),
Settings.System.NETWORK_TRAFFIC_TYPE, val,
UserHandle.USER_CURRENT);
int index = mNetTrafficType.findIndexOfValue((String) objValue);
mNetTrafficType.setSummary(mNetTrafficType.getEntries()[index]);
return true;
} else if (preference == mNetTrafficSize) {
int width = ((Integer)objValue).intValue();
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.NETWORK_TRAFFIC_FONT_SIZE, width);
return true;
}
return false;
}
public void updateTrafficLocation(int location) {
switch(location){
case 0:
mThreshold.setEnabled(false);
mShowArrows.setEnabled(false);
mNetTrafficType.setEnabled(false);
mNetTrafficSize.setEnabled(false);
mNetTrafficLayout.setEnabled(false);
break;
case 1:
case 2:
mThreshold.setEnabled(true);
mShowArrows.setEnabled(true);
mNetTrafficType.setEnabled(true);
mNetTrafficSize.setEnabled(true);
mNetTrafficLayout.setEnabled(true);
break;
default:
break;
}
}
/**
* For Search.
*/
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
boolean enabled) {
ArrayList<SearchIndexableResource> result =
new ArrayList<SearchIndexableResource>();
SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.traffic;
result.add(sir);
return result;
}
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
return keys;
}
};
}