|
| 1 | +package dev.bluehouse.enablevolte |
| 2 | + |
| 3 | +import android.service.quicksettings.Tile |
| 4 | +import android.service.quicksettings.TileService |
| 5 | +import android.telephony.CarrierConfigManager |
| 6 | +import org.lsposed.hiddenapibypass.HiddenApiBypass |
| 7 | +import java.lang.IllegalStateException |
| 8 | + |
| 9 | +class SIM1VoLTEConfigToggleQSTileService : VoLTEConfigToggleQSTileService(0) |
| 10 | +class SIM2VoLTEConfigToggleQSTileService : VoLTEConfigToggleQSTileService(1) |
| 11 | + |
| 12 | +open class VoLTEConfigToggleQSTileService(private val simSlotIndex: Int) : TileService() { |
| 13 | + private val TAG = "SIM${simSlotIndex}VoLTEConfigToggleQSTileService" |
| 14 | + |
| 15 | + init { |
| 16 | + HiddenApiBypass.addHiddenApiExemptions("L") |
| 17 | + HiddenApiBypass.addHiddenApiExemptions("I") |
| 18 | + } |
| 19 | + |
| 20 | + private val moder: SubscriptionModer? get() { |
| 21 | + val carrierModer = CarrierModer(this.applicationContext) |
| 22 | + |
| 23 | + try { |
| 24 | + if (checkShizukuPermission(0) == ShizukuStatus.GRANTED && carrierModer.deviceSupportsIMS) { |
| 25 | + carrierModer.subscriptions |
| 26 | + val sub = carrierModer.getActiveSubscriptionInfoForSimSlotIndex(this.simSlotIndex) |
| 27 | + ?: return null |
| 28 | + return SubscriptionModer(sub.subscriptionId) |
| 29 | + } |
| 30 | + } catch (_: IllegalStateException) {} |
| 31 | + return null |
| 32 | + } |
| 33 | + |
| 34 | + private val volteEnabled: Boolean? get() { |
| 35 | + /* |
| 36 | + * true: VoLTE enabled |
| 37 | + * false: VoLTE disabled |
| 38 | + * null: cannot determine status (Shizuku not running or permission not granted, SIM slot not active, ...) |
| 39 | + */ |
| 40 | + val moder = this.moder ?: return null |
| 41 | + try { |
| 42 | + return moder.isVoLteConfigEnabled |
| 43 | + } catch (_: IllegalStateException) {} |
| 44 | + return null |
| 45 | + } |
| 46 | + |
| 47 | + override fun onTileAdded() { |
| 48 | + super.onTileAdded() |
| 49 | + if (this.volteEnabled == null) { |
| 50 | + qsTile.state = Tile.STATE_UNAVAILABLE |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + override fun onStartListening() { |
| 55 | + super.onStartListening() |
| 56 | + qsTile.state = when (this.volteEnabled) { |
| 57 | + true -> Tile.STATE_ACTIVE |
| 58 | + false -> Tile.STATE_INACTIVE |
| 59 | + null -> Tile.STATE_UNAVAILABLE |
| 60 | + } |
| 61 | + qsTile.subtitle = getString( |
| 62 | + when (this.volteEnabled) { |
| 63 | + true -> R.string.enabled |
| 64 | + false -> R.string.disabled |
| 65 | + null -> R.string.unknown |
| 66 | + }, |
| 67 | + ) |
| 68 | + qsTile.updateTile() |
| 69 | + } |
| 70 | + |
| 71 | + private fun toggleVoLTEStatus() { |
| 72 | + val moder = this.moder ?: return |
| 73 | + val volteEnabled = this.volteEnabled ?: return |
| 74 | + moder.updateCarrierConfig(CarrierConfigManager.KEY_CARRIER_VOLTE_AVAILABLE_BOOL, !volteEnabled) |
| 75 | + moder.restartIMSRegistration() |
| 76 | + qsTile.state = if (volteEnabled) Tile.STATE_INACTIVE else Tile.STATE_ACTIVE |
| 77 | + qsTile.subtitle = getString(if (volteEnabled) R.string.disabled else R.string.enabled) |
| 78 | + qsTile.updateTile() |
| 79 | + } |
| 80 | + |
| 81 | + // Called when the user taps on your tile in an active or inactive state. |
| 82 | + override fun onClick() { |
| 83 | + super.onClick() |
| 84 | + if (isLocked) { |
| 85 | + unlockAndRun { toggleVoLTEStatus() } |
| 86 | + } else { |
| 87 | + toggleVoLTEStatus() |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments