Skip to content

Commit

Permalink
Finish 3.5.4
Browse files Browse the repository at this point in the history
add a way to ignore battery optimization
  • Loading branch information
Akkariiin committed Mar 8, 2018
2 parents 32bded1 + 608404f commit b320c2e
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" package="com.github.shadowsocks"
android:versionCode="324"
android:versionName="3.5.3">
android:versionCode="325"
android:versionName="3.5.4">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

<uses-feature android:name="android.hardware.touchscreen"
android:required="false"/>
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
<string name="remove_profile">このプロファイルを削除「%s」?</string>
<string name="recovery">リセット</string>
<string name="recovery_summary">すべてのバックグラウンドサービスをリセット</string>
<string name="ignore_battery_optimization">Request ignore battery optimizations</string>
<string name="ignore_battery_optimization_summary">Click to request trigger Ignore the battery optimization, to avoid background hiccups caused by hibernation when off screen, will increase the battery consumption, may not be triggered properly on some devices.</string>
<string name="about">本アプリについて</string>
<string name="about_title">シャドーソックス(shadowsocks)%s</string>
<string name="flush_dnscache">DNS キャッシュをリフレッシュ</string>
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
<string name="settings">Настройки</string>
<string name="recovery">Сброс</string>
<string name="recovery_summary">Сброс всех фоновых задач</string>
<string name="ignore_battery_optimization">Request ignore battery optimizations</string>
<string name="ignore_battery_optimization_summary">Click to request trigger Ignore the battery optimization, to avoid background hiccups caused by hibernation when off screen, will increase the battery consumption, may not be triggered properly on some devices.</string>
<string name="about">О приложении</string>
<string name="about_title">Shadowsocks R %s</string>
<string name="share">Поделиться</string>
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
<string name="remove_profile">删除此配置文件「%s」?</string>
<string name="recovery">重置</string>
<string name="recovery_summary">重置所有后台服务</string>
<string name="ignore_battery_optimization">请求忽略电池优化</string>
<string name="ignore_battery_optimization_summary">点击来请求触发忽略电池优化,可避免息屏休眠导致的后台断流,会增加电池消耗,在部分设备上可能无法正常触发。</string>
<string name="about">关于</string>
<string name="about_title">ShadowsocksR %s</string>
<string name="flush_dnscache">刷新 DNS 缓存</string>
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
<string name="remove_profile">移除此設定檔 "%s"?</string>
<string name="recovery">重設</string>
<string name="recovery_summary">重設一切背景服務</string>
<string name="ignore_battery_optimization">請求忽略電池優化</string>
<string name="ignore_battery_optimization_summary">點擊來請求觸發忽略電池優化,可避免息屏休眠導致的後台斷流,會增加電池消耗,在部分設備上可能無法正常觸發。</string>
<string name="about">關於</string>
<string name="about_title">ShadowsocksR %s</string>
<string name="flush_dnscache">清除 DNS 快取</string>
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
<string name="settings">Settings</string>
<string name="recovery">Reset</string>
<string name="recovery_summary">Reset all the background services</string>
<string name="ignore_battery_optimization">Request ignore battery optimizations</string>
<string name="ignore_battery_optimization_summary">Click to request trigger Ignore the battery optimization, to avoid background hiccups caused by hibernation when off screen, will increase the battery consumption, may not be triggered properly on some devices.</string>
<string name="about">About</string>
<string name="about_title">ShadowsocksR %s</string>
<string name="share">Share</string>
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/xml/pref_all.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@
android:summary="@string/nat_summary"/>
<Preference android:key="recovery" android:title="@string/recovery"
android:summary="@string/recovery_summary"/>
<Preference android:key="ignore_battery_optimization" android:title="@string/ignore_battery_optimization"
android:summary="@string/ignore_battery_optimization_summary"/>
<Preference android:key="aclupdate" android:title="@string/aclupdate"
android:summary="@string/aclupdate_summary"/>
<Preference android:key="frontproxy" android:title="@string/frontproxy_set"/>
Expand Down
70 changes: 61 additions & 9 deletions src/main/scala/com/github/shadowsocks/Shadowsocks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ object Shadowsocks {
}

class Shadowsocks extends AppCompatActivity with ServiceBoundContext {

import Shadowsocks._

// Variables
Expand Down Expand Up @@ -129,7 +130,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
changeSwitch(checked = true)
preferences.setEnabled(false)
stat.setVisibility(View.VISIBLE)
if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE) else {
if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE)
else {
connectionTestText.setVisibility(View.VISIBLE)
connectionTestText.setText(getString(R.string.connection_test_pending))
}
Expand All @@ -152,13 +154,14 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
fab.setBackgroundTintList(greyTint)
fab.setImageResource(R.drawable.ic_start_busy)
fab.setEnabled(false)
if (state == State.CONNECTED) fabProgressCircle.show() // ignore for stopped
if (state == State.CONNECTED) fabProgressCircle.show() // ignore for stopped
preferences.setEnabled(false)
stat.setVisibility(View.GONE)
}
state = s
})
}

def trafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long) {
handler.post(() => updateTraffic(txRate, rxRate, txTotal, rxTotal))
}
Expand Down Expand Up @@ -207,7 +210,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
private lazy val greenTint = ContextCompat.getColorStateList(this, R.color.material_green_700)
//private var adView: AdView = _
private lazy val preferences =
getFragmentManager.findFragmentById(android.R.id.content).asInstanceOf[ShadowsocksSettings]
getFragmentManager.findFragmentById(android.R.id.content).asInstanceOf[ShadowsocksSettings]

val handler = new Handler()

Expand Down Expand Up @@ -237,7 +240,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {

def prepareStartService() {
Utils.ThrowableFuture {
if (app.isNatEnabled) serviceLoad() else {
if (app.isNatEnabled) serviceLoad()
else {
val intent = VpnService.prepare(this)
if (intent != null) {
startActivityForResult(intent, REQUEST_CONNECT)
Expand Down Expand Up @@ -308,7 +312,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
result = getString(R.string.connection_test_error, e.getMessage)
}
synchronized(if (testCount == id && app.isVpnEnabled) handler.post(() =>
if (success) connectionTestText.setText(result) else {
if (success) connectionTestText.setText(result)
else {
connectionTestText.setText(R.string.connection_test_fail)
Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show
}))
Expand All @@ -320,8 +325,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
fab = findViewById(R.id.fab).asInstanceOf[FloatingActionButton]
fabProgressCircle = findViewById(R.id.fabProgressCircle).asInstanceOf[FABProgressCircle]
fab.setOnClickListener(_ => if (serviceStarted) serviceStop()
else if (bgService != null) prepareStartService()
else changeSwitch(checked = false))
else if (bgService != null) prepareStartService()
else changeSwitch(checked = false))
fab.setOnLongClickListener((v: View) => {
Utils.positionToast(Toast.makeText(this, if (serviceStarted) R.string.stop else R.string.connect,
Toast.LENGTH_SHORT), fab, getWindow, 0, Utils.dpToPx(this, 8)).show
Expand Down Expand Up @@ -367,7 +372,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
fabProgressCircle.postDelayed(hideCircle, 100)
stat.setVisibility(View.VISIBLE)
if (resetConnectionTest || state != State.CONNECTED)
if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE) else {
if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE)
else {
connectionTestText.setVisibility(View.VISIBLE)
connectionTestText.setText(getString(R.string.connection_test_pending))
}
Expand Down Expand Up @@ -395,7 +401,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
if (preferences.profile == null || app.profileId != preferences.profile.id) {
updatePreferenceScreen(app.currentProfile match {
case Some(profile) => profile // updated
case None => // removed
case None => // removed
app.switchProfile((app.profileManager.getFirstProfile match {
case Some(first) => first
case None => app.profileManager.createDefault()
Expand Down Expand Up @@ -427,14 +433,17 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
super.onStart()
registerCallback
}

override def onStop() {
super.onStop()
unregisterCallback
clearDialog()
}

private var _isDestroyed: Boolean = _

override def isDestroyed = if (Build.VERSION.SDK_INT >= 17) super.isDestroyed else _isDestroyed

override def onDestroy() {
super.onDestroy()
_isDestroyed = true
Expand All @@ -452,6 +461,49 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
}
}

def ignoreBatteryOptimization() {
// TODO do . ignore_battery_optimization ......................................
// http://blog.csdn.net/laxian2009/article/details/52474214

var exception = false
try {
val powerManager: PowerManager = this.getSystemService(Context.POWER_SERVICE).asInstanceOf[PowerManager]
val packageName = this.getPackageName
val hasIgnored = powerManager.isIgnoringBatteryOptimizations(packageName)
if (!hasIgnored) {
val intent = new Intent()
intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
intent.setData(android.net.Uri.parse("package:" + packageName))
startActivity(intent)
}
exception = false
} catch {
case _: Throwable =>
exception = true
} finally {
}
if (exception) {
try {
val intent = new Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)

val cn = new ComponentName(
"com.android.settings",
"com.android.com.settings.Settings@HighPowerApplicationsActivity"
)

intent.setComponent(cn)
startActivity(intent)

exception = false
} catch {
case _: Throwable =>
exception = true
} finally {
}
}
}

override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) = resultCode match {
case Activity.RESULT_OK =>
serviceLoad()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
true
})

findPreference("ignore_battery_optimization").setOnPreferenceClickListener((preference: Preference) => {
app.track(TAG, "ignore_battery_optimization")
activity.ignoreBatteryOptimization()
true
})

findPreference("aclupdate").setOnPreferenceClickListener((preference: Preference) => {
app.track(TAG, "aclupdate")
val url = getPreferenceManager.getSharedPreferences.getString(Key.aclurl, "");
Expand Down

0 comments on commit b320c2e

Please sign in to comment.