@@ -96,6 +96,7 @@ object Shadowsocks {
9696}
9797
9898class 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()
0 commit comments