Commit 21518732 authored by Max Lv's avatar Max Lv

Merge pull request #449 from Mygod/master

Refine traffic format
parents b269d9a6 9b2671d5
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<declare-styleable name="SummaryPreference">
<attr name="summary" format="string" />
</declare-styleable>
<declare-styleable name="DropDownPreference"> <declare-styleable name="DropDownPreference">
<attr name="entries" format="reference" /> <attr name="entries" format="reference" />
<attr name="entryValues" format="reference" /> <attr name="entryValues" format="reference" />
......
...@@ -26,13 +26,13 @@ ...@@ -26,13 +26,13 @@
app:min="1" app:min="1"
app:max="65535" app:max="65535"
android:key="remotePortNum" android:key="remotePortNum"
app:summary="@string/remote_port_summary" android:summary="@string/remote_port_summary"
android:title="@string/remote_port"/> android:title="@string/remote_port"/>
<com.github.shadowsocks.preferences.NumberPickerPreference <com.github.shadowsocks.preferences.NumberPickerPreference
app:min="1025" app:min="1025"
app:max="65535" app:max="65535"
android:key="localPortNum" android:key="localPortNum"
app:summary="@string/port_summary" android:summary="@string/port_summary"
android:title="@string/port"/> android:title="@string/port"/>
<com.github.shadowsocks.preferences.PasswordEditTextPreference <com.github.shadowsocks.preferences.PasswordEditTextPreference
android:inputType="textPassword" android:inputType="textPassword"
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
android:key="encMethod" android:key="encMethod"
app:entries="@array/enc_method_entry" app:entries="@array/enc_method_entry"
app:entryValues="@array/enc_method_value" app:entryValues="@array/enc_method_value"
app:summary="%s" android:summary="%s"
android:title="@string/enc_method"/> android:title="@string/enc_method"/>
<SwitchPreference <SwitchPreference
android:key="isAuth" android:key="isAuth"
...@@ -59,7 +59,7 @@ ...@@ -59,7 +59,7 @@
android:key="route" android:key="route"
app:entries="@array/route_entry" app:entries="@array/route_entry"
app:entryValues="@array/route_value" app:entryValues="@array/route_value"
app:summary="%s" android:summary="%s"
android:title="@string/route_list"/> android:title="@string/route_list"/>
<SwitchPreference <SwitchPreference
android:key="isIpv6" android:key="isIpv6"
......
...@@ -38,7 +38,6 @@ final class DropDownPreference(private val mContext: Context, attrs: AttributeSe ...@@ -38,7 +38,6 @@ final class DropDownPreference(private val mContext: Context, attrs: AttributeSe
mSpinner.performClick mSpinner.performClick
true true
}) })
initSummary(mContext, attrs)
val a: TypedArray = mContext.obtainStyledAttributes(attrs, R.styleable.DropDownPreference) val a: TypedArray = mContext.obtainStyledAttributes(attrs, R.styleable.DropDownPreference)
setEntries(a.getTextArray(R.styleable.DropDownPreference_entries)) setEntries(a.getTextArray(R.styleable.DropDownPreference_entries))
mEntryValues = a.getTextArray(R.styleable.DropDownPreference_entryValues) mEntryValues = a.getTextArray(R.styleable.DropDownPreference_entryValues)
......
...@@ -22,7 +22,6 @@ final class NumberPickerPreference(context: Context, attrs: AttributeSet = null) ...@@ -22,7 +22,6 @@ final class NumberPickerPreference(context: Context, attrs: AttributeSet = null)
setMax(a.getInt(R.styleable.NumberPickerPreference_max, Int.MaxValue - 1)) setMax(a.getInt(R.styleable.NumberPickerPreference_max, Int.MaxValue - 1))
a.recycle a.recycle
} }
initSummary(context, attrs)
def getValue = value def getValue = value
def getMin = if (picker == null) 0 else picker.getMinValue def getMin = if (picker == null) 0 else picker.getMinValue
......
package com.github.shadowsocks.preferences package com.github.shadowsocks.preferences
import android.content.Context
import android.preference.Preference import android.preference.Preference
import android.util.AttributeSet
import com.github.shadowsocks.R
/** /**
* Make your preference support %s in summary. Override getSummaryValue to customize what to put in. * Make your preference support %s in summary. Override getSummaryValue to customize what to put in.
* @author Mygod * @author Mygod
*/ */
trait SummaryPreference extends Preference { trait SummaryPreference extends Preference {
private var mSummary: String = _
protected def initSummary(context: Context, attrs: AttributeSet) {
val a = context.obtainStyledAttributes(attrs, R.styleable.SummaryPreference)
mSummary = a.getString(R.styleable.SummaryPreference_summary)
a.recycle
}
protected def getSummaryValue: AnyRef protected def getSummaryValue: AnyRef
/** /**
...@@ -26,19 +15,5 @@ trait SummaryPreference extends Preference { ...@@ -26,19 +15,5 @@ trait SummaryPreference extends Preference {
* *
* @return the summary with appropriate string substitution * @return the summary with appropriate string substitution
*/ */
override def getSummary = { override def getSummary = String.format(super.getSummary.toString, getSummaryValue)
val entry = getSummaryValue
if (mSummary == null || entry == null) super.getSummary else String.format(mSummary, entry)
}
/**
* Sets the summary for this Preference with a CharSequence. If the summary has a String formatting marker in it
* (i.e. "%s" or "%1$s"), then the current entry value will be substituted in its place when it's retrieved.
*
* @param summary The summary for the preference.
*/
override def setSummary(summary: CharSequence) {
super.setSummary(summary)
if (summary == null && mSummary != null) mSummary = null
else if (summary != null && summary != mSummary) mSummary = summary.toString
}
} }
...@@ -27,11 +27,11 @@ object TrafficMonitor { ...@@ -27,11 +27,11 @@ object TrafficMonitor {
} }
private val units = Array("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB", "NB", "DB", "CB") private val units = Array("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB", "NB", "DB", "CB")
private val numberFormat = new DecimalFormat("0.00") private val numberFormat = new DecimalFormat("@@@")
def formatTraffic(size: Long): String = { def formatTraffic(size: Long): String = {
var n: Double = size var n: Double = size
var i = -1 var i = -1
while (n >= 1024) { while (n >= 1000) {
n /= 1024 n /= 1024
i = i + 1 i = i + 1
} }
...@@ -52,8 +52,8 @@ object TrafficMonitor { ...@@ -52,8 +52,8 @@ object TrafficMonitor {
} }
def update(tx: Long, rx: Long) { def update(tx: Long, rx: Long) {
txTotal = tx; txTotal = tx
rxTotal = rx; rxTotal = rx
} }
def reset() { def reset() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment