// 5G を検出して接続タイプを特定する 【NR NSA/SA/MMWAVE】
// https://android.benigumo.com/20210407/5g/

@ExperimentalCoroutinesApi
internal suspend inline fun TelephonyManager.fiveGInfo() = callbackFlow<Type> {
  val callback = object : PhoneStateListener() {
    override fun onDisplayInfoChanged(info: TelephonyDisplayInfo) {
      try {
        super.onDisplayInfoChanged(info)
        val type = Type(dataNetworkType, info.networkType, info.overrideNetworkType)
        offer(type)
      } catch (e: SecurityException) {
        Log.e("", "Permission not required.", e)
      }
    }
  }

  listen(callback, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)

  awaitClose {
    listen(callback, PhoneStateListener.LISTEN_NONE)
  }
}

/*
lifecycleScope.launch {
  getSystemService<TelephonyManager>()!!.fiveGInfo().collect { type ->
    updateView(type.toString)  
  }
}
*/