diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index 47d83b39..509804b8 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -69,10 +69,11 @@ import android.content.SharedPreferences.OnSharedPreferenceChangeListener import com.google.ads.{AdRequest, AdSize, AdView} import net.simonvt.menudrawer.MenuDrawer -import com.github.shadowsocks.database.{MenuAdapter, Item, Category} +import com.github.shadowsocks.database._ import scala.collection.mutable.ListBuffer import com.github.shadowsocks.database.Category import com.github.shadowsocks.database.Item +import com.github.shadowsocks.database.Profile object Shadowsocks { @@ -228,6 +229,7 @@ class Shadowsocks lazy val drawer = MenuDrawer.attach(this) lazy val listView = new ListView(this) lazy val menuAdapter = new MenuAdapter(this, getMenuList) + lazy val profileManager = getApplication.asInstanceOf[ShadowsocksApplication].profileManager private val handler: Handler = new Handler { override def handleMessage(msg: Message) { @@ -446,8 +448,13 @@ class Shadowsocks } + def updateProfile(id: Int) { + + } + def getProfileList: List[Item] = { - List[Item]() + val list = profileManager.getAllProfiles getOrElse List[Profile]() + list.map(p => new Item(p.id, p.name, -1, updateProfile)) } def getMenuList: List[Any] = { diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala index f704c868..51548324 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala @@ -40,9 +40,14 @@ package com.github.shadowsocks import android.app.Application import com.google.analytics.tracking.android.EasyTracker +import com.github.shadowsocks.database.{ProfileManager, DBHelper} class ShadowsocksApplication extends Application { + lazy val dbHelper = new DBHelper(this) + lazy val profileManager = new ProfileManager(dbHelper) + override def onCreate() { EasyTracker.getInstance.setContext(this) } + } diff --git a/src/main/scala/com/github/shadowsocks/database/DBHelper.scala b/src/main/scala/com/github/shadowsocks/database/DBHelper.scala index 654c0acb..00b91314 100644 --- a/src/main/scala/com/github/shadowsocks/database/DBHelper.scala +++ b/src/main/scala/com/github/shadowsocks/database/DBHelper.scala @@ -1,3 +1,42 @@ +/* + * Shadowsocks - A shadowsocks client for Android + * Copyright (C) 2013 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * ___====-_ _-====___ + * _--^^^#####// \\#####^^^--_ + * _-^##########// ( ) \\##########^-_ + * -############// |\^^/| \\############- + * _/############// (@::@) \\############\_ + * /#############(( \\// ))#############\ + * -###############\\ (oo) //###############- + * -#################\\ / VV \ //#################- + * -###################\\/ \//###################- + * _#/|##########/\######( /\ )######/\##########|\#_ + * |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \| + * ` |/ V V ` V \#\| | | |/#/ V ' V V \| ' + * ` ` ` ` / | | | | \ ' ' ' ' + * ( | | | | ) + * __\ | | | | /__ + * (vvv(VVV)(VVV)vvv) + * + * HERE BE DRAGONS + * + */ + package com.github.shadowsocks.database import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper @@ -7,7 +46,7 @@ import com.j256.ormlite.support.ConnectionSource import com.j256.ormlite.table.TableUtils import com.j256.ormlite.dao.Dao -class DBHelper(implicit val context: Context) extends OrmLiteSqliteOpenHelper(context, "postnauka", null, 3) { +class DBHelper(val context: Context) extends OrmLiteSqliteOpenHelper(context, "profile.db", null, 3) { lazy val profileDao:Dao[Profile,Int] = getDao(classOf[Profile]) diff --git a/src/main/scala/com/github/shadowsocks/database/MenuAdapter.scala b/src/main/scala/com/github/shadowsocks/database/MenuAdapter.scala index 1554d6a9..096f3564 100644 --- a/src/main/scala/com/github/shadowsocks/database/MenuAdapter.scala +++ b/src/main/scala/com/github/shadowsocks/database/MenuAdapter.scala @@ -1,3 +1,42 @@ +/* + * Shadowsocks - A shadowsocks client for Android + * Copyright (C) 2013 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * ___====-_ _-====___ + * _--^^^#####// \\#####^^^--_ + * _-^##########// ( ) \\##########^-_ + * -############// |\^^/| \\############- + * _/############// (@::@) \\############\_ + * /#############(( \\// ))#############\ + * -###############\\ (oo) //###############- + * -#################\\ / VV \ //#################- + * -###################\\/ \//###################- + * _#/|##########/\######( /\ )######/\##########|\#_ + * |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \| + * ` |/ V V ` V \#\| | | |/#/ V ' V V \| ' + * ` ` ` ` / | | | | \ ' ' ' ' + * ( | | | | ) + * __\ | | | | /__ + * (vvv(VVV)(VVV)vvv) + * + * HERE BE DRAGONS + * + */ + package com.github.shadowsocks.database import android.content.{DialogInterface, Context} diff --git a/src/main/scala/com/github/shadowsocks/database/Profile.scala b/src/main/scala/com/github/shadowsocks/database/Profile.scala index b034aab0..8b4aca78 100644 --- a/src/main/scala/com/github/shadowsocks/database/Profile.scala +++ b/src/main/scala/com/github/shadowsocks/database/Profile.scala @@ -1,3 +1,42 @@ +/* + * Shadowsocks - A shadowsocks client for Android + * Copyright (C) 2013 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * ___====-_ _-====___ + * _--^^^#####// \\#####^^^--_ + * _-^##########// ( ) \\##########^-_ + * -############// |\^^/| \\############- + * _/############// (@::@) \\############\_ + * /#############(( \\// ))#############\ + * -###############\\ (oo) //###############- + * -#################\\ / VV \ //#################- + * -###################\\/ \//###################- + * _#/|##########/\######( /\ )######/\##########|\#_ + * |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \| + * ` |/ V V ` V \#\| | | |/#/ V ' V V \| ' + * ` ` ` ` / | | | | \ ' ' ' ' + * ( | | | | ) + * __\ | | | | /__ + * (vvv(VVV)(VVV)vvv) + * + * HERE BE DRAGONS + * + */ + package com.github.shadowsocks.database import com.j256.ormlite.field.{DataType, DatabaseField} @@ -9,7 +48,7 @@ class Profile { @DatabaseField var name: String = null - @DatabaseField(foreign = true, canBeNull = false) + @DatabaseField var host: String = null @DatabaseField diff --git a/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala b/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala new file mode 100644 index 00000000..d456a268 --- /dev/null +++ b/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala @@ -0,0 +1,91 @@ +/* + * Shadowsocks - A shadowsocks client for Android + * Copyright (C) 2013 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * ___====-_ _-====___ + * _--^^^#####// \\#####^^^--_ + * _-^##########// ( ) \\##########^-_ + * -############// |\^^/| \\############- + * _/############// (@::@) \\############\_ + * /#############(( \\// ))#############\ + * -###############\\ (oo) //###############- + * -#################\\ / VV \ //#################- + * -###################\\/ \//###################- + * _#/|##########/\######( /\ )######/\##########|\#_ + * |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \| + * ` |/ V V ` V \#\| | | |/#/ V ' V V \| ' + * ` ` ` ` / | | | | \ ' ' ' ' + * ( | | | | ) + * __\ | | | | /__ + * (vvv(VVV)(VVV)vvv) + * + * HERE BE DRAGONS + * + */ + +package com.github.shadowsocks.database + +import android.util.Log +import com.github.shadowsocks.Shadowsocks + +class ProfileManager(dbHelper: DBHelper) { + def addProfile(profile: Profile): Boolean = { + try { + dbHelper.profileDao.createOrUpdate(profile) + } catch { + case ex: Exception => + Log.e(Shadowsocks.TAG, "addProfile", ex) + return false + } + true + } + + def getProfile(id: Int): Option[Profile] = { + try { + dbHelper.profileDao.queryForId(id) match { + case profile: Profile => Option(profile) + case _ => None + } + } catch { + case ex: Exception => + Log.e(Shadowsocks.TAG, "getProfile", ex) + return None + } + } + + def delProfile(id: Int): Boolean = { + try { + dbHelper.profileDao.deleteById(id) + } catch { + case ex: Exception => + Log.e(Shadowsocks.TAG, "delProfile", ex) + return false + } + true + } + + def getAllProfiles: Option[List[Profile]] = { + try { + import scala.collection.JavaConversions._ + Option(dbHelper.profileDao.queryForAll().toList) + } catch { + case ex: Exception => + Log.e(Shadowsocks.TAG, "getAllProfiles", ex) + None + } + } +}