Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading images from local storage for listview in appwidget may cause SecurityException #1405

Closed
ywwynm opened this issue Aug 11, 2016 · 8 comments
Labels
Milestone

Comments

@ywwynm
Copy link

ywwynm commented Aug 11, 2016

I'm using Glide 3.7.0 to load images from local storage to a ListView in an appwidget. Here's a part of my code:

BitmapRequestBuilder builder =
        Glide.with(context)
                .load(pathName) // absolute path of local image file
                .asBitmap()
                .override(width, height)
                .centerCrop();
FutureTarget futureTarget = builder.into(width, height);
try {
    remoteViews.setImageViewBitmap(R.id.iv_image_attachment, (Bitmap) futureTarget.get());
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}

I didn't use Glide.with(context).asBitmap().into(AppWidgetTarget) because it's useless to load image for ListView in appwidget. Anyway, above code will be executed in RemoteViewsFactory#getViewAt(int position) inside my custom RemoteViewsService. It can work well at most time, but sometimes I will get following error info with images not loaded:

java.lang.SecurityException: ConnectivityService: Neither user 10214 nor current process has android.permission.ACCESS_NETWORK_STATE.
    at android.os.Parcel.readException(Parcel.java:1620)
    at android.os.Parcel.readException(Parcel.java:1573)
    at android.net.IConnectivityManager$Stub$Proxy.getActiveNetworkInfo(IConnectivityManager.java:1029)
    at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:667)
    at com.bumptech.glide.manager.DefaultConnectivityMonitor.isConnected(DefaultConnectivityMonitor.java:55)
    at com.bumptech.glide.manager.DefaultConnectivityMonitor.register(DefaultConnectivityMonitor.java:38)
    at com.bumptech.glide.manager.DefaultConnectivityMonitor.onStart(DefaultConnectivityMonitor.java:61)
    at com.bumptech.glide.manager.ApplicationLifecycle.addListener(ApplicationLifecycle.java:15)
    at com.bumptech.glide.RequestManager.<init>(RequestManager.java:81)
    at com.bumptech.glide.RequestManager.<init>(RequestManager.java:53)
    at com.bumptech.glide.manager.RequestManagerRetriever.getApplicationManager(RequestManagerRetriever.java:73)
    at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:95)
    at com.bumptech.glide.Glide.with(Glide.java:629)

It seems that this exception is caused by checking network state without corresponding permission. I didn't request ACCESS_NETWORK_STATUS permission in AndroidManifest.xml indeed, but my app really doesn't need this permission. In my user case, Glide will only be used to load images from local storage so I think it should not do such kind of operation like checking network state. Maybe it's designed for loading images from everywhere including Internet(OK, perhaps this is most important user case), but I think it should handle this mistake if it's not asked to load images from Internet, and then load images correctly.
Thank you.

@TWiStErRob
Copy link
Collaborator

TWiStErRob commented Aug 11, 2016

ConnectivityMonitorFactory checks for the existence of that permission, and only creates DefaultConnectivityMonitor if it's granted. Weird that milliseconds after that check the system doesn't find the same result.

Can you please run

aapt dump badging build/outputs/apk/app-debug.apk | grep permission

to confirm what permissions you have.

If you indeed doesn't have that permission, it's possible the app-widget callbacks are tripping you. As a workaround you can try to do this:

public class App extends Application {
    public void onCreate() {
        super.onCreate();
        Glide.with(this); // force initialization within your app's domain
    }
}

@TWiStErRob
Copy link
Collaborator

What is context in your Glide.with, is it the service object or getApplicationContext()?

@ywwynm
Copy link
Author

ywwynm commented Aug 12, 2016

@TWiStErRob I followed your suggestion that writing Glide.with(this) in my Application. It seems that the problem is solved but I need more tests to ensure that. However, the context in Glide.with(context) is CustomRemoteViewsService.this.getApplicationContext(). I've tried to cast context to an Application object and logged it, the result shows that it is. So this is very strange for Glide.

@TWiStErRob TWiStErRob added the bug label Aug 12, 2016
@TWiStErRob
Copy link
Collaborator

The check for the permission may be wrong, and likely detects something that only happens in that service, because it is called by the system. @sjudd any idea why this could be?

@eygraber
Copy link

eygraber commented Nov 8, 2016

AppWidget sometimes had weird issues with permissions. See http://stackoverflow.com/a/20645908/691639 for a possible solution.

@sjudd
Copy link
Collaborator

sjudd commented Apr 17, 2017

I'm not sure, open to suggestions/pull requests.

@sjudd sjudd added the v3 label Sep 14, 2017
@sjudd sjudd removed the bug label Nov 12, 2017
@sjudd
Copy link
Collaborator

sjudd commented Nov 19, 2017

Actually I think this is an explanation for #1417, I'll call this a duplicate of that for now.

@sjudd sjudd closed this as completed Nov 19, 2017
@sjudd sjudd reopened this Nov 19, 2017
@sjudd sjudd added bug and removed v3 labels Nov 19, 2017
@sjudd sjudd added this to the 4.4 milestone Nov 19, 2017
@sjudd
Copy link
Collaborator

sjudd commented Nov 19, 2017

I take it back, that only catches exceptions register the receiver, which is probably different. I need to catch more things here...

@sjudd sjudd closed this as completed in bcd6cc2 Nov 19, 2017
mikepalarz added a commit to mikepalarz/JammyJamz that referenced this issue Oct 8, 2018
I was able to fix the image loading issue for the RemoteViews
within the widget by using Glide. This solution was largely
taken from the following GitHub issue:

bumptech/glide#1405
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants