package com.yidian.common.utils

import android.app.Activity
import android.content.Intent
import android.provider.Settings


/**
 * 系统设置工具类
 */
object SystemSettingUtils {

  /**
   * 打开通知设置
   */
  fun openNotifySetting(activity: Activity) {
    val applicationInfo = activity.applicationInfo
    val packageName = activity.applicationContext.packageName
    val intent = Intent()
    try {
      //8.0及以后版本使用这两个extra.  >=API 26
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
        intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
        intent.putExtra(Settings.EXTRA_CHANNEL_ID, applicationInfo.uid)
      }
      //5.0-7.1 使用这两个extra.  <= API 25, >=API 21
      intent.putExtra("app_package", packageName)
      intent.putExtra("app_uid", applicationInfo.uid)
      activity.startActivity(intent)
    } catch (e: Exception) {
      e.printStackTrace()
      //其他低版本或者异常情况，走该节点。进入APP设置界面
      intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
      intent.putExtra("package", packageName)

      //val uri = Uri.fromParts("package", packageName, null)
      //intent.data = uri
      activity.startActivity(intent)
    }
  }
}
