Commit 6f6cb461 authored by Mygod's avatar Mygod

Fix ParserActivity

parent 42c5a01d
...@@ -66,7 +66,8 @@ ...@@ -66,7 +66,8 @@
android:name=".ProfileManagerActivity" android:name=".ProfileManagerActivity"
android:label="@string/profiles" android:label="@string/profiles"
android:parentActivityName=".Shadowsocks" android:parentActivityName=".Shadowsocks"
android:exported="false"> android:exported="false"
android:launchMode="singleTask">
<intent-filter> <intent-filter>
<action android:name="com.github.shadowsocks.ProfileManagerActivity"/> <action android:name="com.github.shadowsocks.ProfileManagerActivity"/>
<category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.DEFAULT"/>
...@@ -76,7 +77,8 @@ ...@@ -76,7 +77,8 @@
<activity <activity
android:name=".AppManager" android:name=".AppManager"
android:label="@string/proxied_apps" android:label="@string/proxied_apps"
android:parentActivityName=".Shadowsocks"/> android:parentActivityName=".Shadowsocks"
android:launchMode="singleTask"/>
<service <service
android:name=".ShadowsocksRunnerService" android:name=".ShadowsocksRunnerService"
......
...@@ -44,33 +44,33 @@ import android.content.DialogInterface ...@@ -44,33 +44,33 @@ import android.content.DialogInterface
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.os.Bundle import android.os.Bundle
import android.preference.PreferenceManager
import android.view.WindowManager import android.view.WindowManager
import com.github.shadowsocks.database.{Profile, ProfileManager}
import com.github.shadowsocks.utils.Parser import com.github.shadowsocks.utils.Parser
class ParserActivity extends Activity { class ParserActivity extends Activity {
override def onCreate(savedInstanceState: Bundle) { override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
showAsPopup(this)
val data = getIntent.getData.toString val data = getIntent.getData.toString
new AlertDialog.Builder(this) val profile = Parser.parse(data)
if (profile.isEmpty) { // ignore
finish()
return
}
showAsPopup(this)
val dialog = new AlertDialog.Builder(this)
.setTitle(R.string.add_profile_dialog) .setTitle(R.string.add_profile_dialog)
.setCancelable(false) .setCancelable(false)
.setPositiveButton(android.R.string.yes, ((dialog: DialogInterface, id: Int) => { .setPositiveButton(android.R.string.yes, ((dialog: DialogInterface, id: Int) => {
Parser.parse(data) match { ShadowsocksApplication.profileManager.createOrUpdateProfile(profile.get)
case Some(profile) => addProfile(profile)
case _ => // ignore
}
dialog.dismiss() dialog.dismiss()
}): DialogInterface.OnClickListener) }): DialogInterface.OnClickListener)
.setNegativeButton(android.R.string.no, ((dialog: DialogInterface, id: Int) => { .setNegativeButton(android.R.string.no, ((dialog: DialogInterface, id: Int) => {
dialog.dismiss() dialog.dismiss()
finish()
}): DialogInterface.OnClickListener) }): DialogInterface.OnClickListener)
.setMessage(data) .setMessage(data)
.create() .create()
.show() dialog.setOnDismissListener((dialog: DialogInterface) => finish()) // builder method was added in API 17
dialog.show()
} }
def showAsPopup(activity: Activity) { def showAsPopup(activity: Activity) {
...@@ -82,13 +82,4 @@ class ParserActivity extends Activity { ...@@ -82,13 +82,4 @@ class ParserActivity extends Activity {
activity.getWindow.setAttributes(params.asInstanceOf[android.view.WindowManager.LayoutParams]) activity.getWindow.setAttributes(params.asInstanceOf[android.view.WindowManager.LayoutParams])
activity.getWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)) 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)
}
} }
...@@ -82,10 +82,8 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe ...@@ -82,10 +82,8 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
removedSnackbar.dismiss removedSnackbar.dismiss
commitRemoves commitRemoves
val pos = getItemCount val pos = getItemCount
if (ShadowsocksApplication.profileManager.createOrUpdateProfile(item)) { profiles += item
profiles += item notifyItemInserted(pos)
notifyItemInserted(pos)
}
} }
def remove(pos: Int) { def remove(pos: Int) {
...@@ -126,6 +124,7 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe ...@@ -126,6 +124,7 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
toolbar.inflateMenu(R.menu.add_profile_methods) toolbar.inflateMenu(R.menu.add_profile_methods)
toolbar.setOnMenuItemClickListener(this) toolbar.setOnMenuItemClickListener(this)
ShadowsocksApplication.profileManager.setProfileAddedListener(profilesAdapter.add)
val profilesList = findViewById(R.id.profilesList).asInstanceOf[RecyclerView] val profilesList = findViewById(R.id.profilesList).asInstanceOf[RecyclerView]
profilesList.setLayoutManager(new LinearLayoutManager(this)) profilesList.setLayoutManager(new LinearLayoutManager(this))
profilesList.setItemAnimator(new DefaultItemAnimator) profilesList.setItemAnimator(new DefaultItemAnimator)
...@@ -147,13 +146,14 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe ...@@ -147,13 +146,14 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
override def onDestroy { override def onDestroy {
super.onDestroy super.onDestroy
ShadowsocksApplication.profileManager.setProfileAddedListener(null)
profilesAdapter.commitRemoves profilesAdapter.commitRemoves
} }
override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
val scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data) val scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
if (scanResult != null) Parser.parse(scanResult.getContents) match { if (scanResult != null) Parser.parse(scanResult.getContents) match {
case Some(profile) => profilesAdapter.add(profile) case Some(profile) => ShadowsocksApplication.profileManager.createOrUpdateProfile(profile)
case _ => // ignore case _ => // ignore
} }
} }
......
...@@ -424,14 +424,6 @@ class Shadowsocks ...@@ -424,14 +424,6 @@ class Shadowsocks
serviceStop() serviceStop()
} }
def addProfile(profile: Profile) {
currentProfile = profile
ShadowsocksApplication.profileManager.createOrUpdateProfile(currentProfile)
ShadowsocksApplication.profileManager.reload(currentProfile.id)
updatePreferenceScreen()
}
protected override def onPause() { protected override def onPause() {
super.onPause() super.onPause()
ShadowsocksApplication.profileManager.save ShadowsocksApplication.profileManager.save
......
...@@ -46,9 +46,13 @@ import com.github.shadowsocks.utils.Key ...@@ -46,9 +46,13 @@ import com.github.shadowsocks.utils.Key
class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) { class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) {
var profileAddedListener: Profile => Any = _
def setProfileAddedListener(listener: Profile => Any) = this.profileAddedListener = listener
def createOrUpdateProfile(profile: Profile): Boolean = { def createOrUpdateProfile(profile: Profile): Boolean = {
try { try {
dbHelper.profileDao.createOrUpdate(profile) dbHelper.profileDao.createOrUpdate(profile)
if (profileAddedListener != null) profileAddedListener(profile)
true true
} catch { } catch {
case ex: Exception => case ex: Exception =>
......
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