Skip to content

Commit 753412e

Browse files
committed
address comment
1 parent 64834ee commit 753412e

File tree

4 files changed

+99
-24
lines changed

4 files changed

+99
-24
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// All rights reserved.
3+
//
4+
// This code is licensed under the MIT License.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
package com.microsoft.identity.common.adal.internal
24+
25+
/**
26+
* Enum representing the battery optimization status of the application.
27+
*/
28+
enum class BatteryOptimizationStatus {
29+
OptOut,
30+
NotOptOut,
31+
CannotRetrievePowerManager,
32+
UnknownError
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// All rights reserved.
3+
//
4+
// This code is licensed under the MIT License.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
package com.microsoft.identity.common.adal.internal
24+
25+
enum class DeviceDozeModeStatus {
26+
NotInDozeMode,
27+
Idle,
28+
LightIdle,
29+
CannotRetrievePowerManager,
30+
UnknownError
31+
}

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

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,38 @@ public String getDeviceIdleMode(@NonNull final Context context){
109109
return "";
110110
}
111111

112+
/**
113+
* Gets the Device Doze Mode Status.
114+
*
115+
* @param context The context to use for PowerManager.
116+
* @return a {@link DeviceDozeModeStatus}
117+
*/
118+
@NonNull
119+
public DeviceDozeModeStatus getDeviceDozeModeStatus(@NonNull final Context context){
120+
final String methodTag = TAG + ":getDeviceIdleMode";
121+
122+
try {
123+
final PowerManager powerManager = ((PowerManager) context.getSystemService(Context.POWER_SERVICE));
124+
if (powerManager == null) {
125+
Logger.error(methodTag, "PowerManager is null", null);
126+
return DeviceDozeModeStatus.CannotRetrievePowerManager;
127+
}
128+
if (powerManager.isDeviceIdleMode()) {
129+
return DeviceDozeModeStatus.Idle;
130+
}
131+
132+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
133+
powerManager.isDeviceLightIdleMode()) {
134+
return DeviceDozeModeStatus.LightIdle;
135+
}
136+
137+
return DeviceDozeModeStatus.NotInDozeMode;
138+
} catch (Exception e){
139+
Logger.error(methodTag, "Unknown Exception when checking doze mode status", e);
140+
return DeviceDozeModeStatus.UnknownError;
141+
}
142+
}
143+
112144
/**
113145
* Gets a string representing Power Optimization settings of the calling app
114146
* Will return an empty string if the app isn't opting out.
@@ -156,28 +188,18 @@ public BatteryOptimizationStatus isAppOptedOutFromBatteryOptimization(@NonNull f
156188
try {
157189
final PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
158190
if (powerManager == null) {
159-
Logger.error(methodTag, "PowerManager is null for package: " + packageName, null);
191+
Logger.error(methodTag, "PowerManager is null", null);
160192
return BatteryOptimizationStatus.CannotRetrievePowerManager;
161193
}
162194

163195
if (powerManager.isIgnoringBatteryOptimizations(key)) {
164196
return BatteryOptimizationStatus.OptOut;
165-
} else
166-
{
197+
} else {
167198
return BatteryOptimizationStatus.NotOptOut;
168199
}
169-
} catch (NullPointerException e) {
170-
Logger.error(methodTag, "NullPointerException when checking battery optimization status for package: " + packageName, e);
171-
return BatteryOptimizationStatus.NullPointerException;
172-
} catch (SecurityException e) {
173-
Logger.error(methodTag, "SecurityException when checking battery optimization status for package: " + packageName, e);
174-
return BatteryOptimizationStatus.SecurityException;
175-
} catch (IllegalArgumentException e) {
176-
Logger.error(methodTag, "IllegalArgumentException when checking battery optimization status for package: " + packageName, e);
177-
return BatteryOptimizationStatus.IllegalArgumentException;
178200
} catch (Exception e) {
179201
Logger.error(methodTag, "Unknown Exception when checking battery optimization status for package: " + packageName, e);
180-
return BatteryOptimizationStatus.UnknownException;
202+
return BatteryOptimizationStatus.UnknownError;
181203
}
182204
});
183205
}

common/src/main/java/com/microsoft/identity/common/adal/internal/enum.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)