0% found this document useful (0 votes)
7 views3 pages

Hgidi

The document is an Android application code that manages connected devices on a Wi-Fi network. It allows users to scan for devices, set time limits for their usage, and notifies when the time limit expires. The app uses a timer to periodically check and enforce these time limits.

Uploaded by

lynnzayar173
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Hgidi

The document is an Android application code that manages connected devices on a Wi-Fi network. It allows users to scan for devices, set time limits for their usage, and notifies when the time limit expires. The app uses a timer to periodically check and enforce these time limits.

Uploaded by

lynnzayar173
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import android.app.

Activity
import android.app.AlertDialog
import android.content.Context
import android.net.wifi.WifiManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import java.util.Timer
import java.util.TimerTask

class MainActivity : Activity() {

private lateinit var wifiManager: WifiManager


private lateinit var deviceListLayout: LinearLayout
private lateinit var scanButton: Button
private val deviceTimeLimits = mutableMapOf<String, Long>()
private val handler = Handler(Looper.getMainLooper())

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as
WifiManager
deviceListLayout = findViewById(R.id.deviceListLayout)
scanButton = findViewById(R.id.scanButton)

scanButton.setOnClickListener {
scanDevices()
}

// Start a timer to check and enforce time limits


Timer().scheduleAtFixedRate(object : TimerTask() {
override fun run() {
checkTimeLimits()
}
}, 0, 60000) // Check every minute
}

private fun scanDevices() {


deviceListLayout.removeAllViews()
val connectedDevices = getConnectedDevices()

if (connectedDevices.isEmpty()) {
Toast.makeText(this, "No devices found", Toast.LENGTH_SHORT).show()
return
}

for (device in connectedDevices) {


addDeviceToUI(device)
}
}
private fun getConnectedDevices(): List<String> {
// Placeholder: Replace with actual logic to scan devices on the network
return listOf("Device 1 (192.168.1.101)", "Device 2 (192.168.1.102)",
"Device 3 (192.168.1.103)")
}

private fun addDeviceToUI(device: String) {


val deviceView = layoutInflater.inflate(R.layout.item_device, null)
val deviceName = deviceView.findViewById<TextView>(R.id.deviceName)
val setLimitButton = deviceView.findViewById<Button>(R.id.setLimitButton)

deviceName.text = device
setLimitButton.setOnClickListener {
showTimeLimitDialog(device)
}

deviceListLayout.addView(deviceView)
}

private fun showTimeLimitDialog(device: String) {


val dialogView = layoutInflater.inflate(R.layout.dialog_time_limit, null)
val timeLimitInput = dialogView.findViewById<EditText>(R.id.timeLimitInput)
val applyButton = dialogView.findViewById<Button>(R.id.applyButton)

val dialog = AlertDialog.Builder(this)


.setTitle("Set Time Limit for $device")
.setView(dialogView)
.create()

applyButton.setOnClickListener {
val timeLimit = timeLimitInput.text.toString().toIntOrNull()
if (timeLimit != null && timeLimit > 0) {
setTimeLimit(device, timeLimit)
dialog.dismiss()
} else {
Toast.makeText(this, "Invalid time limit",
Toast.LENGTH_SHORT).show()
}
}

dialog.show()
}

private fun setTimeLimit(device: String, minutes: Int) {


val expiryTime = System.currentTimeMillis() + (minutes * 60 * 1000)
deviceTimeLimits[device] = expiryTime
Toast.makeText(this, "Time limit set for $device",
Toast.LENGTH_SHORT).show()
}

private fun checkTimeLimits() {


val currentTime = System.currentTimeMillis()
for ((device, expiryTime) in deviceTimeLimits) {
if (currentTime >= expiryTime) {
handler.post {
Toast.makeText(this, "Time limit expired for $device",
Toast.LENGTH_SHORT).show()
}
deviceTimeLimits.remove(device)
}
}
}
}

You might also like