Commit 58c613b3 authored by Max Lv's avatar Max Lv

use runner service to start the background service on Lolipop or above

parent 446a71db
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.shadowsocks" package="com.github.shadowsocks"
android:versionCode="120" android:versionCode="120"
android:versionName="2.8.3"> android:versionName="2.8.3">
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_SUPERUSER"/> <uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
<uses-feature <uses-feature
android:name="android.hardware.touchscreen" android:name="android.hardware.touchscreen"
android:required="false"/> android:required="false"/>
<uses-sdk <uses-sdk
android:minSdkVersion="16" android:minSdkVersion="16"
android:targetSdkVersion="23"/> android:targetSdkVersion="23"/>
<application <application
android:allowBackup="true" android:allowBackup="true"
android:name=".ShadowsocksApplication" android:name=".ShadowsocksApplication"
android:hardwareAccelerated="true" android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_launcher"
android:backupAgent=".ShadowsocksBackupAgent" android:backupAgent=".ShadowsocksBackupAgent"
android:label="@string/app_name"> android:label="@string/app_name">
<meta-data android:name="com.google.android.gms.version" <meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/> android:value="@integer/google_play_services_version"/>
<meta-data android:name="com.google.android.backup.api_key" <meta-data android:name="com.google.android.backup.api_key"
android:value="AEdPqrEAAAAI_zVxZthz2HDuz9toTvkYvL0L5GA-OjeUIfBeXg"/> android:value="AEdPqrEAAAAI_zVxZthz2HDuz9toTvkYvL0L5GA-OjeUIfBeXg"/>
<activity <activity
android:name="com.google.android.gms.ads.AdActivity" android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<activity <activity
android:name=".Shadowsocks" android:name=".Shadowsocks"
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@style/Theme.Shadowsocks" android:theme="@style/Theme.Shadowsocks"
android:launchMode="singleTask"> android:launchMode="singleTask">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
</activity> </activity>
<activity <activity
android:name=".ParserActivity" android:name=".ParserActivity"
android:theme="@style/PopupTheme" android:theme="@style/PopupTheme"
android:launchMode="singleTask"> android:launchMode="singleTask">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW"/> <action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/> <category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="ss"/> <data android:scheme="ss"/>
</intent-filter> </intent-filter>
</activity> </activity>
<activity <activity
android:name=".ShadowsocksRunnerActivity" android:name=".ShadowsocksRunnerActivity"
android:theme="@android:style/Theme.NoDisplay" android:theme="@android:style/Theme.NoDisplay"
android:launchMode="singleTask"> android:launchMode="singleTask">
</activity> </activity>
<activity <activity
android:name=".AppManager" android:name=".AppManager"
android:theme="@style/Theme.AppManager" android:theme="@style/Theme.AppManager"
android:label="@string/app_name" android:label="@string/app_name"
android:parentActivityName=".Shadowsocks"> android:parentActivityName=".Shadowsocks">
<intent-filter> <intent-filter>
<action android:name="com.github.shadowsocks.AppManager"/> <action android:name="com.github.shadowsocks.AppManager"/>
<category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.DEFAULT"/>
</intent-filter> </intent-filter>
</activity> </activity>
<service <service
android:name=".ShadowsocksNatService" android:name=".ShadowsocksRunnerService"
android:process=":nat" android:exported="false">
android:exported="false"> </service>
</service>
<service
<service android:name=".ShadowsocksNatService"
android:name=".ShadowsocksVpnService" android:process=":nat"
android:process=":vpn" android:exported="false">
android:label="@string/app_name" </service>
android:permission="android.permission.BIND_VPN_SERVICE"
android:exported="false"> <service
<intent-filter> android:name=".ShadowsocksVpnService"
<action android:name="android.net.VpnService"/> android:process=":vpn"
</intent-filter> android:label="@string/app_name"
</service> android:permission="android.permission.BIND_VPN_SERVICE"
android:exported="false">
<receiver android:name=".ShadowsocksReceiver"> <intent-filter>
<intent-filter> <action android:name="android.net.VpnService"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter>
<action android:name="com.github.shadowsocks.ACTION_UPDATE_STATE"/> </service>
</intent-filter>
</receiver> <receiver android:name=".ShadowsocksReceiver">
<intent-filter>
</application> <action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.github.shadowsocks.ACTION_UPDATE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest> </manifest>
...@@ -65,10 +65,15 @@ class ShadowsocksReceiver extends BroadcastReceiver { ...@@ -65,10 +65,15 @@ class ShadowsocksReceiver extends BroadcastReceiver {
val isAutoConnect: Boolean = settings.getBoolean(Key.isAutoConnect, false) val isAutoConnect: Boolean = settings.getBoolean(Key.isAutoConnect, false)
val isInstalled: Boolean = status.getBoolean(versionName, false) val isInstalled: Boolean = status.getBoolean(versionName, false)
if (isAutoConnect && isInstalled) { if (isAutoConnect && isInstalled) {
val intent = new Intent(context, classOf[ShadowsocksRunnerActivity]) if (Utils.isLollipopOrAbove) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) val intent = new Intent(context, classOf[ShadowsocksRunnerService])
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) context.startService(intent)
context.startActivity(intent) } else {
val intent = new Intent(context, classOf[ShadowsocksRunnerActivity])
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
context.startActivity(intent)
}
} }
} }
} }
/*
* Shadowsocks - A shadowsocks client for Android
* Copyright (C) 2014 <max.c.lv@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* ___====-_ _-====___
* _--^^^#####// \\#####^^^--_
* _-^##########// ( ) \\##########^-_
* -############// |\^^/| \\############-
* _/############// (@::@) \\############\_
* /#############(( \\// ))#############\
* -###############\\ (oo) //###############-
* -#################\\ / VV \ //#################-
* -###################\\/ \//###################-
* _#/|##########/\######( /\ )######/\##########|\#_
* |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \|
* ` |/ V V ` V \#\| | | |/#/ V ' V V \| '
* ` ` ` ` / | | | | \ ' ' ' '
* ( | | | | )
* __\ | | | | /__
* (vvv(VVV)(VVV)vvv)
*
* HERE BE DRAGONS
*
*/
package com.github.shadowsocks
import android.app.Service
import android.content._
import android.net.VpnService
import android.os._
import android.preference.PreferenceManager
import com.github.shadowsocks.aidl.IShadowsocksService
import com.github.shadowsocks.utils._
class ShadowsocksRunnerService extends Service {
private lazy val settings = PreferenceManager.getDefaultSharedPreferences(this)
private lazy val status = getSharedPreferences(Key.status, Context.MODE_PRIVATE)
val handler = new Handler()
val connection = new ServiceConnection {
override def onServiceConnected(name: ComponentName, service: IBinder) {
bgService = IShadowsocksService.Stub.asInterface(service)
handler.postDelayed(() => if (bgService != null) startBackgroundService(), 1000)
}
override def onServiceDisconnected(name: ComponentName) {
bgService = null
}
}
// Variables
var vpnEnabled = -1
var bgService: IShadowsocksService = null
def isVpnEnabled: Boolean = {
if (vpnEnabled < 0) {
vpnEnabled = if ((!Utils.isLollipopOrAbove || status.getBoolean(Key.isNAT, false)) && Console.isRoot) {
0
} else {
1
}
}
if (vpnEnabled == 1) true else false
}
override def onBind(intent: Intent): IBinder = {
null
}
def startBackgroundService() {
if (isVpnEnabled) {
val intent = VpnService.prepare(ShadowsocksRunnerService.this)
if (intent == null) {
if (bgService != null) {
bgService.start(ConfigUtils.load(settings))
}
}
} else {
bgService.start(ConfigUtils.load(settings))
}
stopSelf()
}
def attachService() {
if (bgService == null) {
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)
startService(new Intent(this, s))
}
}
def deattachService() {
if (bgService != null) {
unbindService(connection)
bgService = null
}
}
override def onCreate() {
super.onCreate()
attachService()
}
override def onDestroy() {
super.onDestroy()
deattachService()
}
}
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