Commit 535561cf authored by Mygod's avatar Mygod

Manage auto connect via Android API

parent 505e40eb
......@@ -125,10 +125,9 @@
</intent-filter>
</service>
<receiver android:name=".ShadowsocksReceiver">
<receiver android:name=".BootReceiver" android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.github.shadowsocks.ACTION_UPDATE_STATE"/>
</intent-filter>
</receiver>
......
......@@ -70,6 +70,7 @@
<SwitchPreference
android:key="isAutoConnect"
android:persistent="false"
android:summary="@string/auto_connect_summary"
android:title="@string/auto_connect"/>
......
......@@ -39,13 +39,19 @@
package com.github.shadowsocks
import android.content.{BroadcastReceiver, Context, Intent}
import android.content.pm.PackageManager
import android.content.{BroadcastReceiver, ComponentName, Context, Intent}
import com.github.shadowsocks.utils._
class ShadowsocksReceiver extends BroadcastReceiver {
def onReceive(context: Context, intent: Intent) {
if (ShadowsocksApplication.settings.getBoolean(Key.isAutoConnect, false)) {
Utils.startSsService(context)
}
}
object BootReceiver {
def getEnabled(context: Context) = PackageManager.COMPONENT_ENABLED_STATE_ENABLED ==
context.getPackageManager.getComponentEnabledSetting(new ComponentName(context, classOf[BootReceiver]))
def setEnabled(context: Context, enabled: Boolean) = context.getPackageManager.setComponentEnabledSetting(
new ComponentName(context, classOf[BootReceiver]),
if (enabled) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP)
}
class BootReceiver extends BroadcastReceiver {
def onReceive(context: Context, intent: Intent) = Utils.startSsService(context)
}
......@@ -16,7 +16,6 @@ import com.github.shadowsocks.utils.{Key, Utils}
object ShadowsocksSettings {
// Constants
private final val TAG = "ShadowsocksSettings"
private final val PREFS_NAME = "Shadowsocks"
private val PROXY_PREFS = Array(Key.profileName, Key.proxy, Key.remotePort, Key.localPort, Key.sitekey, Key.encMethod,
Key.isAuth)
private val FEATURE_PREFS = Array(Key.route, Key.isProxyApps, Key.isUdpDns, Key.isIpv6)
......@@ -81,6 +80,17 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
false
})
val switch = findPreference(Key.isAutoConnect).asInstanceOf[SwitchPreference]
switch.setOnPreferenceChangeListener((_, value) => {
BootReceiver.setEnabled(activity, value.asInstanceOf[Boolean])
true
})
if (getPreferenceManager.getSharedPreferences.getBoolean(Key.isAutoConnect, false)) {
BootReceiver.setEnabled(activity, true)
getPreferenceManager.getSharedPreferences.edit.remove(Key.isAutoConnect).apply
}
switch.setChecked(BootReceiver.getEnabled(activity))
findPreference("recovery").setOnPreferenceClickListener((preference: Preference) => {
ShadowsocksApplication.track(TAG, "reset")
activity.recovery()
......@@ -130,7 +140,7 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
ShadowsocksApplication.settings.unregisterOnSharedPreferenceChangeListener(this)
}
def onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) = key match {
def onSharedPreferenceChanged(pref: SharedPreferences, key: String) = key match {
case Key.isNAT =>
activity.handler.post(() => {
activity.detachService
......
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