Commit bdac32bd authored by Max Lv's avatar Max Lv

inter-process persistent preferences

parent 8f9ebabd
...@@ -174,11 +174,12 @@ class ShadowVpnService extends VpnService { ...@@ -174,11 +174,12 @@ class ShadowVpnService extends VpnService {
return return
} }
settings.edit().putBoolean("isRunning", true).commit() settings.edit().putBoolean(Key.isRunning, true).commit()
changeState(State.CONNECTING) changeState(State.CONNECTING)
config = Extra.get(intent) config = Extra.get(intent)
Extra.save(settings, config)
new Thread(new Runnable { new Thread(new Runnable {
def run() { def run() {
...@@ -376,7 +377,7 @@ class ShadowVpnService extends VpnService { ...@@ -376,7 +377,7 @@ class ShadowVpnService extends VpnService {
def destroy() { def destroy() {
changeState(State.STOPPED) changeState(State.STOPPED)
settings.edit.putBoolean("isRunning", false).commit settings.edit.putBoolean(Key.isRunning, false).commit
EasyTracker.getTracker.sendEvent(TAG, "stop", getVersionName, 0L) EasyTracker.getTracker.sendEvent(TAG, "stop", getVersionName, 0L)
if (receiver != null) { if (receiver != null) {
unregisterReceiver(receiver) unregisterReceiver(receiver)
......
...@@ -211,18 +211,16 @@ class Shadowsocks ...@@ -211,18 +211,16 @@ class Shadowsocks
private val handler: Handler = new Handler { private val handler: Handler = new Handler {
override def handleMessage(msg: Message) { override def handleMessage(msg: Message) {
val ed: SharedPreferences.Editor = settings.edit
msg.what match { msg.what match {
case MSG_CRASH_RECOVER => case MSG_CRASH_RECOVER =>
Crouton.makeText(Shadowsocks.this, R.string.crash_alert, Style.ALERT).show() Crouton.makeText(Shadowsocks.this, R.string.crash_alert, Style.ALERT).show()
ed.putBoolean("isRunning", false) settings.edit().putBoolean(Key.isRunning, false).apply()
case MSG_INITIAL_FINISH => case MSG_INITIAL_FINISH =>
if (progressDialog != null) { if (progressDialog != null) {
progressDialog.dismiss() progressDialog.dismiss()
progressDialog = null progressDialog = null
} }
} }
ed.commit
super.handleMessage(msg) super.handleMessage(msg)
} }
} }
...@@ -380,9 +378,7 @@ class Shadowsocks ...@@ -380,9 +378,7 @@ class Shadowsocks
} }
} }
if (!settings.getBoolean(versionName, false)) { if (!settings.getBoolean(versionName, false)) {
val edit: SharedPreferences.Editor = settings.edit settings.edit.putBoolean(versionName, true).apply()
edit.putBoolean(versionName, true)
edit.commit
reset() reset()
} }
handler.sendEmptyMessage(MSG_INITIAL_FINISH) handler.sendEmptyMessage(MSG_INITIAL_FINISH)
......
...@@ -127,18 +127,16 @@ class ShadowsocksService extends Service { ...@@ -127,18 +127,16 @@ class ShadowsocksService extends Service {
val handler: Handler = new Handler { val handler: Handler = new Handler {
override def handleMessage(msg: Message) { override def handleMessage(msg: Message) {
val ed: SharedPreferences.Editor = settings.edit
msg.what match { msg.what match {
case MSG_CONNECT_SUCCESS => case MSG_CONNECT_SUCCESS =>
changeState(State.CONNECTED) changeState(State.CONNECTED)
ed.putBoolean("isRunning", true) settings.edit().putBoolean(Key.isRunning, true).apply()
case MSG_CONNECT_FAIL => case MSG_CONNECT_FAIL =>
changeState(State.STOPPED) changeState(State.STOPPED)
case MSG_STOP_SELF => case MSG_STOP_SELF =>
stopSelf() stopSelf()
case _ => case _ =>
} }
ed.commit
super.handleMessage(msg) super.handleMessage(msg)
} }
} }
...@@ -215,6 +213,7 @@ class ShadowsocksService extends Service { ...@@ -215,6 +213,7 @@ class ShadowsocksService extends Service {
changeState(State.CONNECTING) changeState(State.CONNECTING)
config = Extra.get(intent) config = Extra.get(intent)
Extra.save(settings, config)
new Thread(new Runnable { new Thread(new Runnable {
def run() { def run() {
...@@ -395,9 +394,7 @@ class ShadowsocksService extends Service { ...@@ -395,9 +394,7 @@ class ShadowsocksService extends Service {
killProcesses() killProcesses()
} }
}.start() }.start()
val ed: SharedPreferences.Editor = settings.edit settings.edit.putBoolean(Key.isRunning, false).apply()
ed.putBoolean("isRunning", false)
ed.commit
if (receiver != null) { if (receiver != null) {
unregisterReceiver(receiver) unregisterReceiver(receiver)
receiver = null receiver = null
......
...@@ -53,6 +53,8 @@ import android.graphics.{Canvas, Bitmap} ...@@ -53,6 +53,8 @@ import android.graphics.{Canvas, Bitmap}
import android.app.ActivityManager import android.app.ActivityManager
object Key { object Key {
val isRunning = "isRunning"
val isGlobalProxy = "isGlobalProxy" val isGlobalProxy = "isGlobalProxy"
val isGFWList = "isGFWList" val isGFWList = "isGFWList"
val isBypassApps = "isBypassApps" val isBypassApps = "isBypassApps"
...@@ -96,6 +98,24 @@ object Extra { ...@@ -96,6 +98,24 @@ object Extra {
val STATE = "state" val STATE = "state"
val MESSAGE = "message" val MESSAGE = "message"
def save(settings: SharedPreferences, config: Config) {
val edit = settings.edit()
edit.putBoolean(Key.isGlobalProxy, config.isGlobalProxy)
edit.putBoolean(Key.isGFWList, config.isGFWList)
edit.putBoolean(Key.isBypassApps, config.isBypassApps)
edit.putBoolean(Key.isDNSProxy, config.isDNSProxy)
edit.putBoolean(Key.isHTTPProxy, config.isHTTPProxy)
edit.putString(Key.proxy, config.proxy)
edit.putString(Key.sitekey, config.sitekey)
edit.putString(Key.encMethod, config.encMethod)
edit.putString(Key.remotePort, config.remotePort.toString)
edit.putString(Key.localPort, config.localPort.toString)
edit.apply()
}
def get(intent: Intent): Config = { def get(intent: Intent): Config = {
val isGlobalProxy = intent.getBooleanExtra(Key.isGlobalProxy, false) val isGlobalProxy = intent.getBooleanExtra(Key.isGlobalProxy, false)
val isGFWList = intent.getBooleanExtra(Key.isGFWList, false) val isGFWList = intent.getBooleanExtra(Key.isGFWList, false)
......
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