446 |
andreas |
1 |
/*
|
|
|
2 |
* Copyright (C) 2022 by Andreas Theofilu <andreas@theosys.at>
|
|
|
3 |
*
|
|
|
4 |
* This program is free software; you can redistribute it and/or modify
|
|
|
5 |
* it under the terms of the GNU General Public License as published by
|
|
|
6 |
* the Free Software Foundation; either version 3 of the License, or
|
|
|
7 |
* (at your option) any later version.
|
|
|
8 |
*
|
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
* GNU General Public License for more details.
|
|
|
13 |
*
|
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
|
15 |
* along with this program; if not, write to the Free Software Foundation,
|
|
|
16 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
#include "tiosbattery.h"
|
|
|
20 |
#include "UIKit/UIKit.h"
|
|
|
21 |
#include <Foundation/Foundation.h>
|
|
|
22 |
#include "terror.h"
|
|
|
23 |
#include "tpagemanager.h"
|
|
|
24 |
|
|
|
25 |
@interface BatteryController : NSObject
|
|
|
26 |
|
|
|
27 |
@property(getter=getLeft) int Left;
|
|
|
28 |
@property(getter=getState) int State;
|
|
|
29 |
|
|
|
30 |
@end
|
|
|
31 |
|
|
|
32 |
@implementation BatteryController
|
|
|
33 |
|
|
|
34 |
- (id)init
|
|
|
35 |
{
|
|
|
36 |
[self setLeft:0];
|
|
|
37 |
[self setState:0];
|
|
|
38 |
|
|
|
39 |
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
|
|
|
40 |
// Request to be notified when battery charge or state changes
|
|
|
41 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
|
|
|
42 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
|
|
|
43 |
return self;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
- (void)batteryChanged:(NSNotification *)notification
|
|
|
47 |
{
|
|
|
48 |
[self UpdateBatteryStatus];
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
- (void)UpdateBatteryStatus
|
|
|
52 |
{
|
|
|
53 |
UIDevice *myDevice = [UIDevice currentDevice];
|
|
|
54 |
[myDevice setBatteryMonitoringEnabled:YES];
|
|
|
55 |
float left = [myDevice batteryLevel] * 100;
|
|
|
56 |
|
|
|
57 |
if ([self getLeft] < 0.0)
|
|
|
58 |
[self setLeft:(int)([self getLeft] * -1.0)];
|
|
|
59 |
else
|
|
|
60 |
[self setLeft:(int)left];
|
|
|
61 |
|
|
|
62 |
[self setState:(int)[myDevice batteryState]];
|
|
|
63 |
TIOSBattery::informStatus([self getLeft], [self getState]);
|
|
|
64 |
MSG_DEBUG("Event battery level: " << [self getLeft] << ", state: " << [self getState]);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
@end
|
|
|
68 |
|
|
|
69 |
BatteryController *battery = nil;
|
|
|
70 |
|
|
|
71 |
// -----------------------------------------------------------------------------
|
|
|
72 |
// ---- C++ part starts here
|
|
|
73 |
// -----------------------------------------------------------------------------
|
|
|
74 |
|
|
|
75 |
int TIOSBattery::mLeft{0};
|
|
|
76 |
TIOSBattery::BSTATE TIOSBattery::mState{BS_UNKNOWN};
|
|
|
77 |
|
|
|
78 |
TIOSBattery::TIOSBattery()
|
|
|
79 |
{
|
|
|
80 |
DECL_TRACER("TIOSBattery::TIOSBattery()");
|
|
|
81 |
|
|
|
82 |
battery = [[BatteryController alloc] init];
|
|
|
83 |
update();
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
TIOSBattery::~TIOSBattery()
|
|
|
87 |
{
|
|
|
88 |
DECL_TRACER("TIOSBattery::~TIOSBattery()");
|
|
|
89 |
|
|
|
90 |
if (battery)
|
|
|
91 |
[battery release];
|
|
|
92 |
|
|
|
93 |
battery = nil;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
void TIOSBattery::update()
|
|
|
97 |
{
|
|
|
98 |
DECL_TRACER("TIOSBattery::update()");
|
|
|
99 |
|
|
|
100 |
if ((mState != BS_UNKNOWN || mLeft != 0) && battery != nullptr)
|
|
|
101 |
return;
|
|
|
102 |
|
|
|
103 |
UIDevice *myDevice = [UIDevice currentDevice];
|
|
|
104 |
[myDevice setBatteryMonitoringEnabled:YES];
|
|
|
105 |
mLeft = [myDevice batteryLevel] * 100;
|
|
|
106 |
|
|
|
107 |
if (mLeft < 0)
|
|
|
108 |
mLeft = mLeft * -1;
|
|
|
109 |
|
|
|
110 |
long status = [myDevice batteryState];
|
|
|
111 |
|
|
|
112 |
switch (status)
|
|
|
113 |
{
|
|
|
114 |
case UIDeviceBatteryStateUnplugged: mState = BS_UNPLUGGED; break;
|
|
|
115 |
case UIDeviceBatteryStateCharging: mState = BS_CHARGING; break;
|
|
|
116 |
case UIDeviceBatteryStateFull: mState = BS_FULL; break;
|
|
|
117 |
|
|
|
118 |
default:
|
|
|
119 |
mState = BS_UNKNOWN;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
void TIOSBattery::informStatus(int left, int state)
|
|
|
124 |
{
|
|
|
125 |
mLeft = left;
|
|
|
126 |
mState = (BSTATE)state;
|
|
|
127 |
|
|
|
128 |
if (gPageManager)
|
|
|
129 |
gPageManager->informBatteryStatus(left, state);
|
|
|
130 |
}
|