android [experimental]: add xposed based hooking

This commit is contained in:
Kavish Devar
2025-03-27 00:01:43 +05:30
parent 13340485b1
commit a206e04ba2
20 changed files with 1732 additions and 49 deletions

View File

@@ -16,7 +16,11 @@ android {
versionCode = 3
versionName = "0.0.3"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags += ""
}
}
}
buildTypes {
@@ -39,6 +43,12 @@ android {
compose = true
viewBinding = true
}
externalNativeBuild {
cmake {
path = file("src/main/cpp/CMakeLists.txt")
version = "3.22.1"
}
}
}
dependencies {
@@ -57,11 +67,5 @@ dependencies {
implementation(libs.androidx.constraintlayout)
implementation(libs.haze)
implementation(libs.haze.materials)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
compileOnly(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar"))))
}

Binary file not shown.

View File

@@ -26,6 +26,7 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"

View File

@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.22.1)
project("l2c_fcr_hook")
set(CMAKE_CXX_STANDARD 23)
add_library(${CMAKE_PROJECT_NAME} SHARED
l2c_fcr_hook.cpp
l2c_fcr_hook.h)
target_link_libraries(${CMAKE_PROJECT_NAME}
android
log)

View File

@@ -0,0 +1,140 @@
/*
* AirPods like Normal (ALN) - Bringing Apple-only features to Linux and Android for seamless AirPods functionality!
*
* Copyright (C) 2024 Kavish Devar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdint>
#include <cstring>
#include <dlfcn.h>
#include <android/log.h>
#include <fstream>
#include <string>
#include <sys/system_properties.h>
#include "l2c_fcr_hook.h"
#define LOG_TAG "AirPodsHook"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
static HookFunType hook_func = nullptr;
static uint8_t (*original_l2c_fcr_chk_chan_modes)(void* p_ccb) = nullptr;
uint8_t fake_l2c_fcr_chk_chan_modes([[maybe_unused]] void* p_ccb) {
LOGI("l2c_fcr_chk_chan_modes hooked! Always returning true");
return 1;
}
uintptr_t loadHookOffset([[maybe_unused]] const char* package_name) {
const char* property_name = "persist.aln.hook_offset";
char value[PROP_VALUE_MAX] = {0};
int len = __system_property_get(property_name, value);
if (len > 0) {
LOGI("Read hook offset from property: %s", value);
uintptr_t offset = 0;
if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) {
sscanf(value + 2, "%x", &offset);
} else {
sscanf(value, "%x", &offset);
}
if (offset > 0) {
LOGI("Parsed offset: 0x%x", offset);
return offset;
}
}
LOGI("Failed to read offset from property, using hardcoded fallback");
return 0x00a55e30;
}
uintptr_t getModuleBase(const char *module_name) {
FILE *fp;
char line[1024];
uintptr_t base_addr = 0;
fp = fopen("/proc/self/maps", "r");
if (!fp) {
LOGE("Failed to open /proc/self/maps");
return 0;
}
while (fgets(line, sizeof(line), fp)) {
if (strstr(line, module_name)) {
char *start_addr_str = line;
char *end_addr_str = strchr(line, '-');
if (end_addr_str) {
*end_addr_str = '\0';
base_addr = strtoull(start_addr_str, nullptr, 16);
break;
}
}
}
fclose(fp);
return base_addr;
}
bool findAndHookFunction([[maybe_unused]] const char *library_path) {
if (!hook_func) {
LOGE("Hook function not initialized");
return false;
}
uintptr_t base_addr = getModuleBase("libbluetooth_jni.so");
if (!base_addr) {
LOGE("Failed to get base address of libbluetooth_jni.so");
return false;
}
uintptr_t offset = loadHookOffset(nullptr);
void* target = reinterpret_cast<void*>(base_addr + offset);
LOGI("Using offset: 0x%x, base: %p, target: %p", offset, (void*)base_addr, target);
int result = hook_func(target, (void*)fake_l2c_fcr_chk_chan_modes, (void**)&original_l2c_fcr_chk_chan_modes);
if (result == 0) {
LOGI("Successfully hooked l2c_fcr_chk_chan_modes");
return true;
} else {
LOGE("Failed to hook function, error: %d", result);
return false;
}
}
void on_library_loaded(const char *name, [[maybe_unused]] void *handle) {
if (strstr(name, "libbluetooth_jni.so")) {
LOGI("Detected Bluetooth library: %s", name);
bool hooked = findAndHookFunction(name);
if (!hooked) {
LOGE("Failed to hook Bluetooth library function");
}
}
}
extern "C" [[gnu::visibility("default")]] [[gnu::used]]
NativeOnModuleLoaded native_init(const NativeAPIEntries* entries) {
LOGI("L2C FCR Hook module initialized");
hook_func = entries->hook_func;
return on_library_loaded;
}

View File

@@ -0,0 +1,21 @@
#pragma once
#include <cstdint>
#include <vector>
typedef int (*HookFunType)(void *func, void *replace, void **backup);
typedef int (*UnhookFunType)(void *func);
typedef void (*NativeOnModuleLoaded)(const char *name, void *handle);
typedef struct {
uint32_t version;
HookFunType hook_func;
UnhookFunType unhook_func;
} NativeAPIEntries;
[[maybe_unused]] typedef NativeOnModuleLoaded (*NativeInit)(const NativeAPIEntries *entries);
uintptr_t loadHookOffset(const char* package_name);
uintptr_t getModuleBase(const char *module_name);

View File

@@ -26,72 +26,113 @@ import android.content.Context.MODE_PRIVATE
import android.content.Intent
import android.content.ServiceConnection
import android.content.SharedPreferences
import android.net.Uri
import android.os.Bundle
import android.os.IBinder
import android.provider.Settings
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.Phone
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.MultiplePermissionsState
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import me.kavishdevar.aln.screens.AirPodsSettingsScreen
import me.kavishdevar.aln.screens.AppSettingsScreen
import me.kavishdevar.aln.screens.DebugScreen
import me.kavishdevar.aln.screens.HeadTrackingScreen
import me.kavishdevar.aln.screens.LongPress
import me.kavishdevar.aln.screens.Onboarding
import me.kavishdevar.aln.screens.RenameScreen
import me.kavishdevar.aln.services.AirPodsService
import me.kavishdevar.aln.services.ServiceManager
import me.kavishdevar.aln.ui.theme.ALNTheme
import me.kavishdevar.aln.utils.AirPodsNotifications
import me.kavishdevar.aln.utils.CrossDevice
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import me.kavishdevar.aln.utils.RadareOffsetFinder
lateinit var serviceConnection: ServiceConnection
lateinit var connectionStatusReceiver: BroadcastReceiver
@ExperimentalMaterial3Api
class MainActivity : ComponentActivity() {
companion object {
init {
System.loadLibrary("l2c_fcr_hook")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
ALNTheme {
getSharedPreferences("settings", MODE_PRIVATE).edit().putLong("textColor",
MaterialTheme.colorScheme.onSurface.toArgb().toLong()).apply()
Main()
startService(Intent(this, AirPodsService::class.java))
}
}
}
@@ -136,6 +177,10 @@ class MainActivity : ComponentActivity() {
fun Main() {
val isConnected = remember { mutableStateOf(false) }
val isRemotelyConnected = remember { mutableStateOf(false) }
val hookAvailable = RadareOffsetFinder(LocalContext.current).isHookOffsetAvailable()
val context = LocalContext.current
var canDrawOverlays by remember { mutableStateOf(Settings.canDrawOverlays(context)) }
val permissionState = rememberMultiplePermissionsState(
permissions = listOf(
"android.permission.BLUETOOTH_CONNECT",
@@ -144,12 +189,18 @@ fun Main() {
"android.permission.POST_NOTIFICATIONS",
"android.permission.READ_PHONE_STATE",
"android.permission.ANSWER_PHONE_CALLS",
"android.permission.MODIFY_AUDIO_SETTINGS"
)
)
val airPodsService = remember { mutableStateOf<AirPodsService?>(null) }
if (permissionState.allPermissionsGranted) {
LaunchedEffect(Unit) {
canDrawOverlays = Settings.canDrawOverlays(context)
}
if (permissionState.allPermissionsGranted && canDrawOverlays) {
val context = LocalContext.current
context.startService(Intent(context, AirPodsService::class.java))
val navController = rememberNavController()
val sharedPreferences = context.getSharedPreferences("settings", MODE_PRIVATE)
@@ -171,7 +222,7 @@ fun Main() {
) {
NavHost(
navController = navController,
startDestination = "settings",
startDestination = if (hookAvailable) "settings" else "onboarding",
enterTransition = {
slideInHorizontally(
initialOffsetX = { it },
@@ -226,8 +277,12 @@ fun Main() {
composable("head_tracking") {
HeadTrackingScreen(navController)
}
composable("onboarding") {
Onboarding(navController, context)
}
}
}
serviceConnection = remember {
object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
@@ -247,20 +302,304 @@ fun Main() {
isConnected.value = true
}
} else {
Column (
modifier = Modifier.padding(24.dp),
){
val textToShow = if (permissionState.shouldShowRationale) {
// If the user has denied the permission but not permanently, explain why it's needed.
"Please enable Bluetooth and Notification permissions to use the app. The Nearby Devices is required to connect to your AirPods, and the notification is required to show the AirPods battery status."
} else {
// If the user has permanently denied the permission, inform them to enable it in settings.
"Please enable Bluetooth and Notification permissions in the app settings to use the app."
PermissionsScreen(
permissionState = permissionState,
canDrawOverlays = canDrawOverlays,
onOverlaySettingsReturn = { canDrawOverlays = Settings.canDrawOverlays(context) }
)
}
}
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun PermissionsScreen(
permissionState: MultiplePermissionsState,
canDrawOverlays: Boolean,
onOverlaySettingsReturn: () -> Unit
) {
val context = LocalContext.current
val isDarkTheme = isSystemInDarkTheme()
val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color.White
val textColor = if (isDarkTheme) Color.White else Color.Black
val accentColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5)
val scrollState = rememberScrollState()
val infiniteTransition = rememberInfiniteTransition(label = "pulse")
val pulseScale by infiniteTransition.animateFloat(
initialValue = 1f,
targetValue = 1.05f,
animationSpec = infiniteRepeatable(
animation = tween(1000),
repeatMode = RepeatMode.Reverse
),
label = "pulse scale"
)
Column(
modifier = Modifier
.fillMaxSize()
.background(if (isDarkTheme) Color.Black else Color(0xFFF2F2F7))
.padding(16.dp)
.verticalScroll(scrollState),
horizontalAlignment = Alignment.CenterHorizontally
) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(180.dp),
contentAlignment = Alignment.Center
) {
Text(
text = "\uDBC2\uDEB7",
style = TextStyle(
fontSize = 48.sp,
fontWeight = FontWeight.Bold,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = textColor,
textAlign = TextAlign.Center
)
)
Canvas(
modifier = Modifier
.size(120.dp)
.scale(pulseScale)
) {
val radius = size.minDimension / 2.2f
val centerX = size.width / 2
val centerY = size.height / 2
rotate(degrees = 45f) {
drawCircle(
color = accentColor.copy(alpha = 0.1f),
radius = radius * 1.3f,
center = Offset(centerX, centerY)
)
drawCircle(
color = accentColor.copy(alpha = 0.2f),
radius = radius * 1.1f,
center = Offset(centerX, centerY)
)
}
}
Text(textToShow)
Button(onClick = { permissionState.launchMultiplePermissionRequest() }) {
Text("Request permission")
}
Spacer(modifier = Modifier.height(16.dp))
Text(
text = "Permission Required",
style = TextStyle(
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = textColor,
textAlign = TextAlign.Center
),
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "To provide the best AirPods experience, we need a few permissions",
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Normal,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = textColor.copy(alpha = 0.7f),
textAlign = TextAlign.Center
),
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(32.dp))
PermissionCard(
title = "Bluetooth Permissions",
description = "Required to communicate with your AirPods",
icon = ImageVector.vectorResource(id = R.drawable.ic_bluetooth),
isGranted = permissionState.permissions.filter {
it.permission.contains("BLUETOOTH")
}.all { it.status.isGranted },
backgroundColor = backgroundColor,
textColor = textColor,
accentColor = accentColor
)
PermissionCard(
title = "Notification Permission",
description = "To show battery status",
icon = Icons.Default.Notifications,
isGranted = permissionState.permissions.find {
it.permission == "android.permission.POST_NOTIFICATIONS"
}?.status?.isGranted == true,
backgroundColor = backgroundColor,
textColor = textColor,
accentColor = accentColor
)
PermissionCard(
title = "Phone Permissions",
description = "For answering calls with Head Gestures",
icon = Icons.Default.Phone,
isGranted = permissionState.permissions.filter {
it.permission.contains("PHONE") || it.permission.contains("CALLS")
}.all { it.status.isGranted },
backgroundColor = backgroundColor,
textColor = textColor,
accentColor = accentColor
)
PermissionCard(
title = "Display Over Other Apps",
description = "For popup animations when AirPods connect",
icon = ImageVector.vectorResource(id = R.drawable.ic_layers),
isGranted = canDrawOverlays,
backgroundColor = backgroundColor,
textColor = textColor,
accentColor = accentColor
)
Spacer(modifier = Modifier.height(24.dp))
Button(
onClick = { permissionState.launchMultiplePermissionRequest() },
modifier = Modifier
.fillMaxWidth()
.height(55.dp),
colors = ButtonDefaults.buttonColors(
containerColor = accentColor
),
shape = RoundedCornerShape(8.dp)
) {
Text(
"Ask for regular permissions",
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Medium,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = Color.White
),
)
}
Spacer(modifier = Modifier.height(12.dp))
Button(
onClick = {
val intent = Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:${context.packageName}")
)
context.startActivity(intent)
onOverlaySettingsReturn()
},
modifier = Modifier
.fillMaxWidth()
.height(55.dp),
colors = ButtonDefaults.buttonColors(
containerColor = if (canDrawOverlays) Color.Gray else accentColor
),
enabled = !canDrawOverlays,
shape = RoundedCornerShape(8.dp)
) {
Text(
if (canDrawOverlays) "Overlay Permission Granted" else "Grant Overlay Permission",
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Medium,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = Color.White
),
)
}
}
}
@Composable
fun PermissionCard(
title: String,
description: String,
icon: ImageVector,
isGranted: Boolean,
backgroundColor: Color,
textColor: Color,
accentColor: Color
) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 6.dp),
colors = CardDefaults.cardColors(
containerColor = backgroundColor
),
shape = RoundedCornerShape(12.dp)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Box(
modifier = Modifier
.size(40.dp)
.clip(RoundedCornerShape(8.dp))
.background(if (isGranted) accentColor.copy(alpha = 0.15f) else Color.Gray.copy(alpha = 0.15f)),
contentAlignment = Alignment.Center
) {
Icon(
imageVector = icon,
contentDescription = title,
tint = if (isGranted) accentColor else Color.Gray,
modifier = Modifier.size(24.dp)
)
}
Column(
modifier = Modifier
.weight(1f)
.padding(start = 16.dp)
) {
Text(
text = title,
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Medium,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = textColor
)
)
Text(
text = description,
style = TextStyle(
fontSize = 14.sp,
fontWeight = FontWeight.Normal,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = textColor.copy(alpha = 0.6f)
)
)
}
Box(
modifier = Modifier
.size(24.dp)
.clip(RoundedCornerShape(12.dp))
.background(if (isGranted) Color(0xFF4CAF50) else Color.Gray),
contentAlignment = Alignment.Center
) {
Text(
text = if (isGranted) "" else "!",
style = TextStyle(
fontSize = 14.sp,
fontWeight = FontWeight.Bold,
color = Color.White
)
)
}
}
}
}

View File

@@ -19,6 +19,7 @@
package me.kavishdevar.aln.screens
import android.content.Context
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
@@ -33,13 +34,19 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowLeft
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Slider
import androidx.compose.material3.SliderDefaults
@@ -64,7 +71,6 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
@@ -72,6 +78,7 @@ import me.kavishdevar.aln.R
import me.kavishdevar.aln.composables.IndependentToggle
import me.kavishdevar.aln.composables.StyledSwitch
import me.kavishdevar.aln.services.ServiceManager
import me.kavishdevar.aln.utils.RadareOffsetFinder
import kotlin.math.roundToInt
@OptIn(ExperimentalMaterial3Api::class)
@@ -80,6 +87,10 @@ fun AppSettingsScreen(navController: NavController) {
val sharedPreferences = LocalContext.current.getSharedPreferences("settings", Context.MODE_PRIVATE)
val name = remember { mutableStateOf(sharedPreferences.getString("name", "") ?: "") }
val isDarkTheme = isSystemInDarkTheme()
val context = LocalContext.current
var showResetDialog by remember { mutableStateOf(false) }
Scaffold(
topBar = {
CenterAlignedTopAppBar(
@@ -131,6 +142,7 @@ fun AppSettingsScreen(navController: NavController) {
val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF)
val textColor = if (isDarkTheme) Color.White else Color.Black
val accentColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5)
IndependentToggle("Show phone battery in widget", ServiceManager.getService()!!, "setPhoneBatteryInWidget", sharedPreferences)
@@ -352,12 +364,104 @@ fun AppSettingsScreen(navController: NavController) {
)
}
}
Spacer(modifier = Modifier.height(24.dp))
Button(
onClick = { showResetDialog = true },
modifier = Modifier
.fillMaxWidth()
.height(50.dp),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.errorContainer
),
shape = RoundedCornerShape(14.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Icon(
imageVector = Icons.Default.Refresh,
contentDescription = "Reset",
tint = MaterialTheme.colorScheme.onErrorContainer,
modifier = Modifier.size(18.dp)
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = "Reset Hook Offset",
color = MaterialTheme.colorScheme.onErrorContainer,
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Medium,
fontFamily = FontFamily(Font(R.font.sf_pro))
)
)
}
}
if (showResetDialog) {
AlertDialog(
onDismissRequest = { showResetDialog = false },
title = {
Text(
"Reset Hook Offset",
fontFamily = FontFamily(Font(R.font.sf_pro)),
fontWeight = FontWeight.Medium
)
},
text = {
Text(
"This will clear the current hook offset and require you to go through the setup process again. Are you sure you want to continue?",
fontFamily = FontFamily(Font(R.font.sf_pro))
)
},
confirmButton = {
TextButton(
onClick = {
if (RadareOffsetFinder.clearHookOffset()) {
Toast.makeText(
context,
"Hook offset has been reset. Redirecting to setup...",
Toast.LENGTH_LONG
).show()
navController.navigate("onboarding") {
popUpTo("settings") { inclusive = true }
}
} else {
Toast.makeText(
context,
"Failed to reset hook offset",
Toast.LENGTH_SHORT
).show()
}
showResetDialog = false
},
colors = ButtonDefaults.textButtonColors(
contentColor = MaterialTheme.colorScheme.error
)
) {
Text(
"Reset",
fontFamily = FontFamily(Font(R.font.sf_pro)),
fontWeight = FontWeight.Medium
)
}
},
dismissButton = {
TextButton(
onClick = { showResetDialog = false }
) {
Text(
"Cancel",
fontFamily = FontFamily(Font(R.font.sf_pro)),
fontWeight = FontWeight.Medium
)
}
}
)
}
}
}
}
@Preview
@Composable
fun AppSettingsScreenPreview() {
AppSettingsScreen(navController = NavController(LocalContext.current))
}

View File

@@ -0,0 +1,529 @@
/*
* AirPods like Normal (ALN) - Bringing Apple-only features to Linux and Android for seamless AirPods functionality!
*
* Copyright (C) 2024 Kavish Devar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package me.kavishdevar.aln.screens
import android.content.Context
import android.util.Log
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.kavishdevar.aln.R
import me.kavishdevar.aln.utils.RadareOffsetFinder
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Onboarding(navController: NavController, activityContext: Context) {
val isDarkTheme = isSystemInDarkTheme()
val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color.White
val textColor = if (isDarkTheme) Color.White else Color.Black
val accentColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5)
val radareOffsetFinder = remember { RadareOffsetFinder(activityContext) }
val progressState by radareOffsetFinder.progressState.collectAsState()
var isComplete by remember { mutableStateOf(false) }
var countdownValue by remember { mutableIntStateOf(5) }
var hasStarted by remember { mutableStateOf(false) }
var rootCheckPassed by remember { mutableStateOf(false) }
var checkingRoot by remember { mutableStateOf(false) }
var rootCheckFailed by remember { mutableStateOf(false) }
fun checkRootAccess() {
checkingRoot = true
rootCheckFailed = false
kotlinx.coroutines.MainScope().launch {
withContext(Dispatchers.IO) {
try {
val process = Runtime.getRuntime().exec("su -c id")
val exitValue = process.waitFor()
withContext(Dispatchers.Main) {
rootCheckPassed = (exitValue == 0)
rootCheckFailed = (exitValue != 0)
checkingRoot = false
}
} catch (e: Exception) {
Log.e("Onboarding", "Root check failed", e)
withContext(Dispatchers.Main) {
rootCheckPassed = false
rootCheckFailed = true
checkingRoot = false
}
}
}
}
}
LaunchedEffect(hasStarted) {
if (hasStarted && rootCheckPassed) {
Log.d("Onboarding", "Checking if hook offset is available...")
val isHookReady = radareOffsetFinder.isHookOffsetAvailable()
Log.d("Onboarding", "Hook offset ready: $isHookReady")
if (isHookReady) {
Log.d("Onboarding", "Hook is ready, starting countdown...")
isComplete = true
for (i in 5 downTo 1) {
countdownValue = i
delay(1000)
}
navController.navigate("settings") {
popUpTo("onboarding") { inclusive = true }
}
} else {
Log.d("Onboarding", "Hook not ready, starting setup process...")
withContext(Dispatchers.IO) {
radareOffsetFinder.setupAndFindOffset()
}
}
}
}
LaunchedEffect(progressState) {
if (progressState is RadareOffsetFinder.ProgressState.Success) {
isComplete = true
for (i in 5 downTo 1) {
countdownValue = i
delay(1000)
}
navController.navigate("settings") {
popUpTo("onboarding") { inclusive = true }
}
}
}
Scaffold(
topBar = {
CenterAlignedTopAppBar(
title = {
Text(
"Setting Up",
fontFamily = FontFamily(Font(R.font.sf_pro)),
fontWeight = FontWeight.Medium
)
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = Color.Transparent
)
)
},
containerColor = if (isDarkTheme) Color(0xFF000000) else Color(0xFFF2F2F7)
) { paddingValues ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Spacer(modifier = Modifier.height(16.dp))
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(containerColor = backgroundColor),
shape = RoundedCornerShape(12.dp)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
if (!rootCheckPassed && !hasStarted) {
Icon(
imageVector = Icons.Default.Settings,
contentDescription = "Root Access",
tint = accentColor,
modifier = Modifier.size(50.dp)
)
Spacer(modifier = Modifier.height(24.dp))
Text(
text = "Root Access Required",
style = TextStyle(
fontSize = 22.sp,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = textColor
)
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "This app needs root access to modify Bluetooth settings",
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Normal,
textAlign = TextAlign.Center,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = textColor.copy(alpha = 0.7f)
)
)
if (rootCheckFailed) {
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "Root access was denied. Please grant root permissions.",
style = TextStyle(
fontSize = 14.sp,
fontWeight = FontWeight.Normal,
textAlign = TextAlign.Center,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = Color(0xFFFF453A)
)
)
}
Spacer(modifier = Modifier.height(24.dp))
Button(
onClick = { checkRootAccess() },
modifier = Modifier
.fillMaxWidth()
.height(50.dp),
colors = ButtonDefaults.buttonColors(
containerColor = accentColor
),
shape = RoundedCornerShape(8.dp),
enabled = !checkingRoot
) {
if (checkingRoot) {
CircularProgressIndicator(
modifier = Modifier.size(24.dp),
color = Color.White,
strokeWidth = 2.dp
)
} else {
Text(
"Check Root Access",
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Medium,
fontFamily = FontFamily(Font(R.font.sf_pro))
),
)
}
}
} else {
StatusIcon(if (hasStarted) progressState else RadareOffsetFinder.ProgressState.Idle, isDarkTheme)
Spacer(modifier = Modifier.height(24.dp))
AnimatedContent(
targetState = if (hasStarted) getStatusTitle(progressState) else "Setup Required",
transitionSpec = { fadeIn() togetherWith fadeOut() }
) { text ->
Text(
text = text,
style = TextStyle(
fontSize = 22.sp,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = textColor
)
)
}
Spacer(modifier = Modifier.height(8.dp))
AnimatedContent(
targetState = if (hasStarted)
getStatusDescription(progressState, isComplete, countdownValue)
else
"AirPods functionality requires one-time setup",
transitionSpec = { fadeIn() togetherWith fadeOut() }
) { text ->
Text(
text = text,
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Normal,
textAlign = TextAlign.Center,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = textColor.copy(alpha = 0.7f)
)
)
}
Spacer(modifier = Modifier.height(24.dp))
if (!hasStarted) {
Button(
onClick = { hasStarted = true },
modifier = Modifier
.fillMaxWidth()
.height(50.dp),
colors = ButtonDefaults.buttonColors(
containerColor = accentColor
),
shape = RoundedCornerShape(8.dp)
) {
Text(
"Start Setup",
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Medium,
fontFamily = FontFamily(Font(R.font.sf_pro))
),
)
}
} else {
when (progressState) {
is RadareOffsetFinder.ProgressState.DownloadProgress -> {
val progress = (progressState as RadareOffsetFinder.ProgressState.DownloadProgress).progress
val animatedProgress by animateFloatAsState(
targetValue = progress,
label = "Download Progress"
)
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
LinearProgressIndicator(
progress = { animatedProgress },
modifier = Modifier
.fillMaxWidth()
.height(8.dp),
strokeCap = StrokeCap.Round,
color = accentColor
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "${(progress * 100).toInt()}%",
style = TextStyle(
fontSize = 14.sp,
fontWeight = FontWeight.Medium,
fontFamily = FontFamily(Font(R.font.sf_pro)),
color = textColor.copy(alpha = 0.6f)
)
)
}
}
is RadareOffsetFinder.ProgressState.Idle,
is RadareOffsetFinder.ProgressState.Success,
is RadareOffsetFinder.ProgressState.Error -> {
}
else -> {
LinearProgressIndicator(
modifier = Modifier
.fillMaxWidth()
.height(8.dp),
strokeCap = StrokeCap.Round,
color = accentColor
)
}
}
}
}
}
}
Spacer(modifier = Modifier.weight(1f))
if (progressState is RadareOffsetFinder.ProgressState.Error && !isComplete && hasStarted) {
Button(
onClick = {
Log.d("Onboarding", "Retrying offset finding")
kotlinx.coroutines.MainScope().launch {
withContext(Dispatchers.IO) {
radareOffsetFinder.setupAndFindOffset()
}
}
},
modifier = Modifier
.fillMaxWidth()
.height(55.dp),
colors = ButtonDefaults.buttonColors(
containerColor = accentColor
),
shape = RoundedCornerShape(8.dp)
) {
Text(
"Try Again",
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Medium,
fontFamily = FontFamily(Font(R.font.sf_pro))
),
)
}
}
}
}
}
@Composable
private fun StatusIcon(
progressState: RadareOffsetFinder.ProgressState,
isDarkTheme: Boolean
) {
val accentColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5)
val errorColor = if (isDarkTheme) Color(0xFFFF453A) else Color(0xFFFF3B30)
val successColor = if (isDarkTheme) Color(0xFF30D158) else Color(0xFF34C759)
Box(
modifier = Modifier.size(80.dp),
contentAlignment = Alignment.Center
) {
when (progressState) {
is RadareOffsetFinder.ProgressState.Error -> {
Icon(
imageVector = Icons.Default.Clear,
contentDescription = "Error",
tint = errorColor,
modifier = Modifier.size(50.dp)
)
}
is RadareOffsetFinder.ProgressState.Success -> {
Icon(
imageVector = Icons.Default.Check,
contentDescription = "Success",
tint = successColor,
modifier = Modifier.size(50.dp)
)
}
is RadareOffsetFinder.ProgressState.Idle -> {
Icon(
imageVector = Icons.Default.Settings,
contentDescription = "Settings",
tint = accentColor,
modifier = Modifier.size(50.dp)
)
}
else -> {
CircularProgressIndicator(
modifier = Modifier.size(50.dp),
color = accentColor,
strokeWidth = 4.dp
)
}
}
}
}
private fun getStatusTitle(state: RadareOffsetFinder.ProgressState): String {
return when (state) {
is RadareOffsetFinder.ProgressState.Idle -> "Getting Ready"
is RadareOffsetFinder.ProgressState.CheckingExisting -> "Checking Configuration"
is RadareOffsetFinder.ProgressState.Downloading -> "Downloading Components"
is RadareOffsetFinder.ProgressState.DownloadProgress -> "Downloading Components"
is RadareOffsetFinder.ProgressState.Extracting -> "Extracting Files"
is RadareOffsetFinder.ProgressState.MakingExecutable -> "Configuring Components"
is RadareOffsetFinder.ProgressState.FindingOffset -> "Finding Bluetooth Hook"
is RadareOffsetFinder.ProgressState.SavingOffset -> "Saving Configuration"
is RadareOffsetFinder.ProgressState.Cleaning -> "Cleaning Up"
is RadareOffsetFinder.ProgressState.Error -> "Setup Failed"
is RadareOffsetFinder.ProgressState.Success -> "Setup Complete"
}
}
private fun getStatusDescription(
state: RadareOffsetFinder.ProgressState,
isComplete: Boolean,
countdownValue: Int
): String {
return when (state) {
is RadareOffsetFinder.ProgressState.Idle -> "Preparing to configure Bluetooth functionality"
is RadareOffsetFinder.ProgressState.CheckingExisting -> "Checking if required components are already installed"
is RadareOffsetFinder.ProgressState.Downloading -> "Downloading necessary components for Bluetooth functionality"
is RadareOffsetFinder.ProgressState.DownloadProgress -> "Downloading necessary components for Bluetooth functionality"
is RadareOffsetFinder.ProgressState.Extracting -> "Extracting files needed for integration"
is RadareOffsetFinder.ProgressState.MakingExecutable -> "Setting up proper permissions for components"
is RadareOffsetFinder.ProgressState.FindingOffset -> "Looking for the required Bluetooth function in system libraries"
is RadareOffsetFinder.ProgressState.SavingOffset -> "Saving found configuration for future use"
is RadareOffsetFinder.ProgressState.Cleaning -> "Removing temporary files"
is RadareOffsetFinder.ProgressState.Error -> state.message
is RadareOffsetFinder.ProgressState.Success -> {
if (isComplete) {
"Moving to AirPods settings in $countdownValue..."
} else {
"Successfully configured Bluetooth functionality"
}
}
}
}
@Preview
@Composable
fun OnboardingPreview() {
Onboarding(navController = NavController(LocalContext.current), activityContext = LocalContext.current)
}
private suspend fun delay(timeMillis: Long) {
kotlinx.coroutines.delay(timeMillis)
}

View File

@@ -83,6 +83,7 @@ import me.kavishdevar.aln.utils.IslandWindow
import me.kavishdevar.aln.utils.LongPressPackets
import me.kavishdevar.aln.utils.MediaController
import me.kavishdevar.aln.utils.PopupWindow
import me.kavishdevar.aln.utils.RadareOffsetFinder
import me.kavishdevar.aln.utils.isHeadTrackingData
import me.kavishdevar.aln.widgets.BatteryWidget
import me.kavishdevar.aln.widgets.NoiseControlWidget
@@ -915,7 +916,8 @@ class AirPodsService : Service() {
fun connectToSocket(device: BluetoothDevice) {
HiddenApiBypass.addHiddenApiExemptions("Landroid/bluetooth/BluetoothSocket;")
val uuid: ParcelUuid = ParcelUuid.fromString("74ec2172-0bad-4d01-8f77-997b2be0722a")
val isHooked = RadareOffsetFinder(this).isHookOffsetAvailable()
assert(isHooked) { "Hook offset not available, stopping" }
if (isConnectedLocally != true && !CrossDevice.isAvailable) {
socket = try {
createBluetoothSocket(device, uuid)

View File

@@ -90,12 +90,21 @@ object CrossDevice {
serverSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("ALNCrossDevice", uuid)
Log.d("CrossDevice", "Server started")
while (serverSocket != null) {
if (!bluetoothAdapter.isEnabled) {
serverSocket?.close()
break
}
if (clientSocket != null) {
try {
clientSocket!!.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
try {
val socket = serverSocket!!.accept()
handleClientConnection(socket)
} catch (e: IOException) {
e.printStackTrace()
}
} catch (e: IOException) { }
}
}
}

View File

@@ -0,0 +1,41 @@
package me.kavishdevar.aln.utils
import android.content.pm.ApplicationInfo
import android.util.Log
import io.github.libxposed.api.XposedInterface
import io.github.libxposed.api.XposedModule
import io.github.libxposed.api.XposedModuleInterface
import io.github.libxposed.api.XposedModuleInterface.ModuleLoadedParam
private const val TAG = "AirPodsHook"
private lateinit var module: KotlinModule
class KotlinModule(base: XposedInterface, param: ModuleLoadedParam): XposedModule(base, param) {
init {
Log.i(TAG, "AirPodsHook module initialized at :: ${param.processName}")
module = this
}
override fun onPackageLoaded(param: XposedModuleInterface.PackageLoadedParam) {
super.onPackageLoaded(param)
Log.i(TAG, "onPackageLoaded :: ${param.packageName}")
if (param.packageName == "com.android.bluetooth") {
Log.i(TAG, "Bluetooth app detected, hooking l2c_fcr_chk_chan_modes")
try {
if (param.isFirstPackage) {
Log.i(TAG, "Loading native library for Bluetooth hook")
System.loadLibrary("l2c_fcr_hook")
Log.i(TAG, "Native library loaded successfully")
}
} catch (e: Exception) {
Log.e(TAG, "Failed to load native library: ${e.message}", e)
}
}
}
override fun getApplicationInfo(): ApplicationInfo {
return super.applicationInfo
}
}

View File

@@ -323,24 +323,19 @@ enum class LongPressPackets(val value: ByteArray) {
//}
fun isHeadTrackingData(data: ByteArray): Boolean {
// Check minimum size requirement first for efficiency
if (data.size <= 60) return false
// Check if the first 10 bytes match
val prefixPattern = byteArrayOf(
0x04, 0x00, 0x04, 0x00, 0x17, 0x00, 0x00, 0x00,
0x10, 0x00
)
// Check prefix (first 10 bytes)
for (i in prefixPattern.indices) {
if (data[i] != prefixPattern[i].toByte()) return false
}
// Check if byte 11 is either 0x44 or 0x45
if (data[10] != 0x44.toByte() && data[10] != 0x45.toByte()) return false
// Check byte 12
if (data[11] != 0x00.toByte()) return false
return true

View File

@@ -0,0 +1,461 @@
/*
* AirPods like Normal (ALN) - Bringing Apple-only features to Linux and Android for seamless AirPods functionality!
*
* Copyright (C) 2024 Kavish Devar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package me.kavishdevar.aln.utils
import android.content.Context
import android.util.Log
import androidx.compose.runtime.NoLiveLiterals
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext
import java.io.BufferedReader
import java.io.File
import java.io.FileOutputStream
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
@NoLiveLiterals
class RadareOffsetFinder(context: Context) {
companion object {
private const val TAG = "RadareOffsetFinder"
private const val RADARE2_URL = "https://hc-cdn.hel1.your-objectstorage.com/s/v3/c9898243c42c0d3d1387de9a37d57ce9df77f9c9_radare2-5.9.9-android-aarch64.tar.gz"
private const val HOOK_OFFSET_PROP = "persist.aln.hook_offset"
private const val EXTRACT_DIR = "/"
private const val RADARE2_BIN_PATH = "$EXTRACT_DIR/data/local/tmp/aln_unzip/org.radare.radare2installer/radare2/bin"
private const val RADARE2_LIB_PATH = "$EXTRACT_DIR/data/local/tmp/aln_unzip/org.radare.radare2installer/radare2/lib"
private const val BUSYBOX_PATH = "$EXTRACT_DIR/data/local/tmp/aln_unzip/busybox"
private val LIBRARY_PATHS = listOf(
"/apex/com.android.bt/lib64/libbluetooth_jni.so",
"/apex/com.android.btservices/lib64/libbluetooth_jni.so",
"/system/lib64/libbluetooth_jni.so",
"/system/lib64/libbluetooth_qti.so",
"/system_ext/lib64/libbluetooth_qti.so"
)
fun findBluetoothLibraryPath(): String? {
for (path in LIBRARY_PATHS) {
if (File(path).exists()) {
Log.d(TAG, "Found Bluetooth library at $path")
return path
}
}
Log.e(TAG, "Could not find Bluetooth library")
return null
}
fun clearHookOffset(): Boolean {
try {
val process = Runtime.getRuntime().exec(arrayOf(
"su", "-c", "setprop $HOOK_OFFSET_PROP ''"
))
val exitCode = process.waitFor()
if (exitCode == 0) {
Log.d(TAG, "Successfully cleared hook offset property")
return true
} else {
Log.e(TAG, "Failed to clear hook offset property, exit code: $exitCode")
}
} catch (e: Exception) {
Log.e(TAG, "Error clearing hook offset property", e)
}
return false
}
}
private val radare2TarballFile = File(context.cacheDir, "radare2.tar.gz")
private val _progressState = MutableStateFlow<ProgressState>(ProgressState.Idle)
val progressState: StateFlow<ProgressState> = _progressState
sealed class ProgressState {
object Idle : ProgressState()
object CheckingExisting : ProgressState()
object Downloading : ProgressState()
data class DownloadProgress(val progress: Float) : ProgressState()
object Extracting : ProgressState()
object MakingExecutable : ProgressState()
object FindingOffset : ProgressState()
object SavingOffset : ProgressState()
object Cleaning : ProgressState()
data class Error(val message: String) : ProgressState()
data class Success(val offset: Long) : ProgressState()
}
fun isHookOffsetAvailable(): Boolean {
_progressState.value = ProgressState.CheckingExisting
try {
val process = Runtime.getRuntime().exec(arrayOf("getprop", HOOK_OFFSET_PROP))
val reader = BufferedReader(InputStreamReader(process.inputStream))
val propValue = reader.readLine()
process.waitFor()
if (propValue != null && propValue.isNotEmpty()) {
Log.d(TAG, "Hook offset property exists: $propValue")
_progressState.value = ProgressState.Idle
return true
}
} catch (e: Exception) {
Log.e(TAG, "Error checking if offset property exists", e)
_progressState.value = ProgressState.Error("Failed to check if offset property exists: ${e.message}")
}
Log.d(TAG, "No hook offset available")
_progressState.value = ProgressState.Idle
return false
}
suspend fun setupAndFindOffset(): Boolean {
val offset = findOffset()
return offset > 0
}
suspend fun findOffset(): Long = withContext(Dispatchers.IO) {
try {
_progressState.value = ProgressState.Downloading
if (!downloadRadare2TarballIfNeeded()) {
_progressState.value = ProgressState.Error("Failed to download radare2 tarball")
Log.e(TAG, "Failed to download radare2 tarball")
return@withContext 0L
}
_progressState.value = ProgressState.Extracting
if (!extractRadare2Tarball()) {
_progressState.value = ProgressState.Error("Failed to extract radare2 tarball")
Log.e(TAG, "Failed to extract radare2 tarball")
return@withContext 0L
}
_progressState.value = ProgressState.MakingExecutable
if (!makeExecutable()) {
_progressState.value = ProgressState.Error("Failed to make binaries executable")
Log.e(TAG, "Failed to make binaries executable")
return@withContext 0L
}
_progressState.value = ProgressState.FindingOffset
val offset = findFunctionOffset()
if (offset == 0L) {
_progressState.value = ProgressState.Error("Failed to find function offset")
Log.e(TAG, "Failed to find function offset")
return@withContext 0L
}
_progressState.value = ProgressState.SavingOffset
if (!saveOffset(offset)) {
_progressState.value = ProgressState.Error("Failed to save offset")
Log.e(TAG, "Failed to save offset")
return@withContext 0L
}
_progressState.value = ProgressState.Cleaning
cleanupExtractedFiles()
_progressState.value = ProgressState.Success(offset)
return@withContext offset
} catch (e: Exception) {
_progressState.value = ProgressState.Error("Error: ${e.message}")
Log.e(TAG, "Error in findOffset", e)
return@withContext 0L
}
}
private suspend fun downloadRadare2TarballIfNeeded(): Boolean = withContext(Dispatchers.IO) {
if (radare2TarballFile.exists() && radare2TarballFile.length() > 0) {
Log.d(TAG, "Radare2 tarball already downloaded to ${radare2TarballFile.absolutePath}")
return@withContext true
}
try {
val url = URL(RADARE2_URL)
val connection = url.openConnection() as HttpURLConnection
connection.connectTimeout = 60000
connection.readTimeout = 60000
val contentLength = connection.contentLength.toFloat()
val inputStream = connection.inputStream
val outputStream = FileOutputStream(radare2TarballFile)
val buffer = ByteArray(4096)
var bytesRead: Int
var totalBytesRead = 0L
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
outputStream.write(buffer, 0, bytesRead)
totalBytesRead += bytesRead
if (contentLength > 0) {
val progress = totalBytesRead.toFloat() / contentLength
_progressState.value = ProgressState.DownloadProgress(progress)
}
}
outputStream.close()
inputStream.close()
Log.d(TAG, "Download successful to ${radare2TarballFile.absolutePath}")
return@withContext true
} catch (e: Exception) {
Log.e(TAG, "Failed to download radare2 tarball", e)
return@withContext false
}
}
private suspend fun extractRadare2Tarball(): Boolean = withContext(Dispatchers.IO) {
try {
val isAlreadyExtracted = checkIfAlreadyExtracted()
if (isAlreadyExtracted) {
Log.d(TAG, "Radare2 files already extracted correctly, skipping extraction")
return@withContext true
}
Log.d(TAG, "Removing existing extract directory")
Runtime.getRuntime().exec(arrayOf("su", "-c", "rm -rf $EXTRACT_DIR/data/local/tmp/aln_unzip")).waitFor()
Runtime.getRuntime().exec(arrayOf("su", "-c", "mkdir -p $EXTRACT_DIR/data/local/tmp/aln_unzip")).waitFor()
Log.d(TAG, "Extracting ${radare2TarballFile.absolutePath} to $EXTRACT_DIR")
val process = Runtime.getRuntime().exec(
arrayOf("su", "-c", "tar xvf ${radare2TarballFile.absolutePath} -C $EXTRACT_DIR")
)
val reader = BufferedReader(InputStreamReader(process.inputStream))
val errorReader = BufferedReader(InputStreamReader(process.errorStream))
var line: String?
while (reader.readLine().also { line = it } != null) {
Log.d(TAG, "Extract output: $line")
}
while (errorReader.readLine().also { line = it } != null) {
Log.e(TAG, "Extract error: $line")
}
val exitCode = process.waitFor()
if (exitCode == 0) {
Log.d(TAG, "Extraction completed successfully")
return@withContext true
} else {
Log.e(TAG, "Extraction failed with exit code $exitCode")
return@withContext false
}
} catch (e: Exception) {
Log.e(TAG, "Failed to extract radare2", e)
return@withContext false
}
}
private suspend fun checkIfAlreadyExtracted(): Boolean = withContext(Dispatchers.IO) {
try {
val checkDirProcess = Runtime.getRuntime().exec(
arrayOf("su", "-c", "[ -d $EXTRACT_DIR/data/local/tmp/aln_unzip ] && echo 'exists'")
)
val dirExists = BufferedReader(InputStreamReader(checkDirProcess.inputStream)).readLine() == "exists"
checkDirProcess.waitFor()
if (!dirExists) {
Log.d(TAG, "Extract directory doesn't exist, need to extract")
return@withContext false
}
val tarProcess = Runtime.getRuntime().exec(
arrayOf("su", "-c", "tar tf ${radare2TarballFile.absolutePath}")
)
val tarFiles = BufferedReader(InputStreamReader(tarProcess.inputStream)).readLines()
.filter { it.isNotEmpty() }
.map { it.trim() }
.toSet()
tarProcess.waitFor()
if (tarFiles.isEmpty()) {
Log.e(TAG, "Failed to get file list from tarball")
return@withContext false
}
val findProcess = Runtime.getRuntime().exec(
arrayOf("su", "-c", "find $EXTRACT_DIR/data/local/tmp/aln_unzip -type f | sort")
)
val extractedFiles = BufferedReader(InputStreamReader(findProcess.inputStream)).readLines()
.filter { it.isNotEmpty() }
.map { it.trim() }
.toSet()
findProcess.waitFor()
if (extractedFiles.isEmpty()) {
Log.d(TAG, "No files found in extract directory, need to extract")
return@withContext false
}
for (tarFile in tarFiles) {
if (tarFile.endsWith("/")) continue
val filePathInExtractDir = "$EXTRACT_DIR/$tarFile"
val fileCheckProcess = Runtime.getRuntime().exec(
arrayOf("su", "-c", "[ -f $filePathInExtractDir ] && echo 'exists'")
)
val fileExists = BufferedReader(InputStreamReader(fileCheckProcess.inputStream)).readLine() == "exists"
fileCheckProcess.waitFor()
if (!fileExists) {
Log.d(TAG, "File $filePathInExtractDir from tarball missing in extract directory")
Runtime.getRuntime().exec(arrayOf("su", "-c", "rm -rf $EXTRACT_DIR/data/local/tmp/aln_unzip")).waitFor()
return@withContext false
}
}
Log.d(TAG, "All ${tarFiles.size} files from tarball exist in extract directory")
return@withContext true
} catch (e: Exception) {
Log.e(TAG, "Error checking extraction status", e)
return@withContext false
}
}
private suspend fun makeExecutable(): Boolean = withContext(Dispatchers.IO) {
try {
Log.d(TAG, "Making binaries executable in $RADARE2_BIN_PATH")
val chmod1Result = Runtime.getRuntime().exec(
arrayOf("su", "-c", "chmod -R 755 $RADARE2_BIN_PATH")
).waitFor()
Log.d(TAG, "Making binaries executable in $BUSYBOX_PATH")
val chmod2Result = Runtime.getRuntime().exec(
arrayOf("su", "-c", "chmod -R 755 $BUSYBOX_PATH")
).waitFor()
if (chmod1Result == 0 && chmod2Result == 0) {
Log.d(TAG, "Successfully made binaries executable")
return@withContext true
} else {
Log.e(TAG, "Failed to make binaries executable, exit codes: $chmod1Result, $chmod2Result")
return@withContext false
}
} catch (e: Exception) {
Log.e(TAG, "Error making binaries executable", e)
return@withContext false
}
}
private suspend fun findFunctionOffset(): Long = withContext(Dispatchers.IO) {
val libraryPath = findBluetoothLibraryPath() ?: return@withContext 0L
var offset = 0L
try {
@Suppress("LocalVariableName") val currentLD_LIBRARY_PATH = ProcessBuilder().command("su", "-c", "printenv LD_LIBRARY_PATH").start().inputStream.bufferedReader().readText().trim()
val currentPATH = ProcessBuilder().command("su", "-c", "printenv PATH").start().inputStream.bufferedReader().readText().trim()
val envSetup = """
export LD_LIBRARY_PATH="$RADARE2_LIB_PATH:$currentLD_LIBRARY_PATH"
export PATH="$BUSYBOX_PATH:$RADARE2_BIN_PATH:$currentPATH"
""".trimIndent()
val command = "$envSetup && $RADARE2_BIN_PATH/rabin2 -q -E $libraryPath | grep fcr_chk_chan"
Log.d(TAG, "Running command: $command")
val process = Runtime.getRuntime().exec(arrayOf("su", "-c", command))
val reader = BufferedReader(InputStreamReader(process.inputStream))
val errorReader = BufferedReader(InputStreamReader(process.errorStream))
var line: String?
while (reader.readLine().also { line = it } != null) {
Log.d(TAG, "rabin2 output: $line")
if (line?.contains("fcr_chk_chan") == true) {
val parts = line.split(" ")
if (parts.isNotEmpty() && parts[0].startsWith("0x")) {
offset = parts[0].substring(2).toLong(16)
Log.d(TAG, "Found offset at ${parts[0]}")
break
}
}
}
while (errorReader.readLine().also { line = it } != null) {
Log.d(TAG, "rabin2 error: $line")
}
val exitCode = process.waitFor()
if (exitCode != 0) {
Log.e(TAG, "rabin2 command failed with exit code $exitCode")
}
} catch (e: Exception) {
Log.e(TAG, "Failed to find function offset", e)
return@withContext 0L
}
if (offset == 0L) {
Log.e(TAG, "Failed to extract function offset from output, aborting")
return@withContext 0L
}
Log.d(TAG, "Successfully found offset: 0x${offset.toString(16)}")
return@withContext offset
}
private suspend fun saveOffset(offset: Long): Boolean = withContext(Dispatchers.IO) {
try {
val hexString = "0x${offset.toString(16)}"
Log.d(TAG, "Saving offset to system property: $hexString")
val process = Runtime.getRuntime().exec(arrayOf(
"su", "-c", "setprop $HOOK_OFFSET_PROP $hexString"
))
val exitCode = process.waitFor()
if (exitCode == 0) {
val verifyProcess = Runtime.getRuntime().exec(arrayOf(
"getprop", HOOK_OFFSET_PROP
))
val propValue = BufferedReader(InputStreamReader(verifyProcess.inputStream)).readLine()
verifyProcess.waitFor()
if (propValue != null && propValue.isNotEmpty()) {
Log.d(TAG, "Successfully saved offset to system property: $propValue")
return@withContext true
} else {
Log.e(TAG, "Property was set but couldn't be verified")
}
} else {
Log.e(TAG, "Failed to set property, exit code: $exitCode")
}
return@withContext false
} catch (e: Exception) {
Log.e(TAG, "Failed to save offset", e)
return@withContext false
}
}
private fun cleanupExtractedFiles() {
try {
Runtime.getRuntime().exec(arrayOf("su", "-c", "rm -rf $EXTRACT_DIR/data/local/tmp/aln_unzip")).waitFor()
Log.d(TAG, "Cleaned up extracted files at $EXTRACT_DIR/data/local/tmp/aln_unzip")
} catch (e: Exception) {
Log.e(TAG, "Failed to cleanup extracted files", e)
}
}
}

View File

@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M440,880v-304L256,760l-56,-56 224,-224 -224,-224 56,-56 184,184v-304h40l228,228 -172,172 172,172L480,880h-40ZM520,384 L596,308 520,234v150ZM520,726 L596,652 520,576v150Z"
android:fillColor="#e8eaed"/>
</vector>

View File

@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M480,842 L120,562l66,-50 294,228 294,-228 66,50 -360,280ZM480,640L120,360l360,-280 360,280 -360,280ZM480,360ZM480,538 L710,360 480,182 250,360 480,538Z"
android:fillColor="#e8eaed"/>
</vector>

View File

@@ -0,0 +1 @@
me.kavishdevar.aln.utils.KotlinModule

View File

@@ -0,0 +1,3 @@
minApiVersion=100
targetApiVersion=100
staticScope=true

View File

@@ -0,0 +1 @@
libl2c_fcr_hook.so

View File

@@ -0,0 +1,2 @@
com.android.bluetooth
me.kavishdevar.aln