Initial draft for porting this project to Gradle + Kotlin
This is one of my stupidest idea ever.
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
/*******************************************************************************
|
||||
* *
|
||||
* Copyright (C) 2017 by Max Lv <max.c.lv@gmail.com> *
|
||||
* Copyright (C) 2017 by Mygod Studio <contact-shadowsocks-android@mygod.be> *
|
||||
* *
|
||||
* 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 <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
*******************************************************************************/
|
||||
|
||||
package com.github.shadowsocks.database
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.support.v7.preference.PreferenceManager
|
||||
import android.text.TextUtils
|
||||
import com.github.shadowsocks.App.Companion.app
|
||||
import com.github.shadowsocks.utils.Key
|
||||
import com.j256.ormlite.android.AndroidDatabaseConnection
|
||||
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper
|
||||
import com.j256.ormlite.dao.Dao
|
||||
import com.j256.ormlite.support.ConnectionSource
|
||||
import com.j256.ormlite.table.TableUtils
|
||||
|
||||
object DBHelper : OrmLiteSqliteOpenHelper(app, Key.PROFILE, null, 24) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val profileDao: Dao<Profile, Int> by lazy { getDao(Profile::class.java) as Dao<Profile, Int> }
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val kvPairDao: Dao<KeyValuePair, String?> by lazy { getDao(KeyValuePair::class.java) as Dao<KeyValuePair, String?> }
|
||||
|
||||
override fun onCreate(database: SQLiteDatabase?, connectionSource: ConnectionSource?) {
|
||||
TableUtils.createTable(connectionSource, Profile::class.java)
|
||||
TableUtils.createTable(connectionSource, KeyValuePair::class.java)
|
||||
}
|
||||
|
||||
private fun recreate(database: SQLiteDatabase?, connectionSource: ConnectionSource?) {
|
||||
TableUtils.dropTable<Profile, Int>(connectionSource, Profile::class.java, true)
|
||||
TableUtils.dropTable<KeyValuePair, String?>(connectionSource, KeyValuePair::class.java, true)
|
||||
onCreate(database, connectionSource)
|
||||
}
|
||||
|
||||
override fun onUpgrade(database: SQLiteDatabase?, connectionSource: ConnectionSource?,
|
||||
oldVersion: Int, newVersion: Int) {
|
||||
if (oldVersion < 7) {
|
||||
recreate(database, connectionSource)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
if (oldVersion < 8) {
|
||||
profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN udpdns SMALLINT;")
|
||||
}
|
||||
if (oldVersion < 9) {
|
||||
profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN route VARCHAR DEFAULT 'all';")
|
||||
} else if (oldVersion < 19) {
|
||||
profileDao.executeRawNoArgs("UPDATE `profile` SET route = 'all' WHERE route IS NULL;")
|
||||
}
|
||||
if (oldVersion < 11) {
|
||||
profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN ipv6 SMALLINT;")
|
||||
}
|
||||
if (oldVersion < 12) {
|
||||
profileDao.executeRawNoArgs("BEGIN TRANSACTION;")
|
||||
profileDao.executeRawNoArgs("ALTER TABLE `profile` RENAME TO `tmp`;")
|
||||
TableUtils.createTable(connectionSource, Profile::class.java)
|
||||
profileDao.executeRawNoArgs(
|
||||
"INSERT INTO `profile`(id, name, host, localPort, remotePort, password, method, route, proxyApps, bypass," +
|
||||
" udpdns, ipv6, individual) " +
|
||||
"SELECT id, name, host, localPort, remotePort, password, method, route, 1 - global, bypass, udpdns, ipv6," +
|
||||
" individual FROM `tmp`;")
|
||||
profileDao.executeRawNoArgs("DROP TABLE `tmp`;")
|
||||
profileDao.executeRawNoArgs("COMMIT;")
|
||||
} else if (oldVersion < 13) {
|
||||
profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN tx LONG;")
|
||||
profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN rx LONG;")
|
||||
profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN date VARCHAR;")
|
||||
}
|
||||
|
||||
if (oldVersion < 15) {
|
||||
if (oldVersion >= 12) profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN userOrder LONG;")
|
||||
var i = 0L
|
||||
val apps by lazy { app.packageManager.getInstalledApplications(0) }
|
||||
for (profile in profileDao.queryForAll()) {
|
||||
if (oldVersion < 14) {
|
||||
val uidSet = profile.individual.split('|').filter(TextUtils::isDigitsOnly)
|
||||
.map(String::toInt).toSet()
|
||||
profile.individual = apps.filter { uidSet.contains(it.uid) }
|
||||
.joinToString("\n") { it.packageName }
|
||||
}
|
||||
profile.userOrder = i
|
||||
profileDao.update(profile)
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
|
||||
if (oldVersion < 16) {
|
||||
profileDao.executeRawNoArgs(
|
||||
"UPDATE `profile` SET route = 'bypass-lan-china' WHERE route = 'bypass-china'")
|
||||
}
|
||||
|
||||
if (oldVersion < 21) {
|
||||
profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN remoteDns VARCHAR DEFAULT '8.8.8.8';")
|
||||
}
|
||||
|
||||
if (oldVersion < 17) {
|
||||
profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN plugin VARCHAR;")
|
||||
} else if (oldVersion < 22) {
|
||||
// upgrade kcptun to SIP003 plugin
|
||||
profileDao.executeRawNoArgs("BEGIN TRANSACTION;")
|
||||
profileDao.executeRawNoArgs("ALTER TABLE `profile` RENAME TO `tmp`;")
|
||||
TableUtils.createTable(connectionSource, Profile::class.java)
|
||||
profileDao.executeRawNoArgs(
|
||||
"INSERT INTO `profile`(id, name, host, localPort, remotePort, password, method, route, " +
|
||||
"remoteDns, proxyApps, bypass, udpdns, ipv6, individual, tx, rx, date, userOrder, " +
|
||||
"plugin) " +
|
||||
"SELECT id, name, host, localPort, " +
|
||||
"CASE WHEN kcp = 1 THEN kcpPort ELSE remotePort END, password, method, route, " +
|
||||
"remoteDns, proxyApps, bypass, udpdns, ipv6, individual, tx, rx, date, userOrder, " +
|
||||
"CASE WHEN kcp = 1 THEN 'kcptun ' || kcpcli ELSE NULL END FROM `tmp`;")
|
||||
profileDao.executeRawNoArgs("DROP TABLE `tmp`;")
|
||||
profileDao.executeRawNoArgs("COMMIT;")
|
||||
}
|
||||
|
||||
if (oldVersion < 23) {
|
||||
profileDao.executeRawNoArgs("BEGIN TRANSACTION;")
|
||||
TableUtils.createTable(connectionSource, KeyValuePair::class.java)
|
||||
profileDao.executeRawNoArgs("COMMIT;")
|
||||
val old = PreferenceManager.getDefaultSharedPreferences(app)
|
||||
kvPairDao.createOrUpdate(KeyValuePair(Key.id).put(old.getInt(Key.id, 0)))
|
||||
kvPairDao.createOrUpdate(KeyValuePair(Key.tfo).put(old.getBoolean(Key.tfo, false)))
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
app.track(ex)
|
||||
recreate(database, connectionSource)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
|
||||
val connection = AndroidDatabaseConnection(db, true)
|
||||
connectionSource.saveSpecialConnection(connection)
|
||||
recreate(db, connectionSource)
|
||||
connectionSource.clearSpecialConnection(connection)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*******************************************************************************
|
||||
* *
|
||||
* Copyright (C) 2017 by Max Lv <max.c.lv@gmail.com> *
|
||||
* Copyright (C) 2017 by Mygod Studio <contact-shadowsocks-android@mygod.be> *
|
||||
* *
|
||||
* 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 <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
*******************************************************************************/
|
||||
|
||||
package com.github.shadowsocks.database
|
||||
|
||||
import com.j256.ormlite.field.DataType
|
||||
import com.j256.ormlite.field.DatabaseField
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
class KeyValuePair() {
|
||||
companion object {
|
||||
const val TYPE_UNINITIALIZED = 0
|
||||
const val TYPE_BOOLEAN = 1
|
||||
const val TYPE_FLOAT = 2
|
||||
const val TYPE_INT = 3
|
||||
const val TYPE_LONG = 4
|
||||
const val TYPE_STRING = 5
|
||||
const val TYPE_STRING_SET = 6
|
||||
}
|
||||
|
||||
@DatabaseField(id = true)
|
||||
var key: String? = ""
|
||||
@DatabaseField
|
||||
var valueType: Int = TYPE_UNINITIALIZED
|
||||
@DatabaseField(dataType = DataType.BYTE_ARRAY)
|
||||
var value: ByteArray = ByteArray(0)
|
||||
|
||||
val boolean: Boolean?
|
||||
get() = if (valueType == TYPE_BOOLEAN) ByteBuffer.wrap(value).get() != 0.toByte() else null
|
||||
val float: Float?
|
||||
get() = if (valueType == TYPE_FLOAT) ByteBuffer.wrap(value).float else null
|
||||
val int: Int?
|
||||
get() = if (valueType == TYPE_INT) ByteBuffer.wrap(value).int else null
|
||||
val long: Long?
|
||||
get() = if (valueType == TYPE_LONG) ByteBuffer.wrap(value).long else null
|
||||
val string: String?
|
||||
get() = if (valueType == TYPE_STRING) String(value) else null
|
||||
val stringSet: MutableSet<String>?
|
||||
get() = if (valueType == TYPE_STRING_SET) {
|
||||
val buffer = ByteBuffer.wrap(value)
|
||||
val result = HashSet<String>()
|
||||
while (buffer.hasRemaining()) {
|
||||
val chArr = ByteArray(buffer.int)
|
||||
buffer.get(chArr)
|
||||
result.add(String(chArr))
|
||||
}
|
||||
result
|
||||
} else null
|
||||
|
||||
constructor(key: String?) : this() {
|
||||
this.key = key
|
||||
}
|
||||
|
||||
// putting null requires using DataStore
|
||||
fun put(value: Boolean): KeyValuePair {
|
||||
valueType = TYPE_BOOLEAN
|
||||
this.value = ByteBuffer.allocate(1).put((if (value) 1 else 0).toByte()).array()
|
||||
return this
|
||||
}
|
||||
fun put(value: Float): KeyValuePair {
|
||||
valueType = TYPE_FLOAT
|
||||
this.value = ByteBuffer.allocate(4).putFloat(value).array()
|
||||
return this
|
||||
}
|
||||
fun put(value: Int): KeyValuePair {
|
||||
valueType = TYPE_INT
|
||||
this.value = ByteBuffer.allocate(4).putInt(value).array()
|
||||
return this
|
||||
}
|
||||
fun put(value: Long): KeyValuePair {
|
||||
valueType = TYPE_LONG
|
||||
this.value = ByteBuffer.allocate(8).putLong(value).array()
|
||||
return this
|
||||
}
|
||||
fun put(value: String): KeyValuePair {
|
||||
valueType = TYPE_STRING
|
||||
this.value = value.toByteArray()
|
||||
return this
|
||||
}
|
||||
fun put(value: MutableSet<String>): KeyValuePair {
|
||||
valueType = TYPE_STRING_SET
|
||||
val stream = ByteArrayOutputStream()
|
||||
for (v in value) {
|
||||
stream.write(ByteBuffer.allocate(4).putInt(v.length).array())
|
||||
stream.write(v.toByteArray())
|
||||
}
|
||||
this.value = stream.toByteArray()
|
||||
return this
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
/*******************************************************************************
|
||||
* *
|
||||
* Copyright (C) 2017 by Max Lv <max.c.lv@gmail.com> *
|
||||
* Copyright (C) 2017 by Mygod Studio <contact-shadowsocks-android@mygod.be> *
|
||||
* *
|
||||
* 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 <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
*******************************************************************************/
|
||||
|
||||
package com.github.shadowsocks.database
|
||||
|
||||
import android.net.Uri
|
||||
import android.util.Base64
|
||||
import android.util.Log
|
||||
import com.github.shadowsocks.plugin.PluginConfiguration
|
||||
import com.github.shadowsocks.preference.DataStore
|
||||
import com.github.shadowsocks.utils.Key
|
||||
import com.github.shadowsocks.utils.parsePort
|
||||
import com.j256.ormlite.field.DataType
|
||||
import com.j256.ormlite.field.DatabaseField
|
||||
import java.net.URI
|
||||
import java.util.*
|
||||
|
||||
class Profile {
|
||||
companion object {
|
||||
private const val TAG = "ShadowParser"
|
||||
private val pattern = """(?i)ss://[-a-zA-Z0-9+&@#/%?=~_|!:,.;\[\]]*[-a-zA-Z0-9+&@#/%=~_|\[\]]""".toRegex()
|
||||
private val userInfoPattern = "^(.+?):(.*)$".toRegex()
|
||||
private val legacyPattern = "^(.+?):(.*)@(.+?):(\\d+?)$".toRegex()
|
||||
|
||||
fun findAll(data: CharSequence?) = pattern.findAll(data ?: "").map {
|
||||
val uri = Uri.parse(it.value)
|
||||
if (uri.userInfo == null) {
|
||||
val match = legacyPattern.matchEntire(String(Base64.decode(uri.host, Base64.NO_PADDING)))
|
||||
if (match != null) {
|
||||
val profile = Profile()
|
||||
profile.method = match.groupValues[1].toLowerCase()
|
||||
profile.password = match.groupValues[2]
|
||||
profile.host = match.groupValues[3]
|
||||
profile.remotePort = match.groupValues[4].toInt()
|
||||
profile.plugin = uri.getQueryParameter(Key.plugin)
|
||||
profile.name = uri.fragment
|
||||
profile
|
||||
} else {
|
||||
Log.e(TAG, "Unrecognized URI: ${it.value}")
|
||||
null
|
||||
}
|
||||
} else {
|
||||
val match = userInfoPattern.matchEntire(String(Base64.decode(uri.userInfo,
|
||||
Base64.NO_PADDING or Base64.NO_WRAP or Base64.URL_SAFE)))
|
||||
if (match != null) {
|
||||
val profile = Profile()
|
||||
profile.method = match.groupValues[1]
|
||||
profile.password = match.groupValues[2]
|
||||
// bug in Android: https://code.google.com/p/android/issues/detail?id=192855
|
||||
val javaURI = URI(it.value)
|
||||
profile.host = javaURI.host
|
||||
if (profile.host.firstOrNull() == '[' && profile.host.lastOrNull() == ']')
|
||||
profile.host = profile.host.substring(1, profile.host.length - 1)
|
||||
profile.remotePort = javaURI.port
|
||||
profile.plugin = uri.getQueryParameter(Key.plugin)
|
||||
profile.name = uri.fragment ?: ""
|
||||
profile
|
||||
} else {
|
||||
Log.e(TAG, "Unknown user info: ${it.value}")
|
||||
null
|
||||
}
|
||||
}
|
||||
}.filterNotNull()
|
||||
}
|
||||
|
||||
@DatabaseField(generatedId = true)
|
||||
var id: Int = 0
|
||||
|
||||
@DatabaseField
|
||||
var name: String = ""
|
||||
|
||||
@DatabaseField
|
||||
var host: String = "198.199.101.152"
|
||||
|
||||
@DatabaseField
|
||||
var remotePort: Int = 8388
|
||||
|
||||
@DatabaseField
|
||||
var password: String = "u1rRWTssNv0p"
|
||||
|
||||
@DatabaseField
|
||||
var method: String = "aes-256-cfb"
|
||||
|
||||
@DatabaseField
|
||||
var route: String = "all"
|
||||
|
||||
@DatabaseField
|
||||
var remoteDns: String = "8.8.8.8"
|
||||
|
||||
@DatabaseField
|
||||
var proxyApps: Boolean = false
|
||||
|
||||
@DatabaseField
|
||||
var bypass: Boolean = false
|
||||
|
||||
@DatabaseField
|
||||
var udpdns: Boolean = false
|
||||
|
||||
@DatabaseField
|
||||
var ipv6: Boolean = true
|
||||
|
||||
@DatabaseField(dataType = DataType.LONG_STRING)
|
||||
var individual: String = ""
|
||||
|
||||
@DatabaseField
|
||||
var tx: Long = 0
|
||||
|
||||
@DatabaseField
|
||||
var rx: Long = 0
|
||||
|
||||
@DatabaseField
|
||||
val date: Date = Date()
|
||||
|
||||
@DatabaseField
|
||||
var userOrder: Long = 0
|
||||
|
||||
@DatabaseField
|
||||
var plugin: String? = null
|
||||
|
||||
val formattedAddress get() = (if (host.contains(":")) "[%s]:%d" else "%s:%d").format(host, remotePort)
|
||||
val formattedName get() = if (name.isEmpty()) formattedAddress else name
|
||||
|
||||
fun toUri(): Uri {
|
||||
val builder = Uri.Builder()
|
||||
.scheme("ss")
|
||||
.encodedAuthority("%s@%s:%d".format(Locale.ENGLISH,
|
||||
Base64.encodeToString("%s:%s".format(Locale.ENGLISH, method, password).toByteArray(),
|
||||
Base64.NO_PADDING or Base64.NO_WRAP or Base64.URL_SAFE),
|
||||
if (host.contains(':')) "[$host]" else host, remotePort))
|
||||
val configuration = PluginConfiguration(plugin ?: "")
|
||||
if (configuration.selected.isNotEmpty())
|
||||
builder.appendQueryParameter(Key.plugin, configuration.selectedOptions.toString(false))
|
||||
if (name.isNotEmpty()) builder.fragment(name)
|
||||
return builder.build()
|
||||
}
|
||||
override fun toString() = toUri().toString()
|
||||
|
||||
fun serialize() {
|
||||
DataStore.putString(Key.name, name)
|
||||
DataStore.putString(Key.host, host)
|
||||
DataStore.putString(Key.remotePort, remotePort.toString())
|
||||
DataStore.putString(Key.password, password)
|
||||
DataStore.putString(Key.route, route)
|
||||
DataStore.putString(Key.remoteDns, remoteDns)
|
||||
DataStore.putString(Key.method, method)
|
||||
DataStore.proxyApps = proxyApps
|
||||
DataStore.bypass = bypass
|
||||
DataStore.putBoolean(Key.udpdns, udpdns)
|
||||
DataStore.putBoolean(Key.ipv6, ipv6)
|
||||
DataStore.individual = individual
|
||||
DataStore.plugin = plugin ?: ""
|
||||
DataStore.remove(Key.dirty)
|
||||
}
|
||||
fun deserialize() {
|
||||
// It's assumed that default values are never used, so 0/false/null is always used even if that isn't the case
|
||||
name = DataStore.getString(Key.name) ?: ""
|
||||
host = DataStore.getString(Key.host) ?: ""
|
||||
remotePort = parsePort(DataStore.getString(Key.remotePort), 8388, 1)
|
||||
password = DataStore.getString(Key.password) ?: ""
|
||||
method = DataStore.getString(Key.method) ?: ""
|
||||
route = DataStore.getString(Key.route) ?: ""
|
||||
remoteDns = DataStore.getString(Key.remoteDns) ?: ""
|
||||
proxyApps = DataStore.proxyApps
|
||||
bypass = DataStore.bypass
|
||||
udpdns = DataStore.getBoolean(Key.udpdns, false)
|
||||
ipv6 = DataStore.getBoolean(Key.ipv6, false)
|
||||
individual = DataStore.individual
|
||||
plugin = DataStore.plugin
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*******************************************************************************
|
||||
* *
|
||||
* Copyright (C) 2017 by Max Lv <max.c.lv@gmail.com> *
|
||||
* Copyright (C) 2017 by Mygod Studio <contact-shadowsocks-android@mygod.be> *
|
||||
* *
|
||||
* 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 <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
*******************************************************************************/
|
||||
|
||||
package com.github.shadowsocks.database
|
||||
|
||||
import android.util.Log
|
||||
import com.github.shadowsocks.App.Companion.app
|
||||
import com.github.shadowsocks.ProfilesFragment
|
||||
|
||||
object ProfileManager {
|
||||
const val TAG = "ProfileManager"
|
||||
|
||||
fun createProfile(p: Profile? = null): Profile {
|
||||
val profile = p ?: Profile()
|
||||
profile.id = 0
|
||||
try {
|
||||
val oldProfile = app.currentProfile
|
||||
if (oldProfile != null) {
|
||||
// Copy Feature Settings from old profile
|
||||
profile.route = oldProfile.route
|
||||
profile.ipv6 = oldProfile.ipv6
|
||||
profile.proxyApps = oldProfile.proxyApps
|
||||
profile.bypass = oldProfile.bypass
|
||||
profile.individual = oldProfile.individual
|
||||
profile.udpdns = oldProfile.udpdns
|
||||
}
|
||||
val last = DBHelper.profileDao.queryRaw(DBHelper.profileDao.queryBuilder().selectRaw("MAX(userOrder)")
|
||||
.prepareStatementString()).firstResult
|
||||
if (last != null && last.size == 1 && last[0] != null) profile.userOrder = last[0].toLong() + 1
|
||||
DBHelper.profileDao.createOrUpdate(profile)
|
||||
ProfilesFragment.instance?.profilesAdapter?.add(profile)
|
||||
} catch (ex: Exception) {
|
||||
Log.e(TAG, "addProfile", ex)
|
||||
app.track(ex)
|
||||
}
|
||||
return profile
|
||||
}
|
||||
|
||||
fun updateProfile(profile: Profile): Boolean = try {
|
||||
DBHelper.profileDao.update(profile)
|
||||
true
|
||||
} catch (ex: Exception) {
|
||||
Log.e(TAG, "updateProfile", ex)
|
||||
app.track(ex)
|
||||
false
|
||||
}
|
||||
|
||||
fun getProfile(id: Int): Profile? = try {
|
||||
DBHelper.profileDao.queryForId(id)
|
||||
} catch (ex: Exception) {
|
||||
Log.e(TAG, "getProfile", ex)
|
||||
app.track(ex)
|
||||
null
|
||||
}
|
||||
|
||||
fun delProfile(id: Int): Boolean = try {
|
||||
DBHelper.profileDao.deleteById(id)
|
||||
ProfilesFragment.instance?.profilesAdapter?.removeId(id)
|
||||
true
|
||||
} catch (ex: Exception) {
|
||||
Log.e(TAG, "delProfile", ex)
|
||||
app.track(ex)
|
||||
false
|
||||
}
|
||||
|
||||
fun getFirstProfile(): Profile? = try {
|
||||
val result = DBHelper.profileDao.query(DBHelper.profileDao.queryBuilder().limit(1L).prepare())
|
||||
if (result.size == 1) result[0] else null
|
||||
} catch (ex: Exception) {
|
||||
Log.e(TAG, "getAllProfiles", ex)
|
||||
app.track(ex)
|
||||
null
|
||||
}
|
||||
|
||||
fun getAllProfiles(): List<Profile>? = try {
|
||||
DBHelper.profileDao.query(DBHelper.profileDao.queryBuilder().orderBy("userOrder", true).prepare())
|
||||
} catch (ex: Exception) {
|
||||
Log.e(TAG, "getAllProfiles", ex)
|
||||
app.track(ex)
|
||||
null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user