I am trying to capture the image every 1 second. I use a handler for the task. Here is the code for that part.
val handler = Handler()
button.setOnClickListener {
prepareUIForCapture()
if(isRunning){
handler.removeCallbacksAndMessages(null)
restartActivity()
}else{
button.text = "Stop"
handler.postDelayed(object : Runnable {
override fun run(){
twoLens.reset()
twoLens.isTwoLensShot = true
MainActivity.cameraParams.get(dualCamLogicalId).let {
if (it?.isOpen == true) {
Logd("In onClick. Taking Dual Cam Photo on logical camera: " + dualCamLogicalId)
takePicture(this#MainActivity, it)
Toast.makeText(applicationContext, "Captured", Toast.LENGTH_LONG).show()
}
}
handler.postDelayed(this, 1000)
}
}, 1000)
}
isRunning = !isRunning
}
}
This works as intended, however after around 3 minutes, the camera preview lags and the capture stops and eventually the application crashes.
Here is the code for takePicture()
fun takePicture(activity: MainActivity, params: CameraParams) {
if (!params.isOpen) {
return
}
try {
Logd("In captureStillPicture.")
val camera = params.captureSession?.getDevice()
if (null != camera) {
params.captureBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE)
// params.captureBuilder?.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE)
params.captureBuilder?.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO)
if (params.id.equals(dualCamLogicalId) && twoLens.isTwoLensShot) {
val normalParams: CameraParams? = MainActivity.cameraParams.get(normalLensId)
val wideParams: CameraParams? = MainActivity.cameraParams.get(wideAngleId)
if (null == normalParams || null == wideParams)
return
Logd("In captureStillPicture. This is a Dual Cam shot.")
params.captureBuilder?.addTarget(normalParams.imageReader?.surface!!)
params.captureBuilder?.addTarget(wideParams.imageReader?.surface!!)
}
params.captureBuilder?.set(CaptureRequest.JPEG_QUALITY, 50)
// Orientation
val rotation = activity.getWindowManager().getDefaultDisplay().getRotation()
var capturedImageRotation = getOrientation(params, rotation)
params.captureBuilder?.set(CaptureRequest.JPEG_ORIENTATION, capturedImageRotation)
try {
params.captureSession?.stopRepeating()
} catch (e: CameraAccessException) {
e.printStackTrace()
}
//Do the capture
if (28 <= Build.VERSION.SDK_INT )
params.captureSession?.captureSingleRequest(params.captureBuilder?.build(), params.backgroundExecutor, StillCaptureSessionCallback(activity, params))
else
params.captureSession?.capture(params.captureBuilder?.build(), StillCaptureSessionCallback(activity, params),
params.backgroundHandler)
}
} catch (e: CameraAccessException) {
e.printStackTrace()
} catch (e: IllegalStateException) {
Logd("captureStillPicture IllegalStateException, aborting: " + e)
}
}
Here is the part to create the preview.
fun createCameraPreviewSession(activity: MainActivity, camera: CameraDevice, params: CameraParams) {
Logd("In createCameraPreviewSession.")
if (!params.isOpen) {
return
}
try {
if (Build.VERSION.SDK_INT >= 28 && params.id.equals(MainActivity.dualCamLogicalId)) {
val normalParams: CameraParams? = MainActivity.cameraParams.get(normalLensId)
val wideParams: CameraParams? = MainActivity.cameraParams.get(wideAngleId)
Logd("In createCameraPreview. This is a Dual Cam stream. Starting up simultaneous streams.")
if (null == normalParams || null == wideParams)
return
// val normalTexture = normalParams.previewTextureView?.surfaceTexture
val wideTexture = wideParams.previewTextureView?.surfaceTexture
// if (null == normalTexture || null == wideTexture)
// return
// normalTexture.setDefaultBufferSize(400,400)
wideTexture!!.setDefaultBufferSize(400,400)
// val normalSurface = Surface(normalTexture)
val wideSurface = Surface(wideTexture)
val wideOutputConfigPreview = OutputConfiguration(wideSurface)
val wideOutputConfigImageReader = OutputConfiguration(wideParams.imageReader?.surface!!)
wideOutputConfigPreview.setPhysicalCameraId(wideAngleId)
wideOutputConfigImageReader.setPhysicalCameraId(wideAngleId)
// val normalOutputConfigPreview = OutputConfiguration(normalSurface)
val normalOutputConfigImageReader = OutputConfiguration(normalParams.imageReader?.surface!!)
normalOutputConfigImageReader.setPhysicalCameraId(normalLensId)
val sessionConfig = SessionConfiguration(SessionConfiguration.SESSION_REGULAR,
Arrays.asList( normalOutputConfigImageReader, wideOutputConfigPreview, wideOutputConfigImageReader),
params.backgroundExecutor, PreviewSessionStateCallback(activity, params))
params.previewBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
// params.previewBuilder?.addTarget(normalSurface)
params.previewBuilder?.addTarget(wideSurface)
camera.createCaptureSession(sessionConfig)
} else {
val texture = params.previewTextureView?.surfaceTexture
if (null == texture)
return
val surface = Surface(texture)
if (null == surface)
return
params.previewBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
params.previewBuilder?.addTarget(surface)
val imageSurface = params.imageReader?.surface
if (null == imageSurface)
return
if (Build.VERSION.SDK_INT >= 28) {
val sessionConfig = SessionConfiguration(SessionConfiguration.SESSION_REGULAR,
Arrays.asList(OutputConfiguration(surface), OutputConfiguration(imageSurface)),
params.backgroundExecutor, PreviewSessionStateCallback(activity, params))
camera.createCaptureSession(sessionConfig)
} else {
camera.createCaptureSession(Arrays.asList(surface, imageSurface),
PreviewSessionStateCallback(activity, params), params.backgroundHandler)
}
}
} catch (e: CameraAccessException) {
e.printStackTrace()
} catch (e: IllegalStateException) {
Logd("createCameraPreviewSession IllegalStateException, aborting: " + e)
}
}
I tried clearing the buffers and bitmaps, but the issue persists. I am thinking I might not be using the correct approach towards continuous capture. Is this way correct?
Any idea why the preview freezes?
Here is the original repo: https://github.com/google/basicbokeh
I did changes to suit my needs since it did not run on my device (HWMate20Pro) off the shelf and I needed continuous captures from both lenses.
Thanks.
Related
I'm using the CameraX for the first time, and following the android documentation guide, but having issues, search a lot but did not find anything helping, so basically I'm trying to capture screen using CameraX and my captureVideo() method code is,
#RequiresApi(api = Build.VERSION_CODES.P)
public void takeVideo() {
ContentValues contentValues;
if (videoCapture == null) {
return;
}
Recording curRecording = recording;
if (curRecording != null) {
// Stop the current recording session.
recording.stop();
recording = null;
return;
}
// create and start a new recording session
String name = DateFormat.getInstance().format(new Date().getTime()).toString();
contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/CameraX-Video");
}
MediaStoreOutputOptions mediaStoreOutputOptions = new MediaStoreOutputOptions.Builder(getContentResolver(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
.setContentValues(contentValues)
.build();
PendingRecording pendingRecording = (videoCapture.getOutput()
.prepareRecording(this, mediaStoreOutputOptions));
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
pendingRecording.withAudioEnabled();
recording = pendingRecording.start(getMainExecutor(), new Consumer<VideoRecordEvent>() {
#Override
public void accept(VideoRecordEvent videoRecordEvent) {
if (videoRecordEvent instanceof VideoRecordEvent.Start) {
btnTakeVide.setText("Stop Video");
} else if (videoRecordEvent instanceof VideoRecordEvent.Pause) {
// Handle the case where the active recording is paused
} else if (videoRecordEvent instanceof VideoRecordEvent.Resume) {
// Handles the case where the active recording is resumed
} else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
btnTakeVide.setText("Start Video");
VideoRecordEvent.Finalize finalizeEvent =
(VideoRecordEvent.Finalize) videoRecordEvent;
// Handles a finalize event for the active recording, checking Finalize.getError()
if (!finalizeEvent.hasError()) {
String msg = "Video capture succeeded: " + ((VideoRecordEvent.Finalize) videoRecordEvent).getOutputResults().getOutputUri();
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT)
.show();
} else {
if (recording != null) {
recording.close();
recording = null;
Log.e("TAG", "Video capture ends with error: ");
}
}
}
}
});
}
}
code inside setPreview is,
private void startCameraPreview() {
listenableFuture = ProcessCameraProvider.getInstance(MainActivity.this);
listenableFuture.addListener(new Runnable() {
#Override
public void run() {
try {
cameraProvider = listenableFuture.get();
preview = new Preview.Builder().build();
preview.setSurfaceProvider(cameraView.getSurfaceProvider());
recorder = new Recorder.Builder()
.setQualitySelector(QualitySelector.from(Quality.LOWEST))
.build();
videoCapture = VideoCapture.withOutput(recorder);
cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle(MainActivity.this, cameraSelector, preview, videoCapture);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, ContextCompat.getMainExecutor(this));
}
the exception being thrown is,
I inserted following dependencies,
def camerax_version = "1.2.0-alpha04"
implementation "androidx.camera:camera-lifecycle:$camerax_version"
implementation "androidx.camera:camera-core:$camerax_version"
implementation "androidx.camera:camera-camera2:$camerax_version"
implementation "androidx.camera:camera-view:1.1.0"
Sorry developers, I forgot to add the video dependency,
implementation "androidx.camera:camera-video:${camerax_version}"
after adding dependency it works fine.
I use this code for Camera X binding. When I run app for the first time everything works fine. But when I close app and start it again I get "ViewPort is NULL". Why viewPort is NULL I do not understand. What is wrong with my code?
#OptIn(markerClass = androidx.camera.lifecycle.ExperimentalUseCaseGroupLifecycle.class)
private void bindAllCameraUseCases() {
if (cameraProvider != null) {
// As required by CameraX API, unbinds all use cases before trying to re-bind any of them.
cameraProvider.unbindAll();
createPreviewUseCase();
createImageCaptureUseCase();
createAnalysisUseCase();
if (previewUseCase != null && analysisUseCase != null && imageCaptureUseCase != null) {
ViewPort viewPort = ((PreviewView)findViewById(R.id.preview_view)).getViewPort();
if (viewPort != null) {
#OptIn(markerClass = androidx.camera.core.ExperimentalUseCaseGroup.class)
UseCaseGroup useCaseGroup = new UseCaseGroup.Builder()
.addUseCase(previewUseCase)
.addUseCase(analysisUseCase)
.addUseCase(imageCaptureUseCase)
.setViewPort(viewPort)
.build();
camera = cameraProvider.bindToLifecycle(/* lifecycleOwner= */ this, cameraSelector, useCaseGroup);
LiveData<Integer> torchStateObserver = camera.getCameraInfo().getTorchState();
torchStateObserver.observe(this, new Observer<Integer>() {
#Override
public void onChanged(Integer state) {
torchState = state;
}
});
} else {
Toast.makeText(this, "VIEWPORT is NULL", Toast.LENGTH_SHORT).show();
}
}
}
}
This question already has answers here:
Context.startForegroundService() did not then call Service.startForeground()
(33 answers)
Closed 1 year ago.
I am trying to call startForegroundService(intent) but my app crashes after few seconds but I am able to call startForegroundService(intent) in my other activity it works fine and both the activities have the same code. I am not able to figure out what is causing this problem. I am trying to upload some photos in activity one it's working without any issues and in this activity it's crashing the app after few seconds I click on the button
Stack Trace
2021-07-18 22:48:16.233 8352-8352/com.android.testproject1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.testproject1, PID: 8352
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{dac122 u0 com.android.testproject1/.services.UploadServiceOffers}
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2005)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
My Code
Activity
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId==R.id.Post){
if (descriptionTextTitle.text.isEmpty()) {
// AnimationUtil.shakeView(mEditText, activity)
} else {
sharedPreferences.edit().putInt("count", ++serviceCount).apply()
Log.d(myTag, "On click sp $serviceCount")
val intent = Intent(this, UploadServiceOffers::class.java)
intent.putExtra("count", serviceCount)
intent.putExtra("notification_id", System.currentTimeMillis().toInt())
intent.action = UploadServiceOffers.ACTION_START_FOREGROUND_SERVICE_UPLOAD_OFFERS
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
Log.d(myTag, "Build Version OP")
// startForegroundService(activity!!,intent)
} else {
Log.d(myTag, "Build Version NP")
// activity!!.startService(intent)
startService(intent)
}
Toasty.info(this, "Uploading images..", Toasty.LENGTH_SHORT, true).show()
finish()
}
}
return super.onOptionsItemSelected(item)
}
Service
class UploadServiceOffers : Service() {
companion object{
val ACTION_START_FOREGROUND_SERVICE_UPLOAD_OFFERS = "ACTION_START_FOREGROUND_SERVICE"
}
private var count = 0
private var bitmap: Bitmap? = null
private var resized: Bitmap? = null
val myTag:String = "MyTag"
override fun onCreate() {
Log.d(myTag, "Service Created ")
super.onCreate()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d(myTag, "onStart $intent $flags $startId")
if (intent!=null) {
val action = intent.action
if (action == ACTION_START_FOREGROUND_SERVICE_UPLOAD_OFFERS) {
val imagesList: ArrayList<Image>? = intent.getParcelableArrayListExtra<Image>("imagesList")
val notificationId = intent.getIntExtra("notification_id", 3)
val postID = intent.getStringExtra("offerID")
val title=intent.getStringExtra("title")
val originalPrice=intent.getStringExtra("originalPrice")
val discountedPrice=intent.getStringExtra("discountedPrice")
val city=intent.getStringExtra("city")
val currentId = intent.getStringExtra("current_id")
val description = intent.getStringExtra("description")
val uploadedImagesUrl = intent.getStringArrayListExtra("uploadedImagesUrl")
count = intent.getIntExtra("count", 0)
if (imagesList != null) {
if (postID != null) {
if (title != null) {
if (city != null) {
if (originalPrice != null) {
if (discountedPrice != null) {
uploadImages(notificationId, 0, imagesList, currentId, description,
uploadedImagesUrl, postID,title,originalPrice,discountedPrice,city)
}
}
}
}
}
}
}
}
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
super.onDestroy()
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
private fun stopForegroundService(removeNotification: Boolean) {
Log.d(myTag,"Stop foreground service.")
// Stop foreground service and remove the notification.
stopForeground(removeNotification)
// Stop the foreground service.
stopSelf()
}
private fun notifyProgress(
id: Int,
icon: Int,
title: String,
message: String,
context: Context,
max_progress: Int,
progress: Int,
indeterminate: Boolean
) {
val builder = NotificationCompat.Builder(context, App.CHANNEL_ID2)
// Create notification default intent.
val intent = Intent()
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
builder.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(message)
.setOngoing(true)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setTicker(message)
.setChannelId(App.CHANNEL_ID2)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setProgress(max_progress, progress, indeterminate)
.setVibrate(LongArray(0))
startForeground(id, builder.build())
}
fun getImageUri(inContext: Context, inImage: Bitmap) {
val bytes = ByteArrayOutputStream()
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
var path: String?=null
try {
path = MediaStore.Images.Media.insertImage(inContext.contentResolver, inImage, "Title", null)
}catch (e: java.lang.Exception){
e.localizedMessage?.toString()?.let { Log.d(myTag, it) }
}
if (path!=null){
Log.d(myTag, Uri.parse(path).toString())
}
else{
Log.d(myTag, "Path is null ")
}
}
private fun uploadImages(
notification_id: Int,
index: Int,
imagesList: ArrayList<Image>,
currentUser_id: String?,
description: String?,
uploadedImagesUrl: ArrayList<String>?,
postID:String,
title: String,
originalPrice:String,
discountPrice:String,
city:String
) {
val imgCount = index + 1
var imageUri: Uri
val imageUri0: Uri?= Uri.fromFile(File(imagesList[index].path))
if (Build.VERSION.SDK_INT >= 29) {
try {
bitmap = imageUri0?.let { ImageDecoder.createSource(this.contentResolver,it)}?.let { ImageDecoder.decodeBitmap(it) }
} catch (e: IOException) {
e.printStackTrace()
e.localizedMessage?.toString()?.let { Log.d(myTag, " errore is $it") }
}
} else {
// Use older version
try {
bitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, imageUri0)
} catch (e: IOException) {
e.printStackTrace()
e.localizedMessage?.toString()?.let { Log.d(myTag, " errore is $it") }
}
}
// val bitmap = BitmapFactory.decodeFile(file.getAbsolutePath())
resized = bitmap?.let { Bitmap.createScaledBitmap(it, 600, 600, true) }
// Log.d(myTag, "path is ${bitmap.toString()}")
var path :String?=null
try {
// path = MediaStore.Images.Media.insertImage(this.contentResolver, resized, "Title", null)
path = MediaStore.Images.Media.insertImage(this.contentResolver, resized, "IMG_"
+ System.currentTimeMillis(), null)
Log.d(myTag, "path is $path")
}catch (e :java.lang.Exception){
Log.d(myTag, "path is exception $path" )
Log.d(myTag, e.localizedMessage.toString() )
}
imageUri = Uri.parse(path)
// imageUri = try {
//
// val compressedFile: File = id.zelory.compressor.Compressor()
// .setQuality(80)
// .setCompressFormat(Bitmap.CompressFormat.JPEG)
// .compressToFile(File(imagesList[index].path))
// Uri.fromFile(compressedFile)
// } catch (e: Exception) {
// e.printStackTrace()
// Uri.fromFile(File(imagesList!![index].path))
// }
val fileToUpload =
currentUser_id?.let {
FirebaseStorage.getInstance().reference.child("Offers").child(it)
.child(postID)
.child("Voila_"+ System.currentTimeMillis() + "_" + imagesList[index].name)
}
fileToUpload?.putFile(imageUri)?.addOnSuccessListener {
Log.d(myTag, "Uploaded Successfully")
fileToUpload.downloadUrl
.addOnSuccessListener { uri: Uri ->
uploadedImagesUrl!!.add(uri.toString())
val nextIndex = index + 1
try {
if (!TextUtils.isEmpty(imagesList[index + 1].path)) {
uploadImages(
notification_id,
nextIndex,
imagesList,
currentUser_id,
description,
uploadedImagesUrl,
postID,
title,originalPrice, discountPrice,city)
} else {
uploadPost(
notification_id,
currentUser_id,
description,
uploadedImagesUrl,
postID,
title,originalPrice,discountPrice,city)
}
} catch (e: Exception) {
e.printStackTrace()
uploadPost(
notification_id,
currentUser_id,
description,
uploadedImagesUrl, postID,
title, originalPrice, discountPrice,city)
}
}
.addOnFailureListener { obj: Exception -> obj.printStackTrace() }
}?.addOnFailureListener { obj: Exception ->
obj.printStackTrace()
obj.localizedMessage?.toString()?.let { Log.d(myTag, "Exception is $it") }
}?.addOnProgressListener { taskSnapshot: UploadTask.TaskSnapshot ->
if (count == 1) {
val title = "Uploading " + imgCount + "/" + imagesList.size + " images..."
val progress = (100.0 * taskSnapshot.bytesTransferred / taskSnapshot.totalByteCount).toInt()
notifyProgress(notification_id, R.drawable.stat_sys_upload, title, "$progress%",
applicationContext, 100, progress, true)
} else if (count > 1) {
notifyProgress(
notification_id,
R.drawable.stat_sys_upload,
"Viola",
"Uploading $count posts",
applicationContext,
100,
0,
true
)
}
}
}
}
In order to avoid crashing your app, you must call startForeground(notification) inside your onStartCommand method, to show notification immediately, or as soon as the service is started.
Check the information here
The new Context.startForegroundService() method starts a foreground service. The system allows apps to call Context.startForegroundService() even while the app is in the background. However, the app must call that service's startForeground() method within five seconds after the service is created.
I am using and Android App to start a Camera preview in order to do some OCR operations , but i can't make it work on Android 5.0.1 device.
i am getting an error on surfaceCreated :
Fail to connect to camera service
here is my sample code :
class OCRActivity : AppCompatActivity()
{
private var mCameraSource by Delegates.notNull<CameraSource>()
private var textRecognizer by Delegates.notNull<TextRecognizer>()
private var tryToOpenView = false
private val PERMISSION_REQUEST_CAMERA = 100
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ocr)
supportActionBar?.hide()
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
.......
generateOCRView()
}
override fun onResume()
{
super.onResume()
}
private fun generateOCRView()
{
// Create text Recognizer
textRecognizer = TextRecognizer.Builder(this).build()
if (!textRecognizer.isOperational)
{
Toast.makeText(this, MSG, Toast.LENGTH_LONG ).show()
finish()
return
}
var bestPreviewSize: Camera.Size? = null
if (isCameraPermissionGranted())
{
// Get best preview size for the camera
val mCamera = Camera.open()
if (mCamera != null)
{
val mCameraParameters = mCamera.parameters
if (mCameraParameters != null)
{
val thisBestPreviewSize = getBestPreviewSize(mCameraParameters)
if (thisBestPreviewSize != null)
{
bestPreviewSize = thisBestPreviewSize
}
}
}
}
// Init camera source to use high resolution and auto focus
mCameraSource = if (bestPreviewSize != null)
{
CameraSource.Builder(this, textRecognizer)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setAutoFocusEnabled(true)
.setRequestedFps(30.0f)
.setRequestedPreviewSize(bestPreviewSize.height, bestPreviewSize.width)
.build()
}
else
{
// Default camera size, can be stretched...
CameraSource.Builder(this, textRecognizer)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setAutoFocusEnabled(true)
.setRequestedFps(30.0f)
.setRequestedPreviewSize(1920, 1080)
.build()
}
surface_camera_preview.holder.addCallback(object : SurfaceHolder.Callback
{
override fun surfaceChanged(p0: SurfaceHolder?, p1: Int, p2: Int, p3: Int)
{
Log.d("surfaceChanged", "surfaceChanged")
return
}
override fun surfaceDestroyed(p0: SurfaceHolder?)
{
Log.d("surfaceDestroyed", "surfaceDestroyed")
mCameraSource.stop()
}
#SuppressLint("MissingPermission")
override fun surfaceCreated(p0: SurfaceHolder?)
{
try
{
// If the camera permission is granted
if (isCameraPermissionGranted())
{
Log.d("surfaceCreated", "surfaceCreated with permission granted")
// Show camera preview
mCameraSource.start(surface_camera_preview.holder)
}
else
{
requestForPermission()
}
} catch (e: Exception) {
Log.e(localClassName, "surfaceCreated error: ${e.message}")
finish()
}
}
})
textRecognizer.setProcessor(object : Detector.Processor<TextBlock>
{
override fun release()
{
return
}
override fun receiveDetections(detections: Detector.Detections<TextBlock>)
{
val items = detections.detectedItems
if (items.size() <= 0)
{
return
}
tryToOpenView = false
mrzResult.post {
val stringBuilder = StringBuilder()
for (i in 0 until items.size())
{
val item = items.valueAt(i)
stringBuilder.append(item.value)
stringBuilder.append("\n")
}
...........
}
}
})
}
/**
* isCameraPermissionGranted function: Check if camera permission is granted.
*
* #return Boolean: true if camera permission is granted and false if not.
*/
fun isCameraPermissionGranted(): Boolean
{
return ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
}
/**
* requestForPermission function:
*/
private fun requestForPermission()
{
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), PERMISSION_REQUEST_CAMERA)
}
#SuppressLint("MissingPermission")
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray)
{
// Go back to previous if camera permission is refuses
if (requestCode != PERMISSION_REQUEST_CAMERA)
{
// Go to previous view
Toast.makeText(this, MSG, Toast.LENGTH_LONG).show()
finish()
}
else
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
// If the camera permission is granted
if (isCameraPermissionGranted())
{
// Show camera preview
mCameraSource.start(surface_camera_preview.holder)
}
else
{
// Go to previous view
Toast.makeText(this, MSG, Toast.LENGTH_LONG).show()
finish()
}
}
else
{
// Go to previous view
Toast.makeText(this, MSG, Toast.LENGTH_LONG).show()
finish()
}
}
}
private fun getBestPreviewSize(parameters: Camera.Parameters): Camera.Size?
{
var bestSize: Camera.Size? = null
val sizeList = parameters.supportedPictureSizes
bestSize = sizeList[0]
for (i in 1 until sizeList.size)
{
if (sizeList[i].width * sizeList[i].height > bestSize!!.width * bestSize.height)
{
bestSize = sizeList[i]
}
}
return bestSize
}
}
Make sure that you set these permissions at the top of your manifest:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
If this isn't the case, initialize the mCamera as a instance variable outside of your method definition. Hope this helps :)
I know this:
A broadcast receiver is ALWAYS called because It sets the alarm for the next day.
When It works if the App is in Foreground, or if it is closed or if it is closed screen.
I use 'com.github.thefuntasty.hauler:library:2.0.0' to dismiss the activity with swipe
No other alarm is set at that time
I don't get any crash reports
I have NO idea, why the activity doesn't start. I tried doing try and catch, to see if that would help but it didn't.
EDIT:
Expected:
When the Broadcast reciever triggers, set the alarm for the next day, start the Activity in the foreground, even if the phone is locked and play the alarm.
On swipe down destroy the activity
Currently:
Sometimes it opens the activity other times it doesn't and I don't know why. If I set it for 5 min from now (locked screen, closed app), it works without problem. If I set it for tomorrow, it works 90% of the time.
I'm trying to figure out, why the activity sometimes doesn't start, because I'm not getting any crash reports from the firebase.
This is the code:
Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".views.alarm.broadcast.ResumeOnBootAlarm"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver
android:name=".views.alarm.broadcast.MyAlarm"
android:exported="true" />
<activity
android:name=".views.alarm.broadcast.TriggeredAlarmActivity"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:theme="#style/AppTheme.Draggable" />
This is my BroadCastReciever:
class MyAlarm : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val alarmID = intent.extras?.getInt(ARG_ALARM_ID)
//Immediatly set new alarm
val realm = Realm.getDefaultInstance()
val alarmFromRealm = DataHelper.getAlarm(realm, alarmID!!)
alarmFromRealm?.let { alarm ->
val shouldEnableAlarm = alarm.isEnabled && alarm.daysList!!.isNotEmpty()
DataHelper.enableAlarm(alarmID, shouldEnableAlarm, realm)
if (shouldEnableAlarm) {
setAlarm(context, alarm, false)
} else {
cancelAlarm(context, alarm.id)
}
}
try {
context.startActivity(TriggeredAlarmActivity.getIntent(context, alarmID))
} catch (ex: Exception) {
Crashlytics.logException(ex)
context.startActivity(TriggeredAlarmActivity.getIntent(MyApplication.appContext, null))
}
}
}
And this is the TriggeredActivity:
class TriggeredAlarmActivity : BaseActivity() {
private var currentUserVolume: Int = 0
private lateinit var timer: Timer
private lateinit var realm: Realm
private lateinit var vibrator: Vibrator
private var mediaPlayer: MediaPlayer? = null
private var shouldResumePlaying: Boolean = false
private val alarmID: Int?
get() {
return intent.extras?.getInt(ARG_ALARM_ID)
}
private val mAudioManager: AudioManager by lazy {
baseContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
}
/*
LifeCycle
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_triggered_alarm)
initTextClock()
showIfScreenIsLocked()
showDanceAnimation()
if (alarmID == null) {
val uri = getDefaultRingtone()
mediaPlayer= MediaPlayer()
mediaPlayer?.setDataSource(this,uri)
mediaPlayer?.prepare()
mediaPlayer?.start()
} else {
realm = Realm.getDefaultInstance()
vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
val alarmFromRealm = DataHelper.getAlarm(realm, alarmID!!)
alarmFromRealm?.let { alarm ->
try {
if (alarm.useDefaultRingtone) {
val uri = getDefaultRingtone()
mediaPlayer = MediaPlayer()
mediaPlayer?.let { increaseVolumeOverTime(it, alarm.shouldVibrate) }
mediaPlayer?.setDataSource(this, uri)
mediaPlayer?.isLooping = true
shouldResumePlaying = alarm.shouldResumePlaying
mediaPlayer?.setOnPreparedListener {
if (alarm.shouldResumePlaying) {
mediaPlayer?.seekTo(alarm.secondsPlayed)
}
mediaPlayer?.start()
}
mediaPlayer?.prepareAsync()
} else {
initMediaPlayer(alarm)
}
} catch (exception: Exception) {
Crashlytics.logException(exception)
val uri = getDefaultRingtone()
mediaPlayer = MediaPlayer()
mediaPlayer?.let { increaseVolumeOverTime(it, true) }
mediaPlayer?.setDataSource(this, uri)
mediaPlayer?.isLooping = true
mediaPlayer?.setOnPreparedListener {
mediaPlayer?.start()
}
mediaPlayer?.prepareAsync()
}
}
}
haulerView?.setOnDragDismissedListener {
finish() // finish activity when dismissed
}
}
private fun getDefaultRingtone(): Uri {
var uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
if (uri == null) {
uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
if (uri == null) {
uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
}
}
return uri
}
private fun initTextClock() {
val period = 5000L
val timer = Timer()
val formatter = SimpleDateFormat("HH:mm")
val task = object : TimerTask() {
override fun run() {
val today = Date()
runOnUiThread {
tvCurrentTimeActual?.text = formatter.format(today)
}
}
}
timer.scheduleAtFixedRate(task, 0L, period)
}
override fun onDestroy() {
super.onDestroy()
if (shouldResumePlaying) {
mediaPlayer?.let {
alarmID?.let { it1 -> DataHelper.updateProgress(it1, it.currentPosition) }
}
}
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentUserVolume, 0)
timer.cancel()
mediaPlayer?.stop()
vibrator.cancel()
realm.close()
}
/*
Private
*/
private fun showIfScreenIsLocked() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true)
setTurnScreenOn(true)
val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
keyguardManager.requestDismissKeyguard(this, null)
} else {
window.addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
)
}
}
private fun showDanceAnimation() {
val lottieFiles = mutableListOf(
"lottie/dance/chicken_6.json",
"lottie/dance/sound.json", //White
"lottie/dance/pinguin.json" //White
)
val file = lottieFiles.random()
messageLottie?.setAnimation(file)
if (file == "lottie/dance/pinguin.json"
|| file == "lottie/dance/sound.json"
) {
messageLottie?.addValueCallback(
KeyPath("**"), LottieProperty.COLOR_FILTER,
{ PorterDuffColorFilter(getColor(R.color.white), PorterDuff.Mode.SRC_ATOP) }
)
}
messageLottie?.playAnimation()
}
private fun increaseVolumeOverTime(mediaPlayer: MediaPlayer, shouldVibrate: Boolean) {
mediaPlayer.setAudioAttributes(
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
)
currentUserVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
var currentVolume = 1
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0)
if (shouldVibrate) {
startVibrating()
}
timer = Timer()
timer.scheduleAtFixedRate(object : TimerTask() {
override fun run() {
currentVolume += 1
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0)
if (currentVolume % 10 == 0) {
if (shouldVibrate) {
startVibrating(currentVolume)
}
}
if (currentVolume >= 90) this.cancel()
}
}, 0, 2000)
}
private fun startVibrating(currentVolume: Int = 10) {
val vibratorLength = ((50 * currentVolume) / 1.2).roundToInt().toLong()
val patternShort = longArrayOf(1200, vibratorLength)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createWaveform(patternShort, 0))
} else {
vibrator.vibrate(patternShort, 0)
}
}
private fun initMediaPlayer(alarm: Alarm) {
mediaPlayer = MediaPlayer()
mediaPlayer?.let { increaseVolumeOverTime(it, alarm.shouldVibrate) }
val currentlySelectedPath = alarm.currentlySelectedPath
if (currentlySelectedPath == null) {
var uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
if (uri == null) {
uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
if (uri == null) {
uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
}
}
mediaPlayer?.setDataSource(this, uri)
} else {
mediaPlayer?.setDataSource(this, Uri.parse(currentlySelectedPath))
}
mediaPlayer?.isLooping = false
mediaPlayer?.setOnCompletionListener {
it?.stop()
it?.reset()
it?.isLooping = false
val path = alarm.songsList?.random()?.path
it?.setDataSource(this, Uri.parse(path))
alarmID?.let { it1 -> DataHelper.nextRandomSong(it1, path) }
it?.setOnPreparedListener {
mediaPlayer?.start()
}
it?.prepareAsync()
}
mediaPlayer?.setOnPreparedListener {
if (alarm.shouldResumePlaying) {
mediaPlayer?.seekTo(alarm.secondsPlayed)
}
mediaPlayer?.start()
}
mediaPlayer?.prepareAsync()
shouldResumePlaying = alarm.shouldResumePlaying
}
companion object {
const val ARG_ALARM_ID = "AlarmID"
fun getIntent(context: Context, alarmID: Int?): Intent {
val intent = Intent(context, TriggeredAlarmActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY) //If it doesn't hide in recent use or Intent.FLAG_ACTIVITY_CLEAR_TASK
intent.putExtra(ARG_ALARM_ID, alarmID)
return intent
}
}
}