DownloadManager.Request.setNotificationVisibility fails with jSecurityException: invalid value for visibility: 1
Asked Answered
D

3

7

I am trying to use the DownloadManager to download large PDF files from my app. I want notifications to be displayed during the download as well as when the download finishes. However setting the visibility causes above exception.

This error is different from this post DownloadManager.Request.setNotificationVisibility fails with jSecurityException: invalid value for visibility: 2

The other post is asking for help when setting the visibility to VISIBILITY_HIDDEN for which you need permission in the manifest. I am trying to set the visibility to DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED like so:

public class DMnotifyTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DownloadManager mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    long downloadID = mgr
        .enqueue(new DownloadManager.Request(Uri.parse("http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf"))
            .setNotificationVisibility(
                    DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "hello.pdf")
            .setDescription("my.test.pack Doc"));
}

}

Which results in this stacktrace:

E/AndroidRuntime(24794): Caused by: java.lang.SecurityException: Invalid value for visibility: 1
E/AndroidRuntime(24794):    at android.os.Parcel.readException(Parcel.java:1321)
E/AndroidRuntime(24794):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182)
E/AndroidRuntime(24794):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
E/AndroidRuntime(24794):    at android.content.ContentProviderProxy.insert(ContentProviderNative.java:447)
E/AndroidRuntime(24794):    at android.content.ContentResolver.insert(ContentResolver.java:721)
E/AndroidRuntime(24794):    at android.app.DownloadManager.enqueue(DownloadManager.java:877)
E/AndroidRuntime(24794):    at my.test.pack.DMnotifyTestActivity.onCreate(DMnotifyTestActivity.java:18)

Without setting visibility the code works fine. I have already attempted adding various permissions to the manifest, but still no go. This is targeting level 11 so honeycomb and up. Permissions I have tried are:

  • android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
  • android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS
  • android.permission.ACCESS_DOWNLOAD_MANAGER
  • android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED
Diffusivity answered 29/3, 2012 at 20:46 Comment(2)
I am having the same problem. Anyone who can shed some light on this?Aarau
Unfortunately this is an Android bug. Not much you can do about it unless you want to fix Google's code yourself.Diffusivity
A
3

Here's my hack to overcome this bug in Honeycomb tablets (Version: 3.2 or API Level: 13):

Request req = new Request(Uri.parse(url));
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.HONEYCOMB_MR2)
{
    req.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
else
{
    req.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
}

Ah... the fun with Android!

Accalia answered 19/1, 2013 at 0:19 Comment(1)
+1, but you should rather catch the exception like this: try { dm.enqueue(request); } catch (SecurityException e) { request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); dm.enqueue(request); } This is better than the API level switch as it doesn't rely on any specific information about platforms and bugs.Dicarlo
M
3

if you want to use 'VISIBILITY_HIDDEN',you should add this permission in androidManifest.xml

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
Magnet answered 17/11, 2015 at 9:15 Comment(0)
W
0

I just faced this error with a similar app (same code for dm) to Marc's. Never encountered it during development, and I don't have Honeycomb users. I have a code similar to the above one but for Gingerbread and above.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }
    else {
        request.setShowRunningNotification(true);
        }   

The previous "hack" is aimed for Honeycomb, but as I have no Honeycomb users, I can confirm the bug is present in >4.0 which are +80% of my users. The issue appeared on developer console, and I'm not able to recreate it with my devices. Will update my answer to the conditions for the error when the users start complaining.

EDIT:

I love my users. We got to test the code with a user who had this issue. The app crashed when he started the download (that created the notification VISIBILITY_VISIBLE_NOTIFY_COMPLETED). He was indeed using android 4.0.3.

How to fix

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        }
    else {
        request.setShowRunningNotification(true);
        }   

Basically the same as the previous answer, but we can confirm the issue is present in api 15, so just make the adjustment to affect all versions api > 11, and don't worry about api 16 and 17 to suffer the same issue

Wendling answered 25/5, 2013 at 23:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.