Skip to content

Commit

Permalink
Convert ServiceBoundContext
Browse files Browse the repository at this point in the history
26/40
  • Loading branch information
HMBSbige committed Nov 4, 2019
1 parent e03541a commit 23bd3f7
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class QuickToggleShortcut : Activity()
{
try
{
when (bgService.state)
when (bgService?.state)
{
Constants.State.STOPPED ->
{
Expand Down
171 changes: 0 additions & 171 deletions app/src/main/java/com/github/shadowsocks/ServiceBoundContext.java

This file was deleted.

147 changes: 147 additions & 0 deletions app/src/main/java/com/github/shadowsocks/ServiceBoundContext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.github.shadowsocks

import android.content.*
import android.os.*
import com.github.shadowsocks.aidl.*
import com.github.shadowsocks.utils.*

open class ServiceBoundContext(base: Context) : ContextWrapper(base), IBinder.DeathRecipient
{
companion object
{
private const val TAG = "ServiceBoundContext"
}

var bgService: IShadowsocksService? = null
private var binder: IBinder? = null
private var callback: IShadowsocksServiceCallback? = null
private var connection: ShadowsocksServiceConnection? = null
private var callbackRegistered: Boolean = false

/**
* register callback
*/
fun registerCallback()
{
if (bgService != null && callback != null && !callbackRegistered)
{
try
{
bgService!!.registerCallback(callback)
callbackRegistered = true
}
catch (e: Exception)
{
VayLog.e(TAG, "registerCallback", e)
}

}
}

/**
* unregister callback
*/
fun unregisterCallback()
{
if (bgService != null && callback != null && callbackRegistered)
{
try
{
bgService!!.unregisterCallback(callback)
}
catch (e: Exception)
{
VayLog.e(TAG, "unregisterCallback", e)
}

callbackRegistered = false
}
}

protected open fun onServiceConnected()
{
}

protected open fun onServiceDisconnected()
{
}

override fun binderDied()
{
}

@JvmOverloads
fun attachService(callback: IShadowsocksServiceCallback.Stub? = null)
{
this.callback = callback
if (bgService == null)
{
val clazz: Class<*>
clazz = ShadowsocksVpnService::class.java

val intent = Intent(this, clazz)
intent.action = Constants.Action.SERVICE

connection = ShadowsocksServiceConnection()
bindService(intent, connection!!, Context.BIND_AUTO_CREATE)
}
}

/**
* detach service
*/
fun detachService()
{
unregisterCallback()
callback = null
if (connection != null)
{
try
{
unbindService(connection!!)
}
catch (e: Exception)
{
VayLog.e(TAG, "detachService", e)
}

connection = null
}

if (binder != null)
{
binder!!.unlinkToDeath(this, 0)
binder = null
}

bgService = null
}

inner class ShadowsocksServiceConnection : ServiceConnection
{
override fun onServiceConnected(name: ComponentName, service: IBinder)
{
try
{
binder = service
service.linkToDeath(this@ServiceBoundContext, 0)
bgService = IShadowsocksService.Stub.asInterface(service)
registerCallback()
this@ServiceBoundContext.onServiceConnected()
}
catch (e: RemoteException)
{
VayLog.e(TAG, "onServiceConnected", e)
}

}

override fun onServiceDisconnected(name: ComponentName)
{
unregisterCallback()
this@ServiceBoundContext.onServiceDisconnected()
bgService = null
binder = null
}
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/com/github/shadowsocks/Shadowsocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public void onClick(View v) {
public void onClick(View v) {
if (serviceStarted) {
serviceStop();
} else if (mServiceBoundContext.bgService != null) {
} else if (mServiceBoundContext.getBgService() != null) {
prepareStartService();
} else {
changeSwitch(false);
Expand Down Expand Up @@ -459,9 +459,9 @@ private void updateState() {
}

private void updateState(boolean resetConnectionTest) {
if (mServiceBoundContext.bgService != null) {
if (mServiceBoundContext.getBgService() != null) {
try {
int state = mServiceBoundContext.bgService.getState();
int state = mServiceBoundContext.getBgService().getState();
switch (state) {
case Constants.State.CONNECTING:
fab.setBackgroundTintList(greyTint);
Expand Down Expand Up @@ -659,9 +659,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}

private void serviceStop() {
if (mServiceBoundContext.bgService != null) {
if (mServiceBoundContext.getBgService() != null) {
try {
mServiceBoundContext.bgService.use(-1);
mServiceBoundContext.getBgService().use(-1);
} catch (RemoteException e) {
e.printStackTrace();
}
Expand All @@ -673,7 +673,7 @@ private void serviceStop() {
*/
private void serviceLoad() {
try {
mServiceBoundContext.bgService.use(ShadowsocksApplication.app.profileId());
mServiceBoundContext.getBgService().use(ShadowsocksApplication.app.profileId());
} catch (RemoteException e) {
e.printStackTrace();
}
Expand Down
Loading

0 comments on commit 23bd3f7

Please sign in to comment.