Make speed frequency and samples configurable

This commit is contained in:
M66B 2015-12-13 15:54:23 +01:00
parent eb30bae389
commit be15c4e2ee
4 changed files with 56 additions and 27 deletions

View File

@ -106,6 +106,12 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
EditTextPreference pref_stats_base = (EditTextPreference) screen.findPreference("stats_base");
pref_stats_base.setTitle(getString(R.string.setting_stats_base, prefs.getString("stats_base", "5")));
EditTextPreference pref_stats_frequency = (EditTextPreference) screen.findPreference("stats_frequency");
pref_stats_frequency.setTitle(getString(R.string.setting_stats_frequency, prefs.getString("stats_frequency", "1000")));
EditTextPreference pref_stats_samples = (EditTextPreference) screen.findPreference("stats_samples");
pref_stats_samples.setTitle(getString(R.string.setting_stats_samples, prefs.getString("stats_samples", "90")));
// Wi-Fi home
MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) screen.findPreference("wifi_homes");
Set<String> ssid = prefs.getStringSet("wifi_homes", new HashSet<String>());
@ -291,6 +297,12 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
else if ("stats_base".equals(name))
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_base, prefs.getString(name, "5")));
else if ("stats_frequency".equals(name))
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_frequency, prefs.getString(name, "1000")));
else if ("stats_samples".equals(name))
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_samples, prefs.getString(name, "90")));
else if ("auto_enable".equals(name))
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_auto, prefs.getString(name, "0")));

View File

@ -266,9 +266,6 @@ public class SinkholeService extends VpnService {
private HashMap<ApplicationInfo, Long> app = new HashMap<>();
private final static int STATS_POINTS = 100;
private final static int STATS_FREQUENCY = 1000;
private void startStats() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
boolean enabled = (!stats && prefs.getBoolean("show_stats", false));
@ -296,12 +293,17 @@ public class SinkholeService extends VpnService {
private void updateStats() {
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.traffic);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
int frequency = Integer.parseInt(prefs.getString("stats_frequency", "1000"));
int samples = Integer.parseInt(prefs.getString("stats_samples", "90"));
float base = Integer.parseInt(prefs.getString("stats_base", "5")) * 1000f;
// Schedule next update
mServiceHandler.sendEmptyMessageDelayed(MSG_STATS_UPDATE, STATS_FREQUENCY);
mServiceHandler.sendEmptyMessageDelayed(MSG_STATS_UPDATE, frequency);
long ct = SystemClock.elapsedRealtime();
// Cleanup
while (gt.size() >= STATS_POINTS) {
while (gt.size() > 0 && ct - gt.get(0) > samples * 1000) {
gt.remove(0);
gtx.remove(0);
grx.remove(0);
@ -310,7 +312,6 @@ public class SinkholeService extends VpnService {
// Calculate network speed
float txsec = 0;
float rxsec = 0;
long ct = SystemClock.elapsedRealtime();
long ctx = TrafficStats.getTotalTxBytes();
long rtx = TrafficStats.getTotalRxBytes();
if (t > 0 && tx > 0 && rx > 0) {
@ -377,8 +378,6 @@ public class SinkholeService extends VpnService {
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.TRANSPARENT);
float base = Integer.parseInt(prefs.getString("stats_base", "5")) * 1000f;
// Determine max
long xmax = 0;
float ymax = base * 1.5f;
@ -398,7 +397,7 @@ public class SinkholeService extends VpnService {
Path ptx = new Path();
Path prx = new Path();
for (int i = 0; i < gtx.size(); i++) {
float x = width - width * (xmax - gt.get(i)) / 1000f / STATS_POINTS;
float x = width - width * (xmax - gt.get(i)) / 1000f / samples;
float ytx = height - height * gtx.get(i) / ymax;
float yrx = height - height * grx.get(i) / ymax;
if (i == 0) {

View File

@ -24,9 +24,6 @@ These issues are caused by bugs in Android, or in the software provided by the m
<string name="setting_options">Options</string>
<string name="setting_system">Manage system applications</string>
<string name="setting_stats">Show speed notification</string>
<string name="setting_stats_base">Speed baseline: %s KB/s</string>
<string name="setting_stats_top">Show top network using applications</string>
<string name="setting_auto">Auto enable after %1$s minutes</string>
<string name="setting_dark">Use dark theme</string>
<string name="setting_wifi_home">Wi-Fi home networks: %1$s</string>
@ -36,6 +33,13 @@ These issues are caused by bugs in Android, or in the software provided by the m
<string name="setting_metered_4g">Consider LTE unmetered</string>
<string name="setting_national_roaming">Ignore national roaming</string>
<string name="setting_stats_category">Speed notification</string>
<string name="setting_stats">Show speed notification</string>
<string name="setting_stats_top">Show top applications</string>
<string name="setting_stats_base">Speed baseline: %s KB/s</string>
<string name="setting_stats_frequency">Sample frequency: %s ms</string>
<string name="setting_stats_samples">Number of samples: %s s</string>
<string name="setting_backup">Backup</string>
<string name="setting_export">Export settings</string>
<string name="setting_import">Import settings</string>

View File

@ -32,21 +32,6 @@
android:key="manage_system"
android:summary="@string/summary_system"
android:title="@string/setting_system" />
<SwitchPreference
android:defaultValue="false"
android:key="show_stats"
android:summary="@string/summary_stats"
android:title="@string/setting_stats" />
<EditTextPreference
android:defaultValue="5"
android:dependency="show_stats"
android:inputType="number"
android:key="stats_base" />
<SwitchPreference
android:defaultValue="false"
android:dependency="show_stats"
android:key="show_top"
android:title="@string/setting_stats_top" />
<EditTextPreference
android:defaultValue="0"
android:inputType="number"
@ -86,6 +71,35 @@
android:summary="@string/summary_national_roaming"
android:title="@string/setting_national_roaming" />
</PreferenceCategory>
<PreferenceCategory
android:key="category_options"
android:title="@string/setting_stats_category">
<SwitchPreference
android:defaultValue="false"
android:key="show_stats"
android:summary="@string/summary_stats"
android:title="@string/setting_stats" />
<SwitchPreference
android:defaultValue="false"
android:dependency="show_stats"
android:key="show_top"
android:title="@string/setting_stats_top" />
<EditTextPreference
android:defaultValue="5"
android:dependency="show_stats"
android:inputType="number"
android:key="stats_base" />
<EditTextPreference
android:defaultValue="1000"
android:dependency="show_stats"
android:inputType="number"
android:key="stats_frequency" />
<EditTextPreference
android:defaultValue="90"
android:dependency="show_stats"
android:inputType="number"
android:key="stats_samples" />
</PreferenceCategory>
<PreferenceCategory
android:key="category_backup"
android:title="@string/setting_backup">