Commit 29c51169 authored by Max Lv's avatar Max Lv

delay the startup when screen locked

parent a17f12d3
...@@ -39,10 +39,10 @@ ...@@ -39,10 +39,10 @@
package com.github.shadowsocks package com.github.shadowsocks
import android.app.Activity import android.app.{Activity, KeyguardManager}
import android.os.{IBinder, Bundle} import android.os.{IBinder, Bundle, Handler}
import android.net.VpnService import android.net.VpnService
import android.content.{Context, ComponentName, ServiceConnection, Intent} import android.content._
import android.util.Log import android.util.Log
import android.preference.PreferenceManager import android.preference.PreferenceManager
import com.github.shadowsocks.utils._ import com.github.shadowsocks.utils._
...@@ -53,28 +53,43 @@ class ShadowsocksRunnerActivity extends Activity { ...@@ -53,28 +53,43 @@ class ShadowsocksRunnerActivity extends Activity {
lazy val settings = PreferenceManager.getDefaultSharedPreferences(this) lazy val settings = PreferenceManager.getDefaultSharedPreferences(this)
lazy val isRoot = Utils.getRoot lazy val isRoot = Utils.getRoot
val handler = new Handler()
val receiver = new BroadcastReceiver() {
override def onReceive(context: Context, intent: Intent) {
if (intent.getAction == Intent.ACTION_USER_PRESENT) {
attachService()
}
}
}
// Services // Services
var bgService: IShadowsocksService = null var bgService: IShadowsocksService = null
val connection = new ServiceConnection { val connection = new ServiceConnection {
override def onServiceConnected(name: ComponentName, service: IBinder) { override def onServiceConnected(name: ComponentName, service: IBinder) {
bgService = IShadowsocksService.Stub.asInterface(service) bgService = IShadowsocksService.Stub.asInterface(service)
if (!isRoot) { handler.postDelayed(new Runnable() {
val intent = VpnService.prepare(ShadowsocksRunnerActivity.this) override def run() = startBackgroundService()
if (intent != null) { }, 1000)
startActivityForResult(intent, Shadowsocks.REQUEST_CONNECT)
} else {
onActivityResult(Shadowsocks.REQUEST_CONNECT, Activity.RESULT_OK, null)
}
} else {
bgService.start(ConfigUtils.load(settings))
finish()
}
} }
override def onServiceDisconnected(name: ComponentName) { override def onServiceDisconnected(name: ComponentName) {
bgService = null bgService = null
} }
} }
def startBackgroundService() {
if (!isRoot) {
val intent = VpnService.prepare(ShadowsocksRunnerActivity.this)
if (intent != null) {
startActivityForResult(intent, Shadowsocks.REQUEST_CONNECT)
} else {
onActivityResult(Shadowsocks.REQUEST_CONNECT, Activity.RESULT_OK, null)
}
} else {
bgService.start(ConfigUtils.load(settings))
finish()
}
}
def attachService() { def attachService() {
if (bgService == null) { if (bgService == null) {
val s = if (isRoot) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService] val s = if (isRoot) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService]
...@@ -94,12 +109,20 @@ class ShadowsocksRunnerActivity extends Activity { ...@@ -94,12 +109,20 @@ class ShadowsocksRunnerActivity extends Activity {
override def onCreate(savedInstanceState: Bundle) { override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
attachService() val km = getSystemService(Context.KEYGUARD_SERVICE).asInstanceOf[KeyguardManager]
val locked = km.inKeyguardRestrictedInputMode
if (locked) {
val filter = new IntentFilter(Intent.ACTION_USER_PRESENT)
registerReceiver(receiver, filter)
} else {
attachService()
}
} }
override def onDestroy() { override def onDestroy() {
super.onDestroy() super.onDestroy()
deattachService() deattachService()
unregisterReceiver(receiver)
} }
override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment