@@ -96,6 +96,7 @@ object Shadowsocks {
96
96
}
97
97
98
98
class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
99
+
99
100
import Shadowsocks ._
100
101
101
102
// Variables
@@ -129,7 +130,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
129
130
changeSwitch(checked = true )
130
131
preferences.setEnabled(false )
131
132
stat.setVisibility(View .VISIBLE )
132
- if (app.isNatEnabled) connectionTestText.setVisibility(View .GONE ) else {
133
+ if (app.isNatEnabled) connectionTestText.setVisibility(View .GONE )
134
+ else {
133
135
connectionTestText.setVisibility(View .VISIBLE )
134
136
connectionTestText.setText(getString(R .string.connection_test_pending))
135
137
}
@@ -152,13 +154,14 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
152
154
fab.setBackgroundTintList(greyTint)
153
155
fab.setImageResource(R .drawable.ic_start_busy)
154
156
fab.setEnabled(false )
155
- if (state == State .CONNECTED ) fabProgressCircle.show() // ignore for stopped
157
+ if (state == State .CONNECTED ) fabProgressCircle.show() // ignore for stopped
156
158
preferences.setEnabled(false )
157
159
stat.setVisibility(View .GONE )
158
160
}
159
161
state = s
160
162
})
161
163
}
164
+
162
165
def trafficUpdated (txRate : Long , rxRate : Long , txTotal : Long , rxTotal : Long ) {
163
166
handler.post(() => updateTraffic(txRate, rxRate, txTotal, rxTotal))
164
167
}
@@ -207,7 +210,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
207
210
private lazy val greenTint = ContextCompat .getColorStateList(this , R .color.material_green_700)
208
211
// private var adView: AdView = _
209
212
private lazy val preferences =
210
- getFragmentManager.findFragmentById(android.R .id.content).asInstanceOf [ShadowsocksSettings ]
213
+ getFragmentManager.findFragmentById(android.R .id.content).asInstanceOf [ShadowsocksSettings ]
211
214
212
215
val handler = new Handler ()
213
216
@@ -237,7 +240,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
237
240
238
241
def prepareStartService () {
239
242
Utils .ThrowableFuture {
240
- if (app.isNatEnabled) serviceLoad() else {
243
+ if (app.isNatEnabled) serviceLoad()
244
+ else {
241
245
val intent = VpnService .prepare(this )
242
246
if (intent != null ) {
243
247
startActivityForResult(intent, REQUEST_CONNECT )
@@ -308,7 +312,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
308
312
result = getString(R .string.connection_test_error, e.getMessage)
309
313
}
310
314
synchronized (if (testCount == id && app.isVpnEnabled) handler.post(() =>
311
- if (success) connectionTestText.setText(result) else {
315
+ if (success) connectionTestText.setText(result)
316
+ else {
312
317
connectionTestText.setText(R .string.connection_test_fail)
313
318
Snackbar .make(findViewById(android.R .id.content), result, Snackbar .LENGTH_LONG ).show
314
319
}))
@@ -320,8 +325,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
320
325
fab = findViewById(R .id.fab).asInstanceOf [FloatingActionButton ]
321
326
fabProgressCircle = findViewById(R .id.fabProgressCircle).asInstanceOf [FABProgressCircle ]
322
327
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 ))
325
330
fab.setOnLongClickListener((v : View ) => {
326
331
Utils .positionToast(Toast .makeText(this , if (serviceStarted) R .string.stop else R .string.connect,
327
332
Toast .LENGTH_SHORT ), fab, getWindow, 0 , Utils .dpToPx(this , 8 )).show
@@ -367,7 +372,8 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
367
372
fabProgressCircle.postDelayed(hideCircle, 100 )
368
373
stat.setVisibility(View .VISIBLE )
369
374
if (resetConnectionTest || state != State .CONNECTED )
370
- if (app.isNatEnabled) connectionTestText.setVisibility(View .GONE ) else {
375
+ if (app.isNatEnabled) connectionTestText.setVisibility(View .GONE )
376
+ else {
371
377
connectionTestText.setVisibility(View .VISIBLE )
372
378
connectionTestText.setText(getString(R .string.connection_test_pending))
373
379
}
@@ -395,7 +401,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
395
401
if (preferences.profile == null || app.profileId != preferences.profile.id) {
396
402
updatePreferenceScreen(app.currentProfile match {
397
403
case Some (profile) => profile // updated
398
- case None => // removed
404
+ case None => // removed
399
405
app.switchProfile((app.profileManager.getFirstProfile match {
400
406
case Some (first) => first
401
407
case None => app.profileManager.createDefault()
@@ -427,14 +433,17 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
427
433
super .onStart()
428
434
registerCallback
429
435
}
436
+
430
437
override def onStop () {
431
438
super .onStop()
432
439
unregisterCallback
433
440
clearDialog()
434
441
}
435
442
436
443
private var _isDestroyed : Boolean = _
444
+
437
445
override def isDestroyed = if (Build .VERSION .SDK_INT >= 17 ) super .isDestroyed else _isDestroyed
446
+
438
447
override def onDestroy () {
439
448
super .onDestroy()
440
449
_isDestroyed = true
@@ -452,6 +461,49 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
452
461
}
453
462
}
454
463
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
+
455
507
override def onActivityResult (requestCode : Int , resultCode : Int , data : Intent ) = resultCode match {
456
508
case Activity .RESULT_OK =>
457
509
serviceLoad()
0 commit comments