Commit e33adc4f authored by Mygod's avatar Mygod

Seriously clean up Shadowsocks MainActivity

This activity contains lots of obsolete code and it's time to do a cleanup.
parent 418ae215
...@@ -75,11 +75,13 @@ ...@@ -75,11 +75,13 @@
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:layout_marginTop="@dimen/fab_margin_top"> android:layout_marginTop="@dimen/fab_margin_top">
<android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:backgroundTint="@color/material_primary_500" app:backgroundTint="@color/material_primary_500"
app:srcCompat="@drawable/ic_start_idle" app:srcCompat="@drawable/ic_start_idle"
app:pressedTranslationZ="6dp" app:borderWidth="0dp" /> app:pressedTranslationZ="6dp"
app:borderWidth="0dp"/>
</com.github.jorgecastilloprz.FABProgressCircle> </com.github.jorgecastilloprz.FABProgressCircle>
<android.support.design.widget.CoordinatorLayout <android.support.design.widget.CoordinatorLayout
android:id="@+id/snackbar" android:id="@+id/snackbar"
......
...@@ -138,7 +138,7 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -138,7 +138,7 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
app.switchProfile(item.id) app.switchProfile(item.id)
profilesAdapter.refreshId(old) profilesAdapter.refreshId(old)
bind(item) bind(item)
if (state == State.CONNECTED) activity.serviceLoad() if (state == State.CONNECTED) activity.bgService.use(item.id) // reconnect to new profile
} }
} }
......
...@@ -30,7 +30,7 @@ import com.github.shadowsocks.ShadowsocksApplication.app ...@@ -30,7 +30,7 @@ import com.github.shadowsocks.ShadowsocksApplication.app
* @author Mygod * @author Mygod
*/ */
trait ServiceBoundContext extends Context with IBinder.DeathRecipient { trait ServiceBoundContext extends Context with IBinder.DeathRecipient {
class ShadowsocksServiceConnection extends ServiceConnection { private class ShadowsocksServiceConnection extends ServiceConnection {
override def onServiceConnected(name: ComponentName, service: IBinder) { override def onServiceConnected(name: ComponentName, service: IBinder) {
binder = service binder = service
service.linkToDeath(ServiceBoundContext.this, 0) service.linkToDeath(ServiceBoundContext.this, 0)
...@@ -52,7 +52,7 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient { ...@@ -52,7 +52,7 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient {
} }
} }
def setListeningForBandwidth(value: Boolean) { protected def setListeningForBandwidth(value: Boolean) {
if (listeningForBandwidth != value && bgService != null && callback != null) if (listeningForBandwidth != value && bgService != null && callback != null)
if (value) bgService.startListeningForBandwidth(callback) else bgService.stopListeningForBandwidth(callback) if (value) bgService.startListeningForBandwidth(callback) else bgService.stopListeningForBandwidth(callback)
listeningForBandwidth = value listeningForBandwidth = value
...@@ -66,8 +66,11 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient { ...@@ -66,8 +66,11 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient {
callbackRegistered = false callbackRegistered = false
} }
def onServiceConnected(): Unit = () protected def onServiceConnected(): Unit = ()
def onServiceDisconnected(): Unit = () /**
* Different from Android framework, this method will be called even when you call `detachService`.
*/
protected def onServiceDisconnected(): Unit = ()
override def binderDied(): Unit = () override def binderDied(): Unit = ()
private var callback: IShadowsocksServiceCallback.Stub = _ private var callback: IShadowsocksServiceCallback.Stub = _
...@@ -76,10 +79,10 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient { ...@@ -76,10 +79,10 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient {
private var listeningForBandwidth: Boolean = _ private var listeningForBandwidth: Boolean = _
// Variables // Variables
var binder: IBinder = _ private var binder: IBinder = _
var bgService: IShadowsocksService = _ var bgService: IShadowsocksService = _
def attachService(callback: IShadowsocksServiceCallback.Stub = null) { protected def attachService(callback: IShadowsocksServiceCallback.Stub = null) {
this.callback = callback this.callback = callback
if (bgService == null) { if (bgService == null) {
val s = if (app.isNatEnabled) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService] val s = if (app.isNatEnabled) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService]
...@@ -92,8 +95,9 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient { ...@@ -92,8 +95,9 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient {
} }
} }
def detachService() { protected def detachService() {
unregisterCallback() unregisterCallback()
onServiceDisconnected()
callback = null callback = null
if (connection != null) { if (connection != null) {
try unbindService(connection) catch { try unbindService(connection) catch {
......
...@@ -162,17 +162,20 @@ object Key { ...@@ -162,17 +162,20 @@ object Key {
} }
object State { object State {
/**
* This state will never be broadcast by the service. This state is only used to indicate that the current context
* hasn't bound to any context.
*/
val IDLE = 0
val CONNECTING = 1 val CONNECTING = 1
val CONNECTED = 2 val CONNECTED = 2
val STOPPING = 3 val STOPPING = 3
val STOPPED = 4 val STOPPED = 4
def isAvailable(state: Int): Boolean = state != CONNECTED && state != CONNECTING
} }
object Action { object Action {
final val SERVICE = "com.github.shadowsocks.SERVICE" final val SERVICE = "com.github.shadowsocks.SERVICE"
final val CLOSE = "com.github.shadowsocks.CLOSE" final val CLOSE = "com.github.shadowsocks.CLOSE"
final val CONNECTED = "com.github.shadowsocks.CONNECTED" // TODO
final val PROFILE_CHANGED = "com.github.shadowsocks.PROFILE_CHANGED" final val PROFILE_CHANGED = "com.github.shadowsocks.PROFILE_CHANGED"
final val PROFILE_REMOVED = "com.github.shadowsocks.PROFILE_REMOVED" final val PROFILE_REMOVED = "com.github.shadowsocks.PROFILE_REMOVED"
......
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