Skip to content

Commit

Permalink
refine the lock screen receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye committed Dec 18, 2014
1 parent 000d03a commit dec098f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/main/scala/com/github/shadowsocks/BaseService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ import android.content.Context

trait BaseService {

@volatile var state = State.INIT
@volatile var callbackCount = 0
@volatile private var state = State.INIT
@volatile private var callbackCount = 0

final val callbacks = new RemoteCallbackList[IShadowsocksServiceCallback]

Expand Down Expand Up @@ -100,6 +100,12 @@ trait BaseService {
def getTag: String
def getContext: Context

def getCallbackCount(): Int = {
callbackCount
}
def getState(): Int = {
state
}
def changeState(s: Int) {
changeState(s, null)
}
Expand Down
49 changes: 42 additions & 7 deletions src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import java.io.File
import java.lang.reflect.{InvocationTargetException, Method}
import java.util.Locale

import android.app.{Notification, NotificationManager, PendingIntent, Service}
import android.app._
import android.content._
import android.content.pm.{PackageInfo, PackageManager}
import android.net.{Network, ConnectivityManager}
Expand Down Expand Up @@ -75,6 +75,7 @@ class ShadowsocksNatService extends Service with BaseService {
private val mStartForegroundArgs = new Array[AnyRef](2)
private val mStopForegroundArgs = new Array[AnyRef](1)

var lockReceiver: BroadcastReceiver = null
var closeReceiver: BroadcastReceiver = null
var connReceiver: BroadcastReceiver = null
var notificationManager: NotificationManager = null
Expand Down Expand Up @@ -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])
openIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
val contentIntent = PendingIntent.getActivity(this, 0, openIntent, 0)
Expand All @@ -344,7 +345,11 @@ class ShadowsocksNatService extends Service with BaseService {
.setSmallIcon(R.drawable.ic_stat_shadowsocks)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.stop),
actionIntent)
.setPriority(NotificationCompat.PRIORITY_MIN)

if (visible)
builder.setPriority(NotificationCompat.PRIORITY_MAX)
else
builder.setPriority(NotificationCompat.PRIORITY_MIN)

startForegroundCompat(1, builder.build)
}
Expand Down Expand Up @@ -500,13 +505,39 @@ class ShadowsocksNatService extends Service with BaseService {
filter.addAction(Intent.ACTION_SHUTDOWN)
filter.addAction(Action.CLOSE)
closeReceiver = new BroadcastReceiver() {
def onReceive(p1: Context, p2: Intent) {
Toast.makeText(p1, R.string.stopping, Toast.LENGTH_SHORT).show()
def onReceive(context: Context, intent: Intent) {
Toast.makeText(context, R.string.stopping, Toast.LENGTH_SHORT).show()
stopRunner()
}
}
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
application.tracker.send(new HitBuilders.EventBuilder()
.setCategory(TAG)
Expand Down Expand Up @@ -554,7 +585,7 @@ class ShadowsocksNatService extends Service with BaseService {
flushDns()

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)
} else {
changeState(State.STOPPED, getString(R.string.service_failed))
Expand All @@ -574,6 +605,10 @@ class ShadowsocksNatService extends Service with BaseService {
unregisterReceiver(closeReceiver)
closeReceiver = null
}
if (lockReceiver != null) {
unregisterReceiver(lockReceiver)
lockReceiver = null
}

// send event
application.tracker.send(new HitBuilders.EventBuilder()
Expand All @@ -586,7 +621,7 @@ class ShadowsocksNatService extends Service with BaseService {
killProcesses()

// stop the service if no callback registered
if (callbackCount == 0) {
if (getCallbackCount == 0) {
stopSelf()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
}

// stop the service if no callback registered
if (callbackCount == 0) {
if (getCallbackCount == 0) {
stopSelf()
}

Expand Down

0 comments on commit dec098f

Please sign in to comment.