package com.zhaohaoting.framework.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationManagerCompat;

/**
 * author：Created by zhaohaoting on 2019/8/30
 * email：526309416@qq.com
 * desc：
 */
public class NotifyHelper {

    /**
     * 跳转系统通知界面
     *
     * @param context
     */
    public static void toSystemNotify(Activity context) {
        try {
            // vivo 点击设置图标>加速白名单>我的app
            //      点击软件管理>软件管理权限>软件>我的app>信任该软件
//            Intent appIntent = context.getPackageManager().getLaunchIntentForPackage("com.iqoo.secure");
//            if(appIntent != null){
//                context.startActivity(appIntent);
//                return;
//            }
//
//            // oppo 点击设置图标>应用权限管理>按应用程序管理>我的app>我信任该应用
//            //      点击权限隐私>自启动管理>我的app
//            appIntent = context.getPackageManager().getLaunchIntentForPackage("com.oppo.safe");
//            if(appIntent != null){
//                context.startActivity(appIntent);
//                return;
//            }

            Intent intent = new Intent();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                intent.putExtra("app_package", context.getPackageName());
                intent.putExtra("app_uid", context.getApplicationInfo().uid);
            } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD){
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.fromParts("package", context.getPackageName(), null));
            } else {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(Intent.ACTION_VIEW);
                intent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
                intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
            }
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package", context.getPackageName(), null);
            intent.setData(uri);
            context.startActivity(intent);
        }
    }

    /**
     * 检查是否存在通知权限
     *
     * @param activity
     * @return Boolean
     */
    public static boolean checkNotify(Activity activity) {
        NotificationManagerCompat manager = NotificationManagerCompat.from(activity);
        // areNotificationsEnabled方法的有效性官方只最低支持到API 19，
        // 低于19的仍可调用此方法不过只会返回true，即默认为用户已经开启了通知。
        return manager.areNotificationsEnabled();
    }
}