Skip to content

Commit

Permalink
New: Mobile.GetConnectionStatus
Browse files Browse the repository at this point in the history
See README for usage
  • Loading branch information
obastemur committed Jul 21, 2015
1 parent 8641f8f commit b9f2056
Show file tree
Hide file tree
Showing 8 changed files with 538 additions and 20 deletions.
72 changes: 58 additions & 14 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
/* Copyright (c) 2015, JXcore Contributors
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
Copyright (c) 2015, Nubisa Inc. and JXcore Contributors

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

---

Reachability

Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Copyright (C) 2014 Apple Inc. All Rights Reserved.

16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ www/jxcore > jx install jxm --autoremove "*.gz"
into application's assets.
#### Mobile.GetDocumentsPath
Returns the location for Application User's Documents folder on the device.
Returns the location for Application User's Documents folder.
```
Mobile.GetDocumentsPath(function(err, location) {
Expand All @@ -187,6 +187,20 @@ Mobile.GetDocumentsPath(function(err, location) {
});
```
#### Mobile.GetConnectionStatus
Returns device's connection status
```
Mobile.GetConnectionStatus(function(err, status) {
if (status.NotConnected)
console.log("No internet connection");
else if (status.WiFi)
console.log("WiFi");
else if (status.WWAN)
console.log("Mobile Connection");
});
```
Android and iOS file systems behave differently. Android OS supports external persistent storage. If you want to store
a persistent information on Android OS, consider using sdcard location.
Expand Down
15 changes: 12 additions & 3 deletions app/jxcore_cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,23 @@ JXMobile.GetDocumentsPath = function(callback) {
});
};

JXMobile.GetConnectionStatus = function(callback) {
if (typeof callback != "function") {
throw new Error("JXMobile.GetConnectionStatus expects a function callback");
}

JXMobile('GetConnectionStatus').callNative(function(res){
callback(null, res);
});
};

internal_methods['registerUIMethod'] = function (methodName, callback_) {
if (methodName && Array.isArray(methodName)) {
methodName = methodName[0];
}

if (!methodName || !methodName.indexOf) {
console.error("Couldn't register UI method. '" + methodName + "' is undefined or not string");
throw new Error("Couldn't register UI method. '" + methodName + "' is undefined or not string");
return;
}

Expand All @@ -178,9 +188,8 @@ internal_methods['loadMainFile'] = function (filePath, callback_) {
filePath = filePath[0];
}

console.log("loadMainFile", filePath);
if (!filePath || !filePath.indexOf) {
console.error("Couldn't load main file. '" + filePath + "' is undefined or not string");
throw new Error("Couldn't load main file. '" + filePath + "' is undefined or not string");
return;
}

Expand Down
8 changes: 8 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<source-file src="src/ios/JXcore.m" />
<header-file src="src/ios/JXMobile.h" />
<source-file src="src/ios/JXMobile.m" />
<header-file src="src/ios/Reachability.h" />
<source-file src="src/ios/Reachability.m" />
<header-file src="src/ios/JXcoreExtension.h" />
<!--<source-file src="src/ios/JXcoreExtension.m" /> OPTIONAL-->
<header-file src="bin/jx.h" />
Expand All @@ -44,6 +46,7 @@
<source-file src="bin/libleveldown.a" framework="true" />
<source-file src="bin/libsnappy.a" framework="true" />

<framework src="SystemConfiguration.framework" />
<framework src="libstdc++.6.0.9.dylib" />
<framework src="CFNetwork.framework" />
<framework src="Foundation.framework" />
Expand All @@ -56,6 +59,11 @@
<param name="android-package" value="io.jxcore.node.jxcore"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</config-file>
<!-- <source-file src="app/jxcore" target-dir="assets/" /> -->
<source-file src="app/jxcore_cordova.js" target-dir="assets/" />

Expand Down
33 changes: 31 additions & 2 deletions src/android/java/io/jxcore/node/JXMobile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class JXMobile {
Expand All @@ -17,11 +20,11 @@ public static void Initialize() {
public void Receiver(ArrayList<Object> params, String callbackId) {
String message = (String) params.get(0);
String stack = (String) params.get(1);

Log.e("jxcore", "Error!: " + message + "\nStack: " + stack);
}
});

jxcore.RegisterMethod("GetDocumentsPath", new JXcoreCallback() {
@SuppressLint("NewApi")
@Override
Expand All @@ -30,5 +33,31 @@ public void Receiver(ArrayList<Object> params, String callbackId) {
jxcore.CallJSMethod(callbackId, "\"" + path + "\"");
}
});

jxcore.RegisterMethod("GetConnectionStatus", new JXcoreCallback() {
@SuppressLint("NewApi")
@Override
public void Receiver(ArrayList<Object> params, String callbackId) {
ConnectivityManager cm = (ConnectivityManager) jxcore.activity
.getBaseContext().getSystemService(Context.CONNECTIVITY_SERVICE);

String info = "{\"NotConnected\":1}";
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected()) {
info = "{\"WiFi\":1}";
break;
}
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected()) {
info = "{\"WWAN\":1}";
break;
}
}

jxcore.CallJSMethod(callbackId, info);
}
});
}
}
26 changes: 26 additions & 0 deletions src/ios/JXMobile.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import "JXcore.h"
#import "JXMobile.h"
#import "CDVJXcore.h"
#import "Reachability.h"

@implementation JXMobile
{}
Expand All @@ -23,5 +24,30 @@ + (void) defineMethods {
NSString *documentsDirectory = [paths objectAtIndex:0];
[JXcore callEventCallback:callbackId withJSON:[NSString stringWithFormat:@"\"%@\"",documentsDirectory]];
} withName:@"GetDocumentsPath"];

[JXcore addNativeBlock:^(NSArray *params, NSString *callbackId) {
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

NetworkStatus status = [reachability currentReachabilityStatus];

NSString *strStatus;
if(status == NotReachable)
{
//No internet
strStatus = @"{\"NotConnected\":1}";
}
else if (status == ReachableViaWiFi)
{
//WiFi
strStatus = @"{\"WiFi\":1}";
}
else if (status == ReachableViaWWAN)
{
//3G
strStatus = @"{\"WWAN\":1}";
}
[JXcore callEventCallback:callbackId withJSON:strStatus];
} withName:@"GetConnectionStatus"];
}
@end
100 changes: 100 additions & 0 deletions src/ios/Reachability.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
File: Reachability.h
Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
Version: 3.5
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2014 Apple Inc. All Rights Reserved.
*/

#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>


typedef enum : NSInteger {
NotReachable = 0,
ReachableViaWiFi,
ReachableViaWWAN
} NetworkStatus;


extern NSString *kReachabilityChangedNotification;


@interface Reachability : NSObject

/*!
* Use to check the reachability of a given host name.
*/
+ (instancetype)reachabilityWithHostName:(NSString *)hostName;

/*!
* Use to check the reachability of a given IP address.
*/
+ (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress;

/*!
* Checks whether the default route is available. Should be used by applications that do not connect to a particular host.
*/
+ (instancetype)reachabilityForInternetConnection;

/*!
* Checks whether a local WiFi connection is available.
*/
+ (instancetype)reachabilityForLocalWiFi;

/*!
* Start listening for reachability notifications on the current run loop.
*/
- (BOOL)startNotifier;
- (void)stopNotifier;

- (NetworkStatus)currentReachabilityStatus;

/*!
* WWAN may be available, but not active until a connection has been established. WiFi may require a connection for VPN on Demand.
*/
- (BOOL)connectionRequired;

@end


Loading

0 comments on commit b9f2056

Please sign in to comment.