Commit 24bb2006 authored by Max Lv's avatar Max Lv

fix several bugs

parent 8933732e
...@@ -5,8 +5,8 @@ import org.scalasbt.androidplugin._ ...@@ -5,8 +5,8 @@ import org.scalasbt.androidplugin._
import org.scalasbt.androidplugin.AndroidKeys._ import org.scalasbt.androidplugin.AndroidKeys._
object App { object App {
val version = "1.4.1" val version = "1.4.2"
val versionCode = 23 val versionCode = 24
} }
object General { object General {
......
...@@ -37,12 +37,8 @@ ...@@ -37,12 +37,8 @@
*/ */
package com.github.shadowsocks package com.github.shadowsocks
import android.app.Notification import android.app.{NotificationManager, Notification, PendingIntent, Service}
import android.app.PendingIntent import android.content._
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageInfo import android.content.pm.PackageInfo
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os._ import android.os._
...@@ -222,7 +218,6 @@ class ShadowVpnService extends VpnService { ...@@ -222,7 +218,6 @@ class ShadowVpnService extends VpnService {
} }
if (resolved && handleConnection) { if (resolved && handleConnection) {
notifyAlert(getString(R.string.forward_success), getString(R.string.service_running))
handler.sendEmptyMessageDelayed(MSG_CONNECT_SUCCESS, 500) handler.sendEmptyMessageDelayed(MSG_CONNECT_SUCCESS, 500)
} else { } else {
notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed)) notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed))
...@@ -340,10 +335,13 @@ class ShadowVpnService extends VpnService { ...@@ -340,10 +335,13 @@ class ShadowVpnService extends VpnService {
val openIntent: Intent = new Intent(this, classOf[Shadowsocks]) val openIntent: Intent = new Intent(this, classOf[Shadowsocks])
openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val contentIntent: PendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0) val contentIntent: PendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0)
val closeIntent: Intent = new Intent(Utils.CLOSE_ACTION)
val actionIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, closeIntent, 0)
val builder: NotificationCompat.Builder = new NotificationCompat.Builder(this) val builder: NotificationCompat.Builder = new NotificationCompat.Builder(this)
builder.setSmallIcon(R.drawable.ic_stat_shadowsocks).setWhen(0).setTicker(title).setContentTitle(getString(R.string.app_name)).setContentText(info).setContentIntent(contentIntent).addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.stop), actionIntent) builder
.setSmallIcon(R.drawable.ic_stat_shadowsocks).setWhen(0)
.setTicker(title).setContentTitle(getString(R.string.app_name))
.setContentText(info).setContentIntent(contentIntent)
.setAutoCancel(true)
notificationManager.notify(1, builder.build)
} }
override def onBind(intent: Intent): IBinder = { override def onBind(intent: Intent): IBinder = {
...@@ -358,11 +356,20 @@ class ShadowVpnService extends VpnService { ...@@ -358,11 +356,20 @@ class ShadowVpnService extends VpnService {
super.onCreate() super.onCreate()
EasyTracker.getTracker.sendEvent("service", "start", getVersionName, 0L) EasyTracker.getTracker.sendEvent("service", "start", getVersionName, 0L)
settings = PreferenceManager.getDefaultSharedPreferences(this) settings = PreferenceManager.getDefaultSharedPreferences(this)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE).asInstanceOf[NotificationManager]
// register close receiver
val filter = new IntentFilter()
filter.addAction(Intent.ACTION_SHUTDOWN)
filter.addAction(Utils.CLOSE_ACTION)
receiver = new CloseReceiver
registerReceiver(receiver, filter)
} }
/** Called when the activity is closed. */ def destroy() {
override def onDestroy() {
EasyTracker.getTracker.sendEvent("service", "stop", getVersionName, 0L) EasyTracker.getTracker.sendEvent("service", "stop", getVersionName, 0L)
if (receiver != null) unregisterReceiver(receiver)
new Thread { new Thread {
override def run() { override def run() {
onDisconnect() onDisconnect()
...@@ -376,21 +383,27 @@ class ShadowVpnService extends VpnService { ...@@ -376,21 +383,27 @@ class ShadowVpnService extends VpnService {
conn.close() conn.close()
conn = null conn = null
} }
super.onDestroy()
markServiceStopped() markServiceStopped()
} }
/** Called when the activity is closed. */
override def onDestroy() {
destroy()
super.onDestroy()
}
def onDisconnect() { def onDisconnect() {
Utils.runRootCommand(Utils.getIptables + " -t nat -F OUTPUT")
val sb = new StringBuilder val sb = new StringBuilder
if (!waitForProcess("redsocks")) {
sb.append("kill -9 `cat /data/data/com.github.shadowsocks/redsocks.pid`").append("\n") sb.append("kill -9 `cat /data/data/com.github.shadowsocks/redsocks.pid`").append("\n")
}
if (!waitForProcess("shadowsocks")) { if (!waitForProcess("shadowsocks")) {
sb.append("kill -9 `cat /data/data/com.github.shadowsocks/shadowsocks.pid`").append("\n") sb.append("kill -9 `cat /data/data/com.github.shadowsocks/shadowsocks.pid`").append("\n")
} }
if (!waitForProcess("tun2socks")) { if (!waitForProcess("tun2socks")) {
sb.append("kill -9 `cat /data/data/com.github.shadowsocks/tun2socks.pid`").append("\n") sb.append("kill -9 `cat /data/data/com.github.shadowsocks/tun2socks.pid`").append("\n")
} }
Utils.runRootCommand(sb.toString()) Utils.runCommand(sb.toString())
} }
override def onStart(intent: Intent, startId: Int) { override def onStart(intent: Intent, startId: Int) {
...@@ -406,6 +419,13 @@ class ShadowVpnService extends VpnService { ...@@ -406,6 +419,13 @@ class ShadowVpnService extends VpnService {
stopSelf() stopSelf()
} }
class CloseReceiver extends BroadcastReceiver {
def onReceive(p1: Context, p2: Intent) {
destroy()
stopSelf()
}
}
val handler: Handler = new Handler { val handler: Handler = new Handler {
override def handleMessage(msg: Message) { override def handleMessage(msg: Message) {
val ed: SharedPreferences.Editor = settings.edit val ed: SharedPreferences.Editor = settings.edit
...@@ -428,6 +448,9 @@ class ShadowVpnService extends VpnService { ...@@ -428,6 +448,9 @@ class ShadowVpnService extends VpnService {
super.handleMessage(msg) super.handleMessage(msg)
} }
} }
var notificationManager: NotificationManager = null
var receiver: CloseReceiver = null
var appHost: String = null var appHost: String = null
var remotePort: Int = 0 var remotePort: Int = 0
var localPort: Int = 0 var localPort: Int = 0
......
...@@ -110,14 +110,15 @@ object Shadowsocks { ...@@ -110,14 +110,15 @@ object Shadowsocks {
private def setPreferenceEnabled() { private def setPreferenceEnabled() {
val settings: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity) val settings: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity)
val enabled: Boolean = !settings.getBoolean("isRunning", false) && !settings.getBoolean("isConnecting", false) val enabled: Boolean = !settings.getBoolean("isRunning", false) && !settings.getBoolean("isConnecting", false)
for (name <- FEATRUE_PREFS) { for (name <- Shadowsocks.FEATRUE_PREFS) {
val pref: Preference = findPreference(name) val pref: Preference = findPreference(name)
if (pref != null) { if (pref != null) {
if ((name == "isBypassApps") || (name == "proxyedApps")) { if ((name == "isBypassApps") || (name == "proxyedApps")) {
val isGlobalProxy: Boolean = settings.getBoolean("isGlobalProxy", false) val isGlobalProxy: Boolean = settings.getBoolean("isGlobalProxy", false)
pref.setEnabled(enabled && !isGlobalProxy) pref.setEnabled(enabled && !isGlobalProxy)
} } else if (name == "isAutoConnect") {
else { pref.setEnabled(Utils.getRoot)
} else {
pref.setEnabled(enabled) pref.setEnabled(enabled)
} }
} }
...@@ -411,8 +412,9 @@ class Shadowsocks extends UnifiedSherlockPreferenceActivity with CompoundButton. ...@@ -411,8 +412,9 @@ class Shadowsocks extends UnifiedSherlockPreferenceActivity with CompoundButton.
if ((name == "isBypassApps") || (name == "proxyedApps")) { if ((name == "isBypassApps") || (name == "proxyedApps")) {
val isGlobalProxy: Boolean = settings.getBoolean("isGlobalProxy", false) val isGlobalProxy: Boolean = settings.getBoolean("isGlobalProxy", false)
pref.setEnabled(enabled && !isGlobalProxy) pref.setEnabled(enabled && !isGlobalProxy)
} } else if (name == "isAutoConnect") {
else { pref.setEnabled(Utils.getRoot)
} else {
pref.setEnabled(enabled) pref.setEnabled(enabled)
} }
} }
...@@ -531,7 +533,7 @@ class Shadowsocks extends UnifiedSherlockPreferenceActivity with CompoundButton. ...@@ -531,7 +533,7 @@ class Shadowsocks extends UnifiedSherlockPreferenceActivity with CompoundButton.
stopService(new Intent(this, classOf[ShadowVpnService])) stopService(new Intent(this, classOf[ShadowVpnService]))
} }
if (ShadowsocksService.isServiceStarted) { if (ShadowsocksService.isServiceStarted) {
stopService(new Intent(this, classOf[ShadowsocksService])) sendBroadcast(new Intent(Utils.CLOSE_ACTION))
} }
} }
......
...@@ -90,11 +90,8 @@ class ShadowsocksReceiver extends BroadcastReceiver { ...@@ -90,11 +90,8 @@ class ShadowsocksReceiver extends BroadcastReceiver {
if (ShadowsocksService.isServiceStarted) return if (ShadowsocksService.isServiceStarted) return
val it: Intent = new Intent(context, classOf[ShadowsocksService]) val it: Intent = new Intent(context, classOf[ShadowsocksService])
context.startService(it) context.startService(it)
} else {
if (ShadowVpnService.isServiceStarted) return
val it: Intent = new Intent(context, classOf[ShadowVpnService])
context.startService(it)
} }
} }
} }
} }
\ No newline at end of file
...@@ -61,9 +61,6 @@ import java.io.IOException ...@@ -61,9 +61,6 @@ import java.io.IOException
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method import java.lang.reflect.Method
import java.net.Inet6Address
import java.net.InetAddress
import java.net.UnknownHostException
import org.apache.http.conn.util.InetAddressUtils import org.apache.http.conn.util.InetAddressUtils
import scala.collection._ import scala.collection._
import org.xbill.DNS._ import org.xbill.DNS._
...@@ -243,7 +240,7 @@ class ShadowsocksService extends Service { ...@@ -243,7 +240,7 @@ class ShadowsocksService extends Service {
Log.d(TAG, "IPTABLES: " + Utils.getIptables) Log.d(TAG, "IPTABLES: " + Utils.getIptables)
hasRedirectSupport = Utils.getHasRedirectSupport hasRedirectSupport = Utils.getHasRedirectSupport
if (resolved && handleConnection) { if (resolved && handleConnection) {
notifyAlert(getString(R.string.forward_success), getString(R.string.service_running)) notifyForegroundAlert(getString(R.string.forward_success), getString(R.string.service_running))
handler.sendEmptyMessageDelayed(MSG_CONNECT_SUCCESS, 500) handler.sendEmptyMessageDelayed(MSG_CONNECT_SUCCESS, 500)
} }
else { else {
...@@ -323,17 +320,35 @@ class ShadowsocksService extends Service { ...@@ -323,17 +320,35 @@ class ShadowsocksService extends Service {
ShadowsocksService.sRunningInstance = null ShadowsocksService.sRunningInstance = null
} }
def notifyAlert(title: String, info: String) { def notifyForegroundAlert(title: String, info: String) {
val openIntent: Intent = new Intent(this, classOf[Shadowsocks]) val openIntent: Intent = new Intent(this, classOf[Shadowsocks])
openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val contentIntent: PendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0) val contentIntent: PendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0)
val closeIntent: Intent = new Intent(Utils.CLOSE_ACTION) val closeIntent: Intent = new Intent(Utils.CLOSE_ACTION)
val actionIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, closeIntent, 0) val actionIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, closeIntent, 0)
val builder: NotificationCompat.Builder = new NotificationCompat.Builder(this) val builder: NotificationCompat.Builder = new NotificationCompat.Builder(this)
builder.setSmallIcon(R.drawable.ic_stat_shadowsocks).setWhen(0).setTicker(title).setContentTitle(getString(R.string.app_name)).setContentText(info).setContentIntent(contentIntent).addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.stop), actionIntent) builder
.setSmallIcon(R.drawable.ic_stat_shadowsocks).setWhen(0)
.setTicker(title).setContentTitle(getString(R.string.app_name))
.setContentText(info).setContentIntent(contentIntent)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.stop), actionIntent)
startForegroundCompat(1, builder.build) startForegroundCompat(1, builder.build)
} }
def notifyAlert(title: String, info: String) {
val openIntent: Intent = new Intent(this, classOf[Shadowsocks])
openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val contentIntent: PendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0)
val builder: NotificationCompat.Builder = new NotificationCompat.Builder(this)
builder
.setSmallIcon(R.drawable.ic_stat_shadowsocks).setWhen(0)
.setTicker(title)
.setContentTitle(getString(R.string.app_name))
.setContentText(info).setContentIntent(contentIntent)
.setAutoCancel(true)
notificationManager.notify(1, builder.build)
}
def onBind(intent: Intent): IBinder = { def onBind(intent: Intent): IBinder = {
null null
} }
...@@ -378,15 +393,8 @@ class ShadowsocksService extends Service { ...@@ -378,15 +393,8 @@ class ShadowsocksService extends Service {
ed.putBoolean("isRunning", false) ed.putBoolean("isRunning", false)
ed.putBoolean("isConnecting", false) ed.putBoolean("isConnecting", false)
ed.commit ed.commit
try {
notificationManager.cancel(0)
}
catch {
case ignore: Exception => {
}
}
super.onDestroy()
markServiceStopped() markServiceStopped()
super.onDestroy()
} }
def onDisconnect() { def onDisconnect() {
......
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