From: Igor Olshevskyi Date: Thu, 20 Apr 2017 11:22:17 +0000 (+0300) Subject: TizenRefApp-8415 [Call UI] Implement BatteryStateSource X-Git-Tag: submit/tizen/20170630.111746~16 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=40d0958110215ad6898ed987f9ce936023687ef2;p=profile%2Fwearable%2Fapps%2Fnative%2Fcall-ui.git TizenRefApp-8415 [Call UI] Implement BatteryStateSource Change-Id: Iaa192d0eaf04ff27d8a520dabc757e86e1e26836 --- diff --git a/inc/model/IndicatorState.h b/inc/model/IndicatorState.h index baf6b60..d842d42 100644 --- a/inc/model/IndicatorState.h +++ b/inc/model/IndicatorState.h @@ -29,7 +29,8 @@ namespace callui { PacketDirection getPacketDirection(IndicatorState state); RssiState getRssiState(IndicatorState state); bool getRoamingState(IndicatorState state); - + int getBatteryLevel(IndicatorState state); + BatteryState getBatteryState(IndicatorState state); } #endif // __CALLUI_MODEL_INDICATOR_STATE_H__ diff --git a/inc/model/types.h b/inc/model/types.h index 867774a..c2db8a2 100644 --- a/inc/model/types.h +++ b/inc/model/types.h @@ -122,6 +122,13 @@ namespace callui { FLIGHT_MODE }; + enum class BatteryState { + NORMAL = 0, + CHARGING, + CHARGING_PROBLEM, + UNKNOWN + }; + enum class IndicatorProperty { NW_CONNECTION = 0, RSSI, diff --git a/src/model/BatteryStateSource.cpp b/src/model/BatteryStateSource.cpp new file mode 100644 index 0000000..6c146a1 --- /dev/null +++ b/src/model/BatteryStateSource.cpp @@ -0,0 +1,198 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "BatteryStateSource.h" + +#include "common.h" + +namespace callui { + + using namespace ucl; + + BatteryStateSource::Bits::Bits(): + property(convertEnumValueToInt(IndicatorProperty::BATTERY)), + chargeValue(0), + chargingState(convertEnumValueToInt(BatteryState::NORMAL)) + { + } + + BatteryStateSource::Bits::Bits(const IndicatorState &state): + value(state.value) + { + if (property != convertEnumValueToInt(IndicatorProperty::BATTERY)) { + ELOG("State of incorrect property type [%d]", property); + *this = {}; + } + } + + BatteryStateSource::BatteryStateSource() + { + } + + BatteryStateSource::~BatteryStateSource() + { + delSysStateCallbacks(); + } + + BatteryStateSourceSRef BatteryStateSource::newInstance() + { + auto result = makeShared(); + FAIL_RETURN_VALUE(result->prepare(), {}, "result->prepare() failed!"); + return result; + } + + Result BatteryStateSource::prepare() + { + FAIL_RETURN(addSysStateCallbacks(), "addSysStateCallbacks() failed!"); + + updateBatteryState(); + + return RES_OK; + } + + bool BatteryStateSource::updateChargingStateBits(BatteryState state) + { + int stateInt = convertEnumValueToInt(state); + if (stateInt != m_bits.chargingState) { + m_bits.chargingState = stateInt; + return true; + } + return false; + } + + bool BatteryStateSource::updateChargeLevelBits(int value) + { + if (value != m_bits.chargeValue) { + m_bits.chargeValue = value; + return true; + } + return false; + } + + int BatteryStateSource::getCurChargeLevel() const + { + int capacity = 0; + if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &capacity) == 0) { + return capacity; + } + ELOG("Get battery capacity failed!"); + return -1; + } + + BatteryState BatteryStateSource::getCurChargingState() const + { + int isCharged = 0; + if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &isCharged) == 0) { + switch (isCharged) { + case -1: return BatteryState::CHARGING_PROBLEM; + case 1: return BatteryState::CHARGING; + default: return BatteryState::NORMAL; + } + } + ELOG("Get battery charging status failed!"); + return BatteryState::NORMAL; + } + + bool BatteryStateSource::updateBatteryState() + { + int chargeLevel = getCurChargeLevel(); + BatteryState state = getCurChargingState(); + + if (chargeLevel < 0) { + chargeLevel = 0; + if (state == BatteryState::NORMAL) { + state = BatteryState::UNKNOWN; + } + } + + bool wasUpdated = updateChargingStateBits(state); + if (updateChargeLevelBits(chargeLevel)) { + wasUpdated = true; + } + return wasUpdated; + } + + void BatteryStateSource::onBatteryStateChangedCb(keynode_t *node) + { + if (updateBatteryState() && m_handler) { + m_handler(); + } + } + + Result BatteryStateSource::addSysStateCallbacks() + { + int res = vconf_notify_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS, + CALLBACK_B(BatteryStateSource::onBatteryStateChangedCb), this); + if (res != 0) { + LOG_RETURN(RES_FAIL, "vconf_notify_key_changed() failed"); + } + + res = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, + CALLBACK_B(BatteryStateSource::onBatteryStateChangedCb), this); + if (res != 0) { + LOG_RETURN(RES_FAIL, "vconf_notify_key_changed() failed"); + } + + res = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, + CALLBACK_B(BatteryStateSource::onBatteryStateChangedCb), this); + if (res != 0) { + LOG_RETURN(RES_FAIL, "vconf_notify_key_changed() failed"); + } + + res = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY, + CALLBACK_B(BatteryStateSource::onBatteryStateChangedCb), this); + if (res != 0) { + LOG_RETURN(RES_FAIL, "vconf_notify_key_changed() failed"); + } + + return RES_OK; + } + + void BatteryStateSource::delSysStateCallbacks() + { + vconf_ignore_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS, + CALLBACK_B(BatteryStateSource::onBatteryStateChangedCb)); + vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, + CALLBACK_B(BatteryStateSource::onBatteryStateChangedCb)); + vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, + CALLBACK_B(BatteryStateSource::onBatteryStateChangedCb)); + vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY, + CALLBACK_B(BatteryStateSource::onBatteryStateChangedCb)); + } + + IndicatorState BatteryStateSource::getState() const + { + return {m_bits.value}; + } + + void BatteryStateSource::setStateChangeHandler(StateChangeHandler handler) + { + m_handler = handler; + } + + int getBatteryLevel(IndicatorState state) + { + return static_cast( + BatteryStateSource::Bits(state).chargeValue); + } + + BatteryState getBatteryState(IndicatorState state) + { + return static_cast( + BatteryStateSource::Bits(state).chargingState); + } + +} diff --git a/src/model/BatteryStateSource.h b/src/model/BatteryStateSource.h new file mode 100644 index 0000000..4ba260e --- /dev/null +++ b/src/model/BatteryStateSource.h @@ -0,0 +1,75 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CALLUI_MODEL_BATTERY_STATE_SOURCE_H__ +#define __CALLUI_MODEL_BATTERY_STATE_SOURCE_H__ + +#include "IIndicatorStateSource.h" + +#include + +#include "implTypes.h" + +namespace callui { + + class BatteryStateSource final : public IIndicatorStateSource { + public: + static BatteryStateSourceSRef newInstance(); + virtual ~BatteryStateSource(); + + // IIndicatorStateSource + + virtual IndicatorState getState() const override final; + virtual void setStateChangeHandler(StateChangeHandler handler) override final; + private: + friend class ucl::RefCountObj; + BatteryStateSource(); + + ucl::Result prepare(); + + ucl::Result addSysStateCallbacks(); + void delSysStateCallbacks(); + + void onBatteryStateChangedCb(keynode_t *node); + + int getCurChargeLevel() const; + BatteryState getCurChargingState() const; + + bool updateChargingStateBits(BatteryState state); + bool updateChargeLevelBits(int value); + bool updateBatteryState(); + + public: + union Bits { + struct { + uint8_t property : 4; + uint8_t chargeValue : 8; + uint8_t chargingState: 4; + }; + uint64_t value; + + Bits(); + Bits(const IndicatorState &state); + }; + + private: + Bits m_bits; + StateChangeHandler m_handler; + }; + +} + +#endif // __CALLUI_MODEL_BATTERY_STATE_SOURCE_H__ diff --git a/src/model/Call.cpp b/src/model/Call.cpp index d24fe62..55027f0 100644 --- a/src/model/Call.cpp +++ b/src/model/Call.cpp @@ -26,6 +26,7 @@ #include "IndicatorStateProvider.h" #include "ConnectionStateSource.h" #include "RssiStateSource.h" +#include "BatteryStateSource.h" #include "common.h" @@ -158,6 +159,14 @@ namespace callui { IndicatorProperty::RSSI, rssiStateSource), "setStateSource() failed!"); + auto batteryStateSource = BatteryStateSource::newInstance(); + if (!batteryStateSource) { + LOG_RETURN(RES_FAIL, "ConnectionStateSource::newInstance() failed!"); + } + FAIL_RETURN(m_indicatorStateProvider->setStateSource( + IndicatorProperty::BATTERY, batteryStateSource), + "setStateSource() failed!"); + return RES_OK; } diff --git a/src/model/ConnectionStateSource.cpp b/src/model/ConnectionStateSource.cpp index 24e3ec0..ff33839 100644 --- a/src/model/ConnectionStateSource.cpp +++ b/src/model/ConnectionStateSource.cpp @@ -25,14 +25,19 @@ namespace callui { using namespace ucl; ConnectionStateSource::Bits::Bits(): - value(0) + property(convertEnumValueToInt(IndicatorProperty::NW_CONNECTION)), + connState(convertEnumValueToInt(ConnectionType::NONE)), + packetDirection(convertEnumValueToInt(PacketDirection::NONE)) { - property = convertEnumValueToInt(IndicatorProperty::NW_CONNECTION); } ConnectionStateSource::Bits::Bits(const IndicatorState &state): value(state.value) { + if (property != convertEnumValueToInt(IndicatorProperty::NW_CONNECTION)) { + ELOG("State of incorrect property type [%d]", property); + *this = {}; + } } ConnectionStateSource::ConnectionStateSource() @@ -44,7 +49,7 @@ namespace callui { unsignSystStateCallbacks(); } - IIndicatorStateSourceSRef ConnectionStateSource::newInstance() + ConnectionStateSourceSRef ConnectionStateSource::newInstance() { auto result = makeShared(); FAIL_RETURN_VALUE(result->prepare(), {}, "result->prepare() failed!"); @@ -53,7 +58,7 @@ namespace callui { Result ConnectionStateSource::prepare() { - FAIL_RETURN(signSysStateCallbacks(), "signSysStateCallbacks() failed!"); + FAIL_RETURN(addSysStateCallbacks(), "addSysStateCallbacks() failed!"); updateConnType(); updatePacketDir(); @@ -86,20 +91,14 @@ namespace callui { int wifiStrength = 0; if (vconf_get_int(VCONFKEY_WIFI_STRENGTH, &wifiStrength) == 0) { switch (wifiStrength) { - case 1: - return ConnectionType::WIFI_01; - case 2: - return ConnectionType::WIFI_02; - case 3: - return ConnectionType::WIFI_03; - case 4: - return ConnectionType::WIFI_04; - default: - return ConnectionType::WIFI_00; + case 1: return ConnectionType::WIFI_01; + case 2: return ConnectionType::WIFI_02; + case 3: return ConnectionType::WIFI_03; + case 4: return ConnectionType::WIFI_04; + default: return ConnectionType::WIFI_00; } - } else { - ELOG("Get WiFi connection type failed!"); } + ELOG("Get WiFi connection type failed!"); return ConnectionType::NONE; } @@ -107,19 +106,17 @@ namespace callui { { int type = 0; if (vconf_get_int(VCONFKEY_TELEPHONY_PSTYPE, &type) == 0) { - switch (type) { - case VCONFKEY_TELEPHONY_PSTYPE_HSDPA: - case VCONFKEY_TELEPHONY_PSTYPE_HSUPA: + switch(type) { + case VCONFKEY_TELEPHONY_PSTYPE_HSDPA : + case VCONFKEY_TELEPHONY_PSTYPE_HSUPA : return ConnectionType::HSDPA_H; - case VCONFKEY_TELEPHONY_PSTYPE_HSPA: - case VCONFKEY_TELEPHONY_PSTYPE_HSPAP: + case VCONFKEY_TELEPHONY_PSTYPE_HSPA : return ConnectionType::HSPA_H_PLUS; - default: - break; + default : + return ConnectionType::UMTS_3G; } - } else { - ELOG("Get connection PS type failed!"); } + ELOG("Get connection PS type failed!"); return ConnectionType::NONE; } @@ -127,23 +124,21 @@ namespace callui { { int svcType = 0; if (vconf_get_int(VCONFKEY_TELEPHONY_SVCTYPE, &svcType) == 0) { - switch (svcType) { - case VCONFKEY_TELEPHONY_SVCTYPE_2G: - return ConnectionType::SIMPLE_2G; - case VCONFKEY_TELEPHONY_SVCTYPE_2_5G: + switch(svcType) { + case VCONFKEY_TELEPHONY_SVCTYPE_2G : + case VCONFKEY_TELEPHONY_SVCTYPE_2_5G : return ConnectionType::GPRS_G; - case VCONFKEY_TELEPHONY_SVCTYPE_3G: - return ConnectionType::UMTS_3G; - case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE: + case VCONFKEY_TELEPHONY_SVCTYPE_3G : + return getConnTypeByTelephonyPsType(); + case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE : return ConnectionType::EDGE_E; - case VCONFKEY_TELEPHONY_SVCTYPE_LTE: + case VCONFKEY_TELEPHONY_SVCTYPE_LTE : return ConnectionType::LTE_4G; - default: - break; + default : + return ConnectionType::NONE; } - } else { - ELOG("Get connection SVC type failed!"); } + ELOG("Get connection SVC type failed!"); return ConnectionType::NONE; } @@ -181,17 +176,7 @@ namespace callui { return updateConnTypeBits(ConnectionType::NONE); } - ConnectionType connType = getConnTypeByTelephonyPsType(); - if (connType != ConnectionType::NONE) { - return updateConnTypeBits(connType); - } - - connType = getConnTypeByTelephonySvcType(); - if (connType != ConnectionType::NONE) { - return updateConnTypeBits(connType); - } - - return updateConnTypeBits(ConnectionType::NONE); + return updateConnTypeBits(getConnTypeByTelephonySvcType()); } void ConnectionStateSource::onConnTypeChangedCb(keynode_t *node) @@ -220,9 +205,8 @@ namespace callui { default: return PacketDirection::NO_INPUT; } - } else { - ELOG("Get WiFi transfer state failed"); } + ELOG("Get WiFi transfer state failed"); return PacketDirection::NONE; } @@ -231,18 +215,13 @@ namespace callui { int state = 0; if (vconf_get_int(VCONFKEY_PACKET_STATE, &state) == 0) { switch (state) { - case VCONFKEY_PACKET_RX: - return PacketDirection::IN; - case VCONFKEY_PACKET_TX: - return PacketDirection::OUT; - case VCONFKEY_PACKET_RXTX: - return PacketDirection::INOUT; - default: - return PacketDirection::NO_INPUT; + case VCONFKEY_PACKET_RX: return PacketDirection::IN; + case VCONFKEY_PACKET_TX: return PacketDirection::OUT; + case VCONFKEY_PACKET_RXTX: return PacketDirection::INOUT; + default: return PacketDirection::NO_INPUT; } - } else { - ELOG("Get packet state failed"); } + ELOG("Get packet state failed"); return PacketDirection::NONE; } @@ -270,7 +249,7 @@ namespace callui { } } - Result ConnectionStateSource::signSysStateCallbacks() + Result ConnectionStateSource::addSysStateCallbacks() { // CONNECTION int res = vconf_notify_key_changed(VCONFKEY_WIFI_STRENGTH, @@ -373,27 +352,16 @@ namespace callui { m_handler = handler; } - ConnectionStateSource::Bits ConnectionStateSource::asBits(IndicatorState state) - { - Bits bits(state); - if (static_cast(bits.property) != IndicatorProperty::NW_CONNECTION) { - ELOG("State of incorrect property type [%d]", bits.property); - return {}; - } - - return bits; - } - ConnectionType getConnectionState(IndicatorState state) { return static_cast( - ConnectionStateSource::asBits(state).connState); + ConnectionStateSource::Bits(state).connState); } PacketDirection getPacketDirection(IndicatorState state) { return static_cast( - ConnectionStateSource::asBits(state).packetDirection); + ConnectionStateSource::Bits(state).packetDirection); } } diff --git a/src/model/ConnectionStateSource.h b/src/model/ConnectionStateSource.h index ba6c749..4598121 100644 --- a/src/model/ConnectionStateSource.h +++ b/src/model/ConnectionStateSource.h @@ -21,11 +21,13 @@ #include +#include "implTypes.h" + namespace callui { class ConnectionStateSource final : public IIndicatorStateSource { public: - static IIndicatorStateSourceSRef newInstance(); + static ConnectionStateSourceSRef newInstance(); virtual ~ConnectionStateSource(); // IIndicatorStateSource @@ -39,7 +41,7 @@ namespace callui { ucl::Result prepare(); - ucl::Result signSysStateCallbacks(); + ucl::Result addSysStateCallbacks(); void unsignSystStateCallbacks(); void onConnTypeChangedCb(keynode_t *node); @@ -59,7 +61,7 @@ namespace callui { PacketDirection getPacketDirByPacketState() const; bool updatePacketDir(); - private: + public: union Bits { struct { uint8_t property : 4; @@ -71,8 +73,6 @@ namespace callui { Bits(); Bits(const IndicatorState &state); }; - public: - static Bits asBits(IndicatorState state); private: Bits m_bits; diff --git a/src/model/RssiStateSource.cpp b/src/model/RssiStateSource.cpp index 58ae978..ef83230 100644 --- a/src/model/RssiStateSource.cpp +++ b/src/model/RssiStateSource.cpp @@ -23,14 +23,19 @@ namespace callui { using namespace ucl; RssiStateSource::Bits::Bits(): - value(0) + property(convertEnumValueToInt(IndicatorProperty::RSSI)), + rssiState(convertEnumValueToInt(RssiState::NONE)), + isRoamingEnable(0) { - property = convertEnumValueToInt(IndicatorProperty::RSSI); } RssiStateSource::Bits::Bits(const IndicatorState &state): value(state.value) { + if (property != convertEnumValueToInt(IndicatorProperty::RSSI)) { + ELOG("State of incorrect property type [%d]", property); + *this = {}; + } } RssiStateSource::RssiStateSource() @@ -39,10 +44,10 @@ namespace callui { RssiStateSource::~RssiStateSource() { - unsignSysStateCallbacks(); + delSysStateCallbacks(); } - IIndicatorStateSourceSRef RssiStateSource::newInstance() + RssiStateSourceSRef RssiStateSource::newInstance() { auto result = makeShared(); FAIL_RETURN_VALUE(result->prepare(), {}, "result->prepare() failed!"); @@ -51,7 +56,7 @@ namespace callui { Result RssiStateSource::prepare() { - FAIL_RETURN(signSysStateCallbacks(), "signSysStateCallbacks() failed!"); + FAIL_RETURN(addSysStateCallbacks(), "addSysStateCallbacks() failed!"); updateRssiState(); updateRoamingState(); @@ -146,7 +151,7 @@ namespace callui { } } - Result RssiStateSource::signSysStateCallbacks() + Result RssiStateSource::addSysStateCallbacks() { int res = vconf_notify_key_changed(VCONFKEY_TELEPHONY_SVC_ROAM, CALLBACK_B(RssiStateSource::onRssiChangedCb), this); @@ -181,7 +186,7 @@ namespace callui { return RES_OK; } - void RssiStateSource::unsignSysStateCallbacks() + void RssiStateSource::delSysStateCallbacks() { vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SVC_ROAM, CALLBACK_B(RssiStateSource::onRssiChangedCb)); @@ -205,26 +210,15 @@ namespace callui { m_handler = handler; } - RssiStateSource::Bits RssiStateSource::asBits(IndicatorState state) - { - Bits bits(state); - if (static_cast(bits.property) != IndicatorProperty::RSSI) { - ELOG("State of incorrect property type [%d]", bits.property); - return {}; - } - - return bits; - } - RssiState getRssiState(IndicatorState state) { return static_cast( - RssiStateSource::asBits(state).rssiState); + RssiStateSource::Bits(state).rssiState); } bool getRoamingState(IndicatorState state) { - return RssiStateSource::asBits(state).isRoamingEnable; + return RssiStateSource::Bits(state).isRoamingEnable; } } diff --git a/src/model/RssiStateSource.h b/src/model/RssiStateSource.h index 58d1fda..862e932 100644 --- a/src/model/RssiStateSource.h +++ b/src/model/RssiStateSource.h @@ -21,11 +21,13 @@ #include +#include "implTypes.h" + namespace callui { class RssiStateSource final : public IIndicatorStateSource { public: - static IIndicatorStateSourceSRef newInstance(); + static RssiStateSourceSRef newInstance(); virtual ~RssiStateSource(); // IIndicatorStateSource @@ -38,8 +40,8 @@ namespace callui { RssiStateSource(); ucl::Result prepare(); - ucl::Result signSysStateCallbacks(); - void unsignSysStateCallbacks(); + ucl::Result addSysStateCallbacks(); + void delSysStateCallbacks(); bool isRoamingActive() const; bool updateRssiStateBits(RssiState state); @@ -50,7 +52,7 @@ namespace callui { void onRssiChangedCb(keynode_t *node); - private: + public: union Bits { struct { uint8_t property : 4; @@ -58,13 +60,11 @@ namespace callui { uint8_t isRoamingEnable: 1; }; uint64_t value; + Bits(); Bits(const IndicatorState &state); }; - public: - static Bits asBits(IndicatorState state); - private: Bits m_bits; StateChangeHandler m_handler; diff --git a/src/model/implTypes.h b/src/model/implTypes.h index 701d4f8..5c5ef9d 100644 --- a/src/model/implTypes.h +++ b/src/model/implTypes.h @@ -61,6 +61,10 @@ namespace callui { UCL_DECLARE_REF_ALIASES(IndicatorStateProvider); UCL_DECLARE_REF_ALIASES(IIndicatorStateSource); + UCL_DECLARE_REF_ALIASES(ConnectionStateSource); + UCL_DECLARE_REF_ALIASES(RssiStateSource); + UCL_DECLARE_REF_ALIASES(BatteryStateSource); + using AudioStateEvent = ucl::Event; using MuteStateEvent = ucl::Event;