update UI

This commit is contained in:
Max Lv
2013-09-29 15:35:49 +08:00
parent 6f55861df5
commit a2848ce538
12 changed files with 450 additions and 195 deletions
@@ -46,7 +46,7 @@ import com.j256.ormlite.support.ConnectionSource
import com.j256.ormlite.table.TableUtils
import com.j256.ormlite.dao.Dao
class DBHelper(val context: Context) extends OrmLiteSqliteOpenHelper(context, "profile.db", null, 3) {
class DBHelper(val context: Context) extends OrmLiteSqliteOpenHelper(context, "profile.db", null, 7) {
lazy val profileDao:Dao[Profile,Int] = getDao(classOf[Profile])
@@ -39,39 +39,39 @@
package com.github.shadowsocks.database
import android.content.{DialogInterface, Context}
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.github.shadowsocks.R
import android.view.View.OnClickListener
import com.github.shadowsocks.{Shadowsocks, R}
import android.view.View.{OnLongClickListener, OnClickListener}
case class Item(id: Int, title: String, iconRes: Int, action: Int => Unit)
case class Item(id: Int, title: String, iconRes: Int,
click: Int => Unit, longClick: Int => Boolean = _ => false)
case class Category (title: String)
case class Category(title: String)
object MenuAdapter {
trait MenuListener {
def onActiveViewChanged(v: View)
def onActiveViewChanged(v: View, pos: Int)
}
val UNDEFINED = -1
val CATEGORY = 0
val ITEM = 1
}
class MenuAdapter (context: Context, var items: List[Any]) extends BaseAdapter {
class MenuAdapter(context: Context, var items: List[Any]) extends BaseAdapter {
def setListener(listener: MenuAdapter.MenuListener) {
mListener = listener
this.listener = listener
}
def setActivePosition(activePosition: Int) {
mActivePosition = activePosition
this.activePosition = activePosition
}
@Override def getCount: Int = {
@@ -121,32 +121,46 @@ class MenuAdapter (context: Context, var items: List[Any]) extends BaseAdapter {
}
val tv: TextView = v.asInstanceOf[TextView]
tv.setText(value.title)
tv.setCompoundDrawablesWithIntrinsicBounds(value.iconRes, 0, 0, 0)
if (value.iconRes != -1) {
tv.setCompoundDrawablesWithIntrinsicBounds(value.iconRes, 0, 0, 0)
}
tv.setOnClickListener(new OnClickListener {
def onClick(view: View) {
val item = view.getTag(R.id.mdItem).asInstanceOf[Item]
item.action(item.id)
item.click(item.id)
}
})
tv.setOnLongClickListener(new OnLongClickListener {
def onLongClick(view: View): Boolean = {
val item = view.getTag(R.id.mdItem).asInstanceOf[Item]
item.longClick(item.id)
}
})
v.setTag(R.id.mdItem, value)
case _ =>
}
v.setTag(R.id.mdActiveViewPosition, position)
if (position == mActivePosition) {
if (mListener != null) mListener.onActiveViewChanged(v)
if (position == activePosition) {
if (listener != null) listener.onActiveViewChanged(v, position)
}
v
}
def updateList(list: List[Any]) {
def updateList(list: List[Any], activeId: Int) {
items = list
activePosition = items.indexWhere{
case item: Item => item.id == activeId
case _ => false
}
notifyDataSetChanged()
}
private var mListener: MenuAdapter.MenuListener = null
private var mActivePosition: Int = 0
private var listener: MenuAdapter.MenuListener = null
private var activePosition: Int = -1
}
@@ -42,14 +42,14 @@ package com.github.shadowsocks.database
import com.j256.ormlite.field.{DataType, DatabaseField}
class Profile {
@DatabaseField(id = true)
@DatabaseField(generatedId = true)
var id: Int = 0
@DatabaseField
var name: String = null
var name: String = "Untitled"
@DatabaseField
var host: String = null
var host: String = ""
@DatabaseField
var localPort: Int = 0
@@ -58,13 +58,13 @@ class Profile {
var remotePort: Int = 0
@DatabaseField
var password: String = null
var password: String = ""
@DatabaseField
var method: String = null
var method: String = ""
@DatabaseField
var date: String = null
var date: String = ""
@DatabaseField
var upload: Int = 0
@@ -78,9 +78,15 @@ class Profile {
@DatabaseField
var global: Boolean = false
@DatabaseField(dataType = DataType.LONG_STRING)
var individual: String = null
@DatabaseField
var bypass: Boolean = false
@DatabaseField
var traffic: Boolean = false
@DatabaseField(dataType = DataType.LONG_STRING)
var description: String = null
var individual: String = ""
@DatabaseField(dataType = DataType.LONG_STRING)
var description: String = ""
}
@@ -40,18 +40,20 @@
package com.github.shadowsocks.database
import android.util.Log
import com.github.shadowsocks.Shadowsocks
import com.github.shadowsocks._
import android.content.{SharedPreferences, Context}
class ProfileManager(dbHelper: DBHelper) {
def addProfile(profile: Profile): Boolean = {
class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) {
def createOrUpdateProfile(profile: Profile): Boolean = {
try {
dbHelper.profileDao.createOrUpdate(profile)
true
} catch {
case ex: Exception =>
Log.e(Shadowsocks.TAG, "addProfile", ex)
return false
false
}
true
}
def getProfile(id: Int): Option[Profile] = {
@@ -70,12 +72,12 @@ class ProfileManager(dbHelper: DBHelper) {
def delProfile(id: Int): Boolean = {
try {
dbHelper.profileDao.deleteById(id)
true
} catch {
case ex: Exception =>
Log.e(Shadowsocks.TAG, "delProfile", ex)
return false
false
}
true
}
def getAllProfiles: Option[List[Profile]] = {
@@ -88,4 +90,70 @@ class ProfileManager(dbHelper: DBHelper) {
None
}
}
def load(id: Int): Profile = {
Log.d(Shadowsocks.TAG, id + " ")
save()
val profile = getProfile(id) getOrElse {
val p = new Profile()
createOrUpdateProfile(p)
p
}
Log.d(Shadowsocks.TAG, "load " + profile.id + " " + profile.name)
val edit = settings.edit()
edit.putBoolean(Key.isGlobalProxy, profile.global)
edit.putBoolean(Key.isGFWList, profile.chnroute)
edit.putBoolean(Key.isBypassApps, profile.bypass)
edit.putBoolean(Key.isTrafficStat, profile.traffic)
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.putString(Key.remotePort, profile.remotePort.toString)
edit.putString(Key.localPort, profile.localPort.toString)
edit.putString(Key.proxied, profile.individual)
edit.putInt(Key.profileId, profile.id)
edit.commit()
profile
}
def save() {
val profile = new Profile()
profile.global = settings.getBoolean(Key.isGlobalProxy, false)
profile.chnroute = settings.getBoolean(Key.isGFWList, false)
profile.bypass = settings.getBoolean(Key.isBypassApps, false)
profile.traffic = settings.getBoolean(Key.isTrafficStat, 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.remotePort = try {
Integer.valueOf(settings.getString(Key.remotePort, "1984"))
} catch {
case ex: NumberFormatException => {
1984
}
}
profile.localPort = try {
Integer.valueOf(settings.getString(Key.localPort, "1984"))
} catch {
case ex: NumberFormatException => {
1984
}
}
profile.individual = settings.getString(Key.proxied, "")
profile.id = settings.getInt(Key.profileId, -1)
Log.d(Shadowsocks.TAG, "save " + profile.id + " " + profile.name)
createOrUpdateProfile(profile)
}
}