forked from mroczis/netmonster-core
-
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.
Merge pull request #6 from mroczis/master
sync
- Loading branch information
Showing
30 changed files
with
329 additions
and
104 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Sat Jul 04 18:21:51 CEST 2020 | ||
#Wed Feb 17 22:16:22 CET 2021 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip |
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
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
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
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
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
85 changes: 85 additions & 0 deletions
85
library/src/main/java/cz/mroczis/netmonster/core/feature/config/DisplayInfoSource.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,85 @@ | ||
package cz.mroczis.netmonster.core.feature.config | ||
|
||
import android.Manifest | ||
import android.annotation.TargetApi | ||
import android.os.Build | ||
import android.telephony.PhoneStateListener | ||
import android.telephony.TelephonyDisplayInfo | ||
import android.telephony.TelephonyManager | ||
import androidx.annotation.RequiresPermission | ||
import cz.mroczis.netmonster.core.model.DisplayInfo | ||
import cz.mroczis.netmonster.core.telephony.mapper.toDisplayInfo | ||
import cz.mroczis.netmonster.core.util.PhoneStateListenerPort | ||
import cz.mroczis.netmonster.core.util.Threads | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* Attempts to fetch fresh [TelephonyDisplayInfo] using [DisplayInfoSource]. | ||
*/ | ||
@TargetApi(Build.VERSION_CODES.R) | ||
class DisplayInfoSource { | ||
|
||
/** | ||
* Registers [DisplayInfoListener] and awaits data. After 100 milliseconds time outs if | ||
* nothing is delivered. | ||
*/ | ||
@RequiresPermission(Manifest.permission.READ_PHONE_STATE) | ||
@Suppress("DEPRECATION") | ||
fun get(telephonyManager: TelephonyManager, subId: Int?): DisplayInfo = | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
getFresh(telephonyManager, subId)?.toDisplayInfo() | ||
} else { | ||
null | ||
} ?: DisplayInfo() | ||
|
||
@RequiresPermission(Manifest.permission.READ_PHONE_STATE) | ||
private fun getFresh(telephonyManager: TelephonyManager, subId: Int?): TelephonyDisplayInfo? { | ||
var listener: DisplayInfoListener? = null | ||
val asyncLock = CountDownLatch(1) | ||
var displayInfo: TelephonyDisplayInfo? = null | ||
|
||
Threads.phoneStateListener.post { | ||
// We'll receive callbacks on thread that created instance of [listener] by default. | ||
// Async processing is required otherwise deadlock would arise cause we block | ||
// original thread | ||
listener = DisplayInfoListener(subId) { | ||
telephonyManager.listen(this, PhoneStateListener.LISTEN_NONE) | ||
displayInfo = it | ||
asyncLock.countDown() | ||
} | ||
|
||
telephonyManager.listen(listener, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED) | ||
} | ||
|
||
// And we also must block original thread | ||
// It'll get unblocked once we receive required data | ||
// This usually takes +/- 20 ms to complete | ||
try { | ||
asyncLock.await(100, TimeUnit.MILLISECONDS) | ||
} catch (e: InterruptedException) { | ||
// System was not able to deliver PhysicalChannelConfig in this time slot | ||
} | ||
|
||
listener?.let { telephonyManager.listen(it, PhoneStateListener.LISTEN_NONE) } | ||
|
||
return displayInfo | ||
} | ||
|
||
|
||
/** | ||
* Kotlin friendly PhoneStateListener that grabs [TelephonyDisplayInfo] | ||
*/ | ||
@TargetApi(Build.VERSION_CODES.R) | ||
private class DisplayInfoListener( | ||
subId: Int?, | ||
private val displayInfoListener: DisplayInfoListener.(info: TelephonyDisplayInfo) -> Unit | ||
) : PhoneStateListenerPort(subId) { | ||
|
||
@RequiresPermission(Manifest.permission.READ_PHONE_STATE) | ||
override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) { | ||
super.onDisplayInfoChanged(telephonyDisplayInfo) | ||
displayInfoListener.invoke(this, telephonyDisplayInfo) | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.