-
Notifications
You must be signed in to change notification settings - Fork 52
[battery_plus] Refactor the C++ code #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
swift-kim
merged 2 commits into
flutter-tizen:master
from
swift-kim:refactor-battery-plus
Apr 15, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Copyright 2022 Samsung Electronics Co., Ltd. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "device_battery.h" | ||
|
|
||
| #include <device/battery.h> | ||
|
|
||
| #include "log.h" | ||
|
|
||
| bool DeviceBattery::StartListen(BatteryStatusCallback callback) { | ||
| // DEVICE_CALLBACK_BATTERY_CHARGING callback is called in only two cases | ||
| // like charging and discharing. When the charging status becomes "Full", | ||
| // discharging status event is called. So if it is full and disconnected | ||
| // from USB or AC charger, then any callbacks will not be called because the | ||
| // status already is the discharging status. To resolve this issue, | ||
| // DEVICE_CALLBACK_BATTERY_LEVEL callback is added. This callback can check | ||
| // whether it is disconnected in "Full" status. That is, when the battery | ||
| // status is full and disconnected, the level status will be changed from | ||
| // DEVICE_BATTERY_LEVEL_FULL to DEVICE_BATTERY_LEVEL_HIGH. | ||
| int ret = device_add_callback(DEVICE_CALLBACK_BATTERY_CHARGING, | ||
| OnBatteryStatusChanged, this); | ||
| if (ret != DEVICE_ERROR_NONE) { | ||
| LOG_ERROR("Failed to add callback: %s", get_error_message(ret)); | ||
| last_error_ = ret; | ||
| return false; | ||
| } | ||
|
|
||
| ret = device_add_callback(DEVICE_CALLBACK_BATTERY_LEVEL, | ||
| OnBatteryStatusChanged, this); | ||
| if (ret != DEVICE_ERROR_NONE) { | ||
| LOG_ERROR("Failed to add callback: %s", get_error_message(ret)); | ||
| last_error_ = ret; | ||
| return false; | ||
| } | ||
|
|
||
| callback_ = callback; | ||
| return true; | ||
| } | ||
|
|
||
| void DeviceBattery::StopListen() { | ||
| int ret = device_remove_callback(DEVICE_CALLBACK_BATTERY_CHARGING, | ||
| OnBatteryStatusChanged); | ||
| if (ret != DEVICE_ERROR_NONE) { | ||
| LOG_ERROR("Failed to remove callback: %s", get_error_message(ret)); | ||
| last_error_ = ret; | ||
| } | ||
|
|
||
| ret = device_remove_callback(DEVICE_CALLBACK_BATTERY_LEVEL, | ||
| OnBatteryStatusChanged); | ||
| if (ret != DEVICE_ERROR_NONE) { | ||
| LOG_ERROR("Failed to remove callback: %s", get_error_message(ret)); | ||
| last_error_ = ret; | ||
| } | ||
| } | ||
|
|
||
| int32_t DeviceBattery::GetLevel() { | ||
| int32_t level; | ||
| int ret = device_battery_get_percent(&level); | ||
| if (ret != DEVICE_ERROR_NONE) { | ||
| LOG_ERROR("Failed to get battery level: %s", get_error_message(ret)); | ||
| last_error_ = ret; | ||
| return -1; | ||
| } | ||
| return level; | ||
| } | ||
|
|
||
| BatteryStatus DeviceBattery::GetStatus() { | ||
| device_battery_status_e status; | ||
| int ret = device_battery_get_status(&status); | ||
| if (ret != DEVICE_ERROR_NONE) { | ||
| LOG_ERROR("Failed to get battery status: %s", get_error_message(ret)); | ||
| last_error_ = ret; | ||
| return BatteryStatus::kError; | ||
| } | ||
|
|
||
| switch (status) { | ||
| case DEVICE_BATTERY_STATUS_CHARGING: | ||
| return BatteryStatus::kCharging; | ||
| case DEVICE_BATTERY_STATUS_FULL: | ||
| return BatteryStatus::kFull; | ||
| case DEVICE_BATTERY_STATUS_DISCHARGING: | ||
| case DEVICE_BATTERY_STATUS_NOT_CHARGING: | ||
| return BatteryStatus::kDischarging; | ||
| default: | ||
| return BatteryStatus::kUnknown; | ||
| } | ||
| } | ||
|
|
||
| void DeviceBattery::OnBatteryStatusChanged(device_callback_e type, void *value, | ||
| void *user_data) { | ||
| auto *self = static_cast<DeviceBattery *>(user_data); | ||
|
|
||
| // DEVICE_CALLBACK_BATTERY_LEVEL callback is used only for checking whether | ||
| // the battery became a discharging status while it is full. | ||
| if (type == DEVICE_CALLBACK_BATTERY_LEVEL && | ||
| intptr_t(value) < DEVICE_BATTERY_LEVEL_HIGH) { | ||
| return; | ||
| } | ||
|
|
||
| BatteryStatus status = self->GetStatus(); | ||
| if (status == BatteryStatus::kFull && self->is_full_) { | ||
| // This function is called twice by registered callbacks when battery | ||
| // status is full. So, it needs to avoid the unnecessary second call. | ||
| return; | ||
| } | ||
| self->is_full_ = status == BatteryStatus::kFull; | ||
|
|
||
| self->callback_(status); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This currently results in an app crash. There will be a fix in the engine code soon.
Issue: flutter/flutter#101682
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@swift-kim Any progress on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bbrto21 It isn't likely the fix will land very soon. How do you think of this or this workaround?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@swift-kim Actually, I also made a patch to prevent crash. how about this?
First one has intentional leak and the second solution you mentioned doesn't seem very good as it depends on instances of other classes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
holder_suffix seems redundant. Otherwise looks good to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@swift-kim Can I make PR to apply all place where have same problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Just one thing I forgot to mention: the
private:section header is missing in the class.