package com.example.util import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import android.media.AudioAttributes import android.media.AudioManager import android.media.RingtoneManager import android.media.ToneGenerator import android.os.Build import androidx.core.app.NotificationCompat import com.example.R object OrderSoundHelper { /** * Plays a loud order alert sound (ringtone + tone generator) * and displays an Android system notification for new orders. */ fun triggerNewOrderAlert(context: Context, orderId: String, customerName: String, amount: Double) { val appContext = context.applicationContext // 1. Play Ringtone Sound try { val notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) ?: RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE) val ringtone = RingtoneManager.getRingtone(appContext, notificationUri) ringtone?.audioAttributes = AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .build() ringtone?.play() } catch (e: Exception) { e.printStackTrace() } // 2. Play Loud Beep Tone as fallback audio alert try { val toneGen = ToneGenerator(AudioManager.STREAM_NOTIFICATION, 80) toneGen.startTone(ToneGenerator.TONE_PROP_BEEP, 500) // Release native ToneGenerator resources cleanly after tone finishes android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({ try { toneGen.stopTone() toneGen.release() } catch (ignored: Exception) { } }, 800) } catch (e: Exception) { e.printStackTrace() } // 3. Trigger Android System Notification try { val channelId = "love_zone_order_alerts" val notificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( channelId, "Love Zone Order Alerts", NotificationManager.IMPORTANCE_HIGH ).apply { description = "High priority sound alerts for new restaurant orders" enableVibration(true) vibrationPattern = longArrayOf(0, 500, 200, 500) } notificationManager.createNotificationChannel(channel) } val title = "🚨 NAYA ORDER AAYA HAI! #$orderId" val message = "$customerName se ₹${amount.toInt()} ka naya order receive hua hai." val builder = NotificationCompat.Builder(appContext, channelId) .setSmallIcon(R.drawable.ic_launcher_foreground) .setContentTitle(title) .setContentText(message) .setPriority(NotificationCompat.PRIORITY_HIGH) .setAutoCancel(true) .setVibrate(longArrayOf(0, 500, 200, 500)) notificationManager.notify((System.currentTimeMillis() % 10000).toInt(), builder.build()) } catch (e: Exception) { e.printStackTrace() } } }