diff --git a/android/jni/com/mapswithme/platform/Platform.cpp b/android/jni/com/mapswithme/platform/Platform.cpp index de1befd9f6..02cde18a3f 100644 --- a/android/jni/com/mapswithme/platform/Platform.cpp +++ b/android/jni/com/mapswithme/platform/Platform.cpp @@ -3,6 +3,9 @@ #include "com/mapswithme/core/jni_helper.hpp" +#include "com/mapswithme/util/NetworkPolicy.hpp" + +#include "platform/network_policy.hpp" #include "platform/settings.hpp" #include "base/logging.hpp" @@ -105,6 +108,15 @@ void Platform::SetGuiThread(unique_ptr guiThread) android::Platform::Instance().SetGuiThread(move(guiThread)); } +namespace platform +{ +platform::NetworkPolicy GetCurrentNetworkPolicy() +{ + JNIEnv *env = jni::GetEnv(); + return platform::NetworkPolicy(network_policy::GetCurrentNetworkUsageStatus(env)); +} +} + namespace android { void Platform::Initialize(JNIEnv * env, jobject functorProcessObject, jstring apkPath, diff --git a/android/jni/com/mapswithme/util/NetworkPolicy.cpp b/android/jni/com/mapswithme/util/NetworkPolicy.cpp index de8588cbd9..cf985255b6 100644 --- a/android/jni/com/mapswithme/util/NetworkPolicy.cpp +++ b/android/jni/com/mapswithme/util/NetworkPolicy.cpp @@ -8,4 +8,12 @@ bool GetNetworkPolicyStatus(JNIEnv * env, jobject obj) jni::GetMethodID(env, obj, "сanUseNetwork", "()Z"); return env->CallBooleanMethod(obj, networkPolicyCanUseMethod); } + +bool GetCurrentNetworkUsageStatus(JNIEnv * env) +{ + static jclass const clazz = jni::GetGlobalClassRef(env, "com/mapswithme/util/NetworkPolicy"); + static jmethodID const method = + jni::GetStaticMethodID(env, clazz, "getCurrentNetworkUsageStatus", "()Z"); + return env->CallStaticBooleanMethod(clazz, method); +} } // namespace network_policy diff --git a/android/jni/com/mapswithme/util/NetworkPolicy.hpp b/android/jni/com/mapswithme/util/NetworkPolicy.hpp index 9e273d08c5..8c6bed5d7b 100644 --- a/android/jni/com/mapswithme/util/NetworkPolicy.hpp +++ b/android/jni/com/mapswithme/util/NetworkPolicy.hpp @@ -3,4 +3,5 @@ namespace network_policy { bool GetNetworkPolicyStatus(JNIEnv * env, jobject obj); +bool GetCurrentNetworkUsageStatus(JNIEnv * env); } diff --git a/android/src/com/mapswithme/util/NetworkPolicy.java b/android/src/com/mapswithme/util/NetworkPolicy.java index c1ad678e7e..21b80308fe 100644 --- a/android/src/com/mapswithme/util/NetworkPolicy.java +++ b/android/src/com/mapswithme/util/NetworkPolicy.java @@ -42,7 +42,6 @@ public final class NetworkPolicy return; } - boolean nowInRoaming = ConnectionState.isInRoaming(); boolean acceptedInRoaming = Config.getMobileDataRoaming(); int type = Config.getUseMobileDataSettings(); @@ -73,13 +72,34 @@ public final class NetworkPolicy } } + public static boolean getCurrentNetworkUsageStatus() + { + if (ConnectionState.isWifiConnected()) + return true; + + if (!ConnectionState.isMobileConnected()) + return false; + + boolean nowInRoaming = ConnectionState.isInRoaming(); + boolean acceptedInRoaming = Config.getMobileDataRoaming(); + if (nowInRoaming && !acceptedInRoaming) + return false; + + int type = Config.getUseMobileDataSettings(); + return type == ALWAYS || (type == TODAY && isToday()); + } + + private static boolean isToday() + { + long timestamp = Config.getMobileDataTimeStamp(); + return TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - timestamp) < 1; + } + private static void showDialogIfNeeded(@NonNull FragmentManager fragmentManager, @NonNull NetworkPolicyListener listener, @NonNull NetworkPolicy policy) { - long timestamp = Config.getMobileDataTimeStamp(); - boolean showDialog = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - timestamp) >= 1; - if (!showDialog) + if (isToday()) { listener.onResult(policy); return; diff --git a/iphone/Maps/Core/NetworkPolicy/MWMNetworkPolicy.h b/iphone/Maps/Core/NetworkPolicy/MWMNetworkPolicy.h index 272644ddc5..8dd5dd1c5f 100644 --- a/iphone/Maps/Core/NetworkPolicy/MWMNetworkPolicy.h +++ b/iphone/Maps/Core/NetworkPolicy/MWMNetworkPolicy.h @@ -1,22 +1,8 @@ #pragma once -#include "platform/network_policy.hpp" +#include "platform/network_policy_ios.h" namespace network_policy { -enum Stage -{ - Ask, - Always, - Never, - Today, - NotToday -}; - void CallPartnersApi(platform::PartnersApiFn fn, bool force = false); - -void SetStage(Stage state); -Stage const GetStage(); - -bool CanUseNetwork(); } // namespace network_policy diff --git a/iphone/Maps/Core/NetworkPolicy/MWMNetworkPolicy.mm b/iphone/Maps/Core/NetworkPolicy/MWMNetworkPolicy.mm index ad544efd6a..f077a44e45 100644 --- a/iphone/Maps/Core/NetworkPolicy/MWMNetworkPolicy.mm +++ b/iphone/Maps/Core/NetworkPolicy/MWMNetworkPolicy.mm @@ -3,34 +3,8 @@ #include "platform/platform.hpp" -namespace -{ -NSString * const kNetworkingPolicyTimeStamp = @"NetworkingPolicyTimeStamp"; -NSString * const kNetworkingPolicyStage = @"NetworkingPolicyStage"; -NSTimeInterval const kSessionDurationSeconds = 24 * 60 * 60; -} // namespace - namespace network_policy { -void SetStage(Stage stage) -{ - NSUserDefaults * ud = NSUserDefaults.standardUserDefaults; - [ud setInteger:static_cast(stage) forKey:kNetworkingPolicyStage]; - [ud setObject:[NSDate dateWithTimeIntervalSinceNow:kSessionDurationSeconds] - forKey:kNetworkingPolicyTimeStamp]; -} - -Stage const GetStage() -{ - return static_cast( - [NSUserDefaults.standardUserDefaults integerForKey:kNetworkingPolicyStage]); -} - -NSDate * GetPolicyDate() -{ - return [NSUserDefaults.standardUserDefaults objectForKey:kNetworkingPolicyTimeStamp]; -} - void CallPartnersApi(platform::PartnersApiFn fn, bool force) { auto const connectionType = GetPlatform().ConnectionStatus(); @@ -52,14 +26,14 @@ void CallPartnersApi(platform::PartnersApiFn fn, bool force) case Stage::Always: fn(true); return true; case Stage::Never: fn(false); return true; case Stage::Today: - if ([GetPolicyDate() compare:[NSDate date]] == NSOrderedDescending) + if (IsActivePolicyDate()) { fn(true); return true; } return false; case Stage::NotToday: - if ([GetPolicyDate() compare:[NSDate date]] == NSOrderedDescending) + if (IsActivePolicyDate()) { fn(false); return true; @@ -79,23 +53,4 @@ void CallPartnersApi(platform::PartnersApiFn fn, bool force) }]; }); } - -bool CanUseNetwork() -{ - using ct = Platform::EConnectionType; - switch (GetPlatform().ConnectionStatus()) - { - case ct::CONNECTION_NONE: return false; - case ct::CONNECTION_WIFI: return true; - case ct::CONNECTION_WWAN: - switch (GetStage()) - { - case Stage::Ask: return false; - case Stage::Always: return true; - case Stage::Never: return false; - case Stage::Today: return [GetPolicyDate() compare:[NSDate date]] == NSOrderedDescending; - case Stage::NotToday: return false; - } - } -} } // namespace network_policy diff --git a/local_ads/statistics.cpp b/local_ads/statistics.cpp index e23952df38..2d2510f005 100644 --- a/local_ads/statistics.cpp +++ b/local_ads/statistics.cpp @@ -3,6 +3,7 @@ #include "local_ads/file_helpers.hpp" #include "platform/http_client.hpp" +#include "platform/network_policy.hpp" #include "platform/platform.hpp" #include "coding/file_name_utils.hpp" @@ -220,6 +221,16 @@ std::vector SerializeForServer(std::list const & even deflate(buffer.get(), strlen(buffer.get()), std::back_inserter(result)); return result; } + +bool CanUpload() +{ + auto const connectionStatus = GetPlatform().ConnectionStatus(); + if (connectionStatus == Platform::EConnectionType::CONNECTION_WIFI) + return true; + + return connectionStatus == Platform::EConnectionType::CONNECTION_WWAN && + platform::GetCurrentNetworkPolicy().CanUse(); +} } // namespace namespace local_ads @@ -364,8 +375,7 @@ void Statistics::ProcessEvents(std::list & events) void Statistics::SendToServer() { - auto const connectionStatus = GetPlatform().ConnectionStatus(); - if (connectionStatus == Platform::EConnectionType::CONNECTION_WIFI) + if (CanUpload()) { for (auto it = m_metadataCache.begin(); it != m_metadataCache.end(); ++it) { diff --git a/map/cloud.cpp b/map/cloud.cpp index 1bf5ff24dc..ff6c5b8516 100644 --- a/map/cloud.cpp +++ b/map/cloud.cpp @@ -6,6 +6,7 @@ #include "coding/internal/file_data.hpp" #include "coding/zip_creator.hpp" +#include "platform/network_policy.hpp" #include "platform/http_client.hpp" #include "platform/platform.hpp" #include "platform/settings.hpp" @@ -131,6 +132,19 @@ std::string GetDeviceName() { return GetPlatform().DeviceName() + " (" + GetPlatform().DeviceModel() + ")"; } + +bool CanUpload(uint64_t totalUploadingSize) +{ + auto const status = GetPlatform().ConnectionStatus(); + switch (status) + { + case Platform::EConnectionType::CONNECTION_NONE: return false; + case Platform::EConnectionType::CONNECTION_WIFI: return true; + case Platform::EConnectionType::CONNECTION_WWAN: + return platform::GetCurrentNetworkPolicy().CanUse() && + totalUploadingSize <= kMaxWwanUploadingSizeInBytes; + } +} } // namespace Cloud::SnapshotRequestData::SnapshotRequestData(std::vector const & files) @@ -405,14 +419,8 @@ void Cloud::ScheduleUploading() return; } - auto const status = GetPlatform().ConnectionStatus(); - auto const totalUploadingSize = CalculateUploadingSizeImpl(); - if (status == Platform::EConnectionType::CONNECTION_NONE || - (status == Platform::EConnectionType::CONNECTION_WWAN && - totalUploadingSize > kMaxWwanUploadingSizeInBytes)) - { + if (!CanUpload(CalculateUploadingSizeImpl())) return; - } SortEntriesBeforeUploadingImpl(); diff --git a/platform/CMakeLists.txt b/platform/CMakeLists.txt index f616bceaef..f030c5f6d4 100644 --- a/platform/CMakeLists.txt +++ b/platform/CMakeLists.txt @@ -60,6 +60,8 @@ if(${PLATFORM_IPHONE}) http_client_apple.mm http_uploader_apple.mm marketing_service_ios.mm + network_policy_ios.h + network_policy_ios.mm platform_ios.mm platform_unix_impl.cpp platform_unix_impl.hpp @@ -79,6 +81,7 @@ else() # neither iPhone nor Android location_service.cpp location_service.hpp marketing_service_dummy.cpp + network_policy_dummy.cpp platform_qt.cpp wifi_info.hpp wifi_location_service.cpp diff --git a/platform/network_policy.hpp b/platform/network_policy.hpp index 7ba7b4ee8d..23e837df56 100644 --- a/platform/network_policy.hpp +++ b/platform/network_policy.hpp @@ -28,6 +28,8 @@ class NetworkPolicy // iOS friend void network_policy::CallPartnersApi(platform::PartnersApiFn fn, bool force); + + friend NetworkPolicy GetCurrentNetworkPolicy(); public: bool CanUse() const { return m_canUse; } @@ -37,4 +39,6 @@ private: bool m_canUse = false; }; + +extern NetworkPolicy GetCurrentNetworkPolicy(); } // namespace platform diff --git a/platform/network_policy_dummy.cpp b/platform/network_policy_dummy.cpp new file mode 100644 index 0000000000..7c92c9df84 --- /dev/null +++ b/platform/network_policy_dummy.cpp @@ -0,0 +1,9 @@ +#include "platform/network_policy.hpp" + +namespace platform +{ +NetworkPolicy GetCurrentNetworkPolicy() +{ + return NetworkPolicy(true); +} +} // namespace platform diff --git a/platform/network_policy_ios.h b/platform/network_policy_ios.h new file mode 100644 index 0000000000..a91350ca81 --- /dev/null +++ b/platform/network_policy_ios.h @@ -0,0 +1,21 @@ +#pragma once + +#include "platform/network_policy.hpp" + +namespace network_policy +{ +enum Stage +{ + Ask, + Always, + Never, + Today, + NotToday +}; + +void SetStage(Stage state); +Stage GetStage(); + +bool CanUseNetwork(); +bool IsActivePolicyDate(); +} // namespace network_policy diff --git a/platform/network_policy_ios.mm b/platform/network_policy_ios.mm new file mode 100644 index 0000000000..bc3076e4f5 --- /dev/null +++ b/platform/network_policy_ios.mm @@ -0,0 +1,68 @@ +#include "platform/network_policy_ios.h" + +#include "platform/platform.hpp" + +#import +#import +#import + +namespace +{ +NSString * const kNetworkingPolicyTimeStamp = @"NetworkingPolicyTimeStamp"; +NSString * const kNetworkingPolicyStage = @"NetworkingPolicyStage"; +NSTimeInterval const kSessionDurationSeconds = 24 * 60 * 60; +} // namespace + +namespace network_policy +{ +void SetStage(Stage stage) +{ + NSUserDefaults * ud = NSUserDefaults.standardUserDefaults; + [ud setInteger:static_cast(stage) forKey:kNetworkingPolicyStage]; + [ud setObject:[NSDate dateWithTimeIntervalSinceNow:kSessionDurationSeconds] + forKey:kNetworkingPolicyTimeStamp]; +} + +Stage GetStage() +{ + return static_cast( + [NSUserDefaults.standardUserDefaults integerForKey:kNetworkingPolicyStage]); +} + +NSDate * GetPolicyDate() +{ + return [NSUserDefaults.standardUserDefaults objectForKey:kNetworkingPolicyTimeStamp]; +} + +bool IsActivePolicyDate() +{ + return [GetPolicyDate() compare:[NSDate date]] == NSOrderedDescending; +} + +bool CanUseNetwork() +{ + using ct = Platform::EConnectionType; + switch (GetPlatform().ConnectionStatus()) + { + case ct::CONNECTION_NONE: return false; + case ct::CONNECTION_WIFI: return true; + case ct::CONNECTION_WWAN: + switch (GetStage()) + { + case Stage::Ask: return false; + case Stage::Always: return true; + case Stage::Never: return false; + case Stage::Today: return IsActivePolicyDate(); + case Stage::NotToday: return false; + } + } +} +} // namespace network_policy + +namespace platform +{ +NetworkPolicy GetCurrentNetworkPolicy() +{ + return NetworkPolicy(network_policy::CanUseNetwork()); +} +} // namespace platform diff --git a/xcode/platform/platform.xcodeproj/project.pbxproj b/xcode/platform/platform.xcodeproj/project.pbxproj index 450dce5ad6..a7109b11f3 100644 --- a/xcode/platform/platform.xcodeproj/project.pbxproj +++ b/xcode/platform/platform.xcodeproj/project.pbxproj @@ -106,6 +106,8 @@ 67AB92DD1B7B3D7300AB5194 /* mwm_version.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 67AB92DB1B7B3D7300AB5194 /* mwm_version.hpp */; }; 67AB92EA1B7B3E9100AB5194 /* get_text_by_id.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 67AB92E81B7B3E9100AB5194 /* get_text_by_id.cpp */; }; 67AB92EB1B7B3E9100AB5194 /* get_text_by_id.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 67AB92E91B7B3E9100AB5194 /* get_text_by_id.hpp */; }; + EB60B4DC204C130300E4953B /* network_policy_ios.mm in Sources */ = {isa = PBXBuildFile; fileRef = EB60B4DB204C130300E4953B /* network_policy_ios.mm */; }; + EB60B4DE204C175700E4953B /* network_policy_ios.h in Headers */ = {isa = PBXBuildFile; fileRef = EB60B4DD204C175700E4953B /* network_policy_ios.h */; }; F6DF73581EC9EAE700D8BA0B /* string_storage_base.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DF73561EC9EAE700D8BA0B /* string_storage_base.cpp */; }; F6DF73591EC9EAE700D8BA0B /* string_storage_base.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DF73561EC9EAE700D8BA0B /* string_storage_base.cpp */; }; F6DF735A1EC9EAE700D8BA0B /* string_storage_base.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DF73561EC9EAE700D8BA0B /* string_storage_base.cpp */; }; @@ -221,6 +223,8 @@ 67AB92DB1B7B3D7300AB5194 /* mwm_version.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = mwm_version.hpp; sourceTree = ""; }; 67AB92E81B7B3E9100AB5194 /* get_text_by_id.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = get_text_by_id.cpp; sourceTree = ""; }; 67AB92E91B7B3E9100AB5194 /* get_text_by_id.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = get_text_by_id.hpp; sourceTree = ""; }; + EB60B4DB204C130300E4953B /* network_policy_ios.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = network_policy_ios.mm; sourceTree = ""; }; + EB60B4DD204C175700E4953B /* network_policy_ios.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = network_policy_ios.h; sourceTree = ""; }; F6DF73561EC9EAE700D8BA0B /* string_storage_base.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = string_storage_base.cpp; sourceTree = ""; }; F6DF73571EC9EAE700D8BA0B /* string_storage_base.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = string_storage_base.hpp; sourceTree = ""; }; /* End PBXFileReference section */ @@ -388,6 +392,8 @@ 56EB1EDB1C6B6E6C0022D831 /* mwm_traits.hpp */, 67AB92DA1B7B3D7300AB5194 /* mwm_version.cpp */, 67AB92DB1B7B3D7300AB5194 /* mwm_version.hpp */, + EB60B4DD204C175700E4953B /* network_policy_ios.h */, + EB60B4DB204C130300E4953B /* network_policy_ios.mm */, 3DE8B98E1DEC3115000E6083 /* network_policy.hpp */, 675343981A3F5D5A00A0A8C3 /* platform_android.cpp */, 344D8A2E204945D000CF532F /* platform_ios.h */, @@ -463,6 +469,7 @@ 675343B71A3F5D5A00A0A8C3 /* http_request.hpp in Headers */, 6741250B1B4C00CC00A3E828 /* country_file.hpp in Headers */, 675343B81A3F5D5A00A0A8C3 /* http_thread_apple.h in Headers */, + EB60B4DE204C175700E4953B /* network_policy_ios.h in Headers */, 56EB1EDF1C6B6E6C0022D831 /* mwm_traits.hpp in Headers */, 675343C21A3F5D5A00A0A8C3 /* location.hpp in Headers */, 67AB92EB1B7B3E9100AB5194 /* get_text_by_id.hpp in Headers */, @@ -629,6 +636,7 @@ 67A2526B1BB40E520063F8A8 /* platform_mac.mm in Sources */, 6741250A1B4C00CC00A3E828 /* country_file.cpp in Sources */, 674125081B4C00CC00A3E828 /* country_defines.cpp in Sources */, + EB60B4DC204C130300E4953B /* network_policy_ios.mm in Sources */, 6741250E1B4C00CC00A3E828 /* local_country_file.cpp in Sources */, 670E8C761BB318AB00094197 /* platform_ios.mm in Sources */, 675E88A01DB7B0F200F8EBDA /* test_socket.cpp in Sources */,