Refine ShadowsocksDeathRecipient and ServiceBoundContext
This commit is contained in:
@@ -4,11 +4,14 @@ package com.github.shadowsocks.utils
|
||||
* @author Mygod
|
||||
*/
|
||||
object CloseUtils {
|
||||
type Closeable = {
|
||||
def close()
|
||||
}
|
||||
type Disconnectable = {
|
||||
def disconnect()
|
||||
}
|
||||
|
||||
def autoClose[A <: AutoCloseable, B](x: => A)(block: A => B): B = {
|
||||
def autoClose[A <: Closeable, B](x: => A)(block: A => B): B = {
|
||||
var a: Option[A] = None
|
||||
try {
|
||||
a = Some(x)
|
||||
|
||||
@@ -3,41 +3,32 @@ package com.github.shadowsocks.utils
|
||||
import java.io.{BufferedReader, FileInputStream, InputStreamReader}
|
||||
|
||||
import android.content.Context
|
||||
import com.github.shadowsocks.utils.IOUtils._
|
||||
import com.github.shadowsocks.utils.CloseUtils._
|
||||
|
||||
/**
|
||||
* @author chentaov5@gmail.com
|
||||
*/
|
||||
object ProcessUtils {
|
||||
|
||||
def getCurrentProcessName: String = {
|
||||
inSafe {
|
||||
val inputStream = new FileInputStream("/proc/self/cmdline")
|
||||
val reader = new BufferedReader(new InputStreamReader(inputStream))
|
||||
autoClose(reader) {
|
||||
val sb = new StringBuilder
|
||||
var c: Int = 0
|
||||
while({c = reader.read(); c > 0})
|
||||
sb.append(c.asInstanceOf[Char])
|
||||
sb.toString()
|
||||
}
|
||||
} match {
|
||||
case Some(name: String) => name
|
||||
case None => ""
|
||||
def getCurrentProcessName = autoClose(new FileInputStream("/proc/self/cmdline")) { inputStream =>
|
||||
autoClose(new BufferedReader(new InputStreamReader(inputStream))) { reader =>
|
||||
val sb = new StringBuilder
|
||||
var c = 0
|
||||
while ({c = reader.read(); c > 0}) sb.append(c.asInstanceOf[Char])
|
||||
sb.toString()
|
||||
}
|
||||
}
|
||||
|
||||
def inShadowsocks[A](context: Context)(fun: => A): Unit = {
|
||||
def inShadowsocks(context: Context)(fun: => Unit) {
|
||||
if (context != null && getCurrentProcessName.equals(context.getPackageName)) fun
|
||||
}
|
||||
|
||||
|
||||
def inVpn[A](context: Context)(fun: => A): Unit = {
|
||||
def inVpn(context: Context)(fun: => Unit) {
|
||||
if (context != null && getCurrentProcessName.equals(context.getPackageName + ":vpn")) fun
|
||||
}
|
||||
|
||||
|
||||
def inNat[A](context: Context)(fun: => A): Unit = {
|
||||
def inNat(context: Context)(fun: => Unit) {
|
||||
if (context != null && getCurrentProcessName.equals(context.getPackageName + ":nat")) fun
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user