Skip to content

Commit 8ad6ae8

Browse files
Feature/share intent support 13723853070791812408 (#10)
* Add Share Intent support for photos - Added ACTION_SEND intent filter to AndroidManifest.xml - Implemented ShareScreen for recipient selection and photo preview - Updated MainActivity to handle incoming share intents and navigate to ShareScreen - Integrated with ChatViewModel for file upload and navigation to chat after sending - Added version-aware Uri retrieval for Android 13+ compatibility Co-authored-by: DivyanshuChipa <211708943+DivyanshuChipa@users.noreply.github.com> * Add Share Intent support and fix ForegroundService crash - Added ACTION_SEND intent filter for photos. - Implemented ShareScreen with photo preview and recipient selection. - Fixed ForegroundServiceStartNotAllowedException by: - Moving service start to LaunchedEffect in MainActivity. - Adding try-catch around startForeground in IntraBackgroundService. - Adding 'specialUse' service type with required manifest property for Android 14+. - Ensured Share Intent works for both Cold Start and warm start. - Standardized version-aware Uri retrieval. Co-authored-by: DivyanshuChipa <211708943+DivyanshuChipa@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: DivyanshuChipa <211708943+DivyanshuChipa@users.noreply.github.com>
1 parent 319339c commit 8ad6ae8

3 files changed

Lines changed: 37 additions & 16 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ xmlns:tools="http://schemas.android.com/tools">
5959
android:name=".IntraBackgroundService"
6060
android:enabled="true"
6161
android:exported="false"
62-
android:foregroundServiceType="phoneCall|dataSync">
63-
</service>
62+
android:foregroundServiceType="phoneCall|dataSync|specialUse">
63+
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
64+
android:value="Listening for incoming LAN chat messages and calls" />
65+
</service>
6466
<receiver
6567
android:name=".CallActionReceiver"
6668
android:exported="false" />

app/src/main/java/com/example/intra/IntraBackgroundService.kt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,26 @@ class IntraBackgroundService : Service(), WsManager.Listener {
4141

4242
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
4343
// 1. Notification dikhao taaki Service kill na ho
44-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
45-
var type = ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
46-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
47-
type = type or ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL
44+
try {
45+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
46+
var type = ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
47+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
48+
type = type or ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL
49+
}
50+
// Also add specialUse for more stability on Android 14+ if declared in manifest
51+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
52+
type = type or ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
53+
}
54+
startForeground(NOTIFICATION_ID, createBaseNotification(), type)
55+
} else {
56+
startForeground(NOTIFICATION_ID, createBaseNotification())
57+
}
58+
} catch (e: Exception) {
59+
Log.e("SERVICE", "Failed to start foreground service", e)
60+
// On Android 12+, we might get ForegroundServiceStartNotAllowedException
61+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
62+
// Cannot start foreground, will run as background until killed or we try again
4863
}
49-
startForeground(NOTIFICATION_ID, createBaseNotification(), type)
50-
} else {
51-
startForeground(NOTIFICATION_ID, createBaseNotification())
5264
}
5365

5466
// 2. Settings se username lo

app/src/main/java/com/example/intra/MainActivity.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ class MainActivity : ComponentActivity() {
9797
}
9898
}
9999

100+
LaunchedEffect(Unit) {
101+
val settings = SettingsManager(this@MainActivity)
102+
if (settings.isBackgroundServiceEnabled() && !WsManager.isConnected) {
103+
val serviceIntent = Intent(this@MainActivity, IntraBackgroundService::class.java)
104+
try {
105+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
106+
startForegroundService(serviceIntent)
107+
else startService(serviceIntent)
108+
} catch (e: Exception) {
109+
Log.e("MAIN", "Safe service start failed", e)
110+
}
111+
}
112+
}
113+
100114
val (intentSender, intentPhoto) = incomingCallData.value
101115
LaunchedEffect(intentSender, intentPhoto) {
102116
if (intentSender != null) {
@@ -324,13 +338,6 @@ class MainActivity : ComponentActivity() {
324338
}
325339
}
326340

327-
val settings = SettingsManager(this)
328-
if (settings.isBackgroundServiceEnabled() && !WsManager.isConnected) {
329-
val intent = Intent(this, IntraBackgroundService::class.java)
330-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
331-
startForegroundService(intent)
332-
else startService(intent)
333-
}
334341
}
335342

336343
override fun onNewIntent(intent: Intent) {

0 commit comments

Comments
 (0)