- Added alert when no connection is available
- Added specific alert when user tries to download big country using 3G
- Max 3G Megabytes limit is set to 20Mb
- Moved WebViewController to Common folder
- Moved WiFi/3G detection code to Common/GetActiveConnectionType.h

Closed #124
This commit is contained in:
Alex Zolotarev 2011-03-22 16:45:37 +00:00 committed by Alex Zolotarev
parent 1c670aa479
commit a9c18795a5
5 changed files with 107 additions and 84 deletions

View file

@ -0,0 +1,40 @@
#import <SystemConfiguration/SCNetworkReachability.h>
#include <sys/socket.h>
#include <netinet/in.h>
enum TActiveConnectionType
{
ENotConnected,
EConnectedByWiFi,
EConnectedBy3G
};
TActiveConnectionType GetActiveConnectionType()
{
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRoute = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRoute, &flags);
CFRelease(defaultRoute);
if (!didRetrieveFlags)
return ENotConnected;
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL isWifi = !(flags & kSCNetworkReachabilityFlagsIsWWAN);
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
BOOL isConnected = isReachable && !needsConnection;
if (isConnected)
{
if (isWifi)
return EConnectedByWiFi;
else
return EConnectedBy3G;
}
return ENotConnected;
}

View file

@ -182,13 +182,14 @@
FA57ADF312F338FB002BFB9D /* countries_poly.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = countries_poly.txt; path = ../../data/countries_poly.txt; sourceTree = SOURCE_ROOT; };
FA87151A12B1518F00592DAF /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
FA8F8937132D5DB00048E3FE /* libtomcrypt.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libtomcrypt.a; sourceTree = SOURCE_ROOT; };
FAAE8D5D1338FF8B003ECAD5 /* GetActiveConnectionType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GetActiveConnectionType.h; sourceTree = "<group>"; };
FAF37EFA126DCE6F005EA154 /* IPhoneDownload.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; name = IPhoneDownload.mm; path = Platform/IPhoneDownload.mm; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
FAF37EFB126DCE6F005EA154 /* IPhoneDownloadManager.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; name = IPhoneDownloadManager.mm; path = Platform/IPhoneDownloadManager.mm; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
FAF37EFD126DCE6F005EA154 /* IPhonePlatform.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; name = IPhonePlatform.mm; path = Platform/IPhonePlatform.mm; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
FAF37EFE126DCE6F005EA154 /* IPhonePlatform.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; lineEnding = 0; name = IPhonePlatform.hpp; path = Platform/IPhonePlatform.hpp; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
FAF37F03126DCF11005EA154 /* IPhoneDownload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = IPhoneDownload.h; path = Platform/IPhoneDownload.h; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
FAFCB63413366E78001A5C59 /* WebViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WebViewController.h; path = Settings/WebViewController.h; sourceTree = "<group>"; };
FAFCB63513366E78001A5C59 /* WebViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = WebViewController.mm; path = Settings/WebViewController.mm; sourceTree = "<group>"; };
FAFCB63413366E78001A5C59 /* WebViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebViewController.h; sourceTree = "<group>"; };
FAFCB63513366E78001A5C59 /* WebViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebViewController.mm; sourceTree = "<group>"; };
FAFCB63713367AD1001A5C59 /* about.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = about.html; path = ../../data/about.html; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */
@ -384,8 +385,11 @@
FA34BEC71338D6DB00FFB2A7 /* Common */ = {
isa = PBXGroup;
children = (
FAFCB63413366E78001A5C59 /* WebViewController.h */,
FAFCB63513366E78001A5C59 /* WebViewController.mm */,
FA34BEC81338D72F00FFB2A7 /* CustomAlertView.mm */,
FA34BEC91338D72F00FFB2A7 /* CustomAlertView.h */,
FAAE8D5D1338FF8B003ECAD5 /* GetActiveConnectionType.h */,
);
name = Common;
path = ../Common;
@ -398,8 +402,6 @@
FA4135E2120A263C0062D5B4 /* CountriesViewController.mm */,
FA4135E6120A263C0062D5B4 /* SettingsManager.h */,
FA4135E7120A263C0062D5B4 /* SettingsManager.mm */,
FAFCB63413366E78001A5C59 /* WebViewController.h */,
FAFCB63513366E78001A5C59 /* WebViewController.mm */,
);
name = Settings;
sourceTree = "<group>";

View file

