Merge branch 'main' into prudhvir3ddy/safe-permission-category

This commit is contained in:
callebtc
2025-07-12 17:42:10 +02:00
committed by GitHub
32 changed files with 733 additions and 572 deletions
@@ -17,6 +17,10 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.ViewModelProvider
import androidx.activity.OnBackPressedCallback
import androidx.activity.addCallback
import com.bitchat.android.mesh.BluetoothMeshService
import com.bitchat.android.onboarding.*
import com.bitchat.android.ui.ChatScreen
import com.bitchat.android.ui.ChatViewModel
@@ -29,7 +33,17 @@ class MainActivity : ComponentActivity() {
private lateinit var permissionManager: PermissionManager
private lateinit var onboardingCoordinator: OnboardingCoordinator
private lateinit var bluetoothStatusManager: BluetoothStatusManager
private val chatViewModel: ChatViewModel by viewModels()
// Core mesh service - managed at app level
private lateinit var meshService: BluetoothMeshService
private val chatViewModel: ChatViewModel by viewModels {
object : ViewModelProvider.Factory {
override fun <T : androidx.lifecycle.ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return ChatViewModel(application, meshService) as T
}
}
}
// UI state for onboarding flow
private var onboardingState by mutableStateOf(OnboardingState.CHECKING)
@@ -50,6 +64,9 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Initialize core mesh service first
meshService = BluetoothMeshService(this)
// Initialize permission management
permissionManager = PermissionManager(this)
bluetoothStatusManager = BluetoothStatusManager(
@@ -107,9 +124,6 @@ class MainActivity : ComponentActivity() {
onContinue = {
onboardingState = OnboardingState.PERMISSION_REQUESTING
onboardingCoordinator.requestPermissions()
},
onCancel = {
finish()
}
)
}
@@ -123,6 +137,24 @@ class MainActivity : ComponentActivity() {
}
OnboardingState.COMPLETE -> {
// Set up back navigation handling for the chat screen
val backCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// Let ChatViewModel handle navigation state
val handled = chatViewModel.handleBackPressed()
if (!handled) {
// If ChatViewModel doesn't handle it, disable this callback
// and let the system handle it (which will exit the app)
this.isEnabled = false
onBackPressedDispatcher.onBackPressed()
this.isEnabled = true
}
}
}
// Add the callback - this will be automatically removed when the activity is destroyed
onBackPressedDispatcher.addCallback(this, backCallback)
ChatScreen(viewModel = chatViewModel)
}
@@ -289,7 +321,7 @@ class MainActivity : ComponentActivity() {
// This solves the issue where app needs restart to work on first install
delay(1000) // Give the system time to process permission grants
android.util.Log.d("MainActivity", "Permissions verified, starting mesh service")
android.util.Log.d("MainActivity", "Permissions verified, initializing chat system")
// Ensure all permissions are still granted (user might have revoked in settings)
if (!permissionManager.areAllPermissionsGranted()) {
@@ -299,8 +331,11 @@ class MainActivity : ComponentActivity() {
return@launch
}
// Initialize chat view model - this will start the mesh service
chatViewModel.meshService.startServices()
// Set up mesh service delegate and start services
meshService.delegate = chatViewModel
meshService.startServices()
android.util.Log.d("MainActivity", "Mesh service started successfully")
// Handle any notification intent
handleNotificationIntent(intent)
@@ -330,6 +365,8 @@ class MainActivity : ComponentActivity() {
super.onResume()
// Check Bluetooth status on resume and handle accordingly
if (onboardingState == OnboardingState.COMPLETE) {
// Set app foreground state
meshService.connectionManager.setAppBackgroundState(false)
chatViewModel.setAppBackgroundState(false)
// Check if Bluetooth was disabled while app was backgrounded
@@ -347,6 +384,8 @@ class MainActivity : ComponentActivity() {
super.onPause()
// Only set background state if app is fully initialized
if (onboardingState == OnboardingState.COMPLETE) {
// Set app background state
meshService.connectionManager.setAppBackgroundState(true)
chatViewModel.setAppBackgroundState(true)
}
}
@@ -376,12 +415,32 @@ class MainActivity : ComponentActivity() {
}
}
/**
* Restart mesh services (for debugging/troubleshooting)
*/
fun restartMeshServices() {
if (onboardingState == OnboardingState.COMPLETE) {
lifecycleScope.launch {
try {
android.util.Log.d("MainActivity", "Restarting mesh services")
meshService.stopServices()
delay(1000)
meshService.startServices()
android.util.Log.d("MainActivity", "Mesh services restarted successfully")
} catch (e: Exception) {
android.util.Log.e("MainActivity", "Error restarting mesh services: ${e.message}")
}
}
}
}
override fun onDestroy() {
super.onDestroy()
// Only stop mesh services if they were started
// Stop mesh services if app was fully initialized
if (onboardingState == OnboardingState.COMPLETE) {
try {
chatViewModel.meshService.stopServices()
meshService.stopServices()
android.util.Log.d("MainActivity", "Mesh services stopped successfully")
} catch (e: Exception) {
android.util.Log.w("MainActivity", "Error stopping mesh services in onDestroy: ${e.message}")
}
@@ -128,15 +128,17 @@ class BluetoothConnectionManager(
try {
isActive = true
// Setup GATT server first
setupGattServer()
// Start power manager and services
connectionScope.launch {
powerManager.start()
delay(500) // Ensure GATT server is ready
delay(300) // Brief delay to ensure GATT server is ready
startAdvertising()
delay(200)
delay(100)
if (powerManager.shouldUseDutyCycle()) {
Log.i(TAG, "Using power-aware duty cycling")
@@ -146,13 +148,14 @@ class BluetoothConnectionManager(
startPeriodicCleanup()
Log.i(TAG, "Power-optimized Bluetooth services started successfully (CLIENT ONLY)")
Log.i(TAG, "Power-optimized Bluetooth services started successfully")
}
return true
} catch (e: Exception) {
Log.e(TAG, "Failed to start Bluetooth services: ${e.message}")
isActive = false
return false
}
}
@@ -355,6 +358,12 @@ class BluetoothConnectionManager(
val serverCallback = object : BluetoothGattServerCallback() {
override fun onConnectionStateChange(device: BluetoothDevice, status: Int, newState: Int) {
// Guard against callbacks after service shutdown
if (!isActive) {
Log.d(TAG, "Server: Ignoring connection state change after shutdown")
return
}
when (newState) {
BluetoothProfile.STATE_CONNECTED -> {
Log.d(TAG, "Server: Device connected ${device.address}")
@@ -371,6 +380,20 @@ class BluetoothConnectionManager(
}
}
override fun onServiceAdded(status: Int, service: BluetoothGattService) {
// Guard against callbacks after service shutdown
if (!isActive) {
Log.d(TAG, "Server: Ignoring service added callback after shutdown")
return
}
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "Server: Service added successfully: ${service.uuid}")
} else {
Log.e(TAG, "Server: Failed to add service: ${service.uuid}, status: $status")
}
}
override fun onCharacteristicWriteRequest(
device: BluetoothDevice,
requestId: Int,
@@ -380,11 +403,22 @@ class BluetoothConnectionManager(
offset: Int,
value: ByteArray
) {
// Guard against callbacks after service shutdown
if (!isActive) {
Log.d(TAG, "Server: Ignoring characteristic write after shutdown")
return
}
if (characteristic.uuid == CHARACTERISTIC_UUID) {
Log.d(TAG, "Server: Received packet from ${device.address}, size: ${value.size} bytes")
val packet = BitchatPacket.fromBinaryData(value)
if (packet != null) {
val peerID = String(packet.senderID).replace("\u0000", "")
Log.d(TAG, "Server: Parsed packet type ${packet.type} from $peerID")
delegate?.onPacketReceived(packet, peerID, device)
} else {
Log.w(TAG, "Server: Failed to parse packet from ${device.address}, size: ${value.size} bytes")
Log.w(TAG, "Server: Packet data: ${value.joinToString(" ") { "%02x".format(it) }}")
}
if (responseNeeded) {
@@ -402,13 +436,21 @@ class BluetoothConnectionManager(
offset: Int,
value: ByteArray
) {
// Guard against callbacks after service shutdown
if (!isActive) {
Log.d(TAG, "Server: Ignoring descriptor write after shutdown")
return
}
if (BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE.contentEquals(value)) {
Log.d(TAG, "Device ${device.address} subscribed to notifications")
subscribedDevices.add(device)
connectionScope.launch {
delay(100)
delegate?.onDeviceConnected(device)
if (isActive) { // Check if still active
delegate?.onDeviceConnected(device)
}
}
}
@@ -418,9 +460,25 @@ class BluetoothConnectionManager(
}
}
// Clean up existing server
gattServer?.close()
// Proper cleanup sequencing to prevent race conditions
gattServer?.let { server ->
Log.d(TAG, "Cleaning up existing GATT server")
try {
server.close()
} catch (e: Exception) {
Log.w(TAG, "Error closing existing GATT server: ${e.message}")
}
}
// Small delay to ensure cleanup is complete
Thread.sleep(100)
if (!isActive) {
Log.d(TAG, "Service inactive, skipping GATT server creation")
return
}
// Create new server
gattServer = bluetoothManager.openGattServer(context, serverCallback)
// Create characteristic with notification support
@@ -782,10 +840,15 @@ class BluetoothConnectionManager(
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
val value = characteristic.value
Log.d(TAG, "Client: Received packet from ${gatt.device.address}, size: ${value.size} bytes")
val packet = BitchatPacket.fromBinaryData(value)
if (packet != null) {
val peerID = String(packet.senderID).replace("\u0000", "")
Log.d(TAG, "Client: Parsed packet type ${packet.type} from $peerID")
delegate?.onPacketReceived(packet, peerID, gatt.device)
} else {
Log.w(TAG, "Client: Failed to parse packet from ${gatt.device.address}, size: ${value.size} bytes")
Log.w(TAG, "Client: Packet data: ${value.joinToString(" ") { "%02x".format(it) }}")
}
}
}
@@ -47,6 +47,9 @@ class BluetoothMeshService(private val context: Context) {
internal val connectionManager = BluetoothConnectionManager(context, myPeerID) // Made internal for access
private val packetProcessor = PacketProcessor(myPeerID)
// Service state management
private var isActive = false
// Delegate for message callbacks (maintains same interface)
var delegate: BluetoothMeshDelegate? = null
@@ -66,10 +69,10 @@ class BluetoothMeshService(private val context: Context) {
while (isActive) {
try {
delay(10000) // 10 seconds
val debugInfo = getDebugStatus()
Log.d(TAG, "=== PERIODIC DEBUG STATUS ===")
Log.d(TAG, debugInfo)
Log.d(TAG, "=== END DEBUG STATUS ===")
if (isActive) { // Double-check before logging
val debugInfo = getDebugStatus()
Log.d(TAG, "=== PERIODIC DEBUG STATUS ===\n$debugInfo\n=== END DEBUG STATUS ===")
}
} catch (e: Exception) {
Log.e(TAG, "Error in periodic debug logging: ${e.message}")
}
@@ -98,7 +101,10 @@ class BluetoothMeshService(private val context: Context) {
// SecurityManager delegate for key exchange notifications
securityManager.delegate = object : SecurityManagerDelegate {
override fun onKeyExchangeCompleted(peerID: String) {
override fun onKeyExchangeCompleted(peerID: String, peerPublicKeyData: ByteArray) {
// Notify delegate about key exchange completion so it can register peer fingerprint
delegate?.registerPeerPublicKey(peerID, peerPublicKeyData)
// Send announcement and cached messages after key exchange
serviceScope.launch {
delay(100)
@@ -276,9 +282,16 @@ class BluetoothMeshService(private val context: Context) {
* Start the mesh service
*/
fun startServices() {
// Prevent double starts (defensive programming)
if (isActive) {
Log.w(TAG, "Mesh service already active, ignoring duplicate start request")
return
}
Log.i(TAG, "Starting Bluetooth mesh service with peer ID: $myPeerID")
if (connectionManager.startServices()) {
isActive = true
Log.i(TAG, "Bluetooth services started successfully")
// Send initial announcements after services are ready
@@ -295,7 +308,13 @@ class BluetoothMeshService(private val context: Context) {
* Stop all mesh services
*/
fun stopServices() {
if (!isActive) {
Log.w(TAG, "Mesh service not active, ignoring stop request")
return
}
Log.i(TAG, "Stopping Bluetooth mesh service")
isActive = false
// Send leave announcement
sendLeaveAnnouncement()
@@ -547,4 +566,5 @@ interface BluetoothMeshDelegate {
fun decryptChannelMessage(encryptedContent: ByteArray, channel: String): String?
fun getNickname(): String?
fun isFavorite(peerID: String): Boolean
fun registerPeerPublicKey(peerID: String, publicKeyData: ByteArray)
}
@@ -113,7 +113,7 @@ class SecurityManager(private val encryptionService: EncryptionService, private
Log.d(TAG, "Successfully processed key exchange from $peerID")
// Notify delegate
delegate?.onKeyExchangeCompleted(peerID)
delegate?.onKeyExchangeCompleted(peerID, packet.payload)
return true
@@ -315,5 +315,5 @@ class SecurityManager(private val encryptionService: EncryptionService, private
* Delegate interface for security manager callbacks
*/
interface SecurityManagerDelegate {
fun onKeyExchangeCompleted(peerID: String)
fun onKeyExchangeCompleted(peerID: String, peerPublicKeyData: ByteArray)
}
@@ -21,120 +21,130 @@ import androidx.compose.ui.unit.sp
@Composable
fun PermissionExplanationScreen(
permissionCategories: List<PermissionCategory>,
onContinue: () -> Unit,
onCancel: () -> Unit
onContinue: () -> Unit
) {
val colorScheme = MaterialTheme.colorScheme
val scrollState = rememberScrollState()
Column(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 24.dp)
.verticalScroll(scrollState),
verticalArrangement = Arrangement.spacedBy(16.dp)
Box(
modifier = Modifier.fillMaxSize()
) {
Spacer(modifier = Modifier.height(24.dp))
// Header
// Scrollable content
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = "Welcome to bitchat*",
style = MaterialTheme.typography.headlineMedium.copy(
fontFamily = FontFamily.Monospace,
fontWeight = FontWeight.Bold,
color = colorScheme.primary
),
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "Decentralized mesh messaging over Bluetooth",
style = MaterialTheme.typography.bodyMedium.copy(
fontFamily = FontFamily.Monospace,
color = colorScheme.onSurface.copy(alpha = 0.7f)
),
textAlign = TextAlign.Center
)
}
Spacer(modifier = Modifier.height(16.dp))
// Privacy assurance section
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = colorScheme.surfaceVariant.copy(alpha = 0.3f)
),
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 24.dp)
.padding(bottom = 88.dp) // Leave space for the fixed button
.verticalScroll(scrollState),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Spacer(modifier = Modifier.height(24.dp))
// Header
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
Text(
text = "Welcome to bitchat*",
style = MaterialTheme.typography.headlineMedium.copy(
fontFamily = FontFamily.Monospace,
fontWeight = FontWeight.Bold,
color = colorScheme.primary
),
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "Decentralized mesh messaging over Bluetooth",
style = MaterialTheme.typography.bodyMedium.copy(
fontFamily = FontFamily.Monospace,
color = colorScheme.onSurface.copy(alpha = 0.7f)
),
textAlign = TextAlign.Center
)
}
Spacer(modifier = Modifier.height(16.dp))
// Privacy assurance section
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = colorScheme.surfaceVariant.copy(alpha = 0.3f)
),
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
) {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(
text = "🔒",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.size(20.dp)
)
Text(
text = "Your Privacy is Protected",
style = MaterialTheme.typography.titleSmall.copy(
fontWeight = FontWeight.Bold,
color = colorScheme.onSurface
)
)
}
Text(
text = "🔒",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.size(20.dp)
)
Text(
text = "Your Privacy is Protected",
style = MaterialTheme.typography.titleSmall.copy(
fontWeight = FontWeight.Bold,
color = colorScheme.onSurface
text = "• bitchat doesn't track you or collect personal data\n" +
"• No servers, no internet required, no data logging\n" +
"• Location permission is only used by Android for Bluetooth scanning\n" +
"• Your messages stay on your device and peer devices only",
style = MaterialTheme.typography.bodySmall.copy(
fontFamily = FontFamily.Monospace,
color = colorScheme.onSurface.copy(alpha = 0.8f)
)
)
}
Text(
text = "• bitchat doesn't track you or collect personal data\n" +
"• No servers, no internet required, no data logging\n" +
"• Location permission is only used by Android for Bluetooth scanning\n" +
"• Your messages stay on your device and peer devices only",
style = MaterialTheme.typography.bodySmall.copy(
fontFamily = FontFamily.Monospace,
color = colorScheme.onSurface.copy(alpha = 0.8f)
)
}
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "To work properly, bitchat needs these permissions:",
style = MaterialTheme.typography.bodyMedium.copy(
fontWeight = FontWeight.Medium,
color = colorScheme.onSurface
)
)
// Permission categories
permissionCategories.forEach { category ->
PermissionCategoryCard(
category = category,
colorScheme = colorScheme
)
}
Spacer(modifier = Modifier.height(24.dp))
}
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "To work properly, bitchat needs these permissions:",
style = MaterialTheme.typography.bodyMedium.copy(
fontWeight = FontWeight.Medium,
color = colorScheme.onSurface
)
)
// Permission categories
permissionCategories.forEach { category ->
PermissionCategoryCard(
category = category,
colorScheme = colorScheme
)
}
Spacer(modifier = Modifier.height(16.dp))
// Action buttons
Column(
modifier = Modifier.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(12.dp)
// Fixed button at bottom
Surface(
modifier = Modifier
.align(Alignment.BottomCenter)
.fillMaxWidth(),
color = colorScheme.surface,
shadowElevation = 8.dp
) {
Button(
onClick = onContinue,
modifier = Modifier.fillMaxWidth(),
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 24.dp, vertical = 16.dp),
colors = ButtonDefaults.buttonColors(
containerColor = colorScheme.primary
)
@@ -148,24 +158,7 @@ fun PermissionExplanationScreen(
modifier = Modifier.padding(vertical = 4.dp)
)
}
OutlinedButton(
onClick = onCancel,
modifier = Modifier.fillMaxWidth(),
colors = ButtonDefaults.outlinedButtonColors(
contentColor = colorScheme.onSurface.copy(alpha = 0.7f)
)
) {
Text(
text = "Exit App",
style = MaterialTheme.typography.bodyMedium.copy(
fontFamily = FontFamily.Monospace
),
modifier = Modifier.padding(vertical = 4.dp)
)
}
}
Spacer(modifier = Modifier.height(24.dp))
}
}
@@ -116,7 +116,7 @@ class PermissionManager(private val context: Context) {
categories.add(
PermissionCategory(
type = PermissionType.NEARBY_DEVICES,
description = "Required to discover and connect to other bitchat users via Bluetooth",
description = "Required to discover bitchat users via Bluetooth",
permissions = bluetoothPermissions,
isGranted = bluetoothPermissions.all { isPermissionGranted(it) },
systemDescription = "Allow bitchat to connect to nearby devices"
@@ -132,10 +132,10 @@ class PermissionManager(private val context: Context) {
categories.add(
PermissionCategory(
type = PermissionType.PRECISE_LOCATION,
description = "Required by Android for Bluetooth scanning.",
description = "Required by Android to discover nearby bitchat users via Bluetooth",
permissions = locationPermissions,
isGranted = locationPermissions.all { isPermissionGranted(it) },
systemDescription = "Allow bitchat to access this device's location"
systemDescription = "bitchat needs this to scan for nearby devices"
)
)
@@ -144,7 +144,7 @@ class PermissionManager(private val context: Context) {
categories.add(
PermissionCategory(
type = PermissionType.NOTIFICATIONS,
description = "Show notifications when you receive private messages while the app is in background",
description = "Notifications to keep you updated",
permissions = listOf(Manifest.permission.POST_NOTIFICATIONS),
isGranted = isPermissionGranted(Manifest.permission.POST_NOTIFICATIONS),
systemDescription = "Allow bitchat to send you notifications"
@@ -0,0 +1,219 @@
package com.bitchat.android.services
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import com.bitchat.android.model.BitchatMessage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.util.*
/**
* Message retention service for saving channel messages locally
* Matches iOS MessageRetentionService functionality
*/
class MessageRetentionService private constructor(private val context: Context) {
companion object {
private const val TAG = "MessageRetentionService"
private const val PREF_NAME = "message_retention"
private const val KEY_FAVORITE_CHANNELS = "favorite_channels"
@Volatile
private var INSTANCE: MessageRetentionService? = null
fun getInstance(context: Context): MessageRetentionService {
return INSTANCE ?: synchronized(this) {
INSTANCE ?: MessageRetentionService(context.applicationContext).also { INSTANCE = it }
}
}
}
private val prefs: SharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
private val retentionDir = File(context.filesDir, "retained_messages")
init {
if (!retentionDir.exists()) {
retentionDir.mkdirs()
}
}
// MARK: - Channel Bookmarking (Favorites)
fun getFavoriteChannels(): Set<String> {
return prefs.getStringSet(KEY_FAVORITE_CHANNELS, emptySet()) ?: emptySet()
}
fun toggleFavoriteChannel(channel: String): Boolean {
val currentFavorites = getFavoriteChannels().toMutableSet()
val wasAdded = if (currentFavorites.contains(channel)) {
currentFavorites.remove(channel)
false
} else {
currentFavorites.add(channel)
true
}
prefs.edit().putStringSet(KEY_FAVORITE_CHANNELS, currentFavorites).apply()
if (!wasAdded) {
// Channel removed from favorites - delete saved messages in background
Thread {
try {
val channelFile = getChannelFile(channel)
if (channelFile.exists()) {
channelFile.delete()
Log.d(TAG, "Deleted saved messages for channel $channel")
}
} catch (e: Exception) {
Log.e(TAG, "Failed to delete messages for channel $channel", e)
}
}.start()
}
Log.d(TAG, "Channel $channel ${if (wasAdded) "bookmarked" else "unbookmarked"}")
return wasAdded
}
fun isChannelBookmarked(channel: String): Boolean {
return getFavoriteChannels().contains(channel)
}
// MARK: - Message Storage
suspend fun saveMessage(message: BitchatMessage, forChannel: String) = withContext(Dispatchers.IO) {
if (!isChannelBookmarked(forChannel)) {
Log.w(TAG, "Attempted to save message for non-bookmarked channel: $forChannel")
return@withContext
}
try {
val channelFile = getChannelFile(forChannel)
val existingMessages = loadMessagesFromFile(channelFile).toMutableList()
// Check if message already exists (by ID)
if (existingMessages.any { it.id == message.id }) {
Log.d(TAG, "Message ${message.id} already saved for channel $forChannel")
return@withContext
}
// Add new message
existingMessages.add(message)
// Sort by timestamp
existingMessages.sortBy { it.timestamp }
// Save back to file
saveMessagesToFile(channelFile, existingMessages)
Log.d(TAG, "Saved message ${message.id} for channel $forChannel")
} catch (e: Exception) {
Log.e(TAG, "Failed to save message for channel $forChannel", e)
}
}
suspend fun loadMessagesForChannel(channel: String): List<BitchatMessage> = withContext(Dispatchers.IO) {
if (!isChannelBookmarked(channel)) {
Log.d(TAG, "Channel $channel not bookmarked, returning empty list")
return@withContext emptyList()
}
try {
val channelFile = getChannelFile(channel)
val messages = loadMessagesFromFile(channelFile)
Log.d(TAG, "Loaded ${messages.size} messages for channel $channel")
return@withContext messages
} catch (e: Exception) {
Log.e(TAG, "Failed to load messages for channel $channel", e)
return@withContext emptyList()
}
}
suspend fun deleteMessagesForChannel(channel: String): Unit = withContext(Dispatchers.IO) {
try {
val channelFile = getChannelFile(channel)
if (channelFile.exists()) {
channelFile.delete()
Log.d(TAG, "Deleted saved messages for channel $channel")
}
} catch (e: Exception) {
Log.e(TAG, "Failed to delete messages for channel $channel", e)
}
}
suspend fun deleteAllStoredMessages(): Unit = withContext(Dispatchers.IO) {
try {
if (retentionDir.exists()) {
retentionDir.listFiles()?.forEach { file ->
file.delete()
}
Log.d(TAG, "Deleted all stored messages")
}
} catch (e: Exception) {
Log.e(TAG, "Failed to delete all stored messages", e)
}
}
// MARK: - File Operations
private fun getChannelFile(channel: String): File {
// Sanitize channel name for filename
val sanitizedChannel = channel.replace("[^a-zA-Z0-9_-]".toRegex(), "_")
return File(retentionDir, "channel_${sanitizedChannel}.dat")
}
private fun loadMessagesFromFile(file: File): List<BitchatMessage> {
if (!file.exists()) {
return emptyList()
}
return try {
FileInputStream(file).use { fis ->
ObjectInputStream(fis).use { ois ->
@Suppress("UNCHECKED_CAST")
ois.readObject() as List<BitchatMessage>
}
}
} catch (e: Exception) {
Log.w(TAG, "Failed to load messages from ${file.name}, returning empty list", e)
emptyList()
}
}
private fun saveMessagesToFile(file: File, messages: List<BitchatMessage>) {
FileOutputStream(file).use { fos ->
ObjectOutputStream(fos).use { oos ->
oos.writeObject(messages)
}
}
}
// MARK: - Statistics
fun getBookmarkedChannelsCount(): Int {
return getFavoriteChannels().size
}
suspend fun getTotalStoredMessagesCount(): Int = withContext(Dispatchers.IO) {
var totalCount = 0
try {
retentionDir.listFiles()?.forEach { file ->
if (file.name.startsWith("channel_") && file.name.endsWith(".dat")) {
val messages = loadMessagesFromFile(file)
totalCount += messages.size
}
}
} catch (e: Exception) {
Log.e(TAG, "Failed to count stored messages", e)
}
totalCount
}
}
@@ -1,5 +1,6 @@
package com.bitchat.android.ui
import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
@@ -151,11 +152,17 @@ fun ChatHeaderContent(
when {
selectedPrivatePeer != null -> {
// Private chat header
// Private chat header - ensure state synchronization
val favoritePeers by viewModel.favoritePeers.observeAsState(emptySet())
val fingerprint = viewModel.privateChatManager.getPeerFingerprint(selectedPrivatePeer)
val isFavorite = favoritePeers.contains(fingerprint)
Log.d("ChatHeader", "Header recomposing: peer=$selectedPrivatePeer, fingerprint=$fingerprint, isFav=$isFavorite")
PrivateChatHeader(
peerID = selectedPrivatePeer,
peerNicknames = viewModel.meshService.getPeerNicknames(),
isFavorite = viewModel.isFavorite(selectedPrivatePeer),
isFavorite = isFavorite,
onBackClick = onBackClick,
onToggleFavorite = { viewModel.toggleFavorite(selectedPrivatePeer) }
)
@@ -253,7 +260,10 @@ private fun PrivateChatHeader(
// Favorite button - positioned on the right
IconButton(
onClick = onToggleFavorite,
onClick = {
Log.d("ChatHeader", "Header toggle favorite: peerID=$peerID, currentFavorite=$isFavorite")
onToggleFavorite()
},
modifier = Modifier.align(Alignment.CenterEnd)
) {
Icon(
@@ -60,15 +60,15 @@ fun ChatScreen(viewModel: ChatViewModel) {
val hasUnreadPrivateMessages by viewModel.unreadPrivateMessages.observeAsState(emptySet())
val privateChats by viewModel.privateChats.observeAsState(emptyMap())
val channelMessages by viewModel.channelMessages.observeAsState(emptyMap())
var showSidebar by remember { mutableStateOf(false) }
val showSidebar by viewModel.showSidebar.observeAsState(false)
val showCommandSuggestions by viewModel.showCommandSuggestions.observeAsState(false)
val commandSuggestions by viewModel.commandSuggestions.observeAsState(emptyList())
val showAppInfo by viewModel.showAppInfo.observeAsState(false)
var messageText by remember { mutableStateOf("") }
var showPasswordPrompt by remember { mutableStateOf(false) }
var showPasswordDialog by remember { mutableStateOf(false) }
var passwordInput by remember { mutableStateOf("") }
var showAppInfo by remember { mutableStateOf(false) }
// Show password dialog when needed
LaunchedEffect(showPasswordPrompt) {
@@ -142,8 +142,8 @@ fun ChatScreen(viewModel: ChatViewModel) {
nickname = nickname,
viewModel = viewModel,
colorScheme = colorScheme,
onSidebarToggle = { showSidebar = true },
onShowAppInfo = { showAppInfo = true },
onSidebarToggle = { viewModel.showSidebar() },
onShowAppInfo = { viewModel.showAppInfo() },
onPanicClear = { viewModel.panicClearAllData() }
)
@@ -162,7 +162,7 @@ fun ChatScreen(viewModel: ChatViewModel) {
) {
SidebarOverlay(
viewModel = viewModel,
onDismiss = { showSidebar = false },
onDismiss = { viewModel.hideSidebar() },
modifier = Modifier.fillMaxSize()
)
}
@@ -188,7 +188,7 @@ fun ChatScreen(viewModel: ChatViewModel) {
passwordInput = ""
},
showAppInfo = showAppInfo,
onAppInfoDismiss = { showAppInfo = false }
onAppInfoDismiss = { viewModel.hideAppInfo() }
)
}
@@ -1,5 +1,6 @@
package com.bitchat.android.ui
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
@@ -84,6 +85,10 @@ class ChatState {
val peerIDToPublicKeyFingerprint = mutableMapOf<String, String>()
// Navigation state
private val _showAppInfo = MutableLiveData<Boolean>(false)
val showAppInfo: LiveData<Boolean> = _showAppInfo
// Unread state computed properties
val hasUnreadChannels: MediatorLiveData<Boolean> = MediatorLiveData<Boolean>()
val hasUnreadPrivateMessages: MediatorLiveData<Boolean> = MediatorLiveData<Boolean>()
@@ -117,6 +122,7 @@ class ChatState {
fun getShowCommandSuggestionsValue() = _showCommandSuggestions.value ?: false
fun getCommandSuggestionsValue() = _commandSuggestions.value ?: emptyList()
fun getFavoritePeersValue() = _favoritePeers.value ?: emptySet()
fun getShowAppInfoValue() = _showAppInfo.value ?: false
// Setters for state updates
fun setMessages(messages: List<BitchatMessage>) {
@@ -188,7 +194,21 @@ class ChatState {
}
fun setFavoritePeers(favorites: Set<String>) {
val currentValue = _favoritePeers.value ?: emptySet()
Log.d("ChatState", "setFavoritePeers called with ${favorites.size} favorites: $favorites")
Log.d("ChatState", "Current value: $currentValue")
Log.d("ChatState", "Values equal: ${currentValue == favorites}")
Log.d("ChatState", "Setting on thread: ${Thread.currentThread().name}")
// Always set the value - even if equal, this ensures observers are triggered
_favoritePeers.value = favorites
Log.d("ChatState", "LiveData value after set: ${_favoritePeers.value}")
Log.d("ChatState", "LiveData has active observers: ${_favoritePeers.hasActiveObservers()}")
}
fun setShowAppInfo(show: Boolean) {
_showAppInfo.value = show
}
}
@@ -2,6 +2,7 @@ package com.bitchat.android.ui
import android.app.Application
import android.content.Context
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
@@ -19,13 +20,13 @@ import kotlin.random.Random
* Refactored ChatViewModel - Main coordinator for bitchat functionality
* Delegates specific responsibilities to specialized managers while maintaining 100% iOS compatibility
*/
class ChatViewModel(application: Application) : AndroidViewModel(application), BluetoothMeshDelegate {
class ChatViewModel(
application: Application,
val meshService: BluetoothMeshService
) : AndroidViewModel(application), BluetoothMeshDelegate {
private val context: Context = application.applicationContext
// Core services
val meshService = BluetoothMeshService(context)
// State management
private val state = ChatState()
@@ -70,9 +71,10 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), B
val showCommandSuggestions: LiveData<Boolean> = state.showCommandSuggestions
val commandSuggestions: LiveData<List<CommandSuggestion>> = state.commandSuggestions
val favoritePeers: LiveData<Set<String>> = state.favoritePeers
val showAppInfo: LiveData<Boolean> = state.showAppInfo
init {
meshService.delegate = this
// Note: Mesh service delegate is now set by MainActivity
loadAndInitialize()
}
@@ -100,8 +102,11 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), B
state.setFavoritePeers(dataManager.favoritePeers)
dataManager.loadBlockedUsers()
// Start mesh service
meshService.startServices()
// Log all favorites at startup
dataManager.logAllFavorites()
logCurrentFavoriteState()
// Note: Mesh service is now started by MainActivity
// Show welcome message if no peers after delay
viewModelScope.launch {
@@ -120,7 +125,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), B
override fun onCleared() {
super.onCleared()
meshService.stopServices()
// Note: Mesh service lifecycle is now managed by MainActivity
}
// MARK: - Nickname Management
@@ -250,11 +255,19 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), B
}
fun toggleFavorite(peerID: String) {
Log.d("ChatViewModel", "toggleFavorite called for peerID: $peerID")
privateChatManager.toggleFavorite(peerID)
// Log current state after toggle
logCurrentFavoriteState()
}
fun registerPeerPublicKey(peerID: String, publicKeyData: ByteArray) {
privateChatManager.registerPeerPublicKey(peerID, publicKeyData)
private fun logCurrentFavoriteState() {
Log.i("ChatViewModel", "=== CURRENT FAVORITE STATE ===")
Log.i("ChatViewModel", "LiveData favorite peers: ${favoritePeers.value}")
Log.i("ChatViewModel", "DataManager favorite peers: ${dataManager.favoritePeers}")
Log.i("ChatViewModel", "Peer fingerprints: ${privateChatManager.getAllPeerFingerprints()}")
Log.i("ChatViewModel", "==============================")
}
// MARK: - Debug and Troubleshooting
@@ -263,18 +276,10 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), B
return meshService.getDebugStatus()
}
fun restartMeshServices() {
viewModelScope.launch {
meshService.stopServices()
delay(1000)
meshService.startServices()
}
}
// Note: Mesh service restart is now handled by MainActivity
// This function is no longer needed
fun setAppBackgroundState(inBackground: Boolean) {
// Forward to connection manager for power optimization
meshService.connectionManager.setAppBackgroundState(inBackground)
// Forward to notification manager for notification logic
notificationManager.setAppBackgroundState(inBackground)
}
@@ -341,6 +346,10 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), B
return meshDelegateHandler.isFavorite(peerID)
}
override fun registerPeerPublicKey(peerID: String, publicKeyData: ByteArray) {
privateChatManager.registerPeerPublicKey(peerID, publicKeyData)
}
// MARK: - Emergency Clear
fun panicClearAllData() {
@@ -355,13 +364,62 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), B
state.setNickname(newNickname)
dataManager.saveNickname(newNickname)
// Disconnect from mesh
meshService.stopServices()
// Restart services with new identity
viewModelScope.launch {
delay(500)
meshService.startServices()
// Note: Mesh service restart is now handled by MainActivity
// This method now only clears data, not mesh service lifecycle
}
// MARK: - Navigation Management
fun showAppInfo() {
state.setShowAppInfo(true)
}
fun hideAppInfo() {
state.setShowAppInfo(false)
}
fun showSidebar() {
state.setShowSidebar(true)
}
fun hideSidebar() {
state.setShowSidebar(false)
}
/**
* Handle Android back navigation
* Returns true if the back press was handled, false if it should be passed to the system
*/
fun handleBackPressed(): Boolean {
return when {
// Close app info dialog
state.getShowAppInfoValue() -> {
hideAppInfo()
true
}
// Close sidebar
state.getShowSidebarValue() -> {
hideSidebar()
true
}
// Close password dialog
state.getShowPasswordPromptValue() -> {
state.setShowPasswordPrompt(false)
state.setPasswordPromptChannel(null)
true
}
// Exit private chat
state.getSelectedPrivateChatPeerValue() != null -> {
endPrivateChat()
true
}
// Exit channel view
state.getCurrentChannelValue() != null -> {
switchToChannel(null)
true
}
// No special navigation state - let system handle (usually exits app)
else -> false
}
}
}
@@ -2,6 +2,7 @@ package com.bitchat.android.ui
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import com.google.gson.Gson
import kotlin.random.Random
@@ -10,6 +11,10 @@ import kotlin.random.Random
*/
class DataManager(private val context: Context) {
companion object {
private const val TAG = "DataManager"
}
private val prefs: SharedPreferences = context.getSharedPreferences("bitchat_prefs", Context.MODE_PRIVATE)
private val gson = Gson()
@@ -126,24 +131,41 @@ class DataManager(private val context: Context) {
fun loadFavorites() {
val savedFavorites = prefs.getStringSet("favorites", emptySet()) ?: emptySet()
_favoritePeers.addAll(savedFavorites)
Log.d(TAG, "Loaded ${savedFavorites.size} favorite users from storage: $savedFavorites")
}
fun saveFavorites() {
prefs.edit().putStringSet("favorites", _favoritePeers).apply()
Log.d(TAG, "Saved ${_favoritePeers.size} favorite users to storage: $_favoritePeers")
}
fun addFavorite(fingerprint: String) {
_favoritePeers.add(fingerprint)
val wasAdded = _favoritePeers.add(fingerprint)
Log.d(TAG, "addFavorite: fingerprint=$fingerprint, wasAdded=$wasAdded")
saveFavorites()
logAllFavorites()
}
fun removeFavorite(fingerprint: String) {
_favoritePeers.remove(fingerprint)
val wasRemoved = _favoritePeers.remove(fingerprint)
Log.d(TAG, "removeFavorite: fingerprint=$fingerprint, wasRemoved=$wasRemoved")
saveFavorites()
logAllFavorites()
}
fun isFavorite(fingerprint: String): Boolean {
return _favoritePeers.contains(fingerprint)
val result = _favoritePeers.contains(fingerprint)
Log.d(TAG, "isFavorite check: fingerprint=$fingerprint, result=$result")
return result
}
fun logAllFavorites() {
Log.i(TAG, "=== ALL FAVORITE USERS ===")
Log.i(TAG, "Total favorites: ${_favoritePeers.size}")
_favoritePeers.forEach { fingerprint ->
Log.i(TAG, "Favorite fingerprint: $fingerprint")
}
Log.i(TAG, "========================")
}
// MARK: - Blocked Users Management
@@ -154,4 +154,8 @@ class MeshDelegateHandler(
override fun isFavorite(peerID: String): Boolean {
return privateChatManager.isFavorite(peerID)
}
override fun registerPeerPublicKey(peerID: String, publicKeyData: ByteArray) {
privateChatManager.registerPeerPublicKey(peerID, publicKeyData)
}
}
@@ -3,6 +3,7 @@ package com.bitchat.android.ui
import com.bitchat.android.model.BitchatMessage
import com.bitchat.android.model.DeliveryStatus
import java.util.*
import android.util.Log
/**
* Handles private chat functionality including peer management and blocking
@@ -13,6 +14,10 @@ class PrivateChatManager(
private val dataManager: DataManager
) {
companion object {
private const val TAG = "PrivateChatManager"
}
// Peer identification mapping
private val peerIDToPublicKeyFingerprint = mutableMapOf<String, String>()
@@ -99,17 +104,43 @@ class PrivateChatManager(
fun toggleFavorite(peerID: String) {
val fingerprint = peerIDToPublicKeyFingerprint[peerID] ?: return
if (dataManager.isFavorite(fingerprint)) {
Log.d(TAG, "toggleFavorite called for peerID: $peerID, fingerprint: $fingerprint")
val wasFavorite = dataManager.isFavorite(fingerprint)
Log.d(TAG, "Current favorite status: $wasFavorite")
val currentFavorites = state.getFavoritePeersValue()
Log.d(TAG, "Current UI state favorites: $currentFavorites")
if (wasFavorite) {
dataManager.removeFavorite(fingerprint)
Log.d(TAG, "Removed from favorites: $fingerprint")
} else {
dataManager.addFavorite(fingerprint)
Log.d(TAG, "Added to favorites: $fingerprint")
}
state.setFavoritePeers(dataManager.favoritePeers)
// Always update state to trigger UI refresh - create new set to ensure change detection
val newFavorites = dataManager.favoritePeers.toSet()
state.setFavoritePeers(newFavorites)
Log.d(TAG, "Force updated favorite peers state. New favorites: $newFavorites")
Log.d(TAG, "All peer fingerprints: $peerIDToPublicKeyFingerprint")
}
fun isFavorite(peerID: String): Boolean {
val fingerprint = peerIDToPublicKeyFingerprint[peerID] ?: return false
return dataManager.isFavorite(fingerprint)
val isFav = dataManager.isFavorite(fingerprint)
Log.d(TAG, "isFavorite check: peerID=$peerID, fingerprint=$fingerprint, result=$isFav")
return isFav
}
fun getPeerFingerprint(peerID: String): String? {
return peerIDToPublicKeyFingerprint[peerID]
}
fun getPeerFingerprints(): Map<String, String> {
return peerIDToPublicKeyFingerprint.toMap()
}
// MARK: - Block/Unblock Operations
@@ -262,10 +293,6 @@ class PrivateChatManager(
// MARK: - Public Getters
fun getPeerFingerprint(peerID: String): String? {
return peerIDToPublicKeyFingerprint[peerID]
}
fun getAllPeerFingerprints(): Map<String, String> {
return peerIDToPublicKeyFingerprint.toMap()
}
@@ -1,5 +1,6 @@
package com.bitchat.android.ui
import android.util.Log
import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.foundation.*
@@ -263,28 +264,40 @@ fun PeopleSection(
val privateChats by viewModel.privateChats.observeAsState(emptyMap())
val favoritePeers by viewModel.favoritePeers.observeAsState(emptySet())
// Pre-calculate all favorite states to ensure proper state synchronization
val peerFavoriteStates = remember(favoritePeers, connectedPeers) {
connectedPeers.associateWith { peerID ->
val fingerprint = viewModel.privateChatManager.getPeerFingerprint(peerID)
favoritePeers.contains(fingerprint)
}
}
Log.d("SidebarComponents", "Recomposing with ${favoritePeers.size} favorites, peer states: $peerFavoriteStates")
// Smart sorting: unread DMs first, then by most recent DM, then favorites, then alphabetical
val sortedPeers = connectedPeers.sortedWith(
compareBy<String> { !hasUnreadPrivateMessages.contains(it) } // Unread DM senders first
.thenByDescending { privateChats[it]?.maxByOrNull { msg -> msg.timestamp }?.timestamp?.time ?: 0L } // Most recent DM (convert Date to Long)
.thenBy {
val fingerprint = viewModel.privateChatManager.getPeerFingerprint(it)
fingerprint == null || !favoritePeers.contains(fingerprint)
} // Favorites
.thenBy { !(peerFavoriteStates[it] ?: false) } // Favorites first
.thenBy { (if (it == nickname) "You" else (peerNicknames[it] ?: it)).lowercase() } // Alphabetical
)
sortedPeers.forEach { peerID ->
val isFavorite = peerFavoriteStates[peerID] ?: false
PeerItem(
peerID = peerID,
displayName = if (peerID == nickname) "You" else (peerNicknames[peerID] ?: peerID),
signalStrength = peerRSSI[peerID] ?: 0,
isSelected = peerID == selectedPrivatePeer,
isFavorite = favoritePeers.contains(viewModel.privateChatManager.getPeerFingerprint(peerID)),
isFavorite = isFavorite,
hasUnreadDM = hasUnreadPrivateMessages.contains(peerID),
colorScheme = colorScheme,
onItemClick = { onPrivateChatStart(peerID) },
onToggleFavorite = { viewModel.toggleFavorite(peerID) }
onToggleFavorite = {
Log.d("SidebarComponents", "Sidebar toggle favorite: peerID=$peerID, currentFavorite=$isFavorite")
viewModel.toggleFavorite(peerID)
}
)
}
}