Android P: UsageStatsManager getAppStandbyBucket - android

I want to display the current bucket of my application with a device under Android P beta. Therefore, I try to use the UsageStatsManager class like this :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= 28) {
UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
if (usageStatsManager != null) {
Log.d(TAG, "getAppStandbyBucket():" + usageStatsManager.getAppStandbyBucket());
}
}
}
My Manifest possesses the permission android.permission.PACKAGE_USAGE_STATS :
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
But I encounter a crash when I launch my application :
07-24 10:23:39.061 26967-26967/com.app.bucket E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.bucket, PID: 26967
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.bucket/com.app.bucket.MainActivity}: java.lang.SecurityException: MATCH_ANY_USER flag requires INTERACT_ACROSS_USERS permission at com.android.server.pm.PackageManagerService.getPackageUid:4343 com.android.server.pm.PackageManagerService$PackageManagerInternalImpl.getPackageUid:23964 com.android.server.usage.UsageStatsService$BinderService.getAppStandbyBucket:927 android.app.usage.IUsageStatsManager$Stub.onTransact:274 android.os.Binder.execTransact:731 : Neither user 10118 nor current process has android.permission.INTERACT_ACROSS_USERS.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.SecurityException: MATCH_ANY_USER flag requires INTERACT_ACROSS_USERS permission at com.android.server.pm.PackageManagerService.getPackageUid:4343 com.android.server.pm.PackageManagerService$PackageManagerInternalImpl.getPackageUid:23964 com.android.server.usage.UsageStatsService$BinderService.getAppStandbyBucket:927 android.app.usage.IUsageStatsManager$Stub.onTransact:274 android.os.Binder.execTransact:731 : Neither user 10118 nor current process has android.permission.INTERACT_ACROSS_USERS.
at android.os.Parcel.createException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1910)
at android.os.Parcel.readException(Parcel.java:1860)
at android.app.usage.IUsageStatsManager$Stub$Proxy.getAppStandbyBucket(IUsageStatsManager.java:658)
at android.app.usage.UsageStatsManager.getAppStandbyBucket(UsageStatsManager.java:479)
at com.app.bucket.MainActivity.onCreate(MainActivity.java:60)
It seems that I missed a permission, but even when I add the following permission :
<uses-permission
android:name="android.permission.INTERACT_ACROSS_USERS"
tools:ignore="ProtectedPermissions" />
I still have the same crash. I was thinking that maybe I miss the runtime call for the permission, but like the documentation stipulate :
NOTE: Most methods on this API require the permission
android.permission.PACKAGE_USAGE_STATS. However, declaring the
permission implies intention to use the API and the user of the device
still needs to grant permission through the Settings application. See
Settings.ACTION_USAGE_ACCESS_SETTINGS. Methods which only return the
information for the calling package do not require this permission.
E.g. getAppStandbyBucket() and queryEventsForSelf(long, long).
It's not necessary. Even knowing that I tried to use the runtime permission, but I still get the crash. If anyone see what is missing, I would really appreciate the help.

I think it's a bug. There is an open issue covering this in the issue tracker: https://issuetracker.google.com/issues/111102580.

Related

Android Application crashed on android 10

My Android App is crashed on android 10 Q mobile but I am unable to reproduce this crash and resolve it.
This is the error message:
Fatal Exception: java.lang.SecurityException: Not allowed to bind to service Intent { act=com.google.android.gms.clearcut.service.START pkg=com.google.android.gms }
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1741)
at android.app.ContextImpl.bindService(ContextImpl.java:1651)
at android.content.ContextWrapper.bindService(ContextWrapper.java:705)
at com.google.android.gms.common.stats.ConnectionTracker.zza(:22)
at com.google.android.gms.common.internal.zzf.zze(:30)
at com.google.android.gms.common.internal.zze.zza(:15)
at com.google.android.gms.common.internal.BaseGmsClient.zza(:103)
at com.google.android.gms.common.internal.BaseGmsClient.connect(:142)
at com.google.android.gms.common.api.internal.GoogleApiManager$zaa.connect(:224)
at com.google.android.gms.common.api.internal.GoogleApiManager.zab(:47)
at com.google.android.gms.common.api.internal.GoogleApiManager.handleMessage(:173)
at android.os.Handler.dispatchMessage(Handler.java:103)
at com.google.android.gms.internal.base.zap.dispatchMessage(:8)
at android.os.Looper.loop(Looper.java:232)
at android.os.HandlerThread.run(HandlerThread.java:67)
Here are the firebase crash pics:
please register your service in manifest if not ex: <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

