Commit dfe469e1 authored by Mygod's avatar Mygod

Prevent bandwidth and border flashing in ProfilesFragment

parent f96f58f3
...@@ -93,7 +93,7 @@ trait BaseService extends Service { ...@@ -93,7 +93,7 @@ trait BaseService extends Service {
if (timer == null) { if (timer == null) {
timer = new Timer(true) timer = new Timer(true)
timer.schedule(new TimerTask { timer.schedule(new TimerTask {
def run(): Unit = if (TrafficMonitor.updateRate()) updateTrafficRate() def run(): Unit = if (state == State.CONNECTED && TrafficMonitor.updateRate()) updateTrafficRate()
}, 1000, 1000) }, 1000, 1000)
} }
TrafficMonitor.updateRate() TrafficMonitor.updateRate()
...@@ -288,8 +288,8 @@ trait BaseService extends Service { ...@@ -288,8 +288,8 @@ trait BaseService extends Service {
protected def changeState(s: Int, msg: String = null) { protected def changeState(s: Int, msg: String = null) {
val handler = new Handler(getMainLooper) val handler = new Handler(getMainLooper)
handler.post(() => if (state != s || msg != null) { if (state != s || msg != null) {
if (callbacks.getRegisteredCallbackCount > 0) { if (callbacks.getRegisteredCallbackCount > 0) handler.post(() => {
val n = callbacks.beginBroadcast() val n = callbacks.beginBroadcast()
for (i <- 0 until n) { for (i <- 0 until n) {
try { try {
...@@ -299,9 +299,9 @@ trait BaseService extends Service { ...@@ -299,9 +299,9 @@ trait BaseService extends Service {
} }
} }
callbacks.finishBroadcast() callbacks.finishBroadcast()
}
state = s
}) })
state = s
}
} }
def getBlackList: String = { def getBlackList: String = {
......
...@@ -143,7 +143,7 @@ class MainActivity extends Activity with ServiceBoundContext with Drawer.OnDrawe ...@@ -143,7 +143,7 @@ class MainActivity extends Activity with ServiceBoundContext with Drawer.OnDrawe
txRateText.setText(TrafficMonitor.formatTraffic(txRate) + "/s") txRateText.setText(TrafficMonitor.formatTraffic(txRate) + "/s")
rxRateText.setText(TrafficMonitor.formatTraffic(rxRate) + "/s") rxRateText.setText(TrafficMonitor.formatTraffic(rxRate) + "/s")
val child = getFragmentManager.findFragmentById(R.id.content).asInstanceOf[ToolbarFragment] val child = getFragmentManager.findFragmentById(R.id.content).asInstanceOf[ToolbarFragment]
if (child != null) child.onTrafficUpdated(txRate, rxRate, txTotal, rxTotal) if (state != State.STOPPING && child != null) child.onTrafficUpdated(txRate, rxRate, txTotal, rxTotal)
} }
override def onServiceConnected() { override def onServiceConnected() {
......
...@@ -23,7 +23,7 @@ package com.github.shadowsocks ...@@ -23,7 +23,7 @@ package com.github.shadowsocks
import java.util.GregorianCalendar import java.util.GregorianCalendar
import android.content._ import android.content._
import android.os.{Bundle, Handler} import android.os.Bundle
import android.support.v7.widget.RecyclerView.ViewHolder import android.support.v7.widget.RecyclerView.ViewHolder
import android.support.v7.widget._ import android.support.v7.widget._
import android.support.v7.widget.helper.ItemTouchHelper import android.support.v7.widget.helper.ItemTouchHelper
...@@ -93,27 +93,21 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -93,27 +93,21 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
share.setOnLongClickListener(cardButtonLongClickListener) share.setOnLongClickListener(cardButtonLongClickListener)
} }
def updateText(txTotal: Long = 0, rxTotal: Long = 0) {
val tx = item.tx + txTotal
val rx = item.rx + rxTotal
val title = item.getName
val address = if (item.nameIsEmpty) "" else item.formattedAddress
val traffic = getString(R.string.stat_profiles,
TrafficMonitor.formatTraffic(tx), TrafficMonitor.formatTraffic(rx))
handler.post(() => {
this.title.setText(title)
this.address.setText(address)
this.traffic.setText(traffic)
})
}
def bind(item: Profile) { def bind(item: Profile) {
this.item = item this.item = item
val editable = isProfileEditable(item.id) val editable = isProfileEditable(item.id)
edit.setEnabled(editable) edit.setEnabled(editable)
edit.setAlpha(if (editable) 1 else 0.5F) edit.setAlpha(if (editable) 1 else 0.5F)
updateText() var tx = item.tx
var rx = item.rx
if (item.id == app.profileId) {
tx += txTotal
rx += rxTotal
}
title.setText(item.getName)
address.setText(if (item.nameIsEmpty) "" else item.formattedAddress)
traffic.setText(getString(R.string.stat_profiles,
TrafficMonitor.formatTraffic(tx), TrafficMonitor.formatTraffic(rx)))
if (item.id == app.profileId) { if (item.id == app.profileId) {
indicator.setBackgroundResource(R.drawable.background_selected) indicator.setBackgroundResource(R.drawable.background_selected)
...@@ -239,10 +233,11 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -239,10 +233,11 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
} }
private var selectedItem: ProfileViewHolder = _ private var selectedItem: ProfileViewHolder = _
private val handler = new Handler
lazy val profilesAdapter = new ProfilesAdapter lazy val profilesAdapter = new ProfilesAdapter
private var undoManager: UndoSnackbarManager[Profile] = _ private var undoManager: UndoSnackbarManager[Profile] = _
private var txTotal: Long = _
private var rxTotal: Long = _
private lazy val clipboard = getActivity.getSystemService(Context.CLIPBOARD_SERVICE).asInstanceOf[ClipboardManager] private lazy val clipboard = getActivity.getSystemService(Context.CLIPBOARD_SERVICE).asInstanceOf[ClipboardManager]
...@@ -262,7 +257,9 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -262,7 +257,9 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
val profilesList = view.findViewById(R.id.profilesList).asInstanceOf[RecyclerView] val profilesList = view.findViewById(R.id.profilesList).asInstanceOf[RecyclerView]
val layoutManager = new LinearLayoutManager(getActivity) val layoutManager = new LinearLayoutManager(getActivity)
profilesList.setLayoutManager(layoutManager) profilesList.setLayoutManager(layoutManager)
profilesList.setItemAnimator(new DefaultItemAnimator()) val animator = new DefaultItemAnimator()
animator.setSupportsChangeAnimations(false) // prevent fading-in/out when rebinding
profilesList.setItemAnimator(animator)
profilesList.setAdapter(profilesAdapter) profilesList.setAdapter(profilesAdapter)
instance = this instance = this
layoutManager.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst { layoutManager.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst {
...@@ -289,8 +286,11 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -289,8 +286,11 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
}).attachToRecyclerView(profilesList) }).attachToRecyclerView(profilesList)
} }
override def onTrafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long): Unit = override def onTrafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long): Unit = {
if (selectedItem != null) selectedItem.updateText(txTotal, rxTotal) this.txTotal = txTotal
this.rxTotal = rxTotal
profilesAdapter.refreshId(app.profileId)
}
override def onDetach() { override def onDetach() {
undoManager.flush() undoManager.flush()
......
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