diff --git a/src/main/scala/com/github/shadowsocks/ParserActivity.scala b/src/main/scala/com/github/shadowsocks/ParserActivity.scala index 6d24af45..f1265ccd 100644 --- a/src/main/scala/com/github/shadowsocks/ParserActivity.scala +++ b/src/main/scala/com/github/shadowsocks/ParserActivity.scala @@ -61,7 +61,7 @@ class ParserActivity extends Activity { .setTitle(R.string.add_profile_dialog) .setCancelable(false) .setPositiveButton(android.R.string.yes, ((dialog: DialogInterface, id: Int) => { - ShadowsocksApplication.profileManager.createOrUpdateProfile(profile.get) + ShadowsocksApplication.profileManager.createProfile(profile.get) dialog.dismiss() }): DialogInterface.OnClickListener) .setNegativeButton(android.R.string.no, ((dialog: DialogInterface, id: Int) => { diff --git a/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala b/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala index 8bbebe0c..5297827a 100644 --- a/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala +++ b/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala @@ -153,7 +153,7 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { val scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data) if (scanResult != null) Parser.parse(scanResult.getContents) match { - case Some(profile) => ShadowsocksApplication.profileManager.createOrUpdateProfile(profile) + case Some(profile) => ShadowsocksApplication.profileManager.createProfile(profile) case _ => // ignore } } diff --git a/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala b/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala index fd2cda35..b83fb597 100644 --- a/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala +++ b/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala @@ -49,15 +49,27 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) { var profileAddedListener: Profile => Any = _ def setProfileAddedListener(listener: Profile => Any) = this.profileAddedListener = listener - def createOrUpdateProfile(profile: Profile): Boolean = { + def createProfile(p: Profile = null): Profile = { try { + val profile = if (p == null) new Profile else p + profile.id = 0 + ShadowsocksApplication.currentProfile match { + case Some(oldProfile) => + // Copy Feature Settings from old profile + profile.route = oldProfile.route + profile.ipv6 = oldProfile.ipv6 + profile.proxyApps = oldProfile.proxyApps + profile.individual = oldProfile.individual + profile.udpdns = oldProfile.udpdns + case _ => + } dbHelper.profileDao.createOrUpdate(profile) if (profileAddedListener != null) profileAddedListener(profile) - true + profile } catch { case ex: Exception => Log.e(Shadowsocks.TAG, "addProfile", ex) - false + p } } @@ -114,11 +126,7 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) { def load(id: Int): Profile = { - val profile = getProfile(id) getOrElse { - val p = new Profile() - createOrUpdateProfile(p) - p - } + val profile = getProfile(id) getOrElse createProfile() val edit = settings.edit() edit.putBoolean(Key.isProxyApps, profile.proxyApps) @@ -175,7 +183,7 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) { remotePort = 443 password = "u1rRWTssNv0p" } - createOrUpdateProfile(profile) + createProfile(profile) profile } } diff --git a/src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala b/src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala index e8ebf22e..00a737bf 100644 --- a/src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala +++ b/src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala @@ -20,7 +20,7 @@ object TrafficMonitor { var txTotal: Long = 0 var rxTotal: Long = 0 - def getTraffic(): Traffic = { + def getTraffic: Traffic = { new Traffic(Math.max(TrafficStats.getTotalTxBytes - TrafficStats.getUidTxBytes(uid), 0), Math.max(TrafficStats.getTotalRxBytes - TrafficStats.getUidRxBytes(uid), 0), System.currentTimeMillis()) } @@ -84,4 +84,3 @@ object TrafficMonitor { } } -class TrafficMonitor ()