[core][jni] power manager mock

This commit is contained in:
Arsentiy Milchakov 2018-12-18 19:51:32 +03:00 committed by Daria Volvenkova
parent b6e56dc39b
commit 6ff2e6b9d4
10 changed files with 497 additions and 5 deletions

View file

@ -41,10 +41,12 @@
#include "platform/preferred_languages.hpp"
#include "platform/settings.hpp"
#include "base/assert.hpp"
#include "base/logging.hpp"
#include "base/math.hpp"
#include "base/sunrise_sunset.hpp"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
@ -101,6 +103,7 @@ Framework::Framework()
{
m_work.GetTrafficManager().SetStateListener(bind(&Framework::TrafficStateChanged, this, _1));
m_work.GetTransitManager().SetStateListener(bind(&Framework::TransitSchemeStateChanged, this, _1));
m_work.GetPowerManager().Subscribe(this);
}
void Framework::OnLocationError(int errorCode)
@ -591,6 +594,12 @@ void Framework::EnableDownloadOn3g()
{
m_work.GetDownloadingPolicy().EnableCellularDownload(true);
}
void Framework::DisableAdProvider(ads::Banner::Type const type, ads::Banner::Place const place)
{
m_work.DisableAdProvider(type, place);
}
uint64_t Framework::RequestTaxiProducts(JNIEnv * env, jobject policy, ms::LatLon const & from,
ms::LatLon const & to,
taxi::SuccessCallback const & onSuccess,
@ -672,9 +681,16 @@ void Framework::LogLocalAdsEvent(local_ads::EventType type, double lat, double l
m_work.GetLocalAdsManager().GetStatistics().RegisterEvent(std::move(event));
}
void Framework::DisableAdProvider(ads::Banner::Type const type, ads::Banner::Place const place)
void Framework::OnFacilityStateChanged(PowerManager::Facility const facility, bool state)
{
m_work.DisableAdProvider(type, place);
// Dummy
// TODO: provide information for UI Properties.
}
void Framework::OnConfigChanged(PowerManager::Config const actualConfig)
{
// Dummy
// TODO: provide information for UI Properties.
}
} // namespace android
@ -1950,4 +1966,27 @@ Java_com_mapswithme_maps_Framework_nativeGetMapObject(JNIEnv * env, jclass,
return nullptr;
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_Framework_nativeOnBatteryLevelChanged(JNIEnv *, jclass, jint level)
{
CHECK_GREATER_OR_EQUAL(level, 0, ());
CHECK_LESS_OR_EQUAL(level, 100, ());
frm()->GetPowerManager().OnBatteryLevelChanged(static_cast<uint8_t>(level));
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_Framework_nativeSetPowerManagerFacility(JNIEnv *, jclass,
jint facilityType, jboolean state)
{
frm()->GetPowerManager().SetFacility(static_cast<PowerManager::Facility>(facilityType),
static_cast<bool>(state));
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_Framework_nativeSetPowerManagerConfig(JNIEnv *, jclass, jint configType)
{
frm()->GetPowerManager().SetConfig(static_cast<PowerManager::Config>(configType));
}
} // extern "C"

View file

@ -4,6 +4,7 @@
#include "map/framework.hpp"
#include "map/place_page_info.hpp"
#include "map/power_manager/power_manager.hpp"
#include "ugc/api.hpp"
@ -48,7 +49,7 @@ struct BlockParams;
namespace android
{
class Framework
class Framework : private PowerManager::Subscriber
{
private:
drape_ptr<dp::ThreadSafeFactory> m_contextFactory;
@ -220,6 +221,10 @@ namespace android
locals::LocalsErrorCallback const & errorFn);
void LogLocalAdsEvent(local_ads::EventType event, double lat, double lon, uint16_t accuracy);
// PowerManager::Subscriber overrides:
void OnFacilityStateChanged(PowerManager::Facility const facility, bool state) override;
void OnConfigChanged(PowerManager::Config const actualConfig) override;
};
}

View file

@ -347,6 +347,8 @@ public class Framework
@Nullable
public static native String[] nativeGenerateNotifications();
private static native void nativeSetSpeedCamManagerMode(int mode);
public static native void nativeSetRoutingListener(RoutingListener listener);
public static native void nativeSetRouteProgressListener(RoutingProgressListener listener);
@ -524,5 +526,7 @@ public class Framework
public static native MapObject nativeGetMapObject(
@NonNull NotificationCandidate.MapObject mapObject);
private static native void nativeSetSpeedCamManagerMode(int mode);
public static native void nativeOnBatteryLevelChanged(int level);
public static native void nativeSetPowerManagerFacility(int facilityType, boolean state);
public static native void nativeSetPowerManagerConfig(int configType);
}

