|
| 1 | +package ai.openclaw.app.node |
| 2 | + |
| 3 | +import android.Manifest |
| 4 | +import android.content.Context |
| 5 | +import android.provider.CallLog |
| 6 | +import androidx.core.content.ContextCompat |
| 7 | +import ai.openclaw.app.gateway.GatewaySession |
| 8 | +import kotlinx.serialization.json.Json |
| 9 | +import kotlinx.serialization.json.JsonArray |
| 10 | +import kotlinx.serialization.json.JsonObject |
| 11 | +import kotlinx.serialization.json.JsonPrimitive |
| 12 | +import kotlinx.serialization.json.buildJsonObject |
| 13 | +import kotlinx.serialization.json.buildJsonArray |
| 14 | +import kotlinx.serialization.json.put |
| 15 | + |
| 16 | +private const val DEFAULT_CALL_LOG_LIMIT = 25 |
| 17 | + |
| 18 | +internal data class CallLogRecord( |
| 19 | + val number: String?, |
| 20 | + val cachedName: String?, |
| 21 | + val date: Long, |
| 22 | + val duration: Long, |
| 23 | + val type: Int, |
| 24 | +) |
| 25 | + |
| 26 | +internal data class CallLogSearchRequest( |
| 27 | + val limit: Int, // Number of records to return |
| 28 | + val offset: Int, // Offset value |
| 29 | + val cachedName: String?, // Search by contact name |
| 30 | + val number: String?, // Search by phone number |
| 31 | + val date: Long?, // Search by time (timestamp, deprecated, use dateStart/dateEnd) |
| 32 | + val dateStart: Long?, // Query start time (timestamp) |
| 33 | + val dateEnd: Long?, // Query end time (timestamp) |
| 34 | + val duration: Long?, // Search by duration (seconds) |
| 35 | + val type: Int?, // Search by call log type |
| 36 | +) |
| 37 | + |
| 38 | +internal interface CallLogDataSource { |
| 39 | + fun hasReadPermission(context: Context): Boolean |
| 40 | + |
| 41 | + fun search(context: Context, request: CallLogSearchRequest): List<CallLogRecord> |
| 42 | +} |
| 43 | + |
| 44 | +private object SystemCallLogDataSource : CallLogDataSource { |
| 45 | + override fun hasReadPermission(context: Context): Boolean { |
| 46 | + return ContextCompat.checkSelfPermission( |
| 47 | + context, |
| 48 | + Manifest.permission.READ_CALL_LOG |
| 49 | + ) == android.content.pm.PackageManager.PERMISSION_GRANTED |
| 50 | + } |
| 51 | + |
| 52 | + override fun search(context: Context, request: CallLogSearchRequest): List<CallLogRecord> { |
| 53 | + val resolver = context.contentResolver |
| 54 | + val projection = arrayOf( |
| 55 | + CallLog.Calls.NUMBER, |
| 56 | + CallLog.Calls.CACHED_NAME, |
| 57 | + CallLog.Calls.DATE, |
| 58 | + CallLog.Calls.DURATION, |
| 59 | + CallLog.Calls.TYPE, |
| 60 | + ) |
| 61 | + |
| 62 | + // Build selection and selectionArgs for filtering |
| 63 | + val selections = mutableListOf<String>() |
| 64 | + val selectionArgs = mutableListOf<String>() |
| 65 | + |
| 66 | + request.cachedName?.let { |
| 67 | + selections.add("${CallLog.Calls.CACHED_NAME} LIKE ?") |
| 68 | + selectionArgs.add("%$it%") |
| 69 | + } |
| 70 | + |
| 71 | + request.number?.let { |
| 72 | + selections.add("${CallLog.Calls.NUMBER} LIKE ?") |
| 73 | + selectionArgs.add("%$it%") |
| 74 | + } |
| 75 | + |
| 76 | + // Support time range query |
| 77 | + if (request.dateStart != null && request.dateEnd != null) { |
| 78 | + selections.add("${CallLog.Calls.DATE} >= ? AND ${CallLog.Calls.DATE} <= ?") |
| 79 | + selectionArgs.add(request.dateStart.toString()) |
| 80 | + selectionArgs.add(request.dateEnd.toString()) |
| 81 | + } else if (request.dateStart != null) { |
| 82 | + selections.add("${CallLog.Calls.DATE} >= ?") |
| 83 | + selectionArgs.add(request.dateStart.toString()) |
| 84 | + } else if (request.dateEnd != null) { |
| 85 | + selections.add("${CallLog.Calls.DATE} <= ?") |
| 86 | + selectionArgs.add(request.dateEnd.toString()) |
| 87 | + } else if (request.date != null) { |
| 88 | + // Compatible with the old date parameter (exact match) |
| 89 | + selections.add("${CallLog.Calls.DATE} = ?") |
| 90 | + selectionArgs.add(request.date.toString()) |
| 91 | + } |
| 92 | + |
| 93 | + request.duration?.let { |
| 94 | + selections.add("${CallLog.Calls.DURATION} = ?") |
| 95 | + selectionArgs.add(it.toString()) |
| 96 | + } |
| 97 | + |
| 98 | + request.type?.let { |
| 99 | + selections.add("${CallLog.Calls.TYPE} = ?") |
| 100 | + selectionArgs.add(it.toString()) |
| 101 | + } |
| 102 | + |
| 103 | + val selection = if (selections.isNotEmpty()) selections.joinToString(" AND ") else null |
| 104 | + val selectionArgsArray = if (selectionArgs.isNotEmpty()) selectionArgs.toTypedArray() else null |
| 105 | + |
| 106 | + val sortOrder = "${CallLog.Calls.DATE} DESC" |
| 107 | + |
| 108 | + resolver.query( |
| 109 | + CallLog.Calls.CONTENT_URI, |
| 110 | + projection, |
| 111 | + selection, |
| 112 | + selectionArgsArray, |
| 113 | + sortOrder, |
| 114 | + ).use { cursor -> |
| 115 | + if (cursor == null) return emptyList() |
| 116 | + |
| 117 | + val numberIndex = cursor.getColumnIndex(CallLog.Calls.NUMBER) |
| 118 | + val cachedNameIndex = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME) |
| 119 | + val dateIndex = cursor.getColumnIndex(CallLog.Calls.DATE) |
| 120 | + val durationIndex = cursor.getColumnIndex(CallLog.Calls.DURATION) |
| 121 | + val typeIndex = cursor.getColumnIndex(CallLog.Calls.TYPE) |
| 122 | + |
| 123 | + // Skip offset rows |
| 124 | + if (request.offset > 0 && cursor.moveToPosition(request.offset - 1)) { |
| 125 | + // Successfully moved to offset position |
| 126 | + } |
| 127 | + |
| 128 | + val out = mutableListOf<CallLogRecord>() |
| 129 | + var count = 0 |
| 130 | + while (cursor.moveToNext() && count < request.limit) { |
| 131 | + out += CallLogRecord( |
| 132 | + number = cursor.getString(numberIndex), |
| 133 | + cachedName = cursor.getString(cachedNameIndex), |
| 134 | + date = cursor.getLong(dateIndex), |
| 135 | + duration = cursor.getLong(durationIndex), |
| 136 | + type = cursor.getInt(typeIndex), |
| 137 | + ) |
| 138 | + count++ |
| 139 | + } |
| 140 | + return out |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +class CallLogHandler private constructor( |
| 146 | + private val appContext: Context, |
| 147 | + private val dataSource: CallLogDataSource, |
| 148 | +) { |
| 149 | + constructor(appContext: Context) : this(appContext = appContext, dataSource = SystemCallLogDataSource) |
| 150 | + |
| 151 | + fun handleCallLogSearch(paramsJson: String?): GatewaySession.InvokeResult { |
| 152 | + if (!dataSource.hasReadPermission(appContext)) { |
| 153 | + return GatewaySession.InvokeResult.error( |
| 154 | + code = "CALL_LOG_PERMISSION_REQUIRED", |
| 155 | + message = "CALL_LOG_PERMISSION_REQUIRED: grant Call Log permission", |
| 156 | + ) |
| 157 | + } |
| 158 | + |
| 159 | + val request = parseSearchRequest(paramsJson) |
| 160 | + ?: return GatewaySession.InvokeResult.error( |
| 161 | + code = "INVALID_REQUEST", |
| 162 | + message = "INVALID_REQUEST: expected JSON object", |
| 163 | + ) |
| 164 | + |
| 165 | + return try { |
| 166 | + val callLogs = dataSource.search(appContext, request) |
| 167 | + GatewaySession.InvokeResult.ok( |
| 168 | + buildJsonObject { |
| 169 | + put( |
| 170 | + "callLogs", |
| 171 | + buildJsonArray { |
| 172 | + callLogs.forEach { add(callLogJson(it)) } |
| 173 | + }, |
| 174 | + ) |
| 175 | + }.toString(), |
| 176 | + ) |
| 177 | + } catch (err: Throwable) { |
| 178 | + GatewaySession.InvokeResult.error( |
| 179 | + code = "CALL_LOG_UNAVAILABLE", |
| 180 | + message = "CALL_LOG_UNAVAILABLE: ${err.message ?: "call log query failed"}", |
| 181 | + ) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private fun parseSearchRequest(paramsJson: String?): CallLogSearchRequest? { |
| 186 | + if (paramsJson.isNullOrBlank()) { |
| 187 | + return CallLogSearchRequest( |
| 188 | + limit = DEFAULT_CALL_LOG_LIMIT, |
| 189 | + offset = 0, |
| 190 | + cachedName = null, |
| 191 | + number = null, |
| 192 | + date = null, |
| 193 | + dateStart = null, |
| 194 | + dateEnd = null, |
| 195 | + duration = null, |
| 196 | + type = null, |
| 197 | + ) |
| 198 | + } |
| 199 | + |
| 200 | + val params = try { |
| 201 | + Json.parseToJsonElement(paramsJson).asObjectOrNull() |
| 202 | + } catch (_: Throwable) { |
| 203 | + null |
| 204 | + } ?: return null |
| 205 | + |
| 206 | + val limit = ((params["limit"] as? JsonPrimitive)?.content?.toIntOrNull() ?: DEFAULT_CALL_LOG_LIMIT) |
| 207 | + .coerceIn(1, 200) |
| 208 | + val offset = ((params["offset"] as? JsonPrimitive)?.content?.toIntOrNull() ?: 0) |
| 209 | + .coerceAtLeast(0) |
| 210 | + val cachedName = (params["cachedName"] as? JsonPrimitive)?.content?.takeIf { it.isNotBlank() } |
| 211 | + val number = (params["number"] as? JsonPrimitive)?.content?.takeIf { it.isNotBlank() } |
| 212 | + val date = (params["date"] as? JsonPrimitive)?.content?.toLongOrNull() |
| 213 | + val dateStart = (params["dateStart"] as? JsonPrimitive)?.content?.toLongOrNull() |
| 214 | + val dateEnd = (params["dateEnd"] as? JsonPrimitive)?.content?.toLongOrNull() |
| 215 | + val duration = (params["duration"] as? JsonPrimitive)?.content?.toLongOrNull() |
| 216 | + val type = (params["type"] as? JsonPrimitive)?.content?.toIntOrNull() |
| 217 | + |
| 218 | + return CallLogSearchRequest( |
| 219 | + limit = limit, |
| 220 | + offset = offset, |
| 221 | + cachedName = cachedName, |
| 222 | + number = number, |
| 223 | + date = date, |
| 224 | + dateStart = dateStart, |
| 225 | + dateEnd = dateEnd, |
| 226 | + duration = duration, |
| 227 | + type = type, |
| 228 | + ) |
| 229 | + } |
| 230 | + |
| 231 | + private fun callLogJson(callLog: CallLogRecord): JsonObject { |
| 232 | + return buildJsonObject { |
| 233 | + put("number", JsonPrimitive(callLog.number)) |
| 234 | + put("cachedName", JsonPrimitive(callLog.cachedName)) |
| 235 | + put("date", JsonPrimitive(callLog.date)) |
| 236 | + put("duration", JsonPrimitive(callLog.duration)) |
| 237 | + put("type", JsonPrimitive(callLog.type)) |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + companion object { |
| 242 | + internal fun forTesting( |
| 243 | + appContext: Context, |
| 244 | + dataSource: CallLogDataSource, |
| 245 | + ): CallLogHandler = CallLogHandler(appContext = appContext, dataSource = dataSource) |
| 246 | + } |
| 247 | +} |
0 commit comments