Commit d9179fcc authored by Mygod's avatar Mygod

Fix ShadowsocksNotification

parent b3d188fb
...@@ -4,6 +4,7 @@ import java.util.Locale ...@@ -4,6 +4,7 @@ import java.util.Locale
import android.app.{KeyguardManager, PendingIntent} import android.app.{KeyguardManager, PendingIntent}
import android.content.{BroadcastReceiver, Context, Intent, IntentFilter} import android.content.{BroadcastReceiver, Context, Intent, IntentFilter}
import android.os.PowerManager
import android.support.v4.app.NotificationCompat import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationCompat.BigTextStyle import android.support.v4.app.NotificationCompat.BigTextStyle
import android.support.v4.content.ContextCompat import android.support.v4.content.ContextCompat
...@@ -39,7 +40,9 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str ...@@ -39,7 +40,9 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str
PendingIntent.getBroadcast(service, 0, new Intent(Action.CLOSE), 0)) PendingIntent.getBroadcast(service, 0, new Intent(Action.CLOSE), 0))
private lazy val style = new BigTextStyle(builder) private lazy val style = new BigTextStyle(builder)
private val showOnUnlock = visible && Utils.isLollipopOrAbove private val showOnUnlock = visible && Utils.isLollipopOrAbove
update() private var isVisible = true
update(if (service.getSystemService(Context.POWER_SERVICE).asInstanceOf[PowerManager].isScreenOn)
Intent.ACTION_SCREEN_ON else Intent.ACTION_SCREEN_OFF, true)
lockReceiver = (context: Context, intent: Intent) => update(intent.getAction) lockReceiver = (context: Context, intent: Intent) => update(intent.getAction)
val screenFilter = new IntentFilter() val screenFilter = new IntentFilter()
screenFilter.addAction(Intent.ACTION_SCREEN_ON) screenFilter.addAction(Intent.ACTION_SCREEN_ON)
...@@ -47,16 +50,16 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str ...@@ -47,16 +50,16 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str
if (showOnUnlock) screenFilter.addAction(Intent.ACTION_USER_PRESENT) if (showOnUnlock) screenFilter.addAction(Intent.ACTION_USER_PRESENT)
service.registerReceiver(lockReceiver, screenFilter) service.registerReceiver(lockReceiver, screenFilter)
def update(action: String = Intent.ACTION_SCREEN_ON) = if (service.getState == State.CONNECTED) { private def update(action: String, forceShow: Boolean = false) =
if (action == Intent.ACTION_SCREEN_OFF) { if (forceShow || service.getState == State.CONNECTED) action match {
if (showOnUnlock) setVisible(false) case Intent.ACTION_SCREEN_OFF =>
setVisible(false, forceShow)
unregisterCallback // unregister callback to save battery unregisterCallback // unregister callback to save battery
} else if (action == Intent.ACTION_SCREEN_ON) { case Intent.ACTION_SCREEN_ON =>
setVisible(showOnUnlock && !keyGuard.inKeyguardRestrictedInputMode, forceShow)
service.binder.registerCallback(callback) service.binder.registerCallback(callback)
callbackRegistered = true callbackRegistered = true
if (!keyGuard.inKeyguardRestrictedInputMode) setVisible(showOnUnlock) case Intent.ACTION_USER_PRESENT => setVisible(showOnUnlock, forceShow)
} else if (action == Intent.ACTION_USER_PRESENT) setVisible(showOnUnlock)
show()
} }
private def unregisterCallback = if (callbackRegistered) { private def unregisterCallback = if (callbackRegistered) {
...@@ -64,12 +67,13 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str ...@@ -64,12 +67,13 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str
callbackRegistered = false callbackRegistered = false
} }
def setVisible(visible: Boolean) = { def setVisible(visible: Boolean, forceShow: Boolean = false) = if (isVisible != visible) {
isVisible = visible
builder.setPriority(if (visible) NotificationCompat.PRIORITY_DEFAULT else NotificationCompat.PRIORITY_MIN) builder.setPriority(if (visible) NotificationCompat.PRIORITY_DEFAULT else NotificationCompat.PRIORITY_MIN)
this show()
} } else if (forceShow) show()
def notification = if (callbackRegistered) style.build() else builder.build() def notification = builder.build()
def show() = service.startForeground(1, notification) def show() = service.startForeground(1, notification)
def destroy() { def destroy() {
......
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