blob: bc3076e4f56aae3075a42c820a06223bec68073c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#include "platform/network_policy_ios.h"
#include "platform/platform.hpp"
#import <Foundation/NSDate.h>
#import <Foundation/NSString.h>
#import <Foundation/NSUserDefaults.h>
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<NSInteger>(stage) forKey:kNetworkingPolicyStage];
[ud setObject:[NSDate dateWithTimeIntervalSinceNow:kSessionDurationSeconds]
forKey:kNetworkingPolicyTimeStamp];
}
Stage GetStage()
{
return static_cast<Stage>(
[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
|