Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Hardware Acceleration at Runtime: Android

Is it possible to consistently detect if an Activity has hardware acceleration enabled when it is created? I'm worried that users of my library will enable it through the manifest when they shouldn't, by not specifically disabling it for my Activity (as I instruct them to do.)

The only solid information I can find (http://android-developers.blogspot.com/2011/03/android-30-hardware-acceleration.html) says that I can query View.isHardwareAccelerated() and Canvas.isHardwareAccelerated(). However, for my purposes, I would like to ensure it is off when my library's Activity is shown. So far, I can't get anything to report a consistent yes/no when it is on or off. I tried hacking in a dummy view, setting it to my activity and then testing it, but it always returns false. Also, I tried testing Window.getAttributes( ).flags, but they aren't showing it either.

I am testing this because the hardware accelerated draw path for my library doesn't function correctly, and there doesn't seem like there is any way to fix it.

like image 212
Jesse Avatar asked Feb 03 '23 15:02

Jesse


2 Answers

Try FLAG_HARDWARE_ACCELERATED in flags in ActivityInfo for the activity, which you would get from PackageManager via getActivityInfo().

like image 159
CommonsWare Avatar answered Feb 05 '23 04:02

CommonsWare


I'm new in Android so I was stuck even with the clues given in the answer above.. went to search around and found this code somewhere in the sea of Google. Hope it helps someone.

/**
 * Returns true if the given Activity has hardware acceleration enabled
 * in its manifest, or in its foreground window.
 *
 * TODO(husky): Remove when initialize() is refactored (see TODO there)
 * TODO(dtrainor) This is still used by other classes.  Make sure to pull some version of this
 * out before removing it.
 */
public static boolean hasHardwareAcceleration(Activity activity) {
    // Has HW acceleration been enabled manually in the current window?
    Window window = activity.getWindow();
    if (window != null) {
        if ((window.getAttributes().flags
                & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
            return true;
        }
    }

    // Has HW acceleration been enabled in the manifest?
    try {
        ActivityInfo info = activity.getPackageManager().getActivityInfo(
                activity.getComponentName(), 0);
        if ((info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
            return true;
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.e("Chrome", "getActivityInfo(self) should not fail");
    }

    return false;
}
like image 27
Bruce Avatar answered Feb 05 '23 04:02

Bruce