View file

@ -87,6 +87,8 @@ set(
notifications/notification_queue_storage.hpp
place_page_info.cpp
place_page_info.hpp
power_manager/power_manager.cpp
power_manager/power_manager.hpp
purchase.cpp
purchase.hpp
reachable_by_taxi_checker.cpp

View file

@ -558,11 +558,14 @@ Framework::Framework(FrameworkParams const & params)
m_notificationManager.TrimExpired();
eye::Eye::Instance().TrimExpired();
eye::Eye::Instance().Subscribe(&m_notificationManager);
GetPowerManager().Subscribe(this);
}
Framework::~Framework()
{
eye::Eye::Instance().UnsubscribeAll();
GetPowerManager().UnsubscribeAll();
m_threadRunner.reset();
@ -3825,6 +3828,12 @@ booking::AvailabilityParams Framework::GetLastBookingAvailabilityParams() const
return m_bookingAvailabilityParams;
}
void Framework::OnFacilityStateChanged(PowerManager::Facility const facility, bool state)
{
// Dummy.
// TODO: process facilities which do not have switch in UI.
}
TipsApi const & Framework::GetTipsApi() const
{
return m_tipsApi;

View file

@ -12,6 +12,7 @@
#include "map/mwm_url.hpp"
#include "map/notifications/notification_manager.hpp"
#include "map/place_page_info.hpp"
#include "map/power_manager/power_manager.hpp"
#include "map/purchase.hpp"
#include "map/routing_manager.hpp"
#include "map/routing_mark.hpp"
@ -143,7 +144,8 @@ struct FrameworkParams
class Framework : public SearchAPI::Delegate,
public RoutingManager::Delegate,
public TipsApi::Delegate,
public notifications::NotificationManager::Delegate
public notifications::NotificationManager::Delegate,
public PowerManager::Subscriber
{
DISALLOW_COPY(Framework);
@ -911,6 +913,7 @@ private:
std::unique_ptr<Purchase> m_purchase;
TipsApi m_tipsApi;
notifications::NotificationManager m_notificationManager;
PowerManager m_powerManager;
public:
TipsApi const & GetTipsApi() const;
@ -920,4 +923,9 @@ public:
double GetLastBackgroundTime() const override;
bool MakePlacePageInfo(eye::MapObject const & mapObject, place_page::Info & info) const;
PowerManager & GetPowerManager() { return m_powerManager; }
// PowerManager::Subscriber override.
void OnFacilityStateChanged(PowerManager::Facility const facility, bool state) override;
};

View file

@ -21,6 +21,7 @@ set(
kmz_unarchive_test.cpp
mwm_url_tests.cpp
notification_tests.cpp
power_manager_tests.cpp
search_api_tests.cpp
tips_tests.cpp
transliteration_test.cpp
@ -31,6 +32,7 @@ omim_add_test(${PROJECT_NAME} ${SRC})
omim_link_libraries(
${PROJECT_NAME}
platform_tests_support
metrics_tests_support
search_tests_support
editor_tests_support

View file

@ -0,0 +1,114 @@
#include "testing/testing.hpp"
#include "map/power_manager/power_manager.hpp"
#include "platform/platform_tests_support/scoped_file.hpp"
#include <functional>
#include <vector>
using namespace platform::tests_support;
struct SubscriberForTesting : public PowerManager::Subscriber
{
public:
// PowerManager::Subscriber overrides:
void OnFacilityStateChanged(PowerManager::Facility const facility, bool state) override
{
m_onFacilityEvents.push_back({facility, state});
}
void OnConfigChanged(PowerManager::Config const actualConfig) override
{
m_onConfigEvents.push_back(actualConfig);
}
struct facilityState
{
PowerManager::Facility m_facility;
bool m_state;
};
std::vector<facilityState> m_onFacilityEvents;
std::vector<PowerManager::Config> m_onConfigEvents;
};
UNIT_TEST(PowerManager_SetFacility)
{
ScopedFile sf("power_manager_config", ScopedFile::Mode::DoNotCreate);
PowerManager manager;
SubscriberForTesting subscriber;
manager.Subscribe(&subscriber);
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::ThreeDimensionalBuildings), true, ());
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::TrackRecord), true, ());
TEST_EQUAL(manager.GetConfig(), PowerManager::Config::Normal, ());
manager.SetFacility(PowerManager::Facility::ThreeDimensionalBuildings, false);
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::ThreeDimensionalBuildings), false, ());
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::TrackRecord), true, ());
TEST_EQUAL(manager.GetConfig(), PowerManager::Config::None, ());
TEST_EQUAL(subscriber.m_onFacilityEvents.size(), 1, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[0].m_facility,
PowerManager::Facility::ThreeDimensionalBuildings, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[0].m_state, false, ());
TEST_EQUAL(subscriber.m_onConfigEvents.size(), 1, ());
TEST_EQUAL(subscriber.m_onConfigEvents[0], PowerManager::Config::None, ());
subscriber.m_onFacilityEvents.clear();
subscriber.m_onConfigEvents.clear();
manager.SetFacility(PowerManager::Facility::TrackRecord, false);
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::TrackRecord), false, ());
TEST_EQUAL(manager.GetConfig(), PowerManager::Config::Economy, ());
TEST_EQUAL(subscriber.m_onFacilityEvents.size(), 1, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[0].m_facility, PowerManager::Facility::TrackRecord, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[0].m_state, false, ());
TEST_EQUAL(subscriber.m_onConfigEvents.size(), 1, ());
TEST_EQUAL(subscriber.m_onConfigEvents[0], PowerManager::Config::Economy, ());
}
UNIT_TEST(PowerManager_SetConfig)
{
ScopedFile sf("power_manager_config", ScopedFile::Mode::DoNotCreate);
PowerManager manager;
SubscriberForTesting subscriber;
manager.Subscribe(&subscriber);
TEST_EQUAL(manager.GetConfig(), PowerManager::Config::Normal, ());
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::ThreeDimensionalBuildings), true, ());
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::TrackRecord), true, ());
manager.SetConfig(PowerManager::Config::Economy);
TEST_EQUAL(manager.GetConfig(), PowerManager::Config::Economy, ());
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::ThreeDimensionalBuildings), false, ());
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::TrackRecord), false, ());
TEST_EQUAL(subscriber.m_onConfigEvents.size(), 1, ());
TEST_EQUAL(subscriber.m_onConfigEvents[0], PowerManager::Config::Economy, ());
TEST_EQUAL(subscriber.m_onFacilityEvents.size(), 2, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[0].m_facility,
PowerManager::Facility::ThreeDimensionalBuildings, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[0].m_state, false, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[1].m_facility, PowerManager::Facility::TrackRecord, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[1].m_state, false, ());
subscriber.m_onFacilityEvents.clear();
subscriber.m_onConfigEvents.clear();
manager.SetConfig(PowerManager::Config::Normal);
TEST_EQUAL(manager.GetConfig(), PowerManager::Config::Normal, ());
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::ThreeDimensionalBuildings), true, ());
TEST_EQUAL(manager.GetFacility(PowerManager::Facility::TrackRecord), true, ());
TEST_EQUAL(subscriber.m_onConfigEvents.size(), 1, ());
TEST_EQUAL(subscriber.m_onConfigEvents[0], PowerManager::Config::Normal, ());
TEST_EQUAL(subscriber.m_onFacilityEvents.size(), 2, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[0].m_facility,
PowerManager::Facility::ThreeDimensionalBuildings, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[0].m_state, true, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[1].m_facility, PowerManager::Facility::TrackRecord, ());
TEST_EQUAL(subscriber.m_onFacilityEvents[1].m_state, true, ());
}

