diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 14c0bb78..dfb8ced2 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -66,7 +66,8 @@ android:name=".ProfileManagerActivity" android:label="@string/profiles" android:parentActivityName=".Shadowsocks" - android:exported="false"> + android:exported="false" + android:launchMode="singleTask"> @@ -76,7 +77,8 @@ + android:parentActivityName=".Shadowsocks" + android:launchMode="singleTask"/> { - Parser.parse(data) match { - case Some(profile) => addProfile(profile) - case _ => // ignore - } + ShadowsocksApplication.profileManager.createOrUpdateProfile(profile.get) dialog.dismiss() }): DialogInterface.OnClickListener) .setNegativeButton(android.R.string.no, ((dialog: DialogInterface, id: Int) => { dialog.dismiss() - finish() }): DialogInterface.OnClickListener) .setMessage(data) .create() - .show() + dialog.setOnDismissListener((dialog: DialogInterface) => finish()) // builder method was added in API 17 + dialog.show() } def showAsPopup(activity: Activity) { @@ -82,13 +82,4 @@ class ParserActivity extends Activity { activity.getWindow.setAttributes(params.asInstanceOf[android.view.WindowManager.LayoutParams]) activity.getWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)) } - - def addProfile(profile: Profile) { - val profileManager = - new ProfileManager(PreferenceManager.getDefaultSharedPreferences(getBaseContext), - ShadowsocksApplication.dbHelper) - profileManager.createOrUpdateProfile(profile) - profileManager.reload(profile.id) - } - } diff --git a/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala b/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala index 544e2dd4..8bbebe0c 100644 --- a/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala +++ b/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala @@ -82,10 +82,8 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe removedSnackbar.dismiss commitRemoves val pos = getItemCount - if (ShadowsocksApplication.profileManager.createOrUpdateProfile(item)) { - profiles += item - notifyItemInserted(pos) - } + profiles += item + notifyItemInserted(pos) } def remove(pos: Int) { @@ -126,6 +124,7 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe toolbar.inflateMenu(R.menu.add_profile_methods) toolbar.setOnMenuItemClickListener(this) + ShadowsocksApplication.profileManager.setProfileAddedListener(profilesAdapter.add) val profilesList = findViewById(R.id.profilesList).asInstanceOf[RecyclerView] profilesList.setLayoutManager(new LinearLayoutManager(this)) profilesList.setItemAnimator(new DefaultItemAnimator) @@ -147,13 +146,14 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe override def onDestroy { super.onDestroy + ShadowsocksApplication.profileManager.setProfileAddedListener(null) profilesAdapter.commitRemoves } 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) => profilesAdapter.add(profile) + case Some(profile) => ShadowsocksApplication.profileManager.createOrUpdateProfile(profile) case _ => // ignore } } diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index ec139b3a..1aa91273 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -424,14 +424,6 @@ class Shadowsocks serviceStop() } - def addProfile(profile: Profile) { - currentProfile = profile - ShadowsocksApplication.profileManager.createOrUpdateProfile(currentProfile) - ShadowsocksApplication.profileManager.reload(currentProfile.id) - - updatePreferenceScreen() - } - protected override def onPause() { super.onPause() ShadowsocksApplication.profileManager.save diff --git a/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala b/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala index 58d3e3a8..fd2cda35 100644 --- a/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala +++ b/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala @@ -46,9 +46,13 @@ import com.github.shadowsocks.utils.Key class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) { + var profileAddedListener: Profile => Any = _ + def setProfileAddedListener(listener: Profile => Any) = this.profileAddedListener = listener + def createOrUpdateProfile(profile: Profile): Boolean = { try { dbHelper.profileDao.createOrUpdate(profile) + if (profileAddedListener != null) profileAddedListener(profile) true } catch { case ex: Exception =>