Skip to content

Commit

Permalink
Wifi Direct Experimental API initial contribution
Browse files Browse the repository at this point in the history
First implementation supports Android. Windows support under work.
Manual test ready to be deployed to crowwsalk-test-suite/usecase
Mock test to be developed after and contributed here - added entries to test gyp files.
  • Loading branch information
astojilj committed Nov 1, 2015
1 parent c3a2512 commit ef2dad6
Show file tree
Hide file tree
Showing 15 changed files with 632 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/android/app_template/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
Expand Down
2 changes: 2 additions & 0 deletions app/android/runtime_client_embedded_shell/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
Expand Down
2 changes: 2 additions & 0 deletions app/android/runtime_client_shell/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
Expand Down
7 changes: 6 additions & 1 deletion app/tools/android/handle_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@
'presentation': [],
'rawsockets': [],
'screenorientation': [],
'vibration': ['android.permission.VIBRATE']
'vibration': ['android.permission.VIBRATE'],
'wifidirect': ['android.permission.ACCESS_WIFI_STATE',
'android.permission.CHANGE_WIFI_STATE',
'android.permission.CHANGE_NETWORK_STATE',
'android.permission.INTERNET',
'android.permission.ACCESS_NETWORK_STATE']
}


Expand Down
3 changes: 2 additions & 1 deletion app/tools/android/make_apk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ def checkApk(self, apk_path, arch, keystore_path=None):
# 'snapshot_blob.bin',
'device_capabilities_api.js',
'launch_screen_api.js',
'presentation_api.js']
'presentation_api.js',
'wifidirect_api.js']
for res_file in embedded_related_files:
self.assertTrue(out.find(res_file) != -1)
if arch == 'x86':
Expand Down
3 changes: 2 additions & 1 deletion build/android/generate_xwalk_core_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def CopyJSBindingFiles(project_source, out_project_dir):
'xwalk/runtime/android/core_internal/src/org/xwalk/core/'
+ 'internal/extension/api/device_capabilities/device_capabilities_api.js',
'xwalk/runtime/android/core_internal/src/org/xwalk/core/'
+ 'internal/extension/api/messaging/messaging_api.js'
+ 'internal/extension/api/messaging/messaging_api.js',
'xwalk/experimental/wifidirect/wifidirect_api.js'
]

# Copy JS binding file to assets/jsapi folder.
Expand Down
1 change: 1 addition & 0 deletions experimental/wifidirect/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]
227 changes: 227 additions & 0 deletions experimental/wifidirect/wifidirect_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
// Copyright (c) 2015 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var g_next_async_call_id = 0;
var g_async_calls = [];
var g_listeners = [];

var WIFI_STATE_CHANGED_EVENT = 'wifistatechanged';
var PEERS_CHANGED_EVENT = 'peerschanged';
var CONNECTION_CHANGED_EVENT = 'connectionchanged';
var THIS_DEVICE_CHANGED_EVENT = 'thisdevicechanged';
var DISCOVERY_STOPPED_EVENT = 'discoverystoppedevent';

function AsyncCall(resolve, reject) {
this.resolve = resolve;
this.reject = reject;
}

function createPromise(msg) {
var promise = new Promise(function(resolve, reject) {
g_async_calls[g_next_async_call_id] = new AsyncCall(resolve, reject);
});
msg.asyncCallId = g_next_async_call_id;
extension.postMessage(JSON.stringify(msg));
++g_next_async_call_id;
return promise;
}

exports.init = function() {
var msg = {
'cmd': 'init'
};
return createPromise(msg);
};

exports.discoverPeers = function() {
var msg = {
'cmd': 'discoverPeers'
};
return createPromise(msg);
};

exports.stopDiscovery = function() {
var msg = {
'cmd': 'stopPeerDiscovery'
};
return createPromise(msg);
};

exports.getPeers = function() {
var msg = {
'cmd': 'getPeers'
};
return createPromise(msg);
};

exports.getConnectionInfo = function() {
var msg = {
'cmd': 'getConnectionInfo'
};
return createPromise(msg);
};

exports.ConnectionMethod = {
PBC : "pbc",
Display : "display",
Label : "label",
Keyboard : "keyboard"
};

exports.connect = function(device) {
var msg = {
'cmd': 'connect',
'data': device,
'method' : "pbc"
};
return createPromise(msg);
};

exports.cancelConnect = function() {
var msg = {
'cmd': 'cancelConnect'
};
return createPromise(msg);
};

exports.disconnect = function() {
var msg = {
'cmd': 'disconnect'
};
return createPromise(msg);
};

function Device(data) {
this.MAC = data.MAC;
this.name = data.name;
this.type = data.type;
this.status = data.status;
}

