Commit 36cdcdac authored by Max Lv's avatar Max Lv

fix some issues

parent c4833ddb
......@@ -54,7 +54,7 @@
</activity>
<activity
android:name=".ShadowVpnActivity"
android:name=".ShadowsocksRunnerActivity"
android:theme="@style/PopupTheme"
android:launchMode="singleTask">
</activity>
......
......@@ -531,7 +531,6 @@ class Shadowsocks
onContentChanged()
}
/** Called when the activity is first created. */
override def onCreate(savedInstanceState: Bundle) {
// Initialize the preference
......@@ -580,17 +579,22 @@ class Shadowsocks
// Register broadcast receiver
registerReceiver(preferenceReceiver, new IntentFilter(Action.UPDATE_PREFS))
// Bind to the service
spawn {
status.edit.putBoolean(Key.isRoot, Utils.getRoot).apply()
handler.post(new Runnable {
override def run() {
}
def attachService() {
if (bgService == null) {
val isRoot = status.getBoolean(Key.isRoot, false)
val s = if (isRoot) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService]
bindService(new Intent(s.getName), connection, Context.BIND_AUTO_CREATE)
startService(new Intent(Shadowsocks.this, s))
}
})
}
def deattachService() {
if (bgService != null) {
bgService.unregisterCallback(callback)
unbindService(connection)
bgService = null
}
}
......@@ -806,11 +810,22 @@ class Shadowsocks
protected override def onPause() {
super.onPause()
deattachService()
prepared = false
}
protected override def onResume() {
super.onResume()
// Bind to the service
spawn {
val isRoot = Utils.getRoot
handler.post(new Runnable {
override def run() {
status.edit.putBoolean(Key.isRoot, isRoot).commit()
attachService()
}
})
}
ConfigUtils.refresh(this)
}
......@@ -860,10 +875,6 @@ class Shadowsocks
override def onDestroy() {
super.onDestroy()
if (bgService != null) {
bgService.unregisterCallback(callback)
unbindService(connection)
}
Crouton.cancelAllCroutons()
unregisterReceiver(preferenceReceiver)
new BackupManager(this).dataChanged()
......@@ -913,6 +924,7 @@ class Shadowsocks
}
case _ =>
clearDialog()
switchButton.setChecked(false)
Log.e(Shadowsocks.TAG, "Failed to start VpnService")
}
}
......@@ -921,7 +933,7 @@ class Shadowsocks
def isVpnEnabled: Boolean = {
if (Shadowsocks.vpnEnabled < 0) {
Shadowsocks.vpnEnabled = if (Build.VERSION.SDK_INT
>= Build.VERSION_CODES.ICE_CREAM_SANDWICH && !Utils.getRoot) {
>= Build.VERSION_CODES.ICE_CREAM_SANDWICH && !status.getBoolean(Key.isRoot, false)) {
1
} else {
0
......
......@@ -57,6 +57,7 @@ import org.apache.http.conn.util.InetAddressUtils
import scala.collection._
import java.util.{TimerTask, Timer}
import android.net.TrafficStats
import scala.concurrent.ops._
import com.github.shadowsocks.utils._
import scala.Some
import android.graphics.Color
......@@ -421,7 +422,7 @@ class ShadowsocksNatService extends Service with BaseService {
override def startRunner(c: Config) {
var config = c
config = c
// register close receiver
val filter = new IntentFilter()
......@@ -474,6 +475,7 @@ class ShadowsocksNatService extends Service with BaseService {
timer.schedule(task, TIMER_INTERVAL * 1000, TIMER_INTERVAL * 1000)
}
spawn {
if (config.proxy == "198.199.101.152") {
val container = getApplication.asInstanceOf[ShadowsocksApplication].tagContainer
try {
......@@ -515,6 +517,7 @@ class ShadowsocksNatService extends Service with BaseService {
}
handler.sendEmptyMessageDelayed(Msg.CONNECT_FINISH, 500)
}
}
override def stopRunner() {
......
......@@ -64,8 +64,7 @@ class ShadowsocksReceiver extends BroadcastReceiver {
val isAutoConnect: Boolean = settings.getBoolean(Key.isAutoConnect, false)
val isInstalled: Boolean = status.getBoolean(versionName, false)
if (isAutoConnect && isInstalled) {
if (Utils.getRoot) {
}
context.startActivity(new Intent(context, classOf[ShadowsocksRunnerActivity]))
}
}
}
......@@ -42,41 +42,68 @@ package com.github.shadowsocks
import android.app.Activity
import android.os.{IBinder, Bundle}
import android.net.VpnService
import android.content.{ComponentName, ServiceConnection, Intent}
import android.content.{Context, ComponentName, ServiceConnection, Intent}
import android.util.Log
import android.preference.PreferenceManager
import com.github.shadowsocks.utils._
import com.github.shadowsocks.aidl.IShadowsocksService
class ShadowVpnActivity extends Activity {
class ShadowsocksRunnerActivity extends Activity {
lazy val settings = PreferenceManager.getDefaultSharedPreferences(this)
lazy val isRoot = Utils.getRoot
// 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))
}
}
override def onServiceDisconnected(name: ComponentName) {
bgService = null
}
}
def attachService() {
if (bgService == null) {
val s = if (isRoot) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService]
bindService(new Intent(s.getName), connection, Context.BIND_AUTO_CREATE)
startService(new Intent(this, s))
}
}
def deattachService() {
if (bgService != null) {
unbindService(connection)
bgService = null
}
}
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
val intent = VpnService.prepare(this)
if (intent != null) {
startActivityForResult(intent, Shadowsocks.REQUEST_CONNECT)
} else {
onActivityResult(Shadowsocks.REQUEST_CONNECT, Activity.RESULT_OK, null)
attachService()
}
override def onDestroy() {
super.onDestroy()
deattachService()
}
override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
resultCode match {
case Activity.RESULT_OK =>
if (bgService != null) {
bgService.start(ConfigUtils.load(PreferenceManager.getDefaultSharedPreferences(this)))
bgService.start(ConfigUtils.load(settings))
}
case _ =>
Log.e(Shadowsocks.TAG, "Failed to start VpnService")
......
......@@ -51,11 +51,12 @@ import java.io._
import android.net.VpnService
import org.apache.http.conn.util.InetAddressUtils
import android.os.Message
import scala.concurrent.ops._
import org.apache.commons.net.util.SubnetUtils
import java.net.InetAddress
import com.github.shadowsocks.utils._
import scala.Some
import com.github.shadowsocks.aidl.Config
import com.github.shadowsocks.aidl.{IShadowsocksService, Config}
class ShadowsocksVpnService extends VpnService with BaseService {
......@@ -266,6 +267,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE)
.asInstanceOf[NotificationManager]
}
def killProcesses() {
......@@ -291,11 +293,11 @@ class ShadowsocksVpnService extends VpnService with BaseService {
override def startRunner(c: Config) {
var config = c
config = c
// ensure the VPNService is prepared
if (VpnService.prepare(this) != null) {
val i = new Intent(this, classOf[ShadowVpnActivity])
val i = new Intent(this, classOf[ShadowsocksRunnerActivity])
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(i)
return
......@@ -321,6 +323,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
changeState(State.CONNECTING)
spawn {
if (config.proxy == "198.199.101.152") {
val container = getApplication.asInstanceOf[ShadowsocksApplication].tagContainer
try {
......@@ -360,6 +363,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
}
handler.sendEmptyMessageDelayed(Msg.CONNECT_FINISH, 300)
}
}
override def stopRunner() {
......@@ -394,6 +398,6 @@ class ShadowsocksVpnService extends VpnService with BaseService {
}
override def getTag = TAG
override def getServiceMode = Mode.VPN
}
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