package com.ydl.audioim.notification

import android.app.*
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import com.ydl.audioim.R

class FloatService : Service() {

    var notificationManager: NotificationManager? = null;
    var notificationId = "audio_av";
    var notificationName = "正在通话中";

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //创建NotificationChannel
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            var channel = NotificationChannel(
                notificationId,
                notificationName,
                NotificationManager.IMPORTANCE_HIGH
            );
            //不震动
            channel.enableVibration(false);
            //静音
            channel.setSound(null, null);
            notificationManager?.createNotificationChannel(channel);
        }
        //创建服务后,五秒内调用该方法
        startForeground(1, getNotification());

    }

    /**
     * 获取通知(Android8.0后需要)
     * @return
     */
    fun getNotification(): Notification {
        var builder = Notification.Builder(this)
            .setSmallIcon(R.drawable.dialog_list_bg)
            .setContentTitle("壹点灵")
            .setContentIntent(getIntent())
            .setContentText("正在通话中");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(notificationId);
        }
        return builder.build();
    }

    /**
     * 点击后,直接打开app(之前的页面),不跳转特定activity
     * @return
     */
    fun getIntent(): PendingIntent {
        var msgIntent =
            applicationContext.packageManager.getLaunchIntentForPackage(packageName);//获取启动Activity
        var pendingIntent = PendingIntent.getActivity(
            applicationContext,
            1,
            msgIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
        return pendingIntent;
    }
}