Remove .aidl.Config and persisting SharedPreferences (#742)

Previously a profile can exist in the following forms:

* `.database.Profile` used for everything backend;
* `.ShadowsocksSettings` which uses Preferences to provide UI to tweak settings;
* `.aidl.Config` used for IPC;
* `<data dir>/shared_pref/com.github.shadowsocks_preferences.xml` which exists for no apparent reason.

Thus long code block can be seen whose purpose is simply converting the data from one form to another. This commit intends to remove the latter two by:

1. Using profile ID and take advantage of fs r/w lock SQLite uses for IPC and better extensibility;
2. Stop persisting redundant fields in the preferences file and update database directly.
This commit is contained in:
Mygod
2016-07-05 01:07:09 -05:00
committed by Max Lv
parent 8515edff60
commit c480137dec
18 changed files with 414 additions and 630 deletions
@@ -39,16 +39,14 @@
package com.github.shadowsocks.database
import android.content.SharedPreferences
import android.util.Log
import com.github.shadowsocks.ShadowsocksApplication.app
import com.github.shadowsocks.utils.Key
object ProfileManager {
private final val TAG = "ProfileManager"
}
class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) {
class ProfileManager(dbHelper: DBHelper) {
import ProfileManager._
var profileAddedListener: Profile => Any = _
@@ -139,67 +137,6 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) {
}
}
def reload(id: Int): Profile = {
save()
load(id)
}
def load(id: Int): Profile = {
val profile = getProfile(id) getOrElse createProfile()
val edit = settings.edit()
edit.putBoolean(Key.isProxyApps, profile.proxyApps)
edit.putBoolean(Key.isBypassApps, profile.bypass)
edit.putBoolean(Key.isUdpDns, profile.udpdns)
edit.putBoolean(Key.isAuth, profile.auth)
edit.putBoolean(Key.isIpv6, profile.ipv6)
edit.putString(Key.profileName, profile.name)
edit.putString(Key.proxy, profile.host)
edit.putString(Key.sitekey, profile.password)
edit.putString(Key.encMethod, profile.method)
edit.putInt(Key.remotePort, profile.remotePort)
edit.putInt(Key.localPort, profile.localPort)
edit.putString(Key.proxied, profile.individual)
edit.putInt(Key.profileId, profile.id)
edit.putString(Key.route, profile.route)
edit.apply()
profile
}
private def loadFromPreferences: Profile = {
val id = settings.getInt(Key.profileId, -1)
val profile: Profile = getProfile(id) match {
case Some(p) => p
case _ => new Profile()
}
profile.proxyApps = settings.getBoolean(Key.isProxyApps, false)
profile.bypass = settings.getBoolean(Key.isBypassApps, false)
profile.udpdns = settings.getBoolean(Key.isUdpDns, false)
profile.auth = settings.getBoolean(Key.isAuth, false)
profile.ipv6 = settings.getBoolean(Key.isIpv6, false)
profile.name = settings.getString(Key.profileName, "default")
profile.host = settings.getString(Key.proxy, "127.0.0.1")
profile.password = settings.getString(Key.sitekey, "default")
profile.method = settings.getString(Key.encMethod, "table")
profile.route = settings.getString(Key.route, "all")
profile.remotePort = settings.getInt(Key.remotePort, 1984)
profile.localPort = settings.getInt(Key.localPort, 1984)
profile.individual = settings.getString(Key.proxied, "")
profile
}
def save(): Profile = {
val profile = loadFromPreferences
updateProfile(profile)
profile
}
def createDefault(): Profile = {
val profile = new Profile {
name = "Default"