forked from HMBSbige/ShadowsocksR-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
23/40
- Loading branch information
Showing
2 changed files
with
131 additions
and
166 deletions.
There are no files selected for viewing
166 changes: 0 additions & 166 deletions
166
app/src/main/java/com/github/shadowsocks/ShadowsocksTileService.java
This file was deleted.
Oops, something went wrong.
131 changes: 131 additions & 0 deletions
131
app/src/main/java/com/github/shadowsocks/ShadowsocksTileService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package com.github.shadowsocks | ||
|
||
import android.annotation.* | ||
import android.content.* | ||
import android.graphics.drawable.* | ||
import android.os.* | ||
import android.service.quicksettings.* | ||
import com.github.shadowsocks.aidl.* | ||
import com.github.shadowsocks.utils.* | ||
|
||
@TargetApi(Build.VERSION_CODES.N) | ||
class ShadowsocksTileService : TileService() | ||
{ | ||
private lateinit var mServiceBoundContext: ServiceBoundContext | ||
|
||
private lateinit var iconIdle: Icon | ||
private lateinit var iconBusy: Icon | ||
private lateinit var iconConnected: Icon | ||
|
||
private val callback = object : IShadowsocksServiceCallback.Stub() | ||
{ | ||
override fun trafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long) | ||
{ | ||
} | ||
|
||
@Throws(RemoteException::class) | ||
override fun stateChanged(state: Int, profileName: String?, msg: String?) | ||
{ | ||
val tile = qsTile | ||
if (tile != null) | ||
{ | ||
when (state) | ||
{ | ||
Constants.State.STOPPED -> | ||
{ | ||
tile.icon = iconIdle | ||
tile.label = getString(R.string.app_name) | ||
tile.state = Tile.STATE_INACTIVE | ||
} | ||
Constants.State.CONNECTED -> | ||
{ | ||
tile.icon = iconConnected | ||
val label = profileName ?: getString(R.string.app_name) | ||
tile.label = label | ||
tile.state = Tile.STATE_ACTIVE | ||
} | ||
else -> | ||
{ | ||
tile.icon = iconBusy | ||
tile.label = getString(R.string.app_name) | ||
tile.state = Tile.STATE_UNAVAILABLE | ||
} | ||
} | ||
tile.updateTile() | ||
} | ||
} | ||
} | ||
|
||
override fun attachBaseContext(base: Context) | ||
{ | ||
super.attachBaseContext(base) | ||
|
||
iconIdle = Icon.createWithResource(this, R.drawable.ic_start_idle) | ||
.setTint(-0x7f000001) | ||
iconBusy = Icon.createWithResource(this, R.drawable.ic_start_busy) | ||
iconConnected = Icon.createWithResource(this, R.drawable.ic_start_connected) | ||
|
||
mServiceBoundContext = object : ServiceBoundContext(base) | ||
{ | ||
override fun onServiceConnected() | ||
{ | ||
try | ||
{ | ||
callback.stateChanged(bgService.state, bgService.profileName, null) | ||
} | ||
catch (e: RemoteException) | ||
{ | ||
e.printStackTrace() | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onStartListening() | ||
{ | ||
super.onStartListening() | ||
mServiceBoundContext.attachService(callback) | ||
} | ||
|
||
override fun onStopListening() | ||
{ | ||
super.onStopListening() | ||
// just in case the user switches to NAT mode, also saves battery | ||
mServiceBoundContext.detachService() | ||
} | ||
|
||
override fun onClick() | ||
{ | ||
super.onClick() | ||
if (isLocked) | ||
{ | ||
unlockAndRun { toggle() } | ||
} | ||
else | ||
{ | ||
toggle() | ||
} | ||
} | ||
|
||
private fun toggle() | ||
{ | ||
if (mServiceBoundContext.bgService != null) | ||
{ | ||
try | ||
{ | ||
when (mServiceBoundContext.bgService.state) | ||
{ | ||
Constants.State.STOPPED -> Utils.startSsService(this) | ||
Constants.State.CONNECTED -> Utils.stopSsService(this) | ||
else -> | ||
{ | ||
} // ignore | ||
} | ||
} | ||
catch (e: RemoteException) | ||
{ | ||
e.printStackTrace() | ||
} | ||
} | ||
} | ||
} |