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