@ -3,15 +3,13 @@
#import "MapsAppDelegate.h"
#import "MapViewController.h"
#import "WebViewController.h"
#import "CustomAlertView.h"
#include "GetActiveConnectionType.h"
#include "IPhonePlatform.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#import <SystemConfiguration/SCNetworkReachability.h>
#define NAVIGATION_BAR_HEIGHT 44
#define MAX_3G_MEGABYTES 100
#define MAX_3G_MEGABYTES 20
#define GB 1000*1000*1000
#define MB 1000*1000
@ -253,30 +251,6 @@ TIndex g_clickedIndex;
}
}
// return NO if not connected or using 3G
+ (BOOL) IsUsingWIFI
{
// Create zero addy
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
return NO;
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL isWifi = !(flags & kSCNetworkReachabilityFlagsIsWWAN);
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
BOOL isConnected = isReachable && !needsConnection;
return isWifi && isConnected;
}
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
// deselect the current row (don't keep the table selection persistent)
@ -305,59 +279,69 @@ TIndex g_clickedIndex;
cancelButtonTitle: @"Cancel"
destructiveButtonTitle: @"Delete"
otherButtonTitles: nil];
if([popupQuery respondsToSelector:@selector(showFromRect)])
[popupQuery showFromRect: [cell frame] inView: tableView animated: YES];
else
[popupQuery showInView: tableView];
[popupQuery showFromRect: [cell frame] inView: tableView animated: YES];
[popupQuery release];
}
break;
case ENotDownloaded:
case EDownloadFailed:
{ // display confirmation popup with country size
BOOL isWifiConnected = [CountriesViewController IsUsingWIFI];
{
TActiveConnectionType connType = GetActiveConnectionType();
if (connType == ENotConnected)
{ // do not initiate any download
CustomAlertView * alert = [[CustomAlertView alloc] initWithTitle:@"No Internet connection detected"
message:@"We recommend to download large countries by using WiFi"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
TLocalAndRemoteSize sizePair = m_storage->CountrySizeInBytes(g_clickedIndex);
TLocalAndRemoteSize::first_type size = sizePair.second - sizePair.first;
if (connType == EConnectedBy3G && size > MAX_3G_MEGABYTES * MB)
{ // If user uses 3G, do not allow him to download large countries
CustomAlertView * alert = [[CustomAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@ is too large for 3G download", countryName]
message:@"Please, use WiFi to download large countries"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
// display confirmation popup with country size
// convert size to human readable values
NSString * strTitle = nil;
NSString * strDownload = nil;
if (size > GB)
{
size /= GB;
strTitle = [NSString stringWithFormat:@"%@", countryName];
strDownload = [NSString stringWithFormat:@"Download %qu GB", size];
}
else if (size > MB)
{
size /= MB;
strTitle = [NSString stringWithFormat:@"%@", countryName];
strDownload = [NSString stringWithFormat:@"Download %qu MB", size];
}
else
{
size = (size + 999) / 1000;
strTitle = [NSString stringWithFormat:@"%@", countryName];
strDownload = [NSString stringWithFormat:@"Download %qu kB", size];
}
TLocalAndRemoteSize sizePair = m_storage->CountrySizeInBytes(g_clickedIndex);
TLocalAndRemoteSize::first_type size = sizePair.second - sizePair.first;
// convert size to human readable values
NSString * strTitle = nil;
NSString * strDownload = nil;
if (size > GB)
{
size /= GB;
if (isWifiConnected)
strTitle = [NSString stringWithFormat:@"%@", countryName];
else
strTitle = [NSString stringWithFormat:@"We strongly recommend using WIFI for downloading %@", countryName];
strDownload = [NSString stringWithFormat:@"Download %qu GB", size];
}
else if (size > MB)
{
size /= MB;
if (isWifiConnected || size < MAX_3G_MEGABYTES)
strTitle = [NSString stringWithFormat:@"%@", countryName];
else
strTitle = [NSString stringWithFormat:@"We strongly recommend using WIFI for downloading %@", countryName];
strDownload = [NSString stringWithFormat:@"Download %qu MB", size];
}
else
{
size = (size + 999) / 1000;
strTitle = [NSString stringWithFormat:@"%@", countryName];
strDownload = [NSString stringWithFormat:@"Download %qu kB", size];
}
UIActionSheet * popupQuery = [[UIActionSheet alloc]
initWithTitle: strTitle
delegate: self
cancelButtonTitle: @"Cancel"
destructiveButtonTitle: nil
otherButtonTitles: strDownload, nil];
if([popupQuery respondsToSelector:@selector(showFromRect)])
[popupQuery showFromRect: [cell frame] inView: tableView animated: YES];
else
[popupQuery showInView: tableView];
[popupQuery release];
UIActionSheet * popupQuery = [[UIActionSheet alloc]
initWithTitle: strTitle
delegate: self
cancelButtonTitle: @"Cancel"
destructiveButtonTitle: nil
otherButtonTitles: strDownload, nil];
[popupQuery showFromRect: [cell frame] inView: tableView animated: YES];
[popupQuery release];
}
}
}
break;
case EDownloading:
@ -368,10 +352,7 @@ TIndex g_clickedIndex;
cancelButtonTitle: @"Do Nothing"
destructiveButtonTitle: @"Cancel Download"
otherButtonTitles: nil];
if([popupQuery respondsToSelector:@selector(showFromRect)])
[popupQuery showFromRect: [cell frame] inView: tableView animated: YES];
else
[popupQuery showInView: tableView];
[popupQuery showFromRect: [cell frame] inView: tableView animated: YES];
[popupQuery release];
}
break;