JavaSecurityException using ClipData on Android - java

I am trying to paste text using ClipData on Android. The following code gives a java.lang.SecurityException. This does not happen all of the time. Should I declare any uses-permission to read the clipboard?
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2750)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2811)
at android.app.ActivityThread.-wrap12 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1528)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6316)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:762)
Caused by: java.lang.SecurityException:
at android.os.Parcel.readException (Parcel.java:1683)
at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel (DatabaseUtils.java:146)
at android.content.ContentProviderProxy.openTypedAssetFile (ContentProviderNative.java:692)
at android.content.ContentResolver.openTypedAssetFileDescriptor (ContentResolver.java:1163)
at android.content.ContentResolver.openTypedAssetFileDescriptor (ContentResolver.java:1107)
at android.content.ClipData$Item.coerceToText (ClipData.java:332)
at br.com.sicoob.camera.clipboard.ClipboardInterface.getText (ClipboardInterface.java:35)
at br.com.sicoobnet.InicioWap.verificarExisteBoletoCopiado (InicioWap.java:312)
at br.com.sicoobnet.InicioWap.verificarAcao (InicioWap.java:308)
at br.com.sicoobnet.InicioWap.onCreate (InicioWap.java:126)
at android.app.Activity.performCreate (Activity.java:6757)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2703)

This question is fixed to my project, but to everyone who whants to know, this is how I fix.
First I figured out when this error happens. It was when I don't have any text in clipboard and my application tried to go to external storage and the error was thrown.
Then to fix that I just change my method to verify has a text inside clipboard.
It was like that:
ClipboardManager clipboard = getManager(context);
ClipData clip = clipboard.getPrimaryClip();
return clip != null && clip.getItemCount() > 0;
Then it was like that:
ClipboardManager clipboard = getManager(context);
clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);

Related

Localelist init: IllegalArgumentException: list[1] is a repetition

I have recently noticed a lot of these crashes in my app.
I am not able to reproduce it myself.
Any idea what the issue might be?
I think it might happen when a bottom sheet dialog is about to be shown.
For reference my app supports 3 languages (English, Spanish and Bulgarian)
Exception java.lang.IllegalArgumentException: list[1] is a repetition
at android.os.LocaleList.<init> (LocaleList.java:193)
at androidx.appcompat.app.LocaleOverlayHelper.combineLocales (LocaleOverlayHelper.java:73)
at androidx.appcompat.app.LocaleOverlayHelper.combineLocalesIfOverlayExists (LocaleOverlayHelper.java:47)
at androidx.appcompat.app.AppCompatDelegateImpl.calculateApplicationLocales (AppCompatDelegateImpl.java:2470)
at androidx.appcompat.app.AppCompatDelegateImpl.applyApplicationSpecificConfig (AppCompatDelegateImpl.java:2426)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreate (AppCompatDelegateImpl.java:514)
at androidx.appcompat.app.AppCompatDialog.<init> (AppCompatDialog.java:65)
at com.google.android.material.bottomsheet.BottomSheetDialog.<init> (BottomSheetDialog.java:95)
at com.google.android.material.bottomsheet.BottomSheetDialogFragment.onCreateDialog (BottomSheetDialogFragment.java:50)
at androidx.fragment.app.DialogFragment.prepareDialog (DialogFragment.java:648)
at androidx.fragment.app.DialogFragment.onGetLayoutInflater (DialogFragment.java:562)
at androidx.fragment.app.Fragment.performGetLayoutInflater (Fragment.java:1725)
at androidx.fragment.app.FragmentStateManager.createView (FragmentStateManager.java:492)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState (FragmentStateManager.java:261)
at androidx.fragment.app.FragmentManager.executeOpsTogether (FragmentManager.java:1862)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute (FragmentManager.java:1786)
at androidx.fragment.app.FragmentManager.execPendingActions (FragmentManager.java:1723)
at androidx.fragment.app.FragmentManager$4.run (FragmentManager.java:510)
at android.os.Handler.handleCallback (Handler.java:873)
at android.os.Handler.dispatchMessage (Handler.java:99)
at androidx.test.espresso.base.Interrogator.loopAndInterrogate (Interrogator.java:10)
at androidx.test.espresso.base.UiControllerImpl.loopUntil (UiControllerImpl.java:7)
at androidx.test.espresso.base.UiControllerImpl.loopUntil (UiControllerImpl.java:1)
at androidx.test.espresso.base.UiControllerImpl.injectMotionEvent (UiControllerImpl.java:5)
at androidx.test.espresso.action.MotionEvents.sendUp (MotionEvents.java:6)
at androidx.test.espresso.action.MotionEvents.sendUp (MotionEvents.java:1)
at androidx.test.espresso.action.Tap.sendSingleTap (Tap.java:5)
at androidx.test.espresso.action.Tap.-$$Nest$smsendSingleTap (Tap.java)
at androidx.test.espresso.action.Tap$1.sendTap (Tap.java:1)
at androidx.test.espresso.action.GeneralClickAction.perform (GeneralClickAction.java:4)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform (ViewInteraction.java:2)
at androidx.test.espresso.ViewInteraction.doPerform (ViewInteraction.java:21)
at androidx.test.espresso.ViewInteraction.-$$Nest$mdoPerform (ViewInteraction.java)
at androidx.test.espresso.ViewInteraction$1.call (ViewInteraction.java:6)
at androidx.test.espresso.ViewInteraction$1.call (ViewInteraction.java:1)
at java.util.concurrent.FutureTask.run (FutureTask.java:266)
at android.os.Handler.handleCallback (Handler.java:873)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loop (Looper.java:193)
at android.app.ActivityThread.main (ActivityThread.java:6846)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:883)
Fixed by updating to androidx.appcompat:appcompat:1.6.0-alpha04.
I was using androidx.appcompat:appcompat:1.6.0-alpha01, and according to this issue, versions below 1.6.0-alpha02 causes a crash when setting a locale from app that is part of the system languages.

