forked from HMBSbige/ShadowsocksR-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
27/40
- Loading branch information
Showing
3 changed files
with
147 additions
and
169 deletions.
There are no files selected for viewing
167 changes: 0 additions & 167 deletions
167
app/src/main/java/com/github/shadowsocks/ServiceBoundService.java
This file was deleted.
Oops, something went wrong.
145 changes: 145 additions & 0 deletions
145
app/src/main/java/com/github/shadowsocks/ServiceBoundService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters