Skip to content

Conversation

@rpdome
Copy link
Member

@rpdome rpdome commented Nov 18, 2025

AB#3428706

The value would also be cached in memory (since it should rarely change).
The caller can invoke this method right after it gets the package name via BrokerDiscoveryClient.getActiveBroker().

(NOTE: OneAuth would also need to invoke getDeviceIdleMode(), which will return the device status.
These two values shall be emitted together.
For that method, given that that value fluctuates based on usage, we are not going to cache it)

@github-actions
Copy link

❌ Work item link check failed. Description does not contain AB#{ID}.

Click here to Learn more.

@rpdome rpdome changed the title wip Get Broker apps 'isPowerOptimized' status Nov 19, 2025
@github-actions
Copy link

✅ Work item link check complete. Description contains link AB#3428706 to an Azure Boards work item.

@rpdome rpdome changed the title Get Broker apps 'isPowerOptimized' status Determine whether broker app opts out from battery optimization Nov 19, 2025
@github-actions github-actions bot changed the title Determine whether broker app opts out from battery optimization Get Broker apps 'isPowerOptimized' status, Fixes AB#3428706 Nov 19, 2025
@github-actions
Copy link

✅ Work item link check complete. Description contains link AB#3428706 to an Azure Boards work item.

@rpdome rpdome changed the title Get Broker apps 'isPowerOptimized' status, Fixes AB#3428706 Determine whether broker app opts out from battery optimization, Fixes AB#3428706 Nov 19, 2025
@rpdome rpdome force-pushed the rapong/powerOpt branch 3 times, most recently from ea0f533 to 146a034 Compare November 19, 2025 23:28
@rpdome rpdome marked this pull request as ready for review November 19, 2025 23:32
@rpdome rpdome requested review from a team as code owners November 19, 2025 23:32
Copilot AI review requested due to automatic review settings November 19, 2025 23:32
Copilot finished reviewing on behalf of rpdome November 19, 2025 23:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds functionality to determine whether a broker app has opted out from battery optimization, addressing work item AB#3428706. The implementation includes an in-memory cache to persist battery optimization status checks across multiple calls for the same package.

Key Changes:

  • Added isAppOptedOutFromBatteryOptimization() method with caching and exception handling
  • Introduced ConcurrentHashMap to cache battery optimization status per package name
  • Removed API level checks from getPowerOptimizationSettings() and @RequiresApi annotation from isIgnoringBatteryOptimizations()

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
common/src/main/java/com/microsoft/identity/common/adal/internal/PowerManagerWrapper.java Adds new public method to check battery optimization status with caching, removes API level guards from existing methods
changelog.txt Documents the new feature as a MINOR change
Comments suppressed due to low confidence (1)

common/src/main/java/com/microsoft/identity/common/adal/internal/PowerManagerWrapper.java:131

  • Severity: High – API level check removed without replacement.

Issue: The diff shows removal of the API level check that prevented calling isIgnoringBatteryOptimizations() on devices below API 23 (Android M). The method PowerManager.isIgnoringBatteryOptimizations() was introduced in API 23 and will cause a NoSuchMethodError or crash on older devices.

Impact: This will cause crashes on devices running Android versions below API 23 (Marshmallow) when getPowerOptimizationSettings() is called.

Recommendation: Restore the API level check that was removed:

public String getPowerOptimizationSettings(@NonNull final Context context){
    try {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return UNKNOWN_STATUS;
        }
        
        final PowerManager powerManager = ((PowerManager) context.getSystemService(Context.POWER_SERVICE));
        if (powerManager.isIgnoringBatteryOptimizations(context.getPackageName())){
            return "OptOut";
        } else {
            return "";
        }
    } catch (final Exception e){
        // Swallow all exception!
        return UNKNOWN_STATUS;
    }
}
    public String getPowerOptimizationSettings(@NonNull final Context context){
        try {
            final PowerManager powerManager = ((PowerManager) context.getSystemService(Context.POWER_SERVICE));
            if (powerManager.isIgnoringBatteryOptimizations(context.getPackageName())){
                return "OptOut";
            } else {
                return "";
            }

        } catch (final Exception e){
            // Swallow all exception!
            return UNKNOWN_STATUS;
        }
    }

Copy link
Contributor

@mohitc1 mohitc1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@rpdome rpdome merged commit 62f8de0 into dev Nov 25, 2025
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants