Commit 4bed3417 authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

[iOS] Add battery state and level to PreviousSessionInfo

Bug: 965393
Change-Id: Ib2c2a2ffdea9b42d4eded645e6a64432fc02d08a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1658760
Auto-Submit: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: default avatarOlivier Robin <olivierrobin@chromium.org>
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#669713}
parent d9a951a0
......@@ -22,6 +22,18 @@ enum class DeviceThermalState {
kSerious = 3,
kCritical = 4,
};
// The values of this enum are persisted representing the state of the last
// session (which may have been running a different version of the application).
// Therefore, entries should not be renumbered and numeric values should never
// be reused.
enum class DeviceBatteryState {
kUnknown = 0,
kUnplugged = 1,
kCharging = 2,
// Battery is plugged into power and the battery is 100% charged.
kFull = 3,
};
} // namespace previous_session_info_constants
// PreviousSessionInfo has two jobs:
......@@ -31,6 +43,13 @@ enum class DeviceThermalState {
// - Persist information about the current session, for use in a next session.
@interface PreviousSessionInfo : NSObject
// The battery level of the device at the end of the previous session.
@property(nonatomic, assign, readonly) float deviceBatteryLevel;
// The battery state of the device at the end of the previous session.
@property(nonatomic, assign, readonly)
previous_session_info_constants::DeviceBatteryState deviceBatteryState;
// The thermal state of the device at the end of the previous session.
@property(nonatomic, assign, readonly)
previous_session_info_constants::DeviceThermalState deviceThermalState;
......@@ -62,6 +81,12 @@ enum class DeviceThermalState {
// persisting information about the current session, for use in a next session.
- (void)beginRecordingCurrentSession;
// Updates the saved last known battery level of the device.
- (void)updateStoredBatteryLevel;
// Updates the saved last known battery state of the device.
- (void)updateStoredBatteryState;
// Updates the saved last known low power mode setting of the device.
- (void)updateStoredLowPowerMode;
......
......@@ -4,6 +4,8 @@
#import "ios/chrome/browser/metrics/previous_session_info.h"
#import <UIKit/UIKit.h>
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "base/system/sys_info.h"
......@@ -14,10 +16,28 @@
#error "This file requires ARC support."
#endif
using previous_session_info_constants::DeviceBatteryState;
using previous_session_info_constants::DeviceThermalState;
namespace {
// Translates a UIDeviceBatteryState value to DeviceBatteryState value.
DeviceBatteryState GetBatteryStateFromUIDeviceBatteryState(
UIDeviceBatteryState device_battery_state) {
switch (device_battery_state) {
case UIDeviceBatteryStateUnknown:
return DeviceBatteryState::kUnknown;
case UIDeviceBatteryStateUnplugged:
return DeviceBatteryState::kUnplugged;
case UIDeviceBatteryStateCharging:
return DeviceBatteryState::kCharging;
case UIDeviceBatteryStateFull:
return DeviceBatteryState::kFull;
}
return DeviceBatteryState::kUnknown;
}
// Translates a NSProcessInfoThermalState value to DeviceThermalState value.
DeviceThermalState GetThermalStateFromNSProcessInfoThermalState(
NSProcessInfoThermalState process_info_thermal_state) {
......@@ -40,6 +60,13 @@ DeviceThermalState GetThermalStateFromNSProcessInfoThermalState(
NSString* const kLastRanVersion = @"LastRanVersion";
// - The (string) device language.
NSString* const kLastRanLanguage = @"LastRanLanguage";
// - The (float) battery charge level.
NSString* const kPreviousSessionInfoBatteryLevel =
@"PreviousSessionInfoBatteryLevel";
// - The (integer) underlying value of the DeviceBatteryState enum representing
// the device battery state.
NSString* const kPreviousSessionInfoBatteryState =
@"PreviousSessionInfoBatteryState";
// - The (string) OS version.
NSString* const kPreviousSessionInfoOSVersion = @"PreviousSessionInfoOSVersion";
// - The (integer) underlying value of the DeviceThermalState enum representing
......@@ -63,6 +90,8 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating =
@property(nonatomic, assign) BOOL didBeginRecordingCurrentSession;
// Redefined to be read-write.
@property(nonatomic, assign) float deviceBatteryLevel;
@property(nonatomic, assign) DeviceBatteryState deviceBatteryState;
@property(nonatomic, assign) DeviceThermalState deviceThermalState;
@property(nonatomic, assign) BOOL deviceWasInLowPowerMode;
@property(nonatomic, assign) BOOL didSeeMemoryWarningShortlyBeforeTerminating;
......@@ -74,6 +103,8 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating =
@implementation PreviousSessionInfo
@synthesize deviceBatteryLevel = _deviceBatteryLevel;
@synthesize deviceBatteryState = _deviceBatteryState;
@synthesize deviceThermalState = _deviceThermalState;
@synthesize deviceWasInLowPowerMode = _deviceWasInLowPowerMode;
@synthesize didBeginRecordingCurrentSession = _didBeginRecordingCurrentSession;
......@@ -98,6 +129,10 @@ static PreviousSessionInfo* gSharedInstance = nil;
kDidSeeMemoryWarningShortlyBeforeTerminating];
gSharedInstance.deviceWasInLowPowerMode =
[defaults boolForKey:kPreviousSessionInfoLowPowerMode];
gSharedInstance.deviceBatteryState = static_cast<DeviceBatteryState>(
[defaults integerForKey:kPreviousSessionInfoBatteryState]);
gSharedInstance.deviceBatteryLevel =
[defaults floatForKey:kPreviousSessionInfoBatteryLevel];
gSharedInstance.deviceThermalState = static_cast<DeviceThermalState>(
[defaults integerForKey:kPreviousSessionInfoThermalState]);
......@@ -152,6 +187,21 @@ static PreviousSessionInfo* gSharedInstance = nil;
removeObjectForKey:previous_session_info_constants::
kDidSeeMemoryWarningShortlyBeforeTerminating];
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
[self updateStoredBatteryLevel];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateStoredBatteryLevel)
name:UIDeviceBatteryLevelDidChangeNotification
object:nil];
[self updateStoredBatteryState];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateStoredBatteryState)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
[self updateStoredLowPowerMode];
[[NSNotificationCenter defaultCenter]
addObserver:self
......@@ -170,6 +220,26 @@ static PreviousSessionInfo* gSharedInstance = nil;
[defaults synchronize];
}
- (void)updateStoredBatteryLevel {
[[NSUserDefaults standardUserDefaults]
setFloat:[UIDevice currentDevice].batteryLevel
forKey:kPreviousSessionInfoBatteryLevel];
}
- (void)updateStoredBatteryState {
UIDevice* device = [UIDevice currentDevice];
// Translate value to an app defined enum as the system could change the
// underlying values of UIDeviceBatteryState between OS versions.
DeviceBatteryState batteryState =
GetBatteryStateFromUIDeviceBatteryState(device.batteryState);
NSInteger batteryStateValue =
static_cast<std::underlying_type<DeviceBatteryState>::type>(batteryState);
[[NSUserDefaults standardUserDefaults]
setInteger:batteryStateValue
forKey:kPreviousSessionInfoBatteryState];
}
- (void)updateStoredLowPowerMode {
BOOL isLowPoweredModeEnabled =
[[NSProcessInfo processInfo] isLowPowerModeEnabled];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment