Skip to content

Commit b320c2e

Browse files
committed
Finish 3.5.4
add a way to ignore battery optimization
2 parents 32bded1 + 608404f commit b320c2e

File tree

9 files changed

+83
-12
lines changed

9 files changed

+83
-12
lines changed

src/main/AndroidManifest.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
xmlns:tools="http://schemas.android.com/tools" package="com.github.shadowsocks"
3-
android:versionCode="324"
4-
android:versionName="3.5.3">
3+
android:versionCode="325"
4+
android:versionName="3.5.4">
55

66
<uses-permission android:name="android.permission.INTERNET"/>
77
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
88
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
99
<uses-permission android:name="android.permission.NFC" />
10-
<uses-permission android:name="android.permission.CAMERA" />
10+
<uses-permission android:name="android.permission.CAMERA" />
11+
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
1112

1213
<uses-feature android:name="android.hardware.touchscreen"
1314
android:required="false"/>

src/main/res/values-ja/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
<string name="remove_profile">このプロファイルを削除「%s」?</string>
6767
<string name="recovery">リセット</string>
6868
<string name="recovery_summary">すべてのバックグラウンドサービスをリセット</string>
69+
<string name="ignore_battery_optimization">Request ignore battery optimizations</string>
70+
<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>
6971
<string name="about">本アプリについて</string>
7072
<string name="about_title">シャドーソックス(shadowsocks)%s</string>
7173
<string name="flush_dnscache">DNS キャッシュをリフレッシュ</string>

src/main/res/values-ru/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@
113113
<string name="settings">Настройки</string>
114114
<string name="recovery">Сброс</string>
115115
<string name="recovery_summary">Сброс всех фоновых задач</string>
116+
<string name="ignore_battery_optimization">Request ignore battery optimizations</string>
117+
<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>
116118
<string name="about">О приложении</string>
117119
<string name="about_title">Shadowsocks R %s</string>
118120
<string name="share">Поделиться</string>

src/main/res/values-zh-rCN/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
<string name="remove_profile">删除此配置文件「%s」?</string>
6969
<string name="recovery">重置</string>
7070
<string name="recovery_summary">重置所有后台服务</string>
71+
<string name="ignore_battery_optimization">请求忽略电池优化</string>
72+
<string name="ignore_battery_optimization_summary">点击来请求触发忽略电池优化,可避免息屏休眠导致的后台断流,会增加电池消耗,在部分设备上可能无法正常触发。</string>
7173
<string name="about">关于</string>
7274
<string name="about_title">ShadowsocksR %s</string>
7375
<string name="flush_dnscache">刷新 DNS 缓存</string>

src/main/res/values-zh-rTW/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
<string name="remove_profile">移除此設定檔 "%s"?</string>
7070
<string name="recovery">重設</string>
7171
<string name="recovery_summary">重設一切背景服務</string>
72+
<string name="ignore_battery_optimization">請求忽略電池優化</string>
73+
<string name="ignore_battery_optimization_summary">點擊來請求觸發忽略電池優化,可避免息屏休眠導致的後台斷流,會增加電池消耗,在部分設備上可能無法正常觸發。</string>
7274
<string name="about">關於</string>
7375
<string name="about_title">ShadowsocksR %s</string>
7476
<string name="flush_dnscache">清除 DNS 快取</string>

src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
<string name="settings">Settings</string>
123123
<string name="recovery">Reset</string>
124124
<string name="recovery_summary">Reset all the background services</string>
125+
<string name="ignore_battery_optimization">Request ignore battery optimizations</string>
126+
<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>
125127
<string name="about">About</string>
126128
<string name="about_title">ShadowsocksR %s</string>
127129
<string name="share">Share</string>

src/main/res/xml/pref_all.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@
127127
android:summary="@string/nat_summary"/>
128128
<Preference android:key="recovery" android:title="@string/recovery"
129129
android:summary="@string/recovery_summary"/>
130+
<Preference android:key="ignore_battery_optimization" android:title="@string/ignore_battery_optimization"
131+
android:summary="@string/ignore_battery_optimization_summary"/>
130132
<Preference android:key="aclupdate" android:title="@string/aclupdate"
131133
android:summary="@string/aclupdate_summary"/>
132134
<Preference android:key="frontproxy" android:title="@string/frontproxy_set"/>

src/main/scala/com/github/shadowsocks/Shadowsocks.scala

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ object Shadowsocks {
9696
}
9797

9898
class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
99+
99100
import Shadowsocks._
100101

101102
// Variables
@@ -129,7 +130,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
129130
changeSwitch(checked = true)
130131
preferences.setEnabled(false)
131132
stat.setVisibility(View.VISIBLE)
132-
if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE) else {
133+
if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE)
134+
else {
133135
connectionTestText.setVisibility(View.VISIBLE)
134136
connectionTestText.setText(getString(R.string.connection_test_pending))
135137
}
@@ -152,13 +154,14 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
152154
fab.setBackgroundTintList(greyTint)
153155
fab.setImageResource(R.drawable.ic_start_busy)
154156
fab.setEnabled(false)
155-
if (state == State.CONNECTED) fabProgressCircle.show() // ignore for stopped
157+
if (state == State.CONNECTED) fabProgressCircle.show() // ignore for stopped
156158
preferences.setEnabled(false)
157159
stat.setVisibility(View.GONE)
158160
}
159161
state = s
160162
})
161163
}
164+
162165
def trafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long) {
163166
handler.post(() => updateTraffic(txRate, rxRate, txTotal, rxTotal))
164167
}
@@ -207,7 +210,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
207210
private lazy val greenTint = ContextCompat.getColorStateList(this, R.color.material_green_700)
208211
//private var adView: AdView = _
209212
private lazy val preferences =
210-
getFragmentManager.findFragmentById(android.R.id.content).asInstanceOf[ShadowsocksSettings]
213+
getFragmentManager.findFragmentById(android.R.id.content).asInstanceOf[ShadowsocksSettings]
211214

212215
val handler = new Handler()
213216

@@ -237,7 +240,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
237240

238241
def prepareStartService() {
239242
Utils.ThrowableFuture {
240-
if (app.isNatEnabled) serviceLoad() else {
243+
if (app.isNatEnabled) serviceLoad()
244+
else {
241245
val intent = VpnService.prepare(this)
242246
if (intent != null) {
243247
startActivityForResult(intent, REQUEST_CONNECT)
@@ -308,7 +312,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
308312
result = getString(R.string.connection_test_error, e.getMessage)
309313
}
310314
synchronized(if (testCount == id && app.isVpnEnabled) handler.post(() =>
311-
if (success) connectionTestText.setText(result) else {
315+
if (success) connectionTestText.setText(result)
316+
else {
312317
connectionTestText.setText(R.string.connection_test_fail)
313318
Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show
314319
}))
@@ -320,8 +325,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
320325
fab = findViewById(R.id.fab).asInstanceOf[FloatingActionButton]
321326
fabProgressCircle = findViewById(R.id.fabProgressCircle).asInstanceOf[FABProgressCircle]
322327
fab.setOnClickListener(_ => if (serviceStarted) serviceStop()
323-
else if (bgService != null) prepareStartService()
324-
else changeSwitch(checked = false))
328+
else if (bgService != null) prepareStartService()
329+
else changeSwitch(checked = false))
325330
fab.setOnLongClickListener((v: View) => {
326331
Utils.positionToast(Toast.makeText(this, if (serviceStarted) R.string.stop else R.string.connect,
327332
Toast.LENGTH_SHORT), fab, getWindow, 0, Utils.dpToPx(this, 8)).show
@@ -367,7 +372,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
367372
fabProgressCircle.postDelayed(hideCircle, 100)
368373
stat.setVisibility(View.VISIBLE)
369374
if (resetConnectionTest || state != State.CONNECTED)
370-
if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE) else {
375+
if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE)
376+
else {
371377
connectionTestText.setVisibility(View.VISIBLE)
372378
connectionTestText.setText(getString(R.string.connection_test_pending))
373379
}
@@ -395,7 +401,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
395401
if (preferences.profile == null || app.profileId != preferences.profile.id) {
396402
updatePreferenceScreen(app.currentProfile match {
397403
case Some(profile) => profile // updated
398-
case None => // removed
404+
case None => // removed
399405
app.switchProfile((app.profileManager.getFirstProfile match {
400406
case Some(first) => first
401407
case None => app.profileManager.createDefault()
@@ -427,14 +433,17 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
427433
super.onStart()
428434
registerCallback
429435
}
436+
430437
override def onStop() {
431438
super.onStop()
432439
unregisterCallback
433440
clearDialog()
434441
}
435442

436443
private var _isDestroyed: Boolean = _
444+
437445
override def isDestroyed = if (Build.VERSION.SDK_INT >= 17) super.isDestroyed else _isDestroyed
446+
438447
override def onDestroy() {
439448
super.onDestroy()
440449
_isDestroyed = true
@@ -452,6 +461,49 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
452461
}
453462
}
454463

464+
def ignoreBatteryOptimization() {
465+
// TODO do . ignore_battery_optimization ......................................
466+
// http://blog.csdn.net/laxian2009/article/details/52474214
467+
468+
var exception = false
469+
try {
470+
val powerManager: PowerManager = this.getSystemService(Context.POWER_SERVICE).asInstanceOf[PowerManager]
471+
val packageName = this.getPackageName
472+
val hasIgnored = powerManager.isIgnoringBatteryOptimizations(packageName)
473+
if (!hasIgnored) {
474+
val intent = new Intent()
475+
intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
476+
intent.setData(android.net.Uri.parse("package:" + packageName))
477+
startActivity(intent)
478+
}
479+
exception = false
480+
} catch {
481+
case _: Throwable =>
482+
exception = true
483+
} finally {
484+
}
485+
if (exception) {
486+
try {
487+
val intent = new Intent(Intent.ACTION_MAIN)
488+
intent.addCategory(Intent.CATEGORY_LAUNCHER)
489+
490+
val cn = new ComponentName(
491+
"com.android.settings",
492+
"com.android.com.settings.Settings@HighPowerApplicationsActivity"
493+
)
494+
495+
intent.setComponent(cn)
496+
startActivity(intent)
497+
498+
exception = false
499+
} catch {
500+
case _: Throwable =>
501+
exception = true
502+
} finally {
503+
}
504+
}
505+
}
506+
455507
override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) = resultCode match {
456508
case Activity.RESULT_OK =>
457509
serviceLoad()

src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
244244
true
245245
})
246246

247+
findPreference("ignore_battery_optimization").setOnPreferenceClickListener((preference: Preference) => {
248+
app.track(TAG, "ignore_battery_optimization")
249+
activity.ignoreBatteryOptimization()
250+
true
251+
})
252+
247253
findPreference("aclupdate").setOnPreferenceClickListener((preference: Preference) => {
248254
app.track(TAG, "aclupdate")
249255
val url = getPreferenceManager.getSharedPreferences.getString(Key.aclurl, "");

0 commit comments

Comments
 (0)