package cn.org.bjca.trust.push.manager

import android.annotation.SuppressLint
import android.content.Context
import androidx.core.net.toUri
import cn.org.bjca.trust.push.kit.QueryInterface
import cn.org.bjca.trust.push.provider.PushProvider

class QueryManager : QueryInterface {

    @SuppressLint("Range")
    override fun getDeviceId(context: Context): String? {
        var deviceId: String? = null
        val resolver = context.contentResolver
        resolver.query(
            "${PushProvider.contentUriStr}/device/all".toUri(),
            arrayOf("*"),
            null,
            null,
            "p_id asc"
        )
            ?.use { cursor ->
                cursor.moveToFirst()
                repeat(cursor.count) { // 这里用repeat来代替foreach，是因为循环中的 it 指代对象 在后续用不到
                    deviceId = cursor.getString(cursor.getColumnIndex("device_id"))
                    cursor.moveToNext()
                }
            }
        return deviceId
    }
}