Fail to connect to camera Service - java

I had seen many questions on stack but no one is telling if a Fail to connect to camera service will occur, how to get rid of this RuntimeException.
I have an camera application its working fine, I already take care to release the resources but if somehow user install the other application which not releasing the resources properly, my application facing RuntimeException: Fail to connect to camera Service and hence got crashed, want to avoid this situation.
If i click on original camera application it shows me a AlertDialog
Camera error: Cannot connect to camera.
That's what i exactly want to handle this. I am trying this code to handle it but cant succeed yet.
try {
camera = Camera.open();
camera.setDisplayOrientation(90);
} catch (RuntimeException e) {
// TODO: handle exception
Log.d("Inside RunTime exception", e+"//");
camera.setErrorCallback(errorCallback);
reConnectCameraVideo();
} catch(Exception e) {
finish();
}
but camera object returning null on camera.setErrorCallback because it wont open.

setErrorCallback() cannot be used for the case where the Camera will not open. You appear to be trying to still use the Camera -- AFAIK this is impossible until the user reboots their phone if some other app leaked the Camera. Simply display your own message to that effect.
Also:
Use an error logging service, like ACRA, Flurry, BugSense, etc.
Never blindly finish an activity due to an exception, as in your last catch block. Always do something to let the user and/or you (via the error logging service) know about the exception

Related

Android Webview ignore onReceivedError when app is in background / minimized

I have an Android app that serves content via webview. I've implemented a network connectivity alert so if my users are in a location where they can't get internet, I show a message. This takes place inside the onReceivedError method. It works as expected; if I disable all internet connections, I get a warning message. However, even when internet connection exists, the warning message still shows. If I minimize my app, open the native browser and visit 8-10 links, when I switch back to my app, I am given the network warning even though the device is connected to the network.
This only happens on one of my 6 test devices (my current phone, a Samsung Galaxy S10e. The problem does not occur on 5 different Android devices, both newer and older. I'm just looking for a way to ignore the warning when the app is in the background state. I've searched for an easy way to do this, but I haven't found anything simple that doesn't involve writing a new class.
How can I write a conditional statement to do nothing when the onReceivedError method is triggered while the app is in the background? Here is a portion of my code, along with what I would like to have happen:
#override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// the part I need help with
if ("app in background"){
// do nothing
} else {
// this is where I have employed my notification, a snackbar
}
}
Thank you for your help!

SpeechRecognizer.startListening not working on Chromebook 10 but works on Pixel 2XL