MediaProjection service type not recognized in android Q

With the new privacy changes in effect with Android Q, it is now mandatory for any app using MediaProjection api to specify android:foregroundServiceType attribute in the service tag under manifest.
But recently, I noticed that though I set the android:foregroundServiceType="mediaprojection" there is a security exception thrown. Does anybody have any idea what I'm doing wrong?
Manifest:
<service
android:name=".services.MediaProjectionService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="mediaProjection" />
Exception thrown:
java.lang.RuntimeException: Unable to start service com.package.app.services.MediaProjectionService#6d0fed2 with Intent { act=com.package.app.services.action.startrecording cmp=com.package.app/.services.MediaProjectionService(has extras) }: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4103)
at android.app.ActivityThread.access$1800(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7343)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933)
Caused by: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
at android.os.Parcel.createException(Parcel.java:2071)
at android.os.Parcel.readException(Parcel.java:2039)
at android.os.Parcel.readException(Parcel.java:1987)
at android.media.projection.IMediaProjection$Stub$Proxy.start(IMediaProjection.java:231)
at android.media.projection.MediaProjection.<init>(MediaProjection.java:58)
at android.media.projection.MediaProjectionManager.getMediaProjection(MediaProjectionManager.java:104)
at com.package.app.services.MediaProjectionService.startRecording(MediaProjectionService.java:190)
at com.package.app.services.MediaProjectionService.onStartCommand(MediaProjectionService.java:142)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4085)
at android.app.ActivityThread.access$1800(ActivityThread.java:219) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7343) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933) 
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.media.projection.MediaProjectionManagerService$MediaProjection.start(MediaProjectionManagerService.java:476)
at android.media.projection.IMediaProjection$Stub.onTransact(IMediaProjection.java:135)
at android.os.Binder.execTransactInternal(Binder.java:1021)
at android.os.Binder.execTransact(Binder.java:994)
P.S: This is experienced on Android Q DP5 GSI with target SDK 29. The app works fine with target sdk 28 without any changes.
Don't you forget to add permission <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> into AndroidManifest.xml ?
UPDATE
Make sure you call startForeground() before getMediaProjection()!
The best solution is to call startForeground() from onCreate() without any conditions, displaying some sort of default messages in notification. Then you can execute your logic and call NotificationManager.notify() with updated notification at any time.
Changing the following attributes
android:foregroundServiceType="mediaProjection"
tools:targetApi="q"
in AndroidManifest.xml worked for me.
< service
android:name="SERVICE_NAME"
android:foregroundServiceType="mediaProjection"
tools:targetApi="q"
android:enabled="true"
android:exported="false" />

Security Exception for Download Manager with targetSdkVersion Below 22

I have a weird crash in Lollipopand below. I get a security Exception when trying to download a file from the server, but in devices running Marshmallow and above the app does not crash. Logcat:
Caused by: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
In App's grandle I use
targetSdkVersion 22
Will change after our first push on Google Console to target the latest sdk version, but for now this will remain to 22, so no need for run time permissions.
Furthermore, in App's Manifest we declare app's permission.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Also, the code that uses the DownloadManagerinside an AsynckTask :
try {
String dirPath = String.format("%5$s/%1$s/Resources/%2$s/%3$s/%4$s/", userID, resource.getiD(), filePackage.getiD(), language, dir.getCanonicalPath());
File makeDirs = new File(dirPath);
makeDirs.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
}
this.downloadManager = (DownloadManager) getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(url);
request.setDestinationInExternalFilesDir(getApplicationContext(), null, filePath);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setVisibleInDownloadsUi(false);
downloadID = downloadManager.enqueue(request);
Again, this crash is observed only in Galaxy A5 with OS version 5.0.2, in an emulator running Samsung Galaxy S6 with Lollipop and in emulator running Kitkat 4.4.4 HTC One.
The full stack trace :
java.lang.RuntimeException: Unable to start activity ComponentInfo{...players.PDFViewer}: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
at android.os.Parcel.readException(Parcel.java:1540)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:475)
at android.content.ContentResolver.insert(ContentResolver.java:1260)
at android.app.DownloadManager.enqueue(DownloadManager.java:1336)
at de.imsystems.crmmobile.players.PDFViewer.onCreate(PDFViewer.java:104)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873) 
at android.app.ActivityThread.access$900(ActivityThread.java:181) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6145) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
If you have any thoughts, please share them, thanks in advance.
Eventually this post WRITE_EXTERNAL_STORAGE not working on lollipop even though it's set in the manifest helped me. What I did was to alter permissions in AndroidManifest.xml file like this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="26" tools:replace="android:maxSdkVersion"/>
This way the app doesn't crashes!

