Commit dec098fb authored by Max Lv's avatar Max Lv

refine the lock screen receiver

parent 000d03ad
...@@ -49,8 +49,8 @@ import android.content.Context ...@@ -49,8 +49,8 @@ import android.content.Context
trait BaseService { trait BaseService {
@volatile var state = State.INIT @volatile private var state = State.INIT
@volatile var callbackCount = 0 @volatile private var callbackCount = 0
final val callbacks = new RemoteCallbackList[IShadowsocksServiceCallback] final val callbacks = new RemoteCallbackList[IShadowsocksServiceCallback]
...@@ -100,6 +100,12 @@ trait BaseService { ...@@ -100,6 +100,12 @@ trait BaseService {
def getTag: String def getTag: String
def getContext: Context def getContext: Context
def getCallbackCount(): Int = {
callbackCount
}
def getState(): Int = {
state
}
def changeState(s: Int) { def changeState(s: Int) {
changeState(s, null) changeState(s, null)
} }
......
...@@ -43,7 +43,7 @@ import java.io.File ...@@ -43,7 +43,7 @@ import java.io.File
import java.lang.reflect.{InvocationTargetException, Method} import java.lang.reflect.{InvocationTargetException, Method}
import java.util.Locale import java.util.Locale
import android.app.{Notification, NotificationManager, PendingIntent, Service} import android.app._
import android.content._ import android.content._
import android.content.pm.{PackageInfo, PackageManager} import android.content.pm.{PackageInfo, PackageManager}
import android.net.{Network, ConnectivityManager} import android.net.{Network, ConnectivityManager}
...@@ -75,6 +75,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -75,6 +75,7 @@ class ShadowsocksNatService extends Service with BaseService {
private val mStartForegroundArgs = new Array[AnyRef](2) private val mStartForegroundArgs = new Array[AnyRef](2)
private val mStopForegroundArgs = new Array[AnyRef](1) private val mStopForegroundArgs = new Array[AnyRef](1)
var lockReceiver: BroadcastReceiver = null
var closeReceiver: BroadcastReceiver = null var closeReceiver: BroadcastReceiver = null
var connReceiver: BroadcastReceiver = null var connReceiver: BroadcastReceiver = null
var notificationManager: NotificationManager = null var notificationManager: NotificationManager = null
...@@ -327,7 +328,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -327,7 +328,7 @@ class ShadowsocksNatService extends Service with BaseService {
} }
} }
def notifyForegroundAlert(title: String, info: String) { def notifyForegroundAlert(title: String, info: String, visible: Boolean) {
val openIntent = new Intent(this, classOf[Shadowsocks]) val openIntent = new Intent(this, classOf[Shadowsocks])
openIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) openIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
val contentIntent = PendingIntent.getActivity(this, 0, openIntent, 0) val contentIntent = PendingIntent.getActivity(this, 0, openIntent, 0)
...@@ -344,7 +345,11 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -344,7 +345,11 @@ class ShadowsocksNatService extends Service with BaseService {
.setSmallIcon(R.drawable.ic_stat_shadowsocks) .setSmallIcon(R.drawable.ic_stat_shadowsocks)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.stop), .addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.stop),
actionIntent) actionIntent)
.setPriority(NotificationCompat.PRIORITY_MIN)
if (visible)
builder.setPriority(NotificationCompat.PRIORITY_MAX)
else
builder.setPriority(NotificationCompat.PRIORITY_MIN)
startForegroundCompat(1, builder.build) startForegroundCompat(1, builder.build)
} }
...@@ -500,13 +505,39 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -500,13 +505,39 @@ class ShadowsocksNatService extends Service with BaseService {
filter.addAction(Intent.ACTION_SHUTDOWN) filter.addAction(Intent.ACTION_SHUTDOWN)
filter.addAction(Action.CLOSE) filter.addAction(Action.CLOSE)
closeReceiver = new BroadcastReceiver() { closeReceiver = new BroadcastReceiver() {
def onReceive(p1: Context, p2: Intent) { def onReceive(context: Context, intent: Intent) {
Toast.makeText(p1, R.string.stopping, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.stopping, Toast.LENGTH_SHORT).show()
stopRunner() stopRunner()
} }
} }
registerReceiver(closeReceiver, filter) registerReceiver(closeReceiver, filter)
val screenFilter = new IntentFilter()
screenFilter.addAction(Intent.ACTION_SCREEN_ON)
screenFilter.addAction(Intent.ACTION_SCREEN_OFF)
screenFilter.addAction(Intent.ACTION_USER_PRESENT)
lockReceiver = new BroadcastReceiver() {
def onReceive(context: Context, intent: Intent) {
if (getState == State.CONNECTED) {
val action = intent.getAction
if (action == Intent.ACTION_SCREEN_OFF) {
notifyForegroundAlert(getString(R.string.forward_success),
getString(R.string.service_running).formatLocal(Locale.ENGLISH, config.profileName), false)
} else if (action == Intent.ACTION_SCREEN_ON) {
val keyGuard = getSystemService(Context.KEYGUARD_SERVICE).asInstanceOf[KeyguardManager]
if (!keyGuard.inKeyguardRestrictedInputMode) {
notifyForegroundAlert(getString(R.string.forward_success),
getString(R.string.service_running).formatLocal(Locale.ENGLISH, config.profileName), true)
}
} else if (action == Intent.ACTION_USER_PRESENT) {
notifyForegroundAlert(getString(R.string.forward_success),
getString(R.string.service_running).formatLocal(Locale.ENGLISH, config.profileName), true)
}
}
}
}
registerReceiver(lockReceiver, screenFilter)
// send event // send event
application.tracker.send(new HitBuilders.EventBuilder() application.tracker.send(new HitBuilders.EventBuilder()
.setCategory(TAG) .setCategory(TAG)
...@@ -554,7 +585,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -554,7 +585,7 @@ class ShadowsocksNatService extends Service with BaseService {
flushDns() flushDns()
notifyForegroundAlert(getString(R.string.forward_success), notifyForegroundAlert(getString(R.string.forward_success),
getString(R.string.service_running).formatLocal(Locale.ENGLISH, config.profileName)) getString(R.string.service_running).formatLocal(Locale.ENGLISH, config.profileName), true)
changeState(State.CONNECTED) changeState(State.CONNECTED)
} else { } else {
changeState(State.STOPPED, getString(R.string.service_failed)) changeState(State.STOPPED, getString(R.string.service_failed))
...@@ -574,6 +605,10 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -574,6 +605,10 @@ class ShadowsocksNatService extends Service with BaseService {
unregisterReceiver(closeReceiver) unregisterReceiver(closeReceiver)
closeReceiver = null closeReceiver = null
} }
if (lockReceiver != null) {
unregisterReceiver(lockReceiver)
lockReceiver = null
}
// send event // send event
application.tracker.send(new HitBuilders.EventBuilder() application.tracker.send(new HitBuilders.EventBuilder()
...@@ -586,7 +621,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -586,7 +621,7 @@ class ShadowsocksNatService extends Service with BaseService {
killProcesses() killProcesses()
// stop the service if no callback registered // stop the service if no callback registered
if (callbackCount == 0) { if (getCallbackCount == 0) {
stopSelf() stopSelf()
} }
......
...@@ -479,7 +479,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -479,7 +479,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
} }
// stop the service if no callback registered // stop the service if no callback registered
if (callbackCount == 0) { if (getCallbackCount == 0) {
stopSelf() stopSelf()
} }
......
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