I have voice recognition code that crashes upon launch (after the splashscreen). My app works great without voice recognition, but it is required for this app that I have it.
This Acer Chromebook 10 outside of my development has no issue with voice recognition (Okay Google, what time is it... etc.)
Here is a partial screenshot of the error I am getting from logcat on the Chromebook:
I have added permissions in the manifest (before I added this it worked on the Pixel 2XL):
<addPermission android:name="android.permission.BIND_VOICE_INTERACTION"/>
... but still get the same error. I am not launching a competing service, but rather a thread runnable activity. Here is a snippet of the code that I think that may be throwing this error (it crashes right after the splash screen so I don't even get the catch response):
try
{
_activity.runOnUiThread(new Runnable()
{
public void run()
{
// I don't know why, but we have to destroy and redo the recognizer after a failure (unlike onResult), then works great.
mSpeechRecognizer.destroy();
mSpeechRecognizer.setRecognitionListener(recognitionListener);
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
});
} // end of try
catch (Exception e)
{
Log.debug("Recognition failed with exception " + e.getMessage());
}
Some background - like the title says this code is working with Pixel 2XL. It is part of an in-house plugin that I've written for the Unreal Engine to support voice recognition. As such I test for the permission "android.permission.BIND_VOICE_INTERACTION" in Blueprints (a flowchart-like language for UE4) when running the code on my Pixel and it comes back true - so I know this permission is okay with the Pixel 2XL.
I get the impression that there may be a competing service causing conflict on the Chromebook specifically (ArcVoiceInteractionService?) and if this is so, I need to find out how to get around it.

How to handle Call during crash?

I'm working on a phone-conference app on Android 7. I found this problem.
When app crash I loose ongoing call control resulting in app closed and voice channel open.
Reopening app result in two ongoing calls.
There are ways to close the first voice call?
I try closing the call at app restart but obviously Android OS don't let me touch it.
the best (still not working) result I achieved is error class extension. that event is fired at crash start.
here is my class CrashKillCall that implements Thread.UncaughtExceptionHandler
public void uncaughtException(Thread t, Throwable e) {
//"the last song kill the audience" by Crash & the boys
Log.e(TAG, "--------------------------------------");
Log.e(TAG,t.getName());
Log.e(TAG,e.getCause().getMessage());
Log.e(TAG, "--------------------------------------");
crashCall.disconnect();
Log.e(TAG,"work?");
}
public static void setCall(Call call){
crashCall=call;
}
the desired result is some way to, or to let system know that i want to, terminate the ongoing or all calls.
thank you for your help.
ended out that i was pointing at wrong Call object. code work, you just need declare an istance of that class as default exception listener and register the right Call

No intent from UserRecoverableAuthIOException using Drive SDK for Android

I've implemented google drive into my android application and it work pretty nice, but I'm trying to figure out a way to run an upload/download in a background thread so that I can leave an activity and do something else on my app. The problem is, drive needs the activity reference in case of exceptions, such as UserRecoverableAuthIOException.
Here's the issue I cannot understand. Here's some try/catch code:
try {
//...drive api stuff here
} catch (UserRecoverableAuthIOException e) {
possibleException = e;
try {
e.getIntent();
} catch ( NullPointerException e2 ) { //this is the crazy part
// e.getIntent() should not throw a nullpointer
e2.printStackTrace();
possibleException = e2;
}
onActivityRestartWhat = RESTART_IMPORT;
// just a note i do handle this exception properly in another section of a code when there is an intent.
} catch (FileNotFoundException e) {
possibleException = e;
e.printStackTrace();
} catch (IOException e) {
possibleException = e;
e.printStackTrace();
}
What I can't figure out is why UserRecoverableAuthIOException is throwing a NullPointerException whey I try to access getIntent.
More Info
I do catch UserRecoverableAuthIOException when more authentication is needed and request it via the startActivityForResult method. Also, this exception is thrown only if I back out of the activity that has started, aka destroy the activity. Basically, I have a process that uploads/downloads drive files in a thread and if I don't leave the activity until completion it works, if I destroy the activity via the back button then I get this exception.
Stack Trace
07-10 14:45:32.903: W/System.err(1450): java.lang.NullPointerException
07-10 14:45:32.913: W/System.err(1450): at android.content.Intent.<init> (Intent.java:3529)
07-10 14:45:32.913: W/System.err(1450): at com.google.android.gms.auth.UserRecoverableAuthException.getIntent(Unknown Source)
07-10 14:45:32.913: W/System.err(1450): at com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException.getIntent(UserRecoverableAuthIOException.java:62)
07-10 14:45:32.913: W/System.err(1450): at my.app.DriveHelper$2.run(DriveHelper.java:211)
My Desire
I want to run downloads/uploads (via google drive) in a background thread. Since the sdk requires startActivityForResult and other methods that might require a reference to an Activity or Context that makes this difficult, but it should work once the app has been granted the sdk permissions that require those references. Hopefully this makes sense.
Below are the steps you can follow to handle UserRecoverableAuthIOException Exception properly and you can even avoid getting that exception when back pressed.
In some cases if you are receiving that error mean the activity is destroyed so you shouldn't depend on the activity
You need to
create fresh com.google.api.services.tasks.Tasks object from
Context of Service not from any Activity directly like shown in 'tasks-android-sample'
When you get Exception
You need show a notification with PendingIntent from Service
PendingIntent should contain the reference to an Activity , say
HomeActivity
Activity should handle the intent extra and should do
the required things like showing choose account dialog
you can go through the sample code here (GoogleTasksService)
What you can do, in this case don't use multiple activities.
By switching to views you can achieve your task.

Reconnect to a device after device crashed

I have the following problem with the libusb-java and some self-made devices.
It could happen that such a device disconnects from the USB Port because it drains to much power (as an example: i have a USB-LED Light which needs sometimes more than 500mA).
In this case the USB Controller will reset the device and the device will startup normaly again.
Now i cant really detect such a problem except for trying to reinit the device on every Exception. But thats not working...
On Every Exception i call my init Method again, which looks like this:
private void initDevice() {
USB.init();
this.dev = USB.getDevice(idVendor, idProduct);
}
The Problem with that is, this runs without any problem, but the i get this error message when i want to send new data:
LibusbJava.controlMsg: error sending control message: Protocol error
How do i can reinit the device? Do i have to reset the bus or something?
You will need to call open() on the device - it is new for the system.

Categories

Resources