Commit 97b107eb authored by Max Lv's avatar Max Lv

fix #47 backup the entire database

parent cf60b54d
...@@ -105,22 +105,6 @@ trait BaseService { ...@@ -105,22 +105,6 @@ trait BaseService {
}) })
} }
def getPid(name: String): Int = {
try {
val reader: BufferedReader = new BufferedReader(new FileReader(Path.BASE + name + ".pid"))
val line = reader.readLine
return Integer.valueOf(line)
} catch {
case e: FileNotFoundException =>
Log.e(getTag, "Cannot open pid file: " + name)
case e: IOException =>
Log.e(getTag, "Cannot read pid file: " + name)
case e: NumberFormatException =>
Log.e(getTag, "Invalid pid", e)
}
-1
}
def initSoundVibrateLights(notification: Notification) { def initSoundVibrateLights(notification: Notification) {
notification.sound = null notification.sound = null
} }
......
/* /*
* Shadowsocks - A shadowsocks client for Android * Shadowsocks - A shadowsocks client for Android
* Copyright (C) 2014 <max.c.lv@gmail.com> * Copyright (C) 2013 <max.c.lv@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
...@@ -41,9 +41,7 @@ package com.github.shadowsocks ...@@ -41,9 +41,7 @@ package com.github.shadowsocks
import android.app.backup.{FileBackupHelper, SharedPreferencesBackupHelper, BackupAgentHelper} import android.app.backup.{FileBackupHelper, SharedPreferencesBackupHelper, BackupAgentHelper}
import android.content.Context import android.content.Context
import com.github.shadowsocks.database.DBHelper
case class DbBackupHelper(context: Context, dbName: String)
extends FileBackupHelper(context, context.getDatabasePath(dbName).getAbsolutePath)
class ShadowsocksBackupAgent extends BackupAgentHelper { class ShadowsocksBackupAgent extends BackupAgentHelper {
...@@ -60,6 +58,6 @@ class ShadowsocksBackupAgent extends BackupAgentHelper { ...@@ -60,6 +58,6 @@ class ShadowsocksBackupAgent extends BackupAgentHelper {
override def onCreate() { override def onCreate() {
val helper = new SharedPreferencesBackupHelper(this, PREFS_DISPLAY) val helper = new SharedPreferencesBackupHelper(this, PREFS_DISPLAY)
addHelper(MY_PREFS_BACKUP_KEY, helper) addHelper(MY_PREFS_BACKUP_KEY, helper)
addHelper(DATABASE, new DbBackupHelper(this, "profile.db")) addHelper(DATABASE, new FileBackupHelper(this, "../databases/" + DBHelper.PROFILE))
} }
} }
...@@ -152,24 +152,6 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -152,24 +152,6 @@ class ShadowsocksNatService extends Service with BaseService {
Utils.runRootCommand(cmd) Utils.runRootCommand(cmd)
} }
def waitForProcess(name: String): Boolean = {
val pid: Int = getPid(name)
if (pid == -1) return false
Exec.hangupProcessGroup(-pid)
val t: Thread = new Thread {
override def run() {
Exec.waitFor(-pid)
}
}
t.start()
try {
t.join(300)
} catch {
case ignored: InterruptedException =>
}
!t.isAlive
}
/** Called when the activity is first created. */ /** Called when the activity is first created. */
def handleConnection: Boolean = { def handleConnection: Boolean = {
...@@ -292,20 +274,17 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -292,20 +274,17 @@ class ShadowsocksNatService extends Service with BaseService {
val sb = new StringBuilder val sb = new StringBuilder
sb ++= "kill -9 `cat /data/data/com.github.shadowsocks/redsocks.pid`" ++= "\n" sb ++= "kill -9 `cat " ++= Path.BASE ++= "redsocks.pid`" ++= "\n"
sb ++= "killall -9 redsocks" ++= "\n" sb ++= "killall -9 redsocks" ++= "\n"
Utils.runRootCommand(sb.toString()) Utils.runRootCommand(sb.toString())
sb.clear() sb.clear()
if (!waitForProcess("pdnsd")) { sb ++= "kill -9 `cat " ++= Path.BASE ++= "pdnsd.pid`" ++= "\n"
sb ++= "kill -9 `cat /data/data/com.github.shadowsocks/pdnsd.pid`" ++= "\n" sb ++= "killall -9 pdnsd" ++= "\n"
sb ++= "killall -9 pdnsd" ++= "\n" sb ++= "kill -9 `cat " ++= Path.BASE ++= "shadowsocks.pid`" ++= "\n"
} sb ++= "killall -9 shadowsocks" ++= "\n"
if (!waitForProcess("shadowsocks")) {
sb ++= "kill -9 `cat /data/data/com.github.shadowsocks/shadowsocks.pid`" ++= "\n"
sb ++= "killall -9 shadowsocks" ++= "\n"
}
Utils.runCommand(sb.toString()) Utils.runCommand(sb.toString())
} }
......
...@@ -117,24 +117,6 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -117,24 +117,6 @@ class ShadowsocksVpnService extends VpnService with BaseService {
version version
} }
def waitForProcess(name: String): Boolean = {
val pid: Int = getPid(name)
if (pid == -1) return false
Exec.hangupProcessGroup(-pid)
val t: Thread = new Thread {
override def run() {
Exec.waitFor(-pid)
}
}
t.start()
try {
t.join(300)
} catch {
case ignored: InterruptedException =>
}
!t.isAlive
}
def startVpn() { def startVpn() {
val proxy_address = config.proxy val proxy_address = config.proxy
...@@ -272,18 +254,14 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -272,18 +254,14 @@ class ShadowsocksVpnService extends VpnService with BaseService {
def killProcesses() { def killProcesses() {
val sb = new StringBuilder val sb = new StringBuilder
if (!waitForProcess("shadowsocks")) {
sb ++= "kill -9 `cat /data/data/com.github.shadowsocks/shadowsocks.pid`" ++= "\n" sb ++= "kill -9 `cat " ++= Path.BASE ++= "shadowsocks.pid`" ++= "\n"
sb ++= "killall -9 shadowsocks" ++= "\n" sb ++= "killall -9 shadowsocks" ++= "\n"
} sb ++= "kill -9 `cat " ++= Path.BASE ++= "tun2socks.pid`" ++= "\n"
if (!waitForProcess("tun2socks")) { sb ++= "killall -9 tun2socks" ++= "\n"
sb ++= "kill -9 `cat /data/data/com.github.shadowsocks/tun2socks.pid`" ++= "\n" sb ++= "kill -9 `cat " ++= Path.BASE ++= "pdnsd.pid`" ++= "\n"
sb ++= "killall -9 tun2socks" ++= "\n" sb ++= "killall -9 pdnsd" ++= "\n"
}
if (!waitForProcess("pdnsd")) {
sb ++= "kill -9 `cat /data/data/com.github.shadowsocks/pdnsd.pid`" ++= "\n"
sb ++= "killall -9 pdnsd" ++= "\n"
}
Utils.runCommand(sb.toString()) Utils.runCommand(sb.toString())
} }
......
/* /*
* Shadowsocks - A shadowsocks client for Android * Shadowsocks - A shadowsocks client for Android
* Copyright (C) 2014 <max.c.lv@gmail.com> * Copyright (C) 2013 <max.c.lv@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
...@@ -46,7 +46,11 @@ import com.j256.ormlite.support.ConnectionSource ...@@ -46,7 +46,11 @@ import com.j256.ormlite.support.ConnectionSource
import com.j256.ormlite.table.TableUtils import com.j256.ormlite.table.TableUtils
import com.j256.ormlite.dao.Dao import com.j256.ormlite.dao.Dao
class DBHelper(val context: Context) extends OrmLiteSqliteOpenHelper(context, "profile.db", null, 7) { object DBHelper {
val PROFILE = "profile.db"
}
class DBHelper(val context: Context) extends OrmLiteSqliteOpenHelper(context, DBHelper.PROFILE, null, 7) {
lazy val profileDao:Dao[Profile,Int] = getDao(classOf[Profile]) lazy val profileDao:Dao[Profile,Int] = getDao(classOf[Profile])
...@@ -55,9 +59,7 @@ class DBHelper(val context: Context) extends OrmLiteSqliteOpenHelper(context, "p ...@@ -55,9 +59,7 @@ class DBHelper(val context: Context) extends OrmLiteSqliteOpenHelper(context, "p
} }
def onUpgrade(database: SQLiteDatabase, connectionSource: ConnectionSource, oldVersion: Int, newVersion: Int) { def onUpgrade(database: SQLiteDatabase, connectionSource: ConnectionSource, oldVersion: Int, newVersion: Int) {
if (newVersion != oldVersion) { TableUtils.dropTable(connectionSource, classOf[Profile], true)
TableUtils.dropTable(connectionSource, classOf[Profile], true) onCreate(database, connectionSource)
onCreate(database, connectionSource)
}
} }
} }
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