-
Notifications
You must be signed in to change notification settings - Fork 85
Fix camera capture crash on devices with lower RAM #4635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+97
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
12e956a
Fix camera capture crash on devices with lower RAM
d393955
Try fix with only foreground notif
286d964
Merge remote-tracking branch 'origin/master' into bugfix/camera-oom-kill
a2f94d7
Added foreground notif to manifest
b6d264a
Remove redundant comment
9f5046c
Update comments
xkello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
app/android/src/uk/co/lutraconsulting/CameraForegroundService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /*************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| package uk.co.lutraconsulting; | ||
|
|
||
| import android.os.Build; | ||
| import android.os.IBinder; | ||
| import android.app.Service; | ||
| import android.app.PendingIntent; | ||
| import android.content.Intent; | ||
| import android.content.pm.ServiceInfo; | ||
|
|
||
| import android.app.Notification; | ||
| import android.app.NotificationChannel; | ||
| import android.app.NotificationManager; | ||
|
|
||
| /** | ||
| * Runs in the app's default process for as long as CameraActivity is waiting on the external | ||
| * camera app, to raise this process's priority and make it much less likely to be killed while | ||
| * the camera is in the foreground. | ||
| * This is a mitigation, not a guarantee -- under severe enough memory pressure the process can | ||
| * still be killed; CameraActivity does not currently resume state after that, so a kill mid-wait | ||
| * causes the camera capture to restart from scratch on the recreated process. | ||
| */ | ||
| public class CameraForegroundService extends Service { | ||
|
|
||
| private static final String CHANNEL_ID = "CameraForegroundServiceChannel"; | ||
| private static final int SERVICE_ID = 1011; | ||
|
|
||
| @Override | ||
| public IBinder onBind(Intent intent) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public int onStartCommand(Intent intent, int flags, int startId) { | ||
| NotificationChannel serviceChannel = new NotificationChannel( | ||
| CHANNEL_ID, | ||
| "Camera Foreground Service Channel", | ||
| NotificationManager.IMPORTANCE_LOW | ||
| ); | ||
|
|
||
| NotificationManager manager = getSystemService(NotificationManager.class); | ||
| manager.createNotificationChannel(serviceChannel); | ||
|
|
||
| Intent notificationIntent = new Intent(this, MMActivity.class); | ||
| PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE); | ||
|
|
||
| Notification notification = new Notification.Builder(this, CHANNEL_ID) | ||
| .setSmallIcon(R.drawable.ic_notification) | ||
| .setContentTitle("Waiting for photo") | ||
| .setColor(getResources().getColor(R.color.grassColor)) | ||
| .setContentIntent(pendingIntent) | ||
| .build(); | ||
|
|
||
| // We never request the POST_NOTIFICATIONS runtime permission from the user, so this | ||
| // notification will silently not be shown unless the user has manually enabled it for | ||
| // the app in system Settings. startForeground() still succeeds and still elevates the | ||
| // process's priority either way -- the permission only gates the notification's | ||
| // visibility, not the foreground service state itself. | ||
| if (Build.VERSION.SDK_INT >= 35) { | ||
| startForeground(SERVICE_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE); | ||
| } else { | ||
| startForeground(SERVICE_ID, notification); | ||
| } | ||
|
|
||
| // if this service alone gets killed there is nothing useful to resume, it only exists to | ||
| // keep the process's priority elevated while CameraActivity waits on startActivityForResult(). | ||
| return START_NOT_STICKY; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.