package com.xty.common.services

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.widget.RemoteViews
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.common.R
import com.xty.common.arouter.ARouterUrl

/**
 * 空服务
 * 只为了让应用有个前台服务存在  让手机在杀掉进程的时候 降低被杀几率
 */

@Route(path = ARouterUrl.NONE_SERVICE)
class NoneService : Service(), OrderProiver {
     var    noficationManager:NotificationManager?=null
    var builder : Notification.Builder?=null
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate() {
        super.onCreate()
        startForeground(2, getNotification())
        //var notificaManager=getSystemService(NOTIFICATION_SERVICE) as NotificationManager


    }

    @RequiresApi(Build.VERSION_CODES.N)
    @SuppressLint("RemoteViewLayout")
    private fun getNotification(): Notification {
        var channelId = "health_user"
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             builder= Notification.Builder(this, channelId)
            noficationManager = getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager
            //Android8.0开始必须给每个通知分配对应的渠道
            var notificationChannel= NotificationChannel(channelId,channelId,NotificationManager.IMPORTANCE_LOW)
            notificationChannel.enableLights(false)
            notificationChannel.setShowBadge(false)
            noficationManager?.createNotificationChannel(notificationChannel)
        } else {
             builder= Notification.Builder(this)
        }

       var notification = builder!!
            .setSmallIcon(R.mipmap.ic_logo)
            .setAutoCancel(false)
           .setContent(RemoteViews(packageName,R.layout.service_none))
            .build()
        notification!!.flags = Notification.FLAG_ONGOING_EVENT //常驻通知栏


        return notification

    }

    override fun init(context: Context?) {

    }

    override fun onDestroy() {
        super.onDestroy()
        stopForeground(true)
    }

}