Skip to content

Commit

Permalink
Convert ServiceBoundService
Browse files Browse the repository at this point in the history
27/40
  • Loading branch information
HMBSbige committed Nov 4, 2019
1 parent 23bd3f7 commit 20482fe
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 169 deletions.
167 changes: 0 additions & 167 deletions app/src/main/java/com/github/shadowsocks/ServiceBoundService.java

This file was deleted.

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

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

abstract class ServiceBoundService : Service(), IBinder.DeathRecipient
{
companion object
{
private const val TAG = "ServiceBoundService"
}

protected 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
*/
private fun registerCallback()
{
if (bgService != null && callback != null && !callbackRegistered)
{
try
{
bgService!!.registerCallback(callback)
callbackRegistered = true
}
catch (e: Exception)
{
VayLog.e(TAG, "registerCallback", e)
}

}
}

/**
* unregister callback
*/
protected 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 intent = Intent(this, ShadowsocksVpnService::class.java)
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@ServiceBoundService, 0)
bgService = IShadowsocksService.Stub.asInterface(service)
registerCallback()
this@ServiceBoundService.onServiceConnected()
}
catch (e: RemoteException)
{
VayLog.e(TAG, "onServiceConnected", e)
}

}

override fun onServiceDisconnected(name: ComponentName)
{
unregisterCallback()
this@ServiceBoundService.onServiceDisconnected()
bgService = null
binder = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {

@Override
protected void onServiceConnected() {
if (bgService != null) {
if (getBgService() != null) {
if (ShadowsocksApplication.app.isNatEnabled()) {
startBackgroundService();
} else if (VpnService.prepare(ShadowsocksRunnerService.this) == null) {
Expand All @@ -110,7 +110,7 @@ protected void onServiceDisconnected() {

private void startBackgroundService() {
try {
bgService.use(ShadowsocksApplication.app.profileId());
getBgService().use(ShadowsocksApplication.app.profileId());
} catch (RemoteException e) {
VayLog.INSTANCE.e(TAG, "startBackgroundService", e);
ShadowsocksApplication.app.track(e);
Expand Down

0 comments on commit 20482fe

Please sign in to comment.