Commit d0c4384c authored by Max Lv's avatar Max Lv

Avoid attach/detach service in the fragment

parent 02513418
......@@ -43,6 +43,9 @@ libraryDependencies ++=
"com.google.android.gms" % "play-services-gcm" % "10.0.1" ::
"com.j256.ormlite" % "ormlite-android" % "5.0" ::
"com.mikepenz" % "materialdrawer" % "5.8.1" ::
"com.mikepenz" % "materialize" % "1.0.0" ::
"com.mikepenz" % "iconics-core" % "2.8.0" ::
"com.mikepenz" % "fastadapter" % "2.1.0" ::
"com.twofortyfouram" % "android-plugin-api-for-locale" % "1.0.2" ::
"dnsjava" % "dnsjava" % "2.1.7" ::
"eu.chainfire" % "libsuperuser" % "1.0.0.201608240809" ::
......
......@@ -51,8 +51,12 @@ import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem
import com.mikepenz.materialdrawer.model.{PrimaryDrawerItem, SecondaryDrawerItem}
import com.mikepenz.materialdrawer.{Drawer, DrawerBuilder}
trait TrafficCallback {
def update(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long)
}
object MainActivity {
private final val TAG = "MainActivity"
private final val TAG = "ShadowsocksMainActivity"
private final val REQUEST_CONNECT = 1
private final val DRAWER_PROFILES = 0L
......@@ -72,6 +76,9 @@ class MainActivity extends Activity with ServiceBoundContext with Drawer.OnDrawe
var state = State.STOPPED
var currentProfile = new Profile
var drawer: Drawer = _
@volatile var trafficCallback: TrafficCallback = _
private lazy val profilesFragment = new ProfilesFragment()
private lazy val globalSettingsFragment = new GlobalSettingsFragment()
......@@ -133,6 +140,7 @@ class MainActivity extends Activity with ServiceBoundContext with Drawer.OnDrawe
rxText.setText(TrafficMonitor.formatTraffic(rxTotal))
txRateText.setText(TrafficMonitor.formatTraffic(txRate) + "/s")
rxRateText.setText(TrafficMonitor.formatTraffic(rxRate) + "/s")
if (trafficCallback != null) trafficCallback.update(txRate, rxRate, txTotal, rxTotal)
}
def attachServiceCallback(): Unit = attachService(callback)
......@@ -372,6 +380,7 @@ class MainActivity extends Activity with ServiceBoundContext with Drawer.OnDrawe
private def updateState() {
if (bgService != null) {
Log.d(TAG, "bgService " + bgService.getState)
bgService.getState match {
case State.CONNECTING =>
fab.setBackgroundTintList(greyTint)
......
......@@ -216,25 +216,22 @@ final class ProfilesFragment extends ToolbarFragment with OnMenuItemClickListene
override def onAttach(activity: Activity) {
super.onAttach(activity)
activity.asInstanceOf[ServiceBoundContext].attachService(new IShadowsocksServiceCallback.Stub {
def stateChanged(state: Int, profileName: String, msg: String): Unit = () // ignore
def trafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long): Unit =
activity.asInstanceOf[MainActivity].trafficCallback = new TrafficCallback {
def update(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long): Unit =
if (selectedItem != null) selectedItem.updateText(txTotal, rxTotal)
})
}
}
override def onStart() {
super.onStart()
getActivity.asInstanceOf[ServiceBoundContext].registerCallback()
}
override def onStop() {
super.onStop()
getActivity.asInstanceOf[ServiceBoundContext].unregisterCallback()
}
override def onDetach() {
getActivity.asInstanceOf[ServiceBoundContext].detachService()
getActivity.asInstanceOf[MainActivity].trafficCallback = null
undoManager.flush()
super.onDetach()
}
......
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