From 29c51169ea2215dbab57e00aef637efb18d00aab Mon Sep 17 00:00:00 2001 From: Max Lv Date: Thu, 20 Feb 2014 12:53:45 +0800 Subject: [PATCH] delay the startup when screen locked --- .../ShadowsocksRunnerActivity.scala | 53 +++++++++++++------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala index 655cd598..a4b9b022 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala @@ -39,10 +39,10 @@ package com.github.shadowsocks -import android.app.Activity -import android.os.{IBinder, Bundle} +import android.app.{Activity, KeyguardManager} +import android.os.{IBinder, Bundle, Handler} import android.net.VpnService -import android.content.{Context, ComponentName, ServiceConnection, Intent} +import android.content._ import android.util.Log import android.preference.PreferenceManager import com.github.shadowsocks.utils._ @@ -53,28 +53,43 @@ class ShadowsocksRunnerActivity extends Activity { lazy val settings = PreferenceManager.getDefaultSharedPreferences(this) 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 var bgService: IShadowsocksService = null val connection = new ServiceConnection { override def onServiceConnected(name: ComponentName, service: IBinder) { bgService = IShadowsocksService.Stub.asInterface(service) - 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() - } + handler.postDelayed(new Runnable() { + override def run() = startBackgroundService() + }, 1000) } override def onServiceDisconnected(name: ComponentName) { 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() { if (bgService == null) { val s = if (isRoot) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService] @@ -94,12 +109,20 @@ class ShadowsocksRunnerActivity extends Activity { override def onCreate(savedInstanceState: Bundle) { 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() { super.onDestroy() deattachService() + unregisterReceiver(receiver) } override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {