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

Crash with targetSdkVersion 31 due to accessing WindowManager from ApplicationContext #323

Open
Zhuinden opened this issue Dec 13, 2021 · 2 comments

Comments

@Zhuinden
Copy link

E/ContextImpl: Tried to access visual service WindowManager from a non-visual Context: WindowManager should be accessed from Activity or other visual Context. Use an Activity or a Context created with Context#createWindowContext(int, Bundle), which are adjusted to the configuration and visual bounds of an area on screen.
    java.lang.IllegalAccessException: Tried to access visual service WindowManager from a non-visual Context
        at android.app.ContextImpl.getSystemService(ContextImpl.java:2059)
        at android.content.ContextWrapper.getSystemService(ContextWrapper.java:857)
        at pro.piwik.sdk.tools.DeviceHelper.getResolution(DeviceHelper.java:123)
        at pro.piwik.sdk.Tracker.<init>(Tracker.java:137)
        at pro.piwik.sdk.Piwik.newTracker(Piwik.java:59)
@hannesa2
Copy link
Collaborator

hannesa2 commented Jul 10, 2022

This line doesn't exist DeviceHelper.java:123 This means the file got same changes and raise the question: does the issue still exists ?

Btw, when I see this
image

The question is valid, if we set minSDk to 19 and loose 0,41 % ? @d4rken

image

@hannesa2 hannesa2 mentioned this issue Jul 10, 2022
@d4rken
Copy link
Member

d4rken commented Jul 10, 2022

The error is from here, not sure why the line numbers are off.

Display display;
try {
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
display = wm.getDefaultDisplay();
} catch (NullPointerException e) {
Timber.tag(TAG).e(e, "Window service was not available from this context");
return null;
}

The SDK inits on the app context, so we get the Display from a non-ui context.

I wouldn't be opposed to raising the minApi (do we do a major version bump for this?), but I don't think it would solve this.

Even with the legacy code removed )#335) we need the Display instance for:

        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getRealMetrics(displayMetrics);
        width = displayMetrics.widthPixels;
        height = displayMetrics.heightPixels;

Otherwise we'd have to get one from the dev ( trackDeviceScreen(activity: Activity)?) (and we'd have to be careful not to leak it). Could maybe be automated by hooking into the Activity lifecycle listeners via the Application instance, but we currently don't explicitly get the Application instance, just a Context.

public static synchronized Matomo getInstance(Context context) {

Is there a way to do it correctly without a UI context? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants