diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index 46913310..bdac3c13 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -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() { diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala index a4b9b022..8a80cb80 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala @@ -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)