InternalSyntheticLambda in Crash report

I got following crash report:
java.lang.NullPointerException:
at com.myapp.actions.SellAction.getAction (SellAction.java:16)
at com.myapp.handler.SellHandler.sell (SellHandler.java:169)
at com.myapp.main.MyActivity.sell (MyActivity.java:537)
at com.myapp.main.Fragment2.lambda$onCreateView$0 (Fragment2.java:31)
at com.myapp.main.Fragment1$$InternalSyntheticLambda$1$c14fd7a0fe0c17e220e277448d1b71579f8038a9784d642f5a2e5b6a00f377eb$2.onClick$bridge (Fragment1.java)
at android.view.View.performClick (View.java:7862)
at android.widget.TextView.performClick (TextView.java:15004)
at android.view.View.performClickInternal (View.java:7831)
at android.view.View.access$3600 (View.java:879)
at android.view.View$PerformClick.run (View.java:29359)
at android.os.Handler.handleCallback (Handler.java:883)
at android.os.Handler.dispatchMessage (Handler.java:100)
at android.os.Looper.loop (Looper.java:237)
at android.app.ActivityThread.main (ActivityThread.java:8167)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1100)
The code from Fragment2.lambda$onCreateView$0 (Fragment2.java:31) to SellAction.getAction is a valid stacktrace in my code. But usually (I don't see how this is possible) the Fragment1 (Fragment1$$InternalSyntheticLambda$1$c14fd7a0fe0c17e220e277448d1b71579f8038a9784d642f5a2e5b6a00f377eb$2.onClick$bridge (Fragment1.java)) has nothing to do with that code and I have no idea how it appears there. I don't know if that has something to do with the InternalSyntheticLambda. But I do not find anything about that on the internet (10 search results at google). Does anyone here has an idea what that is or how that can happen?
Looks like some handler or listener is leaking.
Try to look through all the call chain between
at com.myapp.actions.SellAction.getAction (SellAction.java:16)
and
at com.myapp.main.Fragment2.lambda$onCreateView$0 (Fragment2.java:31)
There might be view or some other object that is set up with listener which is not dropped correctly on view destroy. Special attention to Fragment2#onCreateView line 31.

utility class's static member (null) lifecycle

I have a utility class that has constants and static methods.
Furthermore, that has one static field.
public class MyUtil implements IMyUtil {
public static String IS_DEBUG = false;
...
private static MyEnumType mMyEnum;
...
// static getter setter methods for mMyEnum
}
I have two fragments (FragmentA and FragmentB for the sake of simplicity).
FragmentA set the mMyEnum value, and then later use it with the getter method. When the user presses a button, I show FragmentB.
In the onActiviyCreated() method of FragmentB I get the mMyEnum value.
On my test devices everything is ok. But in the Google Play Console I see a bunch of NullPointerException errors at this line:
String testString = MyUtil.getMyEnum().getSomeStringValue();
I can't figure it out why can be myEnum null on that line and why I can't reproduce it. MyUtil obviously can't be null, getSomeStringValue() can be null, but it doesn't throw a NullPointerException, so the only myEnum can be null. But why? And if it can be null how can I reproduce it?
Thank you.
UPDATE #1:
Play shows a wide range of devices (Samsung A3, S6, S7, S8, LG X, G3, Huawei P9 etc.) and API versions (5.0 -> 7.1). Full stack trace is:
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1642)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6776)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException:
at com.mycompany.fragment.FragmentB.onActivityCreated (FragmentB.java:35)
at android.support.v4.app.Fragment.performActivityCreated (Fragment.java:2089)
at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManagerImpl.java:1133)
at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManagerImpl.java:1290)
at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManagerImpl.java:1272)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated (FragmentManagerImpl.java:2149)
at android.support.v4.app.FragmentController.dispatchActivityCreated (FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart (FragmentActivity.java:600)
at android.support.v7.app.AppCompatActivity.onStart (AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart (Instrumentation.java:1256)
at android.app.Activity.performStart (Activity.java:6972)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2937)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1642)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6776)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1386)
Long-lived Static members present a problem because Android may have freed memory and removed this value at some time (when no fragment is active with a reference to it). You also haven't explicitly said whether you can get to FragmentB without setting a value from FragmentA first, so this could also be your problem.
Additional note - a constant should be constant - so should have the final keyword.
My suggested solution would be to get rid of this bad architecture where two components are relying on an outside god-object to maintain their state. Pass your enum value as an Extra in the intent.
You can write to the bundle with
public static final String KEY_ENUM = "MyEnumKey";
bundle.putString(KEY_ENUM, myEnum.name()):
and then read it back with
Bundle bundle = intent.getExtras();
MyEnumTypemyEnum = MyEnumType.valueOf(bundle.getString(FragmentA.KEY_ENUM));
Another suggestion is to ensure that myEnum is never null (which by default it is). Declare it like:
private static MyEnumType mMyEnum = MyEnumType.MY_DEFAULT_VALUE;

