Skip to content

Commit

Permalink
Merge pull request #236 from myofficework000/broadcast_receivers
Browse files Browse the repository at this point in the history
Added two new broadcast receivers
  • Loading branch information
myofficework000 authored Feb 1, 2024
2 parents 1151f2b + 5c585c7 commit e35ee1f
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission
android:name="android.permission.READ_MEDIA_IMAGES"
android:minSdkVersion="33" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.content.IntentFilter
import android.net.wifi.WifiManager
import android.provider.Telephony
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
Expand All @@ -19,7 +21,9 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import com.example.jetpack_compose_all_in_one.R
import com.example.jetpack_compose_all_in_one.application_components.broadcastreceiver.airplanemode.AirplaneModeReceiver
import com.example.jetpack_compose_all_in_one.application_components.broadcastreceiver.batterybroadcast.BatteryBroadcast
import com.example.jetpack_compose_all_in_one.application_components.broadcastreceiver.bluetooth.BluetoothReceiver
import com.example.jetpack_compose_all_in_one.application_components.broadcastreceiver.smsrecevier.SmsBroadcast
import com.example.jetpack_compose_all_in_one.application_components.broadcastreceiver.wifi.WifiReceiver
import com.example.jetpack_compose_all_in_one.ui.components.LessonHeader
import com.example.jetpack_compose_all_in_one.ui.theme.dp_15
Expand All @@ -30,7 +34,7 @@ private fun BroadCastReceiverContent() {
val currentPage = rememberSaveable { mutableStateOf(0) }

LogicPager(
pageCount = 3,
pageCount = 5,
currentPage = currentPage
) {
Column(
Expand All @@ -50,6 +54,8 @@ private fun BroadCastReceiverContent() {
0 -> BluetoothComponent()
1 -> AirplaneModeComponent()
2 -> WifiComponent()
3 -> SmsReceivedComponent()
4 -> BatteryReceivedComponent()
}
}
}
Expand Down Expand Up @@ -108,4 +114,34 @@ fun AirplaneModeComponent() {
}
}

}

@Composable
fun SmsReceivedComponent() {
val context = LocalContext.current

DisposableEffect(Unit) {
val receiver = SmsBroadcast()
val intentFilter = IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)
context.registerReceiver(receiver, intentFilter)

onDispose {
context.unregisterReceiver(receiver)
}
}
}

@Composable
fun BatteryReceivedComponent() {
val context = LocalContext.current

DisposableEffect(Unit) {
val receiver = BatteryBroadcast()
val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
context.registerReceiver(receiver, intentFilter)

onDispose {
context.unregisterReceiver(receiver)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.jetpack_compose_all_in_one.application_components.broadcastreceiver.batterybroadcast

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.BatteryManager
import android.util.Log

class BatteryBroadcast: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val level = intent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: 1
val scale = intent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: 1
val batteryPercentage = level * 100 / scale.toFloat()

Log.i("Battery Broadcast", "Your battery is at: $batteryPercentage %")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.jetpack_compose_all_in_one.application_components.broadcastreceiver.smsrecevier

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.provider.Telephony
import android.util.Log

class SmsBroadcast: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == Telephony.Sms.Intents.SMS_RECEIVED_ACTION) {
Log.i("SMS Broadcast","You have received a SMS message")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ class MainActivity : ComponentActivity() {
private lateinit var fusedLocationClient: FusedLocationProviderClient
private val locationCancelToken by lazy{ CancellationTokenSource() }



@OptIn(ExperimentalAnimationApi::class)
@SuppressLint("MissingPermission") // isLocationAllowed() checked this, but Lint doesn't know.
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.content.pm.PackageManager
import android.os.Build
import androidx.compose.runtime.Composable
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityCompat.requestPermissions
import androidx.core.content.ContextCompat.checkSelfPermission
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import com.google.accompanist.permissions.rememberPermissionState
Expand Down Expand Up @@ -65,4 +67,14 @@ fun requestCamera(isGranted: (Boolean) -> Unit) = rememberPermissionState(
@Composable
fun requestReadContacts(isGranted: (Boolean) -> Unit) = rememberPermissionState(
Manifest.permission.READ_CONTACTS
) { isGranted(it) }

/*
implementation to check if sms permission is enabled so
broadcast can notify user if they received SMS
*/
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun isSmsPermissionEnabled(isGranted: (Boolean) -> Unit) = rememberPermissionState(
Manifest.permission.RECEIVE_SMS
) { isGranted(it) }
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@
<item>Broadcast Bluetooth</item>
<item>Broadcast Airplane</item>
<item>Broadcast Wifi</item>
<item>Broadcast Sms</item>
<item>Broadcast Battery</item>
</string-array>

<string-array name="content_provider_header_text">
Expand Down

0 comments on commit e35ee1f

Please sign in to comment.