Obsolete code cleanup

This commit is contained in:
Mygod
2016-05-20 21:33:01 +08:00
parent c52c33f3ac
commit 0b7fb75b7a
12 changed files with 54 additions and 395 deletions
@@ -96,49 +96,6 @@ object ConfigUtils {
|}
""".stripMargin
val PDNSD_BYPASS =
"""
|global {
| perm_cache = 2048;
| cache_dir = "%s";
| server_ip = %s;
| server_port = %d;
| query_method = tcp_only;
| run_ipv4 = on;
| min_ttl = 15m;
| max_ttl = 1w;
| timeout = 10;
| daemon = off;
|}
|
|server {
| label = "china-servers";
| ip = 114.114.114.114, 223.5.5.5;
| uptest = none;
| preset = on;
| include = %s;
| policy = excluded;
| timeout = 2;
|}
|
|server {
| label = "local-server";
| ip = 127.0.0.1;
| uptest = none;
| preset = on;
| port = %d;
| timeout = 5;
|}
|
|rr {
| name=localhost;
| reverse=on;
| a=127.0.0.1;
| owner=localhost;
| soa=localhost,root.localhost,42,86400,900,86400,86400;
|}
""".stripMargin
val PDNSD_DIRECT =
"""
|global {
@@ -63,10 +63,6 @@ object Console {
.open()
}
def runCommand(command: String) {
runCommand(Array(command))
}
def runCommand(commands: Array[String]) {
val shell = openShell()
shell.addCommand(commands, 0, ((commandCode: Int, exitCode: Int, output: util.List[String]) =>
@@ -44,17 +44,9 @@ object Executable {
val PDNSD = "pdnsd"
val SS_LOCAL = "ss-local"
val SS_TUNNEL = "ss-tunnel"
val IPTABLES = "iptables"
val TUN2SOCKS = "tun2socks"
}
object Msg {
val CONNECT_FINISH = 1
val CONNECT_SUCCESS = 2
val CONNECT_FAIL = 3
val VPN_ERROR = 6
}
object Key {
val profileId = "profileId"
val profileName = "profileName"
@@ -63,9 +55,7 @@ object Key {
val isNAT = "isNAT"
val route = "route"
val stat = "stat"
val isRunning = "isRunning"
val isAutoConnect = "isAutoConnect"
val isProxyApps = "isProxyApps"
@@ -83,17 +73,6 @@ object Key {
val profileTip = "profileTip"
}
object Scheme {
val APP = "app://"
val PROFILE = "profile://"
val SS = "ss"
}
object Mode {
val NAT = 0
val VPN = 1
}
object State {
val CONNECTING = 1
val CONNECTED = 2
@@ -49,8 +49,6 @@ object TaskerSettings {
def fromIntent(intent: Intent) = new TaskerSettings(if (intent.hasExtra(ApiIntent.EXTRA_BUNDLE))
intent.getBundleExtra(ApiIntent.EXTRA_BUNDLE) else Bundle.EMPTY)
def fromBundle(bundle: Bundle) = new TaskerSettings(bundle)
}
class TaskerSettings(bundle: Bundle) {
@@ -38,19 +38,15 @@
*/
package com.github.shadowsocks.utils
import java.io._
import java.net._
import java.security.MessageDigest
import android.animation.{Animator, AnimatorListenerAdapter}
import android.app.ActivityManager
import android.content.pm.{ApplicationInfo, PackageManager}
import android.content.pm.PackageManager
import android.content.{Context, Intent}
import android.graphics._
import android.graphics.drawable.{BitmapDrawable, Drawable}
import android.os.Build
import android.provider.Settings
import android.support.v4.content.ContextCompat
import android.util.{Base64, DisplayMetrics, Log}
import android.view.View.MeasureSpec
import android.view.{Gravity, View, Window}
@@ -61,12 +57,19 @@ import org.xbill.DNS._
object Utils {
val TAG: String = "Shadowsocks"
val DEFAULT_IPTABLES: String = "/system/bin/iptables"
val ALTERNATIVE_IPTABLES: String = "iptables"
var initialized: Boolean = false
var hasRedirectSupport: Int = -1
var iptables: String = null
private val TAG = "Shadowsocks"
/**
* Check the system's iptables
* Default to use the app's iptables
*/
private val DEFAULT_IPTABLES = "/system/bin/iptables"
private val ALTERNATIVE_IPTABLES = "iptables"
lazy val iptables = if (Console.isRoot) {
val lines = Console.runRootCommand(DEFAULT_IPTABLES + " --version", DEFAULT_IPTABLES + " -L -t nat -n")
if (Console.isRoot && lines != null && !lines.contains("OUTPUT") || !lines.contains("v1.4.")) ALTERNATIVE_IPTABLES
else DEFAULT_IPTABLES
} else DEFAULT_IPTABLES
def isLollipopOrAbove: Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
@@ -86,31 +89,6 @@ object Utils {
* round or floor depending on whether you are using offsets(floor) or
* widths(round)
*/
def dpToPx(context: Context, dp: Float): Float = {
val density = context.getResources.getDisplayMetrics.density
dp * density
}
def pxToDp(context: Context, px: Float): Float = {
val density = context.getResources.getDisplayMetrics.density
px / density
}
def getBitmap(text: String, width: Int, height: Int, background: Int): Bitmap = {
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val size = bitmap.getHeight / 4
val canvas = new Canvas(bitmap)
canvas.drawColor(background)
val paint = new Paint()
paint.setColor(Color.WHITE)
paint.setTextSize(size)
val bounds = new Rect()
paint.getTextBounds(text, 0, text.length, bounds)
canvas
.drawText(text, (bitmap.getWidth - bounds.width()) / 2,
bitmap.getHeight - (bitmap.getHeight - bounds.height()) / 2, paint)
bitmap
}
// Based on: http://stackoverflow.com/a/21026866/2245107
def positionToast(toast: Toast, view: View, window: Window, offsetX: Int = 0, offsetY: Int = 0) = {
@@ -165,25 +143,6 @@ object Utils {
context.sendBroadcast(disableIntent)
}
def isServiceStarted(name: String, context: Context): Boolean = {
import scala.collection.JavaConversions._
val activityManager = context
.getSystemService(Context.ACTIVITY_SERVICE)
.asInstanceOf[ActivityManager]
val services = activityManager.getRunningServices(Integer.MAX_VALUE)
if (services != null) {
for (service <- services) {
if (service.service.getClassName == name) {
return true
}
}
}
false
}
def resolve(host: String, addrType: Int): Option[String] = {
try {
val lookup = new Lookup(host, addrType)
@@ -240,31 +199,6 @@ object Utils {
private lazy val isNumericMethod = classOf[InetAddress].getMethod("isNumeric", classOf[String])
def isNumeric(address: String): Boolean = isNumericMethod.invoke(null, address).asInstanceOf[Boolean]
/**
* Get local IPv4 address
*/
def getIPv4Address: Option[String] = {
try {
val interfaces = NetworkInterface.getNetworkInterfaces
while (interfaces.hasMoreElements) {
val intf = interfaces.nextElement()
val addrs = intf.getInetAddresses
while (addrs.hasMoreElements) {
val addr = addrs.nextElement()
if (!addr.isLoopbackAddress && !addr.isLinkLocalAddress) {
if (addr.isInstanceOf[Inet4Address]) {
return Some(addr.getHostAddress.toUpperCase)
}
}
}
}
} catch {
case ex: Exception =>
Log.e(TAG, "Failed to get interfaces' addresses.", ex)
}
None
}
/**
* If there exists a valid IPv6 interface
*/
@@ -291,116 +225,6 @@ object Utils {
false
}
/**
* Check the system's iptables
* Default to use the app's iptables
*/
def checkIptables() {
if (!Console.isRoot) {
iptables = DEFAULT_IPTABLES
return
}
iptables = DEFAULT_IPTABLES
var compatible: Boolean = false
var version: Boolean = false
val lines = Console.runRootCommand(iptables + " --version", iptables + " -L -t nat -n")
if (lines == null) return
if (lines.contains("OUTPUT")) {
compatible = true
}
if (lines.contains("v1.4.")) {
version = true
}
if (!compatible || !version) {
iptables = ALTERNATIVE_IPTABLES
if (!new File(iptables).exists) iptables = "iptables"
}
}
def drawableToBitmap(drawable: Drawable): Bitmap = {
drawable match {
case d: BitmapDrawable =>
return d.getBitmap
case _ =>
}
val width = if (drawable.getIntrinsicWidth > 0) drawable.getIntrinsicWidth else 1
val height = if (drawable.getIntrinsicWidth > 0) drawable.getIntrinsicWidth else 1
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = new Canvas(bitmap)
drawable.setBounds(0, 0, canvas.getWidth, canvas.getHeight)
drawable.draw(canvas)
bitmap
}
def getAppIcon(c: Context, packageName: String): Drawable = {
val pm: PackageManager = c.getPackageManager
val icon: Drawable = ContextCompat.getDrawable(c, android.R.drawable.sym_def_app_icon)
try {
pm.getApplicationIcon(packageName)
} catch {
case e: PackageManager.NameNotFoundException => icon
}
}
def getAppIcon(c: Context, uid: Int): Drawable = {
val pm: PackageManager = c.getPackageManager
val icon: Drawable = ContextCompat.getDrawable(c, 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)
return pm.getApplicationIcon(appInfo)
} catch {
case e: PackageManager.NameNotFoundException =>
Log.e(c.getPackageName, "No package found matching with the uid " + uid)
}
}
} else {
Log.e(c.getPackageName, "Package not found for uid " + uid)
}
icon
}
def getHasRedirectSupport: Boolean = {
if (hasRedirectSupport == -1) initHasRedirectSupported()
hasRedirectSupport == 1
}
def getIptables: String = {
if (iptables == null) checkIptables()
iptables
}
def initHasRedirectSupported() {
if (!Console.isRoot) return
hasRedirectSupport = 1
val command = Utils.getIptables + " -t nat -A OUTPUT -p udp --dport 54 -j REDIRECT --to 8154"
val lines = Console.runRootCommand(command, command.replace("-A", "-D"))
if (lines == null) return
if (lines.contains("No chain/target/match")) {
hasRedirectSupport = 0
}
}
def isInitialized: Boolean = {
initialized match {
case true => true
case _ =>
initialized = true
false
}
}
def startSsService(context: Context) {
val isInstalled: Boolean = ShadowsocksApplication.settings.getBoolean(ShadowsocksApplication.getVersionName, false)
if (!isInstalled) return
@@ -414,5 +238,3 @@ object Utils {
context.sendBroadcast(intent)
}
}