View file

@ -0,0 +1,233 @@
#include "map/power_manager/power_manager.hpp"
#include "platform/platform.hpp"
#include "coding/file_name_utils.hpp"
#include "coding/file_reader.hpp"
#include "coding/file_writer.hpp"
#include "coding/internal/file_data.hpp"
#include "coding/serdes_json.hpp"
#include "base/assert.hpp"
#include "base/logging.hpp"
#include <unordered_map>
namespace
{
std::unordered_map<PowerManager::Config, PowerManager::FacilitiesState> const kConfigToState =
{
{PowerManager::Config::Normal, {{true, true}}},
{PowerManager::Config::Economy, {{false, false}}}
};
std::string GetFilePath()
{
return base::JoinPath(GetPlatform().SettingsDir(), "power_manager_config");
}
} // namespace
void PowerManager::Load()
{
Data result;
try
{
FileReader reader(GetFilePath());
NonOwningReaderSource source(reader);
coding::DeserializerJson des(source);
des(result);
m_data = result;
for (size_t i = 0; i < m_data.m_facilities.size(); ++i)
{
CHECK_NOT_EQUAL(static_cast<Facility>(i), Facility::Count, ());
for (auto & subscriber : m_subscribers)
subscriber->OnFacilityStateChanged(static_cast<Facility>(i), m_data.m_facilities[i]);
}
for (auto & subscriber : m_subscribers)
subscriber->OnConfigChanged(m_data.m_config);
}
catch (base::Json::Exception & ex)
{
LOG(LERROR, ("Cannot deserialize power manager data from file. Exception:", ex.Msg()));
}
catch (FileReader::Exception const & ex)
{
LOG(LERROR, ("Cannot read power manager config file. Exception:", ex.Msg()));
}
// Reset to default state.
m_data = {};
}
void PowerManager::SetFacility(Facility const facility, bool state)
{
CHECK_NOT_EQUAL(facility, Facility::Count, ());
if (m_data.m_facilities[static_cast<size_t>(facility)] == state)
return;
m_data.m_facilities[static_cast<size_t>(facility)] = state;
bool isConfigChanged = BalanceConfig();
if (!Save())
return;
for (auto & subscriber : m_subscribers)
subscriber->OnFacilityStateChanged(facility, state);
if (isConfigChanged)
{
for (auto & subscriber : m_subscribers)
subscriber->OnConfigChanged(m_data.m_config);
}
}
void PowerManager::SetConfig(Config const config)
{
if (m_data.m_config == config)
return;
m_data.m_config = config;
if (m_data.m_config == Config::None || m_data.m_config == Config::Auto)
return;
auto actualState = kConfigToState.at(config);
if (m_data.m_facilities == actualState)
return;
std::swap(m_data.m_facilities, actualState);
if (!Save())
return;
for (auto & subscriber : m_subscribers)
subscriber->OnConfigChanged(m_data.m_config);
for (size_t i = 0; i < actualState.size(); ++i)
{
if (m_data.m_facilities[i] != actualState[i])
{
for (auto & subscriber : m_subscribers)
subscriber->OnFacilityStateChanged(static_cast<Facility>(i), m_data.m_facilities[i]);
}
}
}
bool PowerManager::GetFacility(Facility const facility) const
{
CHECK_NOT_EQUAL(facility, Facility::Count, ());
return m_data.m_facilities[static_cast<size_t>(facility)];
}
PowerManager::FacilitiesState const & PowerManager::GetFacilities() const
{
return m_data.m_facilities;
}
PowerManager::Config const & PowerManager::GetConfig() const
{
return m_data.m_config;
}
void PowerManager::OnBatteryLevelChanged(uint8_t level)
{
if (m_data.m_config != Config::Auto)
return;
// TODO.
}
void PowerManager::Subscribe(Subscriber * subscriber)
{
ASSERT(subscriber, ());
m_subscribers.push_back(subscriber);
}
void PowerManager::UnsubscribeAll()
{
m_subscribers.clear();
}
bool PowerManager::BalanceConfig()
{
bool found = false;
Config actualConfig = m_data.m_config;
for (auto const & item : kConfigToState)
{
if (item.second == m_data.m_facilities)
{
actualConfig = item.first;
found = true;
break;
}
}
if (!found)
actualConfig = Config::None;
if (m_data.m_config == actualConfig)
return false;
m_data.m_config = actualConfig;
return true;
}
bool PowerManager::Save()
{
auto const result = base::WriteToTempAndRenameToFile(GetFilePath(), [this](string const & fileName)
{
try
{
FileWriter writer(fileName);
coding::SerializerJson<FileWriter> ser(writer);
ser(m_data);
return true;
}
catch (base::Json::Exception & ex)
{
LOG(LERROR, ("Cannot serialize power manager data into file. Exception:", ex.Msg()));
}
catch (FileReader::Exception const & ex)
{
LOG(LERROR, ("Cannot write power manager file. Exception:", ex.Msg()));
}
return false;
});
if (result)
return true;
// Try to load last correct state and notify subscribers.
Load();
return false;
}
std::string DebugPrint(PowerManager::Facility const facility)
{
switch (facility)
{
case PowerManager::Facility::ThreeDimensionalBuildings: return "ThreeDimensionalBuildings";
case PowerManager::Facility::TrackRecord: return "TrackRecord";
case PowerManager::Facility::Count: return "Count";
}
}
std::string DebugPrint(PowerManager::Config const config)
{
switch (config)
{
case PowerManager::Config::None: return "None";
case PowerManager::Config::Normal: return "Normal";
case PowerManager::Config::Economy: return "Economy";
case PowerManager::Config::Auto: return "Auto";
}
}