WindowManager$BadTokenException when showing AlertDialog on Android 7

Google Play shows a lot of crash reports caused by WindowManager$BadTokenException.
Full log:
java.lang.RuntimeException:
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3344)
at android.app.ActivityThread.-wrap21 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1583)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6121)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:779)
Caused by: android.view.WindowManager$BadTokenException:
at android.view.ViewRootImpl.setView (ViewRootImpl.java:679)
at android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView (WindowManagerImpl.java:93)
at android.app.Dialog.show (Dialog.java:329)
at android.app.AlertDialog$Builder.show (AlertDialog.java:1112)
at com.nutomic.syncthingandroid.activities.SyncthingActivity.showLoadingDialog (SyncthingActivity.java:142)
at com.nutomic.syncthingandroid.activities.SyncthingActivity.onApiChange (SyncthingActivity.java:88)
at com.nutomic.syncthingandroid.activities.SyncthingActivity.access$lambda$1 (SyncthingActivity.java)
at com.nutomic.syncthingandroid.activities.SyncthingActivity$$Lambda$2.onApiChange (Unknown Source)
at com.nutomic.syncthingandroid.service.SyncthingService.onApiChange (SyncthingService.java:530)
at com.nutomic.syncthingandroid.service.SyncthingService.updateState (SyncthingService.java:246)
at com.nutomic.syncthingandroid.service.SyncthingService.onStartCommand (SyncthingService.java:181)
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3327)
Relevant code:
if (isFinishing() || mLoadingDialog != null)
return;
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_loading, null);
TextView loadingText = (TextView) dialogLayout.findViewById(R.id.loading_text);
loadingText.setText((getIntent().getBooleanExtra(EXTRA_FIRST_START, false))
? R.string.web_gui_creating_key
: R.string.api_loading);
mLoadingDialog = new AlertDialog.Builder(this)
.setCancelable(false)
.setView(dialogLayout)
.show();
What could be the cause of this crash? It would also help if I had a way to reproduce the crash. The full source of the app is available on Github.
Such a problem might occur if the app is closing, or is in the background somehow.
You should check if your activity is not being closed (for example, onDestroy was called), and that it is active in the foreground (and visible).
There are several options for how to check if it is in the background, here's one that should work:
RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
isInBackground = myProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
Okay I found the problem. In my code I was doing something like this (in a much more complicated way):
if (isFinishing())
return;
new AlertDialog.Builder(this).show();
Thread.sleep(1000);
The show() method call finished normally, but the dialog was only attached to the Activity after the sleep was finished. So if you have this problem, make sure you're not doing any long-running operation on the main thread.

Android JAVA RX exception only from the play store

Hello everyone i have this exception from RX on my google play console, i've have never see it on my project but it occurred about 200 times as google play says... any one has an idea about that ?
java.lang.IllegalStateException:
at rx.android.schedulers.LooperScheduler$ScheduledAction.run (LooperScheduler.java:114)
at android.os.Handler.handleCallback (Handler.java:739)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:158)
at android.app.ActivityThread.main (ActivityThread.java:7230)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)
**Caused by: rx.exceptions.OnErrorFailedException:**
at rx.observers.SafeSubscriber._onError (SafeSubscriber.java:187)
at rx.observers.SafeSubscriber.onError (SafeSubscriber.java:115)
at rx.exceptions.Exceptions.throwOrReport (Exceptions.java:216)
at rx.observers.SafeSubscriber.onNext (SafeSubscriber.java:139)
at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onNext (OnSubscribeDoOnEach.java:101)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call (OperatorObserveOn.java:224)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run (LooperScheduler.java:107)
Your code is throwing an exception that is not caught.
Have you specified an function for onError?
[Your Observable].subscribe(value -> {
// Your onNext code
}, throwable -> {
// Handle error
});

Categories