diff --git a/app/src/main/java/com/bitchat/android/MainActivity.kt b/app/src/main/java/com/bitchat/android/MainActivity.kt index 9b95c341..bfe57ca5 100644 --- a/app/src/main/java/com/bitchat/android/MainActivity.kt +++ b/app/src/main/java/com/bitchat/android/MainActivity.kt @@ -2,6 +2,7 @@ package com.bitchat.android import android.content.Intent import android.os.Bundle +import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.OnBackPressedCallback import androidx.activity.compose.setContent @@ -253,7 +254,7 @@ class MainActivity : ComponentActivity() { } private fun checkOnboardingStatus() { - android.util.Log.d("MainActivity", "Checking onboarding status") + Log.d("MainActivity", "Checking onboarding status") lifecycleScope.launch { // Small delay to show the checking state @@ -268,12 +269,12 @@ class MainActivity : ComponentActivity() { * Check Bluetooth status and proceed with onboarding flow */ private fun checkBluetoothAndProceed() { - // android.util.Log.d("MainActivity", "Checking Bluetooth status") + // Log.d("MainActivity", "Checking Bluetooth status") // For first-time users, skip Bluetooth check and go straight to permissions // We'll check Bluetooth after permissions are granted if (permissionManager.isFirstTimeLaunch()) { - android.util.Log.d("MainActivity", "First-time launch, skipping Bluetooth check - will check after permissions") + Log.d("MainActivity", "First-time launch, skipping Bluetooth check - will check after permissions") proceedWithPermissionCheck() return } @@ -289,7 +290,7 @@ class MainActivity : ComponentActivity() { } BluetoothStatus.DISABLED -> { // Show Bluetooth enable screen (should have permissions as existing user) - android.util.Log.d("MainActivity", "Bluetooth disabled, showing enable screen") + Log.d("MainActivity", "Bluetooth disabled, showing enable screen") mainViewModel.updateOnboardingState(OnboardingState.BLUETOOTH_CHECK) mainViewModel.updateBluetoothLoading(false) } @@ -306,20 +307,20 @@ class MainActivity : ComponentActivity() { * Proceed with permission checking */ private fun proceedWithPermissionCheck() { - android.util.Log.d("MainActivity", "Proceeding with permission check") + Log.d("MainActivity", "Proceeding with permission check") lifecycleScope.launch { delay(200) // Small delay for smooth transition if (permissionManager.isFirstTimeLaunch()) { - android.util.Log.d("MainActivity", "First time launch, showing permission explanation") + Log.d("MainActivity", "First time launch, showing permission explanation") mainViewModel.updateOnboardingState(OnboardingState.PERMISSION_EXPLANATION) } else if (permissionManager.areAllPermissionsGranted()) { - android.util.Log.d("MainActivity", "Existing user with permissions, initializing app") + Log.d("MainActivity", "Existing user with permissions, initializing app") mainViewModel.updateOnboardingState(OnboardingState.INITIALIZING) initializeApp() } else { - android.util.Log.d("MainActivity", "Existing user missing permissions, showing explanation") + Log.d("MainActivity", "Existing user missing permissions, showing explanation") mainViewModel.updateOnboardingState(OnboardingState.PERMISSION_EXPLANATION) } } @@ -329,7 +330,7 @@ class MainActivity : ComponentActivity() { * Handle Bluetooth enabled callback */ private fun handleBluetoothEnabled() { - android.util.Log.d("MainActivity", "Bluetooth enabled by user") + Log.d("MainActivity", "Bluetooth enabled by user") mainViewModel.updateBluetoothLoading(false) mainViewModel.updateBluetoothStatus(BluetoothStatus.ENABLED) checkLocationAndProceed() @@ -339,12 +340,12 @@ class MainActivity : ComponentActivity() { * Check Location services status and proceed with onboarding flow */ private fun checkLocationAndProceed() { - android.util.Log.d("MainActivity", "Checking location services status") + Log.d("MainActivity", "Checking location services status") // For first-time users, skip location check and go straight to permissions // We'll check location after permissions are granted if (permissionManager.isFirstTimeLaunch()) { - android.util.Log.d("MainActivity", "First-time launch, skipping location check - will check after permissions") + Log.d("MainActivity", "First-time launch, skipping location check - will check after permissions") proceedWithPermissionCheck() return } @@ -360,13 +361,13 @@ class MainActivity : ComponentActivity() { } LocationStatus.DISABLED -> { // Show location enable screen (should have permissions as existing user) - android.util.Log.d("MainActivity", "Location services disabled, showing enable screen") + Log.d("MainActivity", "Location services disabled, showing enable screen") mainViewModel.updateOnboardingState(OnboardingState.LOCATION_CHECK) mainViewModel.updateLocationLoading(false) } LocationStatus.NOT_AVAILABLE -> { // Device doesn't support location services (very unusual) - android.util.Log.e("MainActivity", "Location services not available") + Log.e("MainActivity", "Location services not available") mainViewModel.updateOnboardingState(OnboardingState.LOCATION_CHECK) mainViewModel.updateLocationLoading(false) } @@ -377,7 +378,7 @@ class MainActivity : ComponentActivity() { * Handle Location enabled callback */ private fun handleLocationEnabled() { - android.util.Log.d("MainActivity", "Location services enabled by user") + Log.d("MainActivity", "Location services enabled by user") mainViewModel.updateLocationLoading(false) mainViewModel.updateLocationStatus(LocationStatus.ENABLED) checkBatteryOptimizationAndProceed() @@ -387,10 +388,10 @@ class MainActivity : ComponentActivity() { * Handle Location disabled callback */ private fun handleLocationDisabled(message: String) { - android.util.Log.w("MainActivity", "Location services disabled or failed: $message") + Log.w("MainActivity", "Location services disabled or failed: $message") mainViewModel.updateLocationLoading(false) mainViewModel.updateLocationStatus(locationStatusManager.checkLocationStatus()) - + when { mainViewModel.locationStatus.value == LocationStatus.NOT_AVAILABLE -> { // Show permanent error for devices without location services @@ -408,7 +409,7 @@ class MainActivity : ComponentActivity() { * Handle Bluetooth disabled callback */ private fun handleBluetoothDisabled(message: String) { - android.util.Log.w("MainActivity", "Bluetooth disabled or failed: $message") + Log.w("MainActivity", "Bluetooth disabled or failed: $message") mainViewModel.updateBluetoothLoading(false) mainViewModel.updateBluetoothStatus(bluetoothStatusManager.checkBluetoothStatus()) @@ -421,12 +422,12 @@ class MainActivity : ComponentActivity() { message.contains("Permission") && permissionManager.isFirstTimeLaunch() -> { // During first-time onboarding, if Bluetooth enable fails due to permissions, // proceed to permission explanation screen where user will grant permissions first - android.util.Log.d("MainActivity", "Bluetooth enable requires permissions, proceeding to permission explanation") + Log.d("MainActivity", "Bluetooth enable requires permissions, proceeding to permission explanation") proceedWithPermissionCheck() } message.contains("Permission") -> { // For existing users, redirect to permission explanation to grant missing permissions - android.util.Log.d("MainActivity", "Bluetooth enable requires permissions, showing permission explanation") + Log.d("MainActivity", "Bluetooth enable requires permissions, showing permission explanation") mainViewModel.updateOnboardingState(OnboardingState.PERMISSION_EXPLANATION) } else -> { @@ -437,7 +438,7 @@ class MainActivity : ComponentActivity() { } private fun handleOnboardingComplete() { - android.util.Log.d("MainActivity", "Onboarding completed, checking Bluetooth, Location, and Battery Optimization before initializing app") + Log.d("MainActivity", "Onboarding completed, checking Bluetooth and Location before initializing app") // After permissions are granted, re-check Bluetooth, Location, and Battery Optimization status val currentBluetoothStatus = bluetoothStatusManager.checkBluetoothStatus() @@ -451,14 +452,14 @@ class MainActivity : ComponentActivity() { when { currentBluetoothStatus != BluetoothStatus.ENABLED -> { // Bluetooth still disabled, but now we have permissions to enable it - android.util.Log.d("MainActivity", "Permissions granted, but Bluetooth still disabled. Showing Bluetooth enable screen.") + Log.d("MainActivity", "Permissions granted, but Bluetooth still disabled. Showing Bluetooth enable screen.") mainViewModel.updateBluetoothStatus(currentBluetoothStatus) mainViewModel.updateOnboardingState(OnboardingState.BLUETOOTH_CHECK) mainViewModel.updateBluetoothLoading(false) } currentLocationStatus != LocationStatus.ENABLED -> { // Location services still disabled, but now we have permissions to enable it - android.util.Log.d("MainActivity", "Permissions granted, but Location services still disabled. Showing Location enable screen.") + Log.d("MainActivity", "Permissions granted, but Location services still disabled. Showing Location enable screen.") mainViewModel.updateLocationStatus(currentLocationStatus) mainViewModel.updateOnboardingState(OnboardingState.LOCATION_CHECK) mainViewModel.updateLocationLoading(false) @@ -472,7 +473,7 @@ class MainActivity : ComponentActivity() { } else -> { // Both are enabled, proceed to app initialization - android.util.Log.d("MainActivity", "Both Bluetooth and Location services are enabled, proceeding to initialization") + Log.d("MainActivity", "Both Bluetooth and Location services are enabled, proceeding to initialization") mainViewModel.updateOnboardingState(OnboardingState.INITIALIZING) initializeApp() } @@ -480,7 +481,7 @@ class MainActivity : ComponentActivity() { } private fun handleOnboardingFailed(message: String) { - android.util.Log.e("MainActivity", "Onboarding failed: $message") + Log.e("MainActivity", "Onboarding failed: $message") mainViewModel.updateErrorMessage(message) mainViewModel.updateOnboardingState(OnboardingState.ERROR) } @@ -550,7 +551,7 @@ class MainActivity : ComponentActivity() { } private fun initializeApp() { - android.util.Log.d("MainActivity", "Starting app initialization") + Log.d("MainActivity", "Starting app initialization") lifecycleScope.launch { try { @@ -558,12 +559,12 @@ 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, initializing chat system") + Log.d("MainActivity", "Permissions verified, initializing chat system") // Ensure all permissions are still granted (user might have revoked in settings) if (!permissionManager.areAllPermissionsGranted()) { val missing = permissionManager.getMissingPermissions() - android.util.Log.w("MainActivity", "Permissions revoked during initialization: $missing") + Log.w("MainActivity", "Permissions revoked during initialization: $missing") handleOnboardingFailed("Some permissions were revoked. Please grant all permissions to continue.") return@launch } @@ -572,19 +573,17 @@ class MainActivity : ComponentActivity() { meshService.delegate = chatViewModel meshService.startServices() - android.util.Log.d("MainActivity", "Mesh service started successfully") + Log.d("MainActivity", "Mesh service started successfully") // Handle any notification intent handleNotificationIntent(intent) // Small delay to ensure mesh service is fully initialized delay(500) - - android.util.Log.d("MainActivity", "App initialization complete") + Log.d("MainActivity", "App initialization complete") mainViewModel.updateOnboardingState(OnboardingState.COMPLETE) - } catch (e: Exception) { - android.util.Log.e("MainActivity", "Failed to initialize app", e) + Log.e("MainActivity", "Failed to initialize app", e) handleOnboardingFailed("Failed to initialize the app: ${e.message}") } } @@ -605,11 +604,11 @@ class MainActivity : ComponentActivity() { // Set app foreground state meshService.connectionManager.setAppBackgroundState(false) chatViewModel.setAppBackgroundState(false) - + // Check if Bluetooth was disabled while app was backgrounded val currentBluetoothStatus = bluetoothStatusManager.checkBluetoothStatus() if (currentBluetoothStatus != BluetoothStatus.ENABLED) { - android.util.Log.w("MainActivity", "Bluetooth disabled while app was backgrounded") + Log.w("MainActivity", "Bluetooth disabled while app was backgrounded") mainViewModel.updateBluetoothStatus(currentBluetoothStatus) mainViewModel.updateOnboardingState(OnboardingState.BLUETOOTH_CHECK) mainViewModel.updateBluetoothLoading(false) @@ -619,7 +618,7 @@ class MainActivity : ComponentActivity() { // Check if location services were disabled while app was backgrounded val currentLocationStatus = locationStatusManager.checkLocationStatus() if (currentLocationStatus != LocationStatus.ENABLED) { - android.util.Log.w("MainActivity", "Location services disabled while app was backgrounded") + Log.w("MainActivity", "Location services disabled while app was backgrounded") mainViewModel.updateLocationStatus(currentLocationStatus) mainViewModel.updateOnboardingState(OnboardingState.LOCATION_CHECK) mainViewModel.updateLocationLoading(false) @@ -651,7 +650,7 @@ class MainActivity : ComponentActivity() { val senderNickname = intent.getStringExtra(com.bitchat.android.ui.NotificationManager.EXTRA_SENDER_NICKNAME) if (peerID != null) { - android.util.Log.d("MainActivity", "Opening private chat with $senderNickname (peerID: $peerID) from notification") + Log.d("MainActivity", "Opening private chat with $senderNickname (peerID: $peerID) from notification") // Open the private chat with this peer chatViewModel.startPrivateChat(peerID) @@ -661,25 +660,6 @@ class MainActivity : ComponentActivity() { } } } - - /** - * Restart mesh services (for debugging/troubleshooting) - */ - fun restartMeshServices() { - if (mainViewModel.onboardingState.value == 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() @@ -687,18 +667,18 @@ class MainActivity : ComponentActivity() { // Cleanup location status manager try { locationStatusManager.cleanup() - android.util.Log.d("MainActivity", "Location status manager cleaned up successfully") + Log.d("MainActivity", "Location status manager cleaned up successfully") } catch (e: Exception) { - android.util.Log.w("MainActivity", "Error cleaning up location status manager: ${e.message}") + Log.w("MainActivity", "Error cleaning up location status manager: ${e.message}") } // Stop mesh services if app was fully initialized if (mainViewModel.onboardingState.value == OnboardingState.COMPLETE) { try { meshService.stopServices() - android.util.Log.d("MainActivity", "Mesh services stopped successfully") + Log.d("MainActivity", "Mesh services stopped successfully") } catch (e: Exception) { - android.util.Log.w("MainActivity", "Error stopping mesh services in onDestroy: ${e.message}") + Log.w("MainActivity", "Error stopping mesh services in onDestroy: ${e.message}") } } }