View file

@ -0,0 +1,76 @@
#pragma once
#include "base/visitor.hpp"
#include <array>
#include <cstdint>
#include <string>
#include <vector>
// Note: this class is NOT thread-safe.
class PowerManager
{
public:
// Note: the order is important.
// Note: new facilities must be added before Facility::Count.
// Note: do not use Facility::Count in external code, this value for internal use only.
enum class Facility : uint8_t
{
ThreeDimensionalBuildings,
TrackRecord,
Count
};
using FacilitiesState = std::array<bool, static_cast<size_t>(Facility::Count)>;
enum class Config : uint8_t
{
None,
Normal,
Economy,
Auto
};
class Subscriber
{
public:
virtual ~Subscriber() = default;
virtual void OnFacilityStateChanged(Facility const facility, bool state) {}
virtual void OnConfigChanged(Config const actualConfig) {}
};
void Load();
void SetFacility(Facility const facility, bool state);
void SetConfig(Config const config);
bool GetFacility(Facility const facility) const;
FacilitiesState const & GetFacilities() const;
Config const & GetConfig() const;
void OnBatteryLevelChanged(uint8_t level);
void Subscribe(Subscriber * subscriber);
void UnsubscribeAll();
private:
struct Data
{
DECLARE_VISITOR(visitor(m_facilities, "current_state"), visitor(m_config, "config"));
FacilitiesState m_facilities = {true, true};
Config m_config = Config::Normal;
};
// Returns true when config was changed.
bool BalanceConfig();
bool Save();
std::vector<Subscriber *> m_subscribers;
Data m_data;
};
std::string DebugPrint(PowerManager::Facility const facility);
std::string DebugPrint(PowerManager::Config const config);