From 333ee1565e3bed18d379467d1cdc50b4f617e906 Mon Sep 17 00:00:00 2001 From: Max Lv Date: Thu, 6 Jun 2013 17:32:05 +0800 Subject: [PATCH] fix the crash_recover --- .../com/github/shadowsocks/Shadowsocks.scala | 11 +++--- .../shadowsocks/ShadowsocksReceiver.scala | 6 ++-- .../scala/com/github/shadowsocks/Utils.scala | 35 ++++--------------- 3 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index 615a1bba..4a04bbc0 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -209,6 +209,7 @@ class Shadowsocks private var switchButton: Switch = null private var progressDialog: ProgressDialog = null private var settings: SharedPreferences = null + private var status: SharedPreferences = null private var prepared = false private var state = State.INIT private var receiver: StateBroadcastReceiver = null @@ -218,7 +219,7 @@ class Shadowsocks msg.what match { case MSG_CRASH_RECOVER => Crouton.makeText(Shadowsocks.this, R.string.crash_alert, Style.ALERT).show() - settings.edit().putBoolean(Key.isRunning, false).apply() + status.edit().putBoolean(Key.isRunning, false).commit() case MSG_INITIAL_FINISH => clearDialog() } @@ -367,6 +368,7 @@ class Shadowsocks switchButton = switchLayout.findViewById(R.id.switchButton).asInstanceOf[Switch] settings = PreferenceManager.getDefaultSharedPreferences(this) + status = getSharedPreferences(Key.status, Context.MODE_PRIVATE) receiver = new StateBroadcastReceiver() registerReceiver(receiver, new IntentFilter(Action.UPDATE_STATE)) @@ -377,9 +379,8 @@ class Shadowsocks } spawn { settings.edit().putBoolean(Key.isRoot, Utils.getRoot).commit() - val update = getSharedPreferences(Key.update, Context.MODE_WORLD_WRITEABLE) - if (!update.getBoolean(getVersionName, false)) { - update.edit.putBoolean(getVersionName, true).apply() + if (!status.getBoolean(getVersionName, false)) { + status.edit.putBoolean(getVersionName, true).apply() reset() } handler.sendEmptyMessage(MSG_INITIAL_FINISH) @@ -459,7 +460,7 @@ class Shadowsocks switchButton.setChecked(false) Crouton.cancelAllCroutons() setPreferenceEnabled(true) - if (settings.getBoolean(Key.isRunning, false)) { + if (status.getBoolean(Key.isRunning, false)) { spawn { crash_recovery() handler.sendEmptyMessage(MSG_CRASH_RECOVER) diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksReceiver.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksReceiver.scala index be810464..80004ac1 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksReceiver.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksReceiver.scala @@ -50,7 +50,7 @@ class ShadowsocksReceiver extends BroadcastReceiver { def onReceive(context: Context, intent: Intent) { val settings: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) - val update = context.getSharedPreferences(Key.update, Context.MODE_WORLD_READABLE) + val status = context.getSharedPreferences(Key.status, Context.MODE_PRIVATE) if (intent.getAction == Action.UPDATE_STATE) { val state = intent.getIntExtra(Extra.STATE, State.INIT) @@ -59,7 +59,7 @@ class ShadowsocksReceiver extends BroadcastReceiver { case State.CONNECTED => true case _ => false } - settings.edit.putBoolean(Key.isRunning, running).commit() + status.edit.putBoolean(Key.isRunning, running).commit() return } @@ -72,7 +72,7 @@ class ShadowsocksReceiver extends BroadcastReceiver { } } val isAutoConnect: Boolean = settings.getBoolean(Key.isAutoConnect, false) - val isInstalled: Boolean = update.getBoolean(versionName, false) + val isInstalled: Boolean = status.getBoolean(versionName, false) if (isAutoConnect && isInstalled) { if (Utils.getRoot) { if (ShadowsocksService.isServiceStarted(context)) return diff --git a/src/main/scala/com/github/shadowsocks/Utils.scala b/src/main/scala/com/github/shadowsocks/Utils.scala index fac895ca..3d09745c 100644 --- a/src/main/scala/com/github/shadowsocks/Utils.scala +++ b/src/main/scala/com/github/shadowsocks/Utils.scala @@ -54,7 +54,7 @@ import android.app.ActivityManager object Key { val isRoot = "isRoot" - val update = "update" + val status = "status" val proxyedApps = "proxyedApps" val isRunning = "isRunning" @@ -325,30 +325,9 @@ object Utils { * @return The Property, or NULL if not found */ def getSystemProperty(propName: String): String = { - var line: String = null - var input: BufferedReader = null - try { - val p: Process = Runtime.getRuntime.exec("getprop " + propName) - input = new BufferedReader(new InputStreamReader(p.getInputStream), 1024) - line = input.readLine - input.close() - } catch { - case ex: IOException => { - Log.e(TAG, "Unable to read sysprop " + propName, ex) - return null - } - } finally { - if (input != null) { - try { - input.close() - } catch { - case ex: IOException => { - Log.e(TAG, "Exception while closing InputStream", ex) - } - } - } - } - line + val p: Process = Runtime.getRuntime.exec("getprop " + propName) + val lines = scala.io.Source.fromInputStream(p.getInputStream).getLines() + if (lines.hasNext) lines.next() else null } /** @@ -404,13 +383,13 @@ object Utils { def getAppIcon(c: Context, uid: Int): Drawable = { val pm: PackageManager = c.getPackageManager - var appIcon: Drawable = c.getResources.getDrawable(android.R.drawable.sym_def_app_icon) + val icon: Drawable = c.getResources.getDrawable(android.R.drawable.sym_def_app_icon) val packages: Array[String] = pm.getPackagesForUid(uid) if (packages != null) { if (packages.length >= 1) { try { val appInfo: ApplicationInfo = pm.getApplicationInfo(packages(0), 0) - appIcon = pm.getApplicationIcon(appInfo) + return pm.getApplicationIcon(appInfo) } catch { case e: PackageManager.NameNotFoundException => { Log.e(c.getPackageName, "No package found matching with the uid " + uid) @@ -420,7 +399,7 @@ object Utils { } else { Log.e(c.getPackageName, "Package not found for uid " + uid) } - appIcon + icon } def getDataPath(ctx: Context): String = {