package com.huawei.hwscreenrecorddemo;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.RemoteViews;

/**
 * 消息栏通知服务，一定要开启这个服务，不然，在Android9.0 后台录音只能录制1分钟。
 */
public class NotificationService extends Service {
    private static final String TAG = "NotificationService";
    private static NotificationManager notificationManager;
    private static Notification notification = null;
    //通知的唯一标识号。
    private int NOTIFICATION = R.string.notification_live_start;

    private static RemoteViews mRemoteViews;

    @Override
    public void onCreate() {
        super.onCreate();
        //消息栏显示通知
        notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        showNotification();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    public static RemoteViews getmRemoteViews() {
        return mRemoteViews;
    }

    public static NotificationManager getNotificationManager() {
        return notificationManager;
    }

    public static Notification getNotification() {
        return notification;
    }

    private void showNotification(){
        // PendingIntent如果用户选择此通知，则启动我们的活动
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this,NotificationService.class),0);

        mRemoteViews = new RemoteViews(getPackageName(), R.layout.record_custom_notification_layout);
        //停止
        PendingIntent stopIntent = PendingIntent.getBroadcast(this,1,new Intent("stop"),PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.stop, stopIntent);
        //暂停
        PendingIntent pauseIntent = PendingIntent.getBroadcast(this,1,new Intent("pause"),PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.pause, pauseIntent);
        //重新播放
        PendingIntent resumeIntent = PendingIntent.getBroadcast(this,1,new Intent("resume"),PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.resume, resumeIntent);
        //返回主页
        PendingIntent homeIntent = PendingIntent.getBroadcast(this,1,new Intent("come_home"),PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.home, homeIntent);


        //设置通知面板中显示的视图的信息。
//        Notification notification = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            notification = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setTicker("正在运行")
                    .setCustomContentView(mRemoteViews)
                    .setContentTitle(getText(R.string.notification_live_start))
                    .setContentTitle("正在运行")
                    .setContentIntent(pendingIntent)
                    .build();
        }
//        notification.contentView.setViewVisibility(R.id.home,View.GONE);
        //发送通知
        notificationManager.notify(NOTIFICATION,notification);
        //前台服务，只有这样，录音才能在后台不间断。
        startForeground(R.string.notification_live_start,notification);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onDestroy() {
        //取消通知
        super.onDestroy();
        notificationManager.cancel(NOTIFICATION);
    }
}
