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.
26/40
- Loading branch information
Showing
6 changed files
with
163 additions
and
184 deletions.
There are no files selected for viewing
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
171 changes: 0 additions & 171 deletions
171
app/src/main/java/com/github/shadowsocks/ServiceBoundContext.java
This file was deleted.
Oops, something went wrong.
147 changes: 147 additions & 0 deletions
147
app/src/main/java/com/github/shadowsocks/ServiceBoundContext.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,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 | ||
} | ||
} | ||
} |
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
Oops, something went wrong.