Commit 834fb378 authored by Max Lv's avatar Max Lv

fix a crash on 2.3 devices

parent 8d163c98
...@@ -126,9 +126,6 @@ object Shadowsocks { ...@@ -126,9 +126,6 @@ object Shadowsocks {
val TAG = "Shadowsocks" val TAG = "Shadowsocks"
val REQUEST_CONNECT = 1 val REQUEST_CONNECT = 1
// Flags
var vpnEnabled = -1
// Helper functions // Helper functions
def updateListPreference(pref: Preference, value: String) { def updateListPreference(pref: Preference, value: String) {
pref.setSummary(value) pref.setSummary(value)
...@@ -296,6 +293,7 @@ class Shadowsocks ...@@ -296,6 +293,7 @@ class Shadowsocks
var state = State.INIT var state = State.INIT
var prepared = false var prepared = false
var currentProfile = new Profile var currentProfile = new Profile
var vpnEnabled = -1
// Services // Services
var currentServiceName = classOf[ShadowsocksNatService].getName var currentServiceName = classOf[ShadowsocksNatService].getName
...@@ -593,8 +591,7 @@ class Shadowsocks ...@@ -593,8 +591,7 @@ class Shadowsocks
def attachService() { def attachService() {
if (bgService == null) { if (bgService == null) {
val isRoot = status.getBoolean(Key.isRoot, false) val s = if (!isVpnEnabled) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService]
val s = if (isRoot) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService]
val intent = new Intent(this, s) val intent = new Intent(this, s)
intent.setAction(Action.SERVICE) intent.setAction(Action.SERVICE)
bindService(intent, connection, Context.BIND_AUTO_CREATE) bindService(intent, connection, Context.BIND_AUTO_CREATE)
...@@ -945,15 +942,15 @@ class Shadowsocks ...@@ -945,15 +942,15 @@ class Shadowsocks
} }
def isVpnEnabled: Boolean = { def isVpnEnabled: Boolean = {
if (Shadowsocks.vpnEnabled < 0) { if (vpnEnabled < 0) {
Shadowsocks.vpnEnabled = if (Build.VERSION.SDK_INT vpnEnabled = if (Build.VERSION.SDK_INT
>= Build.VERSION_CODES.ICE_CREAM_SANDWICH && !status.getBoolean(Key.isRoot, false)) { >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && !status.getBoolean(Key.isRoot, false)) {
1 1
} else { } else {
0 0
} }
} }
if (Shadowsocks.vpnEnabled == 1) true else false if (vpnEnabled == 1) true else false
} }
def serviceStop() { def serviceStop() {
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
package com.github.shadowsocks package com.github.shadowsocks
import android.app.{Activity, KeyguardManager} import android.app.{Activity, KeyguardManager}
import android.os.{IBinder, Bundle, Handler} import android.os._
import android.net.VpnService import android.net.VpnService
import android.content._ import android.content._
import android.util.Log import android.util.Log
...@@ -51,7 +51,6 @@ import com.github.shadowsocks.aidl.IShadowsocksService ...@@ -51,7 +51,6 @@ import com.github.shadowsocks.aidl.IShadowsocksService
class ShadowsocksRunnerActivity extends Activity { class ShadowsocksRunnerActivity extends Activity {
lazy val settings = PreferenceManager.getDefaultSharedPreferences(this) lazy val settings = PreferenceManager.getDefaultSharedPreferences(this)
lazy val isRoot = Utils.getRoot
val handler = new Handler() val handler = new Handler()
val receiver = new BroadcastReceiver() { val receiver = new BroadcastReceiver() {
...@@ -62,8 +61,6 @@ class ShadowsocksRunnerActivity extends Activity { ...@@ -62,8 +61,6 @@ class ShadowsocksRunnerActivity extends Activity {
} }
} }
// Services
var bgService: IShadowsocksService = null
val connection = new ServiceConnection { val connection = new ServiceConnection {
override def onServiceConnected(name: ComponentName, service: IBinder) { override def onServiceConnected(name: ComponentName, service: IBinder) {
bgService = IShadowsocksService.Stub.asInterface(service) bgService = IShadowsocksService.Stub.asInterface(service)
...@@ -76,8 +73,25 @@ class ShadowsocksRunnerActivity extends Activity { ...@@ -76,8 +73,25 @@ class ShadowsocksRunnerActivity extends Activity {
} }
} }
// Variables
var vpnEnabled = -1
var bgService: IShadowsocksService = null
def isVpnEnabled: Boolean = {
if (vpnEnabled < 0) {
vpnEnabled = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
&& !Utils.getRoot) {
1
} else {
0
}
}
if (vpnEnabled == 1) true else false
}
def startBackgroundService() { def startBackgroundService() {
if (!isRoot) { if (isVpnEnabled) {
val intent = VpnService.prepare(ShadowsocksRunnerActivity.this) val intent = VpnService.prepare(ShadowsocksRunnerActivity.this)
if (intent != null) { if (intent != null) {
startActivityForResult(intent, Shadowsocks.REQUEST_CONNECT) startActivityForResult(intent, Shadowsocks.REQUEST_CONNECT)
...@@ -92,7 +106,7 @@ class ShadowsocksRunnerActivity extends Activity { ...@@ -92,7 +106,7 @@ class ShadowsocksRunnerActivity extends Activity {
def attachService() { def attachService() {
if (bgService == null) { if (bgService == null) {
val s = if (isRoot) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService] val s = if (!isVpnEnabled) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService]
val intent = new Intent(this, s) val intent = new Intent(this, s)
intent.setAction(Action.SERVICE) intent.setAction(Action.SERVICE)
bindService(intent, connection, Context.BIND_AUTO_CREATE) bindService(intent, connection, Context.BIND_AUTO_CREATE)
......
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