Commit 28ea9dd8 authored by Mygod's avatar Mygod

Use app.* instead of ShadowsocksApplication.*

Because typing ShadowsocksApplication is quite troublesome.
parent c6958474
...@@ -53,6 +53,7 @@ import android.support.v7.widget.{DefaultItemAnimator, LinearLayoutManager, Recy ...@@ -53,6 +53,7 @@ import android.support.v7.widget.{DefaultItemAnimator, LinearLayoutManager, Recy
import android.view._ import android.view._
import android.widget._ import android.widget._
import com.github.shadowsocks.utils.{Key, Utils} import com.github.shadowsocks.utils.{Key, Utils}
import com.github.shadowsocks.ShadowsocksApplication.app
import scala.collection.JavaConversions._ import scala.collection.JavaConversions._
import scala.collection.mutable import scala.collection.mutable
...@@ -71,7 +72,7 @@ object AppManager { ...@@ -71,7 +72,7 @@ object AppManager {
val filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED) val filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED)
filter.addAction(Intent.ACTION_PACKAGE_REMOVED) filter.addAction(Intent.ACTION_PACKAGE_REMOVED)
filter.addDataScheme("package") filter.addDataScheme("package")
ShadowsocksApplication.instance.registerReceiver((context: Context, intent: Intent) => app.registerReceiver((context: Context, intent: Intent) =>
if (intent.getAction != Intent.ACTION_PACKAGE_REMOVED || if (intent.getAction != Intent.ACTION_PACKAGE_REMOVED ||
!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
synchronized(cachedApps = null) synchronized(cachedApps = null)
...@@ -96,13 +97,13 @@ class AppManager extends AppCompatActivity with OnMenuItemClickListener { ...@@ -96,13 +97,13 @@ class AppManager extends AppCompatActivity with OnMenuItemClickListener {
private final class AppViewHolder(val view: View) extends RecyclerView.ViewHolder(view) with View.OnClickListener { private final class AppViewHolder(val view: View) extends RecyclerView.ViewHolder(view) with View.OnClickListener {
private val icon = itemView.findViewById(R.id.itemicon).asInstanceOf[ImageView] private val icon = itemView.findViewById(R.id.itemicon).asInstanceOf[ImageView]
private val check = itemView.findViewById(R.id.itemcheck).asInstanceOf[Switch] private val check = itemView.findViewById(R.id.itemcheck).asInstanceOf[Switch]
private var app: ProxiedApp = _ private var item: ProxiedApp = _
itemView.setOnClickListener(this) itemView.setOnClickListener(this)
private def proxied = proxiedApps.contains(app.packageName) private def proxied = proxiedApps.contains(item.packageName)
def bind(app: ProxiedApp) { def bind(app: ProxiedApp) {
this.app = app this.item = app
icon.setImageDrawable(app.icon) icon.setImageDrawable(app.icon)
check.setText(app.name) check.setText(app.name)
check.setChecked(proxied) check.setChecked(proxied)
...@@ -110,14 +111,14 @@ class AppManager extends AppCompatActivity with OnMenuItemClickListener { ...@@ -110,14 +111,14 @@ class AppManager extends AppCompatActivity with OnMenuItemClickListener {
def onClick(v: View) { def onClick(v: View) {
if (proxied) { if (proxied) {
proxiedApps.remove(app.packageName) proxiedApps.remove(item.packageName)
check.setChecked(false) check.setChecked(false)
} else { } else {
proxiedApps.add(app.packageName) proxiedApps.add(item.packageName)
check.setChecked(true) check.setChecked(true)
} }
if (!appsLoading.get) if (!appsLoading.get)
ShadowsocksApplication.settings.edit.putString(Key.proxied, proxiedApps.mkString("\n")).apply app.editor.putString(Key.proxied, proxiedApps.mkString("\n")).apply
} }
} }
...@@ -133,7 +134,7 @@ class AppManager extends AppCompatActivity with OnMenuItemClickListener { ...@@ -133,7 +134,7 @@ class AppManager extends AppCompatActivity with OnMenuItemClickListener {
new AppViewHolder(LayoutInflater.from(vg.getContext).inflate(R.layout.layout_apps_item, vg, false)) new AppViewHolder(LayoutInflater.from(vg.getContext).inflate(R.layout.layout_apps_item, vg, false))
} }
private val proxiedApps = ShadowsocksApplication.settings.getString(Key.proxied, "").split('\n').to[mutable.HashSet] private val proxiedApps = app.settings.getString(Key.proxied, "").split('\n').to[mutable.HashSet]
private var toolbar: Toolbar = _ private var toolbar: Toolbar = _
private var appListView: RecyclerView = _ private var appListView: RecyclerView = _
private var loadingView: View = _ private var loadingView: View = _
...@@ -208,17 +209,17 @@ class AppManager extends AppCompatActivity with OnMenuItemClickListener { ...@@ -208,17 +209,17 @@ class AppManager extends AppCompatActivity with OnMenuItemClickListener {
toolbar.inflateMenu(R.menu.app_manager_menu) toolbar.inflateMenu(R.menu.app_manager_menu)
toolbar.setOnMenuItemClickListener(this) toolbar.setOnMenuItemClickListener(this)
ShadowsocksApplication.settings.edit().putBoolean(Key.isProxyApps, true).commit() app.editor.putBoolean(Key.isProxyApps, true).apply
findViewById(R.id.onSwitch).asInstanceOf[Switch] findViewById(R.id.onSwitch).asInstanceOf[Switch]
.setOnCheckedChangeListener((_, checked) => { .setOnCheckedChangeListener((_, checked) => {
ShadowsocksApplication.settings.edit().putBoolean(Key.isProxyApps, checked).commit() app.editor.putBoolean(Key.isProxyApps, checked).apply
finish() finish()
}) })
val bypassSwitch = findViewById(R.id.bypassSwitch).asInstanceOf[Switch] val bypassSwitch = findViewById(R.id.bypassSwitch).asInstanceOf[Switch]
bypassSwitch.setOnCheckedChangeListener((_, checked) => bypassSwitch.setOnCheckedChangeListener((_, checked) =>
ShadowsocksApplication.settings.edit().putBoolean(Key.isBypassApps, checked).apply()) app.editor.putBoolean(Key.isBypassApps, checked).apply())
bypassSwitch.setChecked(ShadowsocksApplication.settings.getBoolean(Key.isBypassApps, false)) bypassSwitch.setChecked(app.settings.getBoolean(Key.isBypassApps, false))
loadingView = findViewById(R.id.loading) loadingView = findViewById(R.id.loading)
appListView = findViewById(R.id.applistview).asInstanceOf[RecyclerView] appListView = findViewById(R.id.applistview).asInstanceOf[RecyclerView]
......
...@@ -49,6 +49,7 @@ import android.util.Log ...@@ -49,6 +49,7 @@ import android.util.Log
import android.widget.Toast import android.widget.Toast
import com.github.shadowsocks.aidl.{Config, IShadowsocksService, IShadowsocksServiceCallback} import com.github.shadowsocks.aidl.{Config, IShadowsocksService, IShadowsocksServiceCallback}
import com.github.shadowsocks.utils._ import com.github.shadowsocks.utils._
import com.github.shadowsocks.ShadowsocksApplication.app
trait BaseService extends Service { trait BaseService extends Service {
...@@ -162,11 +163,11 @@ trait BaseService extends Service { ...@@ -162,11 +163,11 @@ trait BaseService extends Service {
def updateTrafficTotal(tx: Long, rx: Long) { def updateTrafficTotal(tx: Long, rx: Long) {
val config = this.config // avoid race conditions without locking val config = this.config // avoid race conditions without locking
if (config != null) { if (config != null) {
ShadowsocksApplication.profileManager.getProfile(config.profileId) match { app.profileManager.getProfile(config.profileId) match {
case Some(profile) => case Some(profile) =>
profile.tx += tx profile.tx += tx
profile.rx += rx profile.rx += rx
ShadowsocksApplication.profileManager.updateProfile(profile) app.profileManager.updateProfile(profile)
case None => // Ignore case None => // Ignore
} }
} }
......
...@@ -22,6 +22,7 @@ import com.github.shadowsocks.aidl.IShadowsocksServiceCallback ...@@ -22,6 +22,7 @@ import com.github.shadowsocks.aidl.IShadowsocksServiceCallback
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.utils.{Key, Parser, TrafficMonitor, Utils} import com.github.shadowsocks.utils.{Key, Parser, TrafficMonitor, Utils}
import com.github.shadowsocks.widget.UndoSnackbarManager import com.github.shadowsocks.widget.UndoSnackbarManager
import com.github.shadowsocks.ShadowsocksApplication.app
import com.google.zxing.integration.android.IntentIntegrator import com.google.zxing.integration.android.IntentIntegrator
import net.glxn.qrgen.android.QRCode import net.glxn.qrgen.android.QRCode
...@@ -96,7 +97,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -96,7 +97,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
def bind(item: Profile) { def bind(item: Profile) {
this.item = item this.item = item
updateText() updateText()
if (item.id == ShadowsocksApplication.profileId) { if (item.id == app.profileId) {
text.setChecked(true) text.setChecked(true)
selectedItem = this selectedItem = this
} else { } else {
...@@ -106,7 +107,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -106,7 +107,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
} }
def onClick(v: View) { def onClick(v: View) {
ShadowsocksApplication.switchProfile(item.id) app.switchProfile(item.id)
finish finish
} }
...@@ -122,7 +123,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -122,7 +123,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] { private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] {
var profiles = new ArrayBuffer[Profile] var profiles = new ArrayBuffer[Profile]
profiles ++= ShadowsocksApplication.profileManager.getAllProfiles.getOrElse(List.empty[Profile]) profiles ++= app.profileManager.getAllProfiles.getOrElse(List.empty[Profile])
def getItemCount = profiles.length def getItemCount = profiles.length
...@@ -149,11 +150,11 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -149,11 +150,11 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
next.userOrder = previousOrder next.userOrder = previousOrder
previousOrder = order previousOrder = order
profiles(i) = next profiles(i) = next
ShadowsocksApplication.profileManager.updateProfile(next) app.profileManager.updateProfile(next)
} }
first.userOrder = previousOrder first.userOrder = previousOrder
profiles(to) = first profiles(to) = first
ShadowsocksApplication.profileManager.updateProfile(first) app.profileManager.updateProfile(first)
notifyItemMoved(from, to) notifyItemMoved(from, to)
} }
...@@ -166,8 +167,8 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -166,8 +167,8 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
notifyItemInserted(index) notifyItemInserted(index)
} }
def commit(actions: Iterator[(Int, Profile)]) = for ((index, item) <- actions) { def commit(actions: Iterator[(Int, Profile)]) = for ((index, item) <- actions) {
ShadowsocksApplication.profileManager.delProfile(item.id) app.profileManager.delProfile(item.id)
if (item.id == ShadowsocksApplication.profileId) ShadowsocksApplication.profileId(-1) if (item.id == app.profileId) app.profileId(-1)
} }
} }
...@@ -203,14 +204,14 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -203,14 +204,14 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
initFab() initFab()
ShadowsocksApplication.profileManager.setProfileAddedListener(profilesAdapter.add) app.profileManager.setProfileAddedListener(profilesAdapter.add)
val profilesList = findViewById(R.id.profilesList).asInstanceOf[RecyclerView] val profilesList = findViewById(R.id.profilesList).asInstanceOf[RecyclerView]
val layoutManager = new LinearLayoutManager(this) val layoutManager = new LinearLayoutManager(this)
profilesList.setLayoutManager(layoutManager) profilesList.setLayoutManager(layoutManager)
profilesList.setItemAnimator(new DefaultItemAnimator) profilesList.setItemAnimator(new DefaultItemAnimator)
profilesList.setAdapter(profilesAdapter) profilesList.setAdapter(profilesAdapter)
layoutManager.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst { layoutManager.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst {
case (profile, i) if profile.id == ShadowsocksApplication.profileId => i case (profile, i) if profile.id == app.profileId => i
}.getOrElse(-1)) }.getOrElse(-1))
undoManager = new UndoSnackbarManager[Profile](profilesList, profilesAdapter.undo, profilesAdapter.commit) undoManager = new UndoSnackbarManager[Profile](profilesList, profilesAdapter.undo, profilesAdapter.commit)
new ItemTouchHelper(new SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, new ItemTouchHelper(new SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
...@@ -232,8 +233,8 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -232,8 +233,8 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
if (selectedItem != null) selectedItem.updateText(txTotal, rxTotal) if (selectedItem != null) selectedItem.updateText(txTotal, rxTotal)
}) })
if (ShadowsocksApplication.settings.getBoolean(Key.profileTip, true)) { if (app.settings.getBoolean(Key.profileTip, true)) {
ShadowsocksApplication.settings.edit.putBoolean(Key.profileTip, false).commit app.editor.putBoolean(Key.profileTip, false).commit
new AlertDialog.Builder(this).setTitle(R.string.profile_manager_dialog) new AlertDialog.Builder(this).setTitle(R.string.profile_manager_dialog)
.setMessage(R.string.profile_manager_dialog_content).setPositiveButton(R.string.gotcha, null).create.show .setMessage(R.string.profile_manager_dialog_content).setPositiveButton(R.string.gotcha, null).create.show
} }
...@@ -270,8 +271,8 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -270,8 +271,8 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
v.getId match { v.getId match {
case R.id.fab_manual_add => case R.id.fab_manual_add =>
menu.toggle(true) menu.toggle(true)
ShadowsocksApplication.profileManager.reload(-1) app.profileManager.reload(-1)
ShadowsocksApplication.switchProfile(ShadowsocksApplication.profileManager.save.id) app.switchProfile(app.profileManager.save.id)
finish finish
case R.id.fab_qrcode_add => case R.id.fab_qrcode_add =>
menu.toggle(false) menu.toggle(false)
...@@ -301,7 +302,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -301,7 +302,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
if (clipboard.hasPrimaryClip) { if (clipboard.hasPrimaryClip) {
val profiles = Parser.findAll(clipboard.getPrimaryClip.getItemAt(0).getText) val profiles = Parser.findAll(clipboard.getPrimaryClip.getItemAt(0).getText)
if (profiles.nonEmpty) { if (profiles.nonEmpty) {
profiles.foreach(ShadowsocksApplication.profileManager.createProfile) profiles.foreach(app.profileManager.createProfile)
Toast.makeText(this, R.string.action_import_msg, Toast.LENGTH_SHORT).show Toast.makeText(this, R.string.action_import_msg, Toast.LENGTH_SHORT).show
return return
} }
...@@ -346,7 +347,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -346,7 +347,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
val dialog = new AlertDialog.Builder(this) val dialog = new AlertDialog.Builder(this)
.setTitle(R.string.add_profile_dialog) .setTitle(R.string.add_profile_dialog)
.setPositiveButton(android.R.string.yes, ((_, _) => .setPositiveButton(android.R.string.yes, ((_, _) =>
profiles.foreach(ShadowsocksApplication.profileManager.createProfile)): DialogInterface.OnClickListener) profiles.foreach(app.profileManager.createProfile)): DialogInterface.OnClickListener)
.setNegativeButton(android.R.string.no, ((_, _) => finish()): DialogInterface.OnClickListener) .setNegativeButton(android.R.string.no, ((_, _) => finish()): DialogInterface.OnClickListener)
.setMessage(profiles.mkString("\n")) .setMessage(profiles.mkString("\n"))
.create() .create()
...@@ -365,7 +366,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -365,7 +366,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
override def onDestroy { override def onDestroy {
detachService() detachService()
undoManager.flush undoManager.flush
ShadowsocksApplication.profileManager.setProfileAddedListener(null) app.profileManager.setProfileAddedListener(null)
super.onDestroy super.onDestroy
} }
...@@ -374,7 +375,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -374,7 +375,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
if (scanResult != null) { if (scanResult != null) {
val contents = scanResult.getContents val contents = scanResult.getContents
if (!TextUtils.isEmpty(contents)) if (!TextUtils.isEmpty(contents))
Parser.findAll(contents).foreach(ShadowsocksApplication.profileManager.createProfile) Parser.findAll(contents).foreach(app.profileManager.createProfile)
} }
} }
...@@ -387,7 +388,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic ...@@ -387,7 +388,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
def onMenuItemClick(item: MenuItem): Boolean = item.getItemId match { def onMenuItemClick(item: MenuItem): Boolean = item.getItemId match {
case R.id.action_export => case R.id.action_export =>
ShadowsocksApplication.profileManager.getAllProfiles match { app.profileManager.getAllProfiles match {
case Some(profiles) => case Some(profiles) =>
clipboard.setPrimaryClip(ClipData.newPlainText(null, profiles.mkString("\n"))) clipboard.setPrimaryClip(ClipData.newPlainText(null, profiles.mkString("\n")))
Toast.makeText(this, R.string.action_export_msg, Toast.LENGTH_SHORT).show Toast.makeText(this, R.string.action_export_msg, Toast.LENGTH_SHORT).show
......
...@@ -4,6 +4,7 @@ import android.content.{ComponentName, Context, Intent, ServiceConnection} ...@@ -4,6 +4,7 @@ import android.content.{ComponentName, Context, Intent, ServiceConnection}
import android.os.{RemoteException, IBinder} import android.os.{RemoteException, IBinder}
import com.github.shadowsocks.aidl.{IShadowsocksServiceCallback, IShadowsocksService} import com.github.shadowsocks.aidl.{IShadowsocksServiceCallback, IShadowsocksService}
import com.github.shadowsocks.utils.Action import com.github.shadowsocks.utils.Action
import com.github.shadowsocks.ShadowsocksApplication.app
/** /**
* @author Mygod * @author Mygod
...@@ -51,7 +52,7 @@ trait ServiceBoundContext extends Context { ...@@ -51,7 +52,7 @@ trait ServiceBoundContext extends Context {
this.callback = callback this.callback = callback
if (bgService == null) { if (bgService == null) {
val s = val s =
if (ShadowsocksApplication.isVpnEnabled) classOf[ShadowsocksVpnService] else classOf[ShadowsocksNatService] if (app.isVpnEnabled) classOf[ShadowsocksVpnService] else classOf[ShadowsocksNatService]
val intent = new Intent(this, s) val intent = new Intent(this, s)
intent.setAction(Action.SERVICE) intent.setAction(Action.SERVICE)
......
...@@ -63,6 +63,7 @@ import com.github.shadowsocks.aidl.IShadowsocksServiceCallback ...@@ -63,6 +63,7 @@ import com.github.shadowsocks.aidl.IShadowsocksServiceCallback
import com.github.shadowsocks.database._ import com.github.shadowsocks.database._
import com.github.shadowsocks.utils.CloseUtils._ import com.github.shadowsocks.utils.CloseUtils._
import com.github.shadowsocks.utils._ import com.github.shadowsocks.utils._
import com.github.shadowsocks.ShadowsocksApplication.app
import com.google.android.gms.ads.{AdRequest, AdSize, AdView} import com.google.android.gms.ads.{AdRequest, AdSize, AdView}
import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer
...@@ -129,7 +130,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -129,7 +130,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
changeSwitch(checked = true) changeSwitch(checked = true)
preferences.setEnabled(false) preferences.setEnabled(false)
stat.setVisibility(View.VISIBLE) stat.setVisibility(View.VISIBLE)
if (ShadowsocksApplication.isVpnEnabled) { if (app.isVpnEnabled) {
connectionTestText.setVisibility(View.VISIBLE) connectionTestText.setVisibility(View.VISIBLE)
connectionTestText.setText(getString(R.string.connection_test_pending)) connectionTestText.setText(getString(R.string.connection_test_pending))
} else connectionTestText.setVisibility(View.GONE) } else connectionTestText.setVisibility(View.GONE)
...@@ -177,31 +178,31 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -177,31 +178,31 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
// Update the UI // Update the UI
if (fab != null) fab.setEnabled(true) if (fab != null) fab.setEnabled(true)
updateState() updateState()
if (Build.VERSION.SDK_INT >= 21 && !ShadowsocksApplication.isVpnEnabled) { if (Build.VERSION.SDK_INT >= 21 && !app.isVpnEnabled) {
val snackbar = Snackbar.make(findViewById(android.R.id.content), R.string.nat_deprecated, Snackbar.LENGTH_LONG) val snackbar = Snackbar.make(findViewById(android.R.id.content), R.string.nat_deprecated, Snackbar.LENGTH_LONG)
snackbar.setAction(R.string.switch_to_vpn, (_ => preferences.natSwitch.setChecked(false)): View.OnClickListener) snackbar.setAction(R.string.switch_to_vpn, (_ => preferences.natSwitch.setChecked(false)): View.OnClickListener)
snackbar.show snackbar.show
} }
if (!ShadowsocksApplication.settings.getBoolean(ShadowsocksApplication.getVersionName, false)) { if (!app.settings.getBoolean(app.getVersionName, false)) {
ShadowsocksApplication.settings.edit.putBoolean(ShadowsocksApplication.getVersionName, true).apply() app.editor.putBoolean(app.getVersionName, true).apply()
try { try {
// Workaround that convert port(String) to port(Int) // Workaround that convert port(String) to port(Int)
val oldLocalPort = ShadowsocksApplication.settings.getString(Key.localPort, "") val oldLocalPort = app.settings.getString(Key.localPort, "")
val oldRemotePort = ShadowsocksApplication.settings.getString(Key.remotePort, "") val oldRemotePort = app.settings.getString(Key.remotePort, "")
if (oldLocalPort != "") { if (oldLocalPort != "") {
ShadowsocksApplication.settings.edit.putInt(Key.localPort, oldLocalPort.toInt).apply() app.editor.putInt(Key.localPort, oldLocalPort.toInt).apply()
} }
if (oldRemotePort != "") { if (oldRemotePort != "") {
ShadowsocksApplication.settings.edit.putInt(Key.remotePort, oldRemotePort.toInt).apply() app.editor.putInt(Key.remotePort, oldRemotePort.toInt).apply()
} }
} catch { } catch {
case ex: Exception => // Ignore case ex: Exception => // Ignore
} }
val oldProxiedApps = ShadowsocksApplication.settings.getString(Key.proxied, "") val oldProxiedApps = app.settings.getString(Key.proxied, "")
if (oldProxiedApps.contains('|')) ShadowsocksApplication.settings.edit if (oldProxiedApps.contains('|'))
.putString(Key.proxied, DBHelper.updateProxiedApps(this, oldProxiedApps)).apply() app.editor.putString(Key.proxied, DBHelper.updateProxiedApps(this, oldProxiedApps)).apply()
recovery() recovery()
...@@ -300,7 +301,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -300,7 +301,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
cmd.append("chmod 666 %s/%s-vpn.pid".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task)) cmd.append("chmod 666 %s/%s-vpn.pid".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task))
} }
if (ShadowsocksApplication.isVpnEnabled) { if (app.isVpnEnabled) {
Console.runCommand(cmd.toArray) Console.runCommand(cmd.toArray)
} else { } else {
Console.runRootCommand(cmd.toArray) Console.runRootCommand(cmd.toArray)
...@@ -324,7 +325,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -324,7 +325,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
cmd.append("rm -f %s/%s-vpn.pid".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task)) cmd.append("rm -f %s/%s-vpn.pid".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task))
cmd.append("rm -f %s/%s-vpn.conf".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task)) cmd.append("rm -f %s/%s-vpn.conf".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task))
} }
if (ShadowsocksApplication.isVpnEnabled) Console.runCommand(cmd.toArray) else { if (app.isVpnEnabled) Console.runCommand(cmd.toArray) else {
Console.runRootCommand(cmd.toArray) Console.runRootCommand(cmd.toArray)
Console.runRootCommand(Utils.iptables + " -t nat -F OUTPUT") Console.runRootCommand(Utils.iptables + " -t nat -F OUTPUT")
} }
...@@ -337,7 +338,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -337,7 +338,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
def prepareStartService() { def prepareStartService() {
Utils.ThrowableFuture { Utils.ThrowableFuture {
if (ShadowsocksApplication.isVpnEnabled) { if (app.isVpnEnabled) {
val intent = VpnService.prepare(this) val intent = VpnService.prepare(this)
if (intent != null) { if (intent != null) {
startActivityForResult(intent, REQUEST_CONNECT) startActivityForResult(intent, REQUEST_CONNECT)
...@@ -406,7 +407,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -406,7 +407,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
result = getString(R.string.connection_test_error, e.getMessage) result = getString(R.string.connection_test_error, e.getMessage)
} }
synchronized(if (testCount == id) { synchronized(if (testCount == id) {
if (ShadowsocksApplication.isVpnEnabled) handler.post(() => { if (app.isVpnEnabled) handler.post(() => {
if (success) connectionTestText.setText(result) else { if (success) connectionTestText.setText(result) else {
connectionTestText.setText(R.string.connection_test_fail) connectionTestText.setText(R.string.connection_test_fail)
Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show
...@@ -435,7 +436,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -435,7 +436,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
protected override def onPause() { protected override def onPause() {
super.onPause() super.onPause()
ShadowsocksApplication.profileManager.save app.profileManager.save
} }
private def hideCircle() { private def hideCircle() {
...@@ -463,7 +464,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -463,7 +464,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
preferences.setEnabled(false) preferences.setEnabled(false)
fabProgressCircle.postDelayed(hideCircle, 100) fabProgressCircle.postDelayed(hideCircle, 100)
stat.setVisibility(View.VISIBLE) stat.setVisibility(View.VISIBLE)
if (ShadowsocksApplication.isVpnEnabled) { if (app.isVpnEnabled) {
connectionTestText.setVisibility(View.VISIBLE) connectionTestText.setVisibility(View.VISIBLE)
connectionTestText.setText(getString(R.string.connection_test_pending)) connectionTestText.setText(getString(R.string.connection_test_pending))
} else connectionTestText.setVisibility(View.GONE) } else connectionTestText.setVisibility(View.GONE)
...@@ -488,13 +489,13 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -488,13 +489,13 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
private def updateCurrentProfile() { private def updateCurrentProfile() {
// Check if current profile changed // Check if current profile changed
if (ShadowsocksApplication.profileId != currentProfile.id) { if (app.profileId != currentProfile.id) {
currentProfile = ShadowsocksApplication.currentProfile match { currentProfile = app.currentProfile match {
case Some(profile) => profile // updated case Some(profile) => profile // updated
case None => // removed case None => // removed
ShadowsocksApplication.profileManager.getFirstProfile match { app.profileManager.getFirstProfile match {
case Some(first) => ShadowsocksApplication.switchProfile(first.id) case Some(first) => app.switchProfile(first.id)
case None => ShadowsocksApplication.profileManager.createDefault() case None => app.profileManager.createDefault()
} }
} }
...@@ -590,7 +591,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -590,7 +591,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
} }
def checkText(key: String): Boolean = { def checkText(key: String): Boolean = {
val text = ShadowsocksApplication.settings.getString(key, "") val text = app.settings.getString(key, "")
if (text != null && text.length > 0) return true if (text != null && text.length > 0) return true
Snackbar.make(findViewById(android.R.id.content), R.string.proxy_empty, Snackbar.LENGTH_LONG).show Snackbar.make(findViewById(android.R.id.content), R.string.proxy_empty, Snackbar.LENGTH_LONG).show
false false
...@@ -598,9 +599,9 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -598,9 +599,9 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
/** Called when connect button is clicked. */ /** Called when connect button is clicked. */
def serviceLoad() { def serviceLoad() {
bgService.use(ConfigUtils.load(ShadowsocksApplication.settings)) bgService.use(ConfigUtils.load(app.settings))
if (ShadowsocksApplication.isVpnEnabled) { if (app.isVpnEnabled) {
changeSwitch(checked = false) changeSwitch(checked = false)
} }
} }
......
...@@ -45,26 +45,32 @@ import java.util.concurrent.TimeUnit ...@@ -45,26 +45,32 @@ import java.util.concurrent.TimeUnit
import android.app.Application import android.app.Application
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.preference.PreferenceManager import android.preference.PreferenceManager
import com.j256.ormlite.logger.LocalLog
import com.github.shadowsocks.database.{DBHelper, ProfileManager} import com.github.shadowsocks.database.{DBHelper, ProfileManager}
import com.github.shadowsocks.utils.{Key, Utils} import com.github.shadowsocks.utils.{Key, Utils}
import com.google.android.gms.analytics.{GoogleAnalytics, HitBuilders} import com.google.android.gms.analytics.{GoogleAnalytics, HitBuilders}
import com.google.android.gms.common.api.ResultCallback import com.google.android.gms.common.api.ResultCallback
import com.google.android.gms.tagmanager.{ContainerHolder, TagManager} import com.google.android.gms.tagmanager.{ContainerHolder, TagManager}
import com.j256.ormlite.logger.LocalLog
object ShadowsocksApplication { object ShadowsocksApplication {
var instance: ShadowsocksApplication = _ var app: ShadowsocksApplication = _
lazy val dbHelper = new DBHelper(instance) }
class ShadowsocksApplication extends Application {
import ShadowsocksApplication._
lazy val dbHelper = new DBHelper(this)
final val SIG_FUNC = "getSignature" final val SIG_FUNC = "getSignature"
var containerHolder: ContainerHolder = _ var containerHolder: ContainerHolder = _
lazy val tracker = GoogleAnalytics.getInstance(instance).newTracker(R.xml.tracker) lazy val tracker = GoogleAnalytics.getInstance(this).newTracker(R.xml.tracker)
lazy val settings = PreferenceManager.getDefaultSharedPreferences(instance) lazy val settings = PreferenceManager.getDefaultSharedPreferences(this)
lazy val editor = settings.edit
lazy val profileManager = new ProfileManager(settings, dbHelper) lazy val profileManager = new ProfileManager(settings, dbHelper)
def isVpnEnabled = !settings.getBoolean(Key.isNAT, false) def isVpnEnabled = !settings.getBoolean(Key.isNAT, false)
def getVersionName = try { def getVersionName = try {
instance.getPackageManager.getPackageInfo(instance.getPackageName, 0).versionName getPackageManager.getPackageInfo(getPackageName, 0).versionName
} catch { } catch {
case _: PackageManager.NameNotFoundException => "Package name not found" case _: PackageManager.NameNotFoundException => "Package name not found"
case _: Throwable => null case _: Throwable => null
...@@ -84,14 +90,10 @@ object ShadowsocksApplication { ...@@ -84,14 +90,10 @@ object ShadowsocksApplication {
profileId(id) profileId(id)
profileManager.load(id) profileManager.load(id)
} }
}
class ShadowsocksApplication extends Application {
import ShadowsocksApplication._
override def onCreate() { override def onCreate() {
java.lang.System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, "ERROR"); java.lang.System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, "ERROR")
ShadowsocksApplication.instance = this app = this
val tm = TagManager.getInstance(this) val tm = TagManager.getInstance(this)
val pending = tm.loadContainerPreferNonDefault("GTM-NT8WS8", R.raw.gtm_default_container) val pending = tm.loadContainerPreferNonDefault("GTM-NT8WS8", R.raw.gtm_default_container)
val callback = new ResultCallback[ContainerHolder] { val callback = new ResultCallback[ContainerHolder] {
......
...@@ -50,6 +50,7 @@ import android.os._ ...@@ -50,6 +50,7 @@ import android.os._
import android.util.{Log, SparseArray} import android.util.{Log, SparseArray}
import com.github.shadowsocks.aidl.Config import com.github.shadowsocks.aidl.Config
import com.github.shadowsocks.utils._ import com.github.shadowsocks.utils._
import com.github.shadowsocks.ShadowsocksApplication.app
import scala.collection.JavaConversions._ import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer
...@@ -355,13 +356,13 @@ class ShadowsocksNatService extends BaseService { ...@@ -355,13 +356,13 @@ class ShadowsocksNatService extends BaseService {
} }
super.startRunner(config) super.startRunner(config)
ShadowsocksApplication.track(TAG, "start") app.track(TAG, "start")
changeState(State.CONNECTING) changeState(State.CONNECTING)
Utils.ThrowableFuture { Utils.ThrowableFuture {
if (config.proxy == "198.199.101.152") { if (config.proxy == "198.199.101.152") {
val holder = ShadowsocksApplication.containerHolder val holder = app.containerHolder
try { try {
this.config = ConfigUtils.getPublicConfig(getBaseContext, holder.getContainer, config) this.config = ConfigUtils.getPublicConfig(getBaseContext, holder.getContainer, config)
} catch { } catch {
...@@ -412,7 +413,7 @@ class ShadowsocksNatService extends BaseService { ...@@ -412,7 +413,7 @@ class ShadowsocksNatService extends BaseService {
if (notification != null) notification.destroy() if (notification != null) notification.destroy()
ShadowsocksApplication.track(TAG, "stop") app.track(TAG, "stop")
// reset NAT // reset NAT
killProcesses() killProcesses()
......
...@@ -10,6 +10,7 @@ import android.support.v4.app.NotificationCompat.BigTextStyle ...@@ -10,6 +10,7 @@ import android.support.v4.app.NotificationCompat.BigTextStyle
import android.support.v4.content.ContextCompat import android.support.v4.content.ContextCompat
import com.github.shadowsocks.aidl.IShadowsocksServiceCallback.Stub import com.github.shadowsocks.aidl.IShadowsocksServiceCallback.Stub
import com.github.shadowsocks.utils.{TrafficMonitor, Action, State, Utils} import com.github.shadowsocks.utils.{TrafficMonitor, Action, State, Utils}
import com.github.shadowsocks.ShadowsocksApplication.app
/** /**
* @author Mygod * @author Mygod
...@@ -41,7 +42,7 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str ...@@ -41,7 +42,7 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str
.setSmallIcon(R.drawable.ic_stat_shadowsocks) .setSmallIcon(R.drawable.ic_stat_shadowsocks)
.addAction(R.drawable.ic_navigation_close, service.getString(R.string.stop), .addAction(R.drawable.ic_navigation_close, service.getString(R.string.stop),
PendingIntent.getBroadcast(service, 0, new Intent(Action.CLOSE), 0)) PendingIntent.getBroadcast(service, 0, new Intent(Action.CLOSE), 0))
ShadowsocksApplication.profileManager.getAllProfiles match { app.profileManager.getAllProfiles match {
case Some(profiles) => if (profiles.length > 1) case Some(profiles) => if (profiles.length > 1)
builder.addAction(R.drawable.ic_action_settings, service.getString(R.string.quick_switch), builder.addAction(R.drawable.ic_action_settings, service.getString(R.string.quick_switch),
PendingIntent.getActivity(service, 0, new Intent(Action.QUICK_SWITCH), 0)) PendingIntent.getActivity(service, 0, new Intent(Action.QUICK_SWITCH), 0))
......
...@@ -8,6 +8,7 @@ import android.view.{LayoutInflater, View, ViewGroup} ...@@ -8,6 +8,7 @@ import android.view.{LayoutInflater, View, ViewGroup}
import android.widget.CheckedTextView import android.widget.CheckedTextView
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.utils.Utils import com.github.shadowsocks.utils.Utils
import com.github.shadowsocks.ShadowsocksApplication.app
/** /**
* Created by Lucas on 3/10/16. * Created by Lucas on 3/10/16.
...@@ -27,18 +28,18 @@ class ShadowsocksQuickSwitchActivity extends AppCompatActivity { ...@@ -27,18 +28,18 @@ class ShadowsocksQuickSwitchActivity extends AppCompatActivity {
def bind(item: Profile) { def bind(item: Profile) {
this.item = item this.item = item
text.setText(item.name) text.setText(item.name)
text.setChecked(item.id == ShadowsocksApplication.profileId) text.setChecked(item.id == app.profileId)
} }
def onClick(v: View) { def onClick(v: View) {
ShadowsocksApplication.switchProfile(item.id) app.switchProfile(item.id)
Utils.startSsService(ShadowsocksQuickSwitchActivity.this) Utils.startSsService(ShadowsocksQuickSwitchActivity.this)
finish finish
} }
} }
private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] { private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] {
val profiles = ShadowsocksApplication.profileManager.getAllProfiles.getOrElse(List.empty[Profile]) val profiles = app.profileManager.getAllProfiles.getOrElse(List.empty[Profile])
def getItemCount = profiles.length def getItemCount = profiles.length
...@@ -66,8 +67,8 @@ class ShadowsocksQuickSwitchActivity extends AppCompatActivity { ...@@ -66,8 +67,8 @@ class ShadowsocksQuickSwitchActivity extends AppCompatActivity {
profilesList.setLayoutManager(lm) profilesList.setLayoutManager(lm)
profilesList.setItemAnimator(new DefaultItemAnimator) profilesList.setItemAnimator(new DefaultItemAnimator)
profilesList.setAdapter(profilesAdapter) profilesList.setAdapter(profilesAdapter)
if (ShadowsocksApplication.profileId >= 0) lm.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst { if (app.profileId >= 0) lm.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst {
case (profile, i) if profile.id == ShadowsocksApplication.profileId => i + 1 case (profile, i) if profile.id == app.profileId => i + 1
}.getOrElse(0)) }.getOrElse(0))
} }
} }
...@@ -45,6 +45,7 @@ import android.net.VpnService ...@@ -45,6 +45,7 @@ import android.net.VpnService
import android.os.{Bundle, Handler} import android.os.{Bundle, Handler}
import android.util.Log import android.util.Log
import com.github.shadowsocks.utils.ConfigUtils import com.github.shadowsocks.utils.ConfigUtils
import com.github.shadowsocks.ShadowsocksApplication.app
object ShadowsocksRunnerActivity { object ShadowsocksRunnerActivity {
private final val TAG = "ShadowsocksRunnerActivity" private final val TAG = "ShadowsocksRunnerActivity"
...@@ -64,7 +65,7 @@ class ShadowsocksRunnerActivity extends Activity with ServiceBoundContext { ...@@ -64,7 +65,7 @@ class ShadowsocksRunnerActivity extends Activity with ServiceBoundContext {
} }
def startBackgroundService() { def startBackgroundService() {
if (ShadowsocksApplication.isVpnEnabled) { if (app.isVpnEnabled) {
val intent = VpnService.prepare(ShadowsocksRunnerActivity.this) val intent = VpnService.prepare(ShadowsocksRunnerActivity.this)
if (intent != null) { if (intent != null) {
startActivityForResult(intent, REQUEST_CONNECT) startActivityForResult(intent, REQUEST_CONNECT)
...@@ -72,7 +73,7 @@ class ShadowsocksRunnerActivity extends Activity with ServiceBoundContext { ...@@ -72,7 +73,7 @@ class ShadowsocksRunnerActivity extends Activity with ServiceBoundContext {
onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null) onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null)
} }
} else { } else {
bgService.use(ConfigUtils.load(ShadowsocksApplication.settings)) bgService.use(ConfigUtils.load(app.settings))
finish() finish()
} }
} }
...@@ -108,7 +109,7 @@ class ShadowsocksRunnerActivity extends Activity with ServiceBoundContext { ...@@ -108,7 +109,7 @@ class ShadowsocksRunnerActivity extends Activity with ServiceBoundContext {
resultCode match { resultCode match {
case Activity.RESULT_OK => case Activity.RESULT_OK =>
if (bgService != null) { if (bgService != null) {
bgService.use(ConfigUtils.load(ShadowsocksApplication.settings)) bgService.use(ConfigUtils.load(app.settings))
} }
case _ => case _ =>
Log.e(TAG, "Failed to start VpnService") Log.e(TAG, "Failed to start VpnService")
......
...@@ -44,6 +44,7 @@ import android.content.Intent ...@@ -44,6 +44,7 @@ import android.content.Intent
import android.net.VpnService import android.net.VpnService
import android.os.{IBinder, Handler} import android.os.{IBinder, Handler}
import com.github.shadowsocks.utils.ConfigUtils import com.github.shadowsocks.utils.ConfigUtils
import com.github.shadowsocks.ShadowsocksApplication.app
class ShadowsocksRunnerService extends Service with ServiceBoundContext { class ShadowsocksRunnerService extends Service with ServiceBoundContext {
val handler = new Handler() val handler = new Handler()
...@@ -57,15 +58,15 @@ class ShadowsocksRunnerService extends Service with ServiceBoundContext { ...@@ -57,15 +58,15 @@ class ShadowsocksRunnerService extends Service with ServiceBoundContext {
} }
def startBackgroundService() { def startBackgroundService() {
if (ShadowsocksApplication.isVpnEnabled) { if (app.isVpnEnabled) {
val intent = VpnService.prepare(ShadowsocksRunnerService.this) val intent = VpnService.prepare(ShadowsocksRunnerService.this)
if (intent == null) { if (intent == null) {
if (bgService != null) { if (bgService != null) {
bgService.use(ConfigUtils.load(ShadowsocksApplication.settings)) bgService.use(ConfigUtils.load(app.settings))
} }
} }
} else { } else {
bgService.use(ConfigUtils.load(ShadowsocksApplication.settings)) bgService.use(ConfigUtils.load(app.settings))
} }
stopSelf() stopSelf()
} }
......
...@@ -12,6 +12,7 @@ import android.webkit.{WebView, WebViewClient} ...@@ -12,6 +12,7 @@ import android.webkit.{WebView, WebViewClient}
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.preferences._ import com.github.shadowsocks.preferences._
import com.github.shadowsocks.utils.{Key, Utils} import com.github.shadowsocks.utils.{Key, Utils}
import com.github.shadowsocks.ShadowsocksApplication.app
object ShadowsocksSettings { object ShadowsocksSettings {
// Constants // Constants
...@@ -92,7 +93,7 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan ...@@ -92,7 +93,7 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
switch.setChecked(BootReceiver.getEnabled(activity)) switch.setChecked(BootReceiver.getEnabled(activity))
findPreference("recovery").setOnPreferenceClickListener((preference: Preference) => { findPreference("recovery").setOnPreferenceClickListener((preference: Preference) => {
ShadowsocksApplication.track(TAG, "reset") app.track(TAG, "reset")
activity.recovery() activity.recovery()
true true
}) })
...@@ -100,13 +101,13 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan ...@@ -100,13 +101,13 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
val flush = findPreference("flush_dnscache") val flush = findPreference("flush_dnscache")
if (Build.VERSION.SDK_INT < 17) flush.setSummary(R.string.flush_dnscache_summary) if (Build.VERSION.SDK_INT < 17) flush.setSummary(R.string.flush_dnscache_summary)
flush.setOnPreferenceClickListener(_ => { flush.setOnPreferenceClickListener(_ => {
ShadowsocksApplication.track(TAG, "flush_dnscache") app.track(TAG, "flush_dnscache")
activity.flushDnsCache() activity.flushDnsCache()
true true
}) })
findPreference("about").setOnPreferenceClickListener((preference: Preference) => { findPreference("about").setOnPreferenceClickListener((preference: Preference) => {
ShadowsocksApplication.track(TAG, "about") app.track(TAG, "about")
val web = new WebView(activity) val web = new WebView(activity)
web.loadUrl("file:///android_asset/pages/about.html") web.loadUrl("file:///android_asset/pages/about.html")
web.setWebViewClient(new WebViewClient() { web.setWebViewClient(new WebViewClient() {
...@@ -121,7 +122,7 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan ...@@ -121,7 +122,7 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
}) })
new AlertDialog.Builder(activity) new AlertDialog.Builder(activity)
.setTitle(getString(R.string.about_title).formatLocal(Locale.ENGLISH, ShadowsocksApplication.getVersionName)) .setTitle(getString(R.string.about_title).formatLocal(Locale.ENGLISH, app.getVersionName))
.setNegativeButton(getString(android.R.string.ok), null) .setNegativeButton(getString(android.R.string.ok), null)
.setView(web) .setView(web)
.create() .create()
...@@ -132,12 +133,12 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan ...@@ -132,12 +133,12 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
override def onResume { override def onResume {
super.onResume() super.onResume()
isProxyApps.setChecked(ShadowsocksApplication.settings.getBoolean(Key.isProxyApps, false)) // update isProxyApps.setChecked(app.settings.getBoolean(Key.isProxyApps, false)) // update
} }
override def onDestroy { override def onDestroy {
super.onDestroy() super.onDestroy()
ShadowsocksApplication.settings.unregisterOnSharedPreferenceChangeListener(this) app.settings.unregisterOnSharedPreferenceChangeListener(this)
} }
def onSharedPreferenceChanged(pref: SharedPreferences, key: String) = key match { def onSharedPreferenceChanged(pref: SharedPreferences, key: String) = key match {
...@@ -155,7 +156,7 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan ...@@ -155,7 +156,7 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
for (name <- Key.isNAT #:: PROXY_PREFS.toStream #::: FEATURE_PREFS.toStream) { for (name <- Key.isNAT #:: PROXY_PREFS.toStream #::: FEATURE_PREFS.toStream) {
val pref = findPreference(name) val pref = findPreference(name)
if (pref != null) pref.setEnabled(enabled && if (pref != null) pref.setEnabled(enabled &&
(name != Key.isProxyApps || Utils.isLollipopOrAbove || !ShadowsocksApplication.isVpnEnabled)) (name != Key.isProxyApps || Utils.isLollipopOrAbove || !app.isVpnEnabled))
} }
} }
......
...@@ -50,6 +50,7 @@ import android.os._ ...@@ -50,6 +50,7 @@ import android.os._
import android.util.Log import android.util.Log
import com.github.shadowsocks.aidl.Config import com.github.shadowsocks.aidl.Config
import com.github.shadowsocks.utils._ import com.github.shadowsocks.utils._
import com.github.shadowsocks.ShadowsocksApplication.app
import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer
...@@ -98,7 +99,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -98,7 +99,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
// channge the state // channge the state
changeState(State.STOPPING) changeState(State.STOPPING)
ShadowsocksApplication.track(TAG, "stop") app.track(TAG, "stop")
// reset VPN // reset VPN
killProcesses() killProcesses()
...@@ -146,13 +147,13 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -146,13 +147,13 @@ class ShadowsocksVpnService extends VpnService with BaseService {
return return
} }
ShadowsocksApplication.track(TAG, "start") app.track(TAG, "start")
changeState(State.CONNECTING) changeState(State.CONNECTING)
Utils.ThrowableFuture { Utils.ThrowableFuture {
if (config.proxy == "198.199.101.152") { if (config.proxy == "198.199.101.152") {
val holder = ShadowsocksApplication.containerHolder val holder = app.containerHolder
try { try {
this.config = ConfigUtils.getPublicConfig(getBaseContext, holder.getContainer, config) this.config = ConfigUtils.getPublicConfig(getBaseContext, holder.getContainer, config)
} catch { } catch {
......
...@@ -47,6 +47,7 @@ import android.view.{LayoutInflater, View, ViewGroup} ...@@ -47,6 +47,7 @@ import android.view.{LayoutInflater, View, ViewGroup}
import android.widget.{CheckedTextView, Switch} import android.widget.{CheckedTextView, Switch}
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.utils.TaskerSettings import com.github.shadowsocks.utils.TaskerSettings
import com.github.shadowsocks.ShadowsocksApplication.app
/** /**
* @author CzBiX * @author CzBiX
...@@ -82,7 +83,7 @@ class TaskerActivity extends AppCompatActivity { ...@@ -82,7 +83,7 @@ class TaskerActivity extends AppCompatActivity {
} }
private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] { private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] {
val profiles = ShadowsocksApplication.profileManager.getAllProfiles.getOrElse(List.empty[Profile]) val profiles = app.profileManager.getAllProfiles.getOrElse(List.empty[Profile])
def getItemCount = 1 + profiles.length def getItemCount = 1 + profiles.length
def onBindViewHolder(vh: ProfileViewHolder, i: Int) = i match { def onBindViewHolder(vh: ProfileViewHolder, i: Int) = i match {
case 0 => vh.bindDefault case 0 => vh.bindDefault
......
...@@ -40,6 +40,7 @@ package com.github.shadowsocks ...@@ -40,6 +40,7 @@ package com.github.shadowsocks
import android.content.{BroadcastReceiver, Context, Intent} import android.content.{BroadcastReceiver, Context, Intent}
import com.github.shadowsocks.utils.{TaskerSettings, Utils} import com.github.shadowsocks.utils.{TaskerSettings, Utils}
import com.github.shadowsocks.ShadowsocksApplication.app
/** /**
* @author CzBiX * @author CzBiX
...@@ -47,9 +48,9 @@ import com.github.shadowsocks.utils.{TaskerSettings, Utils} ...@@ -47,9 +48,9 @@ import com.github.shadowsocks.utils.{TaskerSettings, Utils}
class TaskerReceiver extends BroadcastReceiver { class TaskerReceiver extends BroadcastReceiver {
override def onReceive(context: Context, intent: Intent) { override def onReceive(context: Context, intent: Intent) {
val settings = TaskerSettings.fromIntent(intent) val settings = TaskerSettings.fromIntent(intent)
val switched = ShadowsocksApplication.profileManager.getProfile(settings.profileId) match { val switched = app.profileManager.getProfile(settings.profileId) match {
case Some(p) => case Some(p) =>
ShadowsocksApplication.switchProfile(settings.profileId) app.switchProfile(settings.profileId)
true true
case _ => false case _ => false
} }
......
...@@ -41,7 +41,7 @@ package com.github.shadowsocks.database ...@@ -41,7 +41,7 @@ package com.github.shadowsocks.database
import android.content.SharedPreferences import android.content.SharedPreferences
import android.util.Log import android.util.Log
import com.github.shadowsocks._ import com.github.shadowsocks.ShadowsocksApplication.app
import com.github.shadowsocks.utils.Key import com.github.shadowsocks.utils.Key
object ProfileManager { object ProfileManager {
...@@ -58,7 +58,7 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) { ...@@ -58,7 +58,7 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) {
try { try {
val profile = if (p == null) new Profile else p val profile = if (p == null) new Profile else p
profile.id = 0 profile.id = 0
ShadowsocksApplication.currentProfile match { app.currentProfile match {
case Some(oldProfile) => case Some(oldProfile) =>
// Copy Feature Settings from old profile // Copy Feature Settings from old profile
profile.route = oldProfile.route profile.route = oldProfile.route
......
...@@ -39,11 +39,12 @@ ...@@ -39,11 +39,12 @@
package com.github.shadowsocks.utils package com.github.shadowsocks.utils
import android.content.{SharedPreferences, Context} import android.content.{Context, SharedPreferences}
import com.github.shadowsocks.{R, ShadowsocksApplication}
import com.google.android.gms.tagmanager.Container
import com.github.kevinsawicki.http.HttpRequest import com.github.kevinsawicki.http.HttpRequest
import com.github.shadowsocks.R
import com.github.shadowsocks.ShadowsocksApplication.app
import com.github.shadowsocks.aidl.Config import com.github.shadowsocks.aidl.Config
import com.google.android.gms.tagmanager.Container
object ConfigUtils { object ConfigUtils {
val SHADOWSOCKS = "{\"server\": \"%s\", \"server_port\": %d, \"local_port\": %d, \"password\": \"%s\", \"method\":\"%s\", \"timeout\": %d}" val SHADOWSOCKS = "{\"server\": \"%s\", \"server_port\": %d, \"local_port\": %d, \"password\": \"%s\", \"method\":\"%s\", \"timeout\": %d}"
...@@ -152,14 +153,14 @@ object ConfigUtils { ...@@ -152,14 +153,14 @@ object ConfigUtils {
} }
def refresh(context: Context) { def refresh(context: Context) {
val holder = ShadowsocksApplication.containerHolder val holder = app.containerHolder
if (holder != null) holder.refresh() if (holder != null) holder.refresh()
} }
def getRejectList(context: Context): String = { def getRejectList(context: Context): String = {
val default = context.getString(R.string.reject) val default = context.getString(R.string.reject)
try { try {
val container = ShadowsocksApplication.containerHolder.getContainer val container = app.containerHolder.getContainer
val update = container.getString("reject") val update = container.getString("reject")
if (update == null || update.isEmpty) default else update if (update == null || update.isEmpty) default else update
} catch { } catch {
...@@ -170,7 +171,7 @@ object ConfigUtils { ...@@ -170,7 +171,7 @@ object ConfigUtils {
def getBlackList(context: Context): String = { def getBlackList(context: Context): String = {
val default = context.getString(R.string.black_list) val default = context.getString(R.string.black_list)
try { try {
val container = ShadowsocksApplication.containerHolder.getContainer val container = app.containerHolder.getContainer
val update = container.getString("black_list") val update = container.getString("black_list")
if (update == null || update.isEmpty) default else update if (update == null || update.isEmpty) default else update
} catch { } catch {
......
...@@ -40,7 +40,8 @@ package com.github.shadowsocks.utils ...@@ -40,7 +40,8 @@ package com.github.shadowsocks.utils
import android.content.{Context, Intent} import android.content.{Context, Intent}
import android.os.Bundle import android.os.Bundle
import com.github.shadowsocks.{R, ShadowsocksApplication} import com.github.shadowsocks.R
import com.github.shadowsocks.ShadowsocksApplication.app
import com.twofortyfouram.locale.api.{Intent => ApiIntent} import com.twofortyfouram.locale.api.{Intent => ApiIntent}
object TaskerSettings { object TaskerSettings {
...@@ -62,7 +63,7 @@ class TaskerSettings(bundle: Bundle) { ...@@ -62,7 +63,7 @@ class TaskerSettings(bundle: Bundle) {
if (!switchOn) bundle.putBoolean(KEY_SWITCH_ON, false) if (!switchOn) bundle.putBoolean(KEY_SWITCH_ON, false)
if (profileId >= 0) bundle.putInt(KEY_PROFILE_ID, profileId) if (profileId >= 0) bundle.putInt(KEY_PROFILE_ID, profileId)
new Intent().putExtra(ApiIntent.EXTRA_BUNDLE, bundle).putExtra(ApiIntent.EXTRA_STRING_BLURB, new Intent().putExtra(ApiIntent.EXTRA_BUNDLE, bundle).putExtra(ApiIntent.EXTRA_STRING_BLURB,
ShadowsocksApplication.profileManager.getProfile(profileId) match { app.profileManager.getProfile(profileId) match {
case Some(p) => context.getString(if (switchOn) R.string.start_service else R.string.stop_service, p.name) case Some(p) => context.getString(if (switchOn) R.string.start_service else R.string.stop_service, p.name)
case None => context.getString(if (switchOn) R.string.start_service_default else R.string.stop) case None => context.getString(if (switchOn) R.string.start_service_default else R.string.stop)
}) })
......
...@@ -2,7 +2,8 @@ package com.github.shadowsocks.utils ...@@ -2,7 +2,8 @@ package com.github.shadowsocks.utils
import java.text.DecimalFormat import java.text.DecimalFormat
import com.github.shadowsocks.{R, ShadowsocksApplication} import com.github.shadowsocks.R
import com.github.shadowsocks.ShadowsocksApplication.app
object TrafficMonitor { object TrafficMonitor {
// Bytes per second // Bytes per second
...@@ -28,7 +29,7 @@ object TrafficMonitor { ...@@ -28,7 +29,7 @@ object TrafficMonitor {
n /= 1024 n /= 1024
i = i + 1 i = i + 1
} }
if (i < 0) size + " " + ShadowsocksApplication.instance.getResources.getQuantityString(R.plurals.bytes, size.toInt) if (i < 0) size + " " + app.getResources.getQuantityString(R.plurals.bytes, size.toInt)
else numberFormat.format(n) + ' ' + units(i) else numberFormat.format(n) + ' ' + units(i)
} }
......
...@@ -51,7 +51,8 @@ import android.util.{Base64, DisplayMetrics, Log} ...@@ -51,7 +51,8 @@ import android.util.{Base64, DisplayMetrics, Log}
import android.view.View.MeasureSpec import android.view.View.MeasureSpec
import android.view.{Gravity, View, Window} import android.view.{Gravity, View, Window}
import android.widget.Toast import android.widget.Toast
import com.github.shadowsocks.{BuildConfig, ShadowsocksApplication, ShadowsocksRunnerService} import com.github.shadowsocks.ShadowsocksApplication.app
import com.github.shadowsocks.{BuildConfig, ShadowsocksRunnerService}
import org.xbill.DNS._ import org.xbill.DNS._
import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.ExecutionContext.Implicits.global
...@@ -59,7 +60,6 @@ import scala.concurrent.Future ...@@ -59,7 +60,6 @@ import scala.concurrent.Future
import scala.util.{Failure, Try} import scala.util.{Failure, Try}
object Utils { object Utils {
private val TAG = "Shadowsocks" private val TAG = "Shadowsocks"
/** /**
...@@ -229,7 +229,7 @@ object Utils { ...@@ -229,7 +229,7 @@ object Utils {
} }
def startSsService(context: Context) { def startSsService(context: Context) {
val isInstalled: Boolean = ShadowsocksApplication.settings.getBoolean(ShadowsocksApplication.getVersionName, false) val isInstalled: Boolean = app.settings.getBoolean(app.getVersionName, false)
if (!isInstalled) return if (!isInstalled) return
val intent = new Intent(context, classOf[ShadowsocksRunnerService]) val intent = new Intent(context, classOf[ShadowsocksRunnerService])
......
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