Commit 535561cf authored by Mygod's avatar Mygod

Manage auto connect via Android API

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