83 lines
2.9 KiB
Kotlin
83 lines
2.9 KiB
Kotlin
package com.faraphel.tasks_valider
|
|
|
|
import android.Manifest
|
|
import android.annotation.SuppressLint
|
|
import android.content.Context
|
|
import android.content.pm.PackageManager
|
|
import android.net.wifi.p2p.WifiP2pManager
|
|
import android.os.Build
|
|
import android.os.Bundle
|
|
import android.util.Log
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.annotation.RequiresApi
|
|
import com.faraphel.tasks_valider.connectivity.bwf.BwfManager
|
|
import com.faraphel.tasks_valider.connectivity.bwf.error.BwfNotSupportedException
|
|
import com.faraphel.tasks_valider.database.Database
|
|
import com.faraphel.tasks_valider.ui.screen.room.RoomScreen
|
|
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
private var bwfManager: BwfManager? = null ///< the WiFi-Direct helper
|
|
|
|
companion object {
|
|
private lateinit var database: Database ///< the database manager
|
|
}
|
|
|
|
@RequiresApi(Build.VERSION_CODES.O)
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
|
|
// TODO(Faraphel): more check on permissions
|
|
|
|
// check if the system support WiFi-Direct
|
|
if (!this.packageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)) {
|
|
Log.e("wifi-p2p", "this device does not support the WiFi-Direct feature")
|
|
throw BwfNotSupportedException()
|
|
}
|
|
|
|
if (
|
|
this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
|
|
this.checkSelfPermission(Manifest.permission.NEARBY_WIFI_DEVICES) != PackageManager.PERMISSION_GRANTED
|
|
) {
|
|
// TODO(Faraphel): should be used with shouldShowRequestPermissionRationale, with a check
|
|
this.requestPermissions(
|
|
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
|
|
BwfManager.PERMISSION_ACCESS_FINE_LOCATION
|
|
)
|
|
}
|
|
|
|
// get the WiFi-Direct manager
|
|
val manager = this.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager?
|
|
if (manager == null) {
|
|
Log.e("wifi-p2p", "cannot access the WiFi-Direct manager")
|
|
throw BwfNotSupportedException()
|
|
}
|
|
|
|
// create a channel for the manager
|
|
val channel = manager.initialize(this, this.mainLooper, null)
|
|
|
|
// create a new manager for handling the WiFi-Direct
|
|
this.bwfManager = BwfManager(manager, channel)
|
|
|
|
// open the room selection screen
|
|
this.setContent {
|
|
RoomScreen(this.bwfManager!!)
|
|
}
|
|
}
|
|
|
|
@SuppressLint("UnspecifiedRegisterReceiverFlag")
|
|
override fun onResume() {
|
|
super.onResume()
|
|
|
|
// enable the WiFi-Direct events
|
|
this.registerReceiver(this.bwfManager, BwfManager.ALL_INTENT_FILTER)
|
|
}
|
|
|
|
override fun onPause() {
|
|
super.onPause()
|
|
|
|
// disable the WiFi-Direct events
|
|
this.unregisterReceiver(this.bwfManager)
|
|
}
|
|
}
|