Error In Wifi Toggle Android Code

I get this error when I press the button for the wifi toggle and the app stops working.It mentions that the user has no permission to access wifi state.The thing is its already declared in the Manifest file.
Permissions used:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Error
FATAL EXCEPTION: main
Process: com.example.something.callblocker, PID: 935
java.lang.SecurityException: WifiService: Neither user 10052 nor current process has android.permission.ACCESS_WIFI_STATE.
at android.os.Parcel.readException(Parcel.java:1472)
at android.os.Parcel.readException(Parcel.java:1426)
at android.net.wifi.IWifiManager$Stub$Proxy.getWifiEnabledState(IWifiManager.java:808)
at android.net.wifi.WifiManager.getWifiState(WifiManager.java:1029)
at android.net.wifi.WifiManager.isWifiEnabled(WifiManager.java:1041)
at com.example.stamatis.callblocker.powersavings$2.onClick(powersavings.java:55)
at android.view.View.performClick(View.java:4438)
at android.widget.CompoundButton.performClick(CompoundButton.java:100)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
If this is happening and you are saying that android.permission.ACCESS_WIFI_STATE this is already present in manifest, then there is only one possibility that user has explicitly stopped this permission.
Check if this crash is in Marshmallow i.e API 23. Google has provided the user power to remove the permission of app explicitly.
You can read : http://www.greenbot.com/article/2990078/android/how-to-toggle-app-permissions-in-android-marshmallow.html
Only solution is to check for permission at runtime before doing toggle.
This is how you can do this :
private boolean checkWriteExternalPermission()
{
String permission = "android.permission.ACCESS_WIFI_STATE";
int res = getContext().checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
Needs to be looked at by authorities for fraud and illegal activity steal the token page

How to handle java.lang.SecurityException: Requires READ_PHONE_STATE for android.text.format.DateFormat.getDateFormat?

My app was sent a stack trace for device Y625-U21 (HWY625-U), the app doesn't ask for READ_PHONE_STATE because the docs say nothing about using this permission for getting dateFormat. We have tested the app in various devices and this doesn't happen.
java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10149 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1472)
at android.os.Parcel.readException(Parcel.java:1426)
at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getSubscriberId(IPhoneSubInfo.java:317)
at android.telephony.TelephonyManager.getSubscriberId(TelephonyManager.java:1094)
at android.text.format.DateFormat.getDateFormatStringForSetting(DateFormat.java:262)
at android.text.format.DateFormat.getDateFormatForSetting(DateFormat.java:250)
at android.text.format.DateFormat.getDateFormat(DateFormat.java:236)
at edu.usf.cutr.opentripplanner.android.util.ConversionUtils.getTimeWithContext(ConversionUtils.java:217)
at edu.usf.cutr.opentripplanner.android.util.ConversionUtils.getTimeWithContext(ConversionUtils.java:210)
at edu.usf.cutr.opentripplanner.android.fragments.MainFragment.fillItinerariesSpinner(MainFragment.java:3521)
at edu.usf.cutr.opentripplanner.android.fragments.MainFragment.onTripRequestComplete(MainFragment.java:3462)
at edu.usf.cutr.opentripplanner.android.tasks.TripRequest.onPostExecute(TripRequest.java:171)
at edu.usf.cutr.opentripplanner.android.tasks.TripRequest.onPostExecute(TripRequest.java:60)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
I can say that when your app gets a lot of installs in Google Play you will receive a lot of strange crash reports in Google Developer console.
Sometimes there is nothing you can do with it and you have to put your code in try/catch block.
I don't know all the details about your case, but in mine it was the only solution.
try {
//your code here
} catch (SecurityException e) {
e.printStackTrace();
//do smth in this scenario
}
include this permission in manifest file
READ_PHONE_STATE
You are getting this exception becoz you haven't declare this permission in manifest file.

Categories

Resources