function _addEventListener(isOn, eventName, callback) {
if (typeof eventName !== 'string') {
console.warn("Invalid parameters of eventName!");
return -1;
}

if (!isOn && (typeof callback !== 'function')) {
console.warn("Invalid parameters of callback!");
return -1;
}

if (isOn && (typeof callback !== null) && (typeof callback !== 'function')) {
console.warn("Invalid parameters of callback!");
return -1;
}

var listener = {
'eventName': eventName,
'callback': callback
};

var listener_id;

if (isOn) {
switch(listener.eventName) {
case PEERS_CHANGED_EVENT:
g_listeners[0] = listener;
listener_id = 0;
break;
case PEERS_CHANGED_EVENT:
g_listeners[1] = listener;
listener_id = 1;
break;
case CONNECTION_CHANGED_EVENT:
g_listeners[2] = listener;
listener_id = 2;
break;
case THIS_DEVICE_CHANGED_EVENT:
g_listeners[3] = listener;
listener_id = 3;
break;
case DISCOVERY_STOPPED_EVENT:
g_listeners[4] = listener;
listener_id = 4;
break;
default:
console.warn("Invalid event name!");
break;
}
} else {
listener_id = g_next_async_call_id;
g_next_async_call_id += 1;
g_listeners[listener_id] = listener;
}
return listener_id;
}

Object.defineProperty(exports, 'onwifistatechanged', {
set: function(callback) {
_addEventListener(true, WIFI_STATE_CHANGED_EVENT, callback);
}
});

Object.defineProperty(exports, 'onpeerschanged', {
set: function(callback) {
_addEventListener(true, PEERS_CHANGED_EVENT, callback);
}
});

Object.defineProperty(exports, 'onconnectionchanged', {
set: function(callback) {
_addEventListener(true, CONNECTION_CHANGED_EVENT, callback);
}
});

Object.defineProperty(exports, 'onthisdevicechanged', {
set: function(callback) {
_addEventListener(true, THIS_DEVICE_CHANGED_EVENT, callback);
}
});

Object.defineProperty(exports, 'ondiscoverystopped', {
set: function(callback) {
_addEventListener(true, DISCOVERY_STOPPED_EVENT, callback);
}
});

exports.addEventListener = function(eventName, callback) {
return _addEventListener(false, eventName, callback);
}

function handleEvent(msg) {
for (var id in g_listeners) {
if (g_listeners[id]['eventName'] === msg.eventName && g_listeners[id]['callback']) {
g_listeners[id]['callback'](msg.data);
}
}
}

function handlePromise(msgObj) {
if (msgObj.data.error) {
g_async_calls[msgObj.asyncCallId].reject(msgObj.data.error);
} else {
g_async_calls[msgObj.asyncCallId].resolve(msgObj.data);
}

delete g_async_calls[msgObj.asyncCallId];
}

extension.setMessageListener(function(json) {
var _msg = JSON.parse(json);
switch (_msg.eventName) {
case WIFI_STATE_CHANGED_EVENT:
case PEERS_CHANGED_EVENT:
case CONNECTION_CHANGED_EVENT:
case THIS_DEVICE_CHANGED_EVENT:
case DISCOVERY_STOPPED_EVENT:
{
handleEvent(_msg);
return;
}
default:
break;
}
handlePromise(_msg);
});
2 changes: 2 additions & 0 deletions runtime/android/core/src/org/xwalk/core/XWalkActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@
*
* <pre>
* &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
* &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
* &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
* &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
* &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
* &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
* </pre>
Expand Down
2 changes: 2 additions & 0 deletions runtime/android/core/src/org/xwalk/core/XWalkUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@
*
* <pre>
* &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
* &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
* &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
* &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
* &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
* &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
* </pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.xwalk.core.internal.extension.api.launchscreen.LaunchScreenExtension;
import org.xwalk.core.internal.extension.api.messaging.Messaging;
import org.xwalk.core.internal.extension.api.presentation.PresentationExtension;
import org.xwalk.core.internal.extension.api.wifidirect.WifiDirect;

import android.app.Activity;
import android.content.Context;
Expand Down Expand Up @@ -89,6 +90,18 @@ public static void load(Context context, Activity activity) {
Log.w(TAG, "Failed to read JS API file: " + Messaging.JS_API_PATH);
}
}

{
String jsApiContent = "";
try {
jsApiContent = getExtensionJSFileContent(
context, WifiDirect.JS_API_PATH, true);
sBuiltinExtensions.put(WifiDirect.JS_API_PATH,
new WifiDirect(jsApiContent, activity));
} catch(IOException e) {
Log.w(TAG, "Failed to read JS API file: " + WifiDirect.JS_API_PATH);
}
}
}


Expand Down
Loading

0 comments on commit ef2dad6

Please sign in to comment.