Commit d0c4384c authored by Max Lv's avatar Max Lv

Avoid attach/detach service in the fragment

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