Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #42 from manifoldjs/v0.1.4
Browse files Browse the repository at this point in the history
V0.1.4
  • Loading branch information
msrodri committed Aug 3, 2015
2 parents 9f8565b + 7abc3a5 commit cbb235f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-hostedwebapp",
"version": "0.1.3",
"version": "0.1.4",
"description": "Hosted Web App Plugin",
"cordova": {
"id": "cordova-plugin-hostedwebapp",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-plugin-hostedwebapp"
version="0.1.3">
version="0.1.4">
<name>HostedWebApp</name>
<description>Hosted Web App Plugin</description>
<license>MIT License</license>
Expand Down
93 changes: 50 additions & 43 deletions scripts/updateConfigurationBeforePrepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,34 @@ function downloadImage(imageUrl, imagesPath, imageSrc) {
});
}

// normalize icon list and download to res folder
function getManifestIcons(manifest) {
var iconList = [];
if (manifest.icons && manifest.icons instanceof Array) {
manifest.icons.forEach(function (icon) {
var imageUrl = url.resolve(manifest.start_url, icon.src);
icon.src = url.parse(imageUrl).pathname;
var sizes = icon.sizes.toLowerCase().split(' ');
sizes.forEach(function (iconSize) {
var dimensions = iconSize.split('x');
var element = {
"src": icon.src,
"width": dimensions[0],
"height": dimensions[1],
"density": icon.density,
"type": icon.type
};

iconList.push(element);
});
// normalize image list and download images to project folder
function processImageList(images, baseUrl) {
var imageList = [];
if (images && images instanceof Array) {
images.forEach(function (image) {
var imageUrl = url.resolve(baseUrl, image.src);
image.src = url.parse(imageUrl).pathname;
var sizes = image.sizes.toLowerCase().split(' ');
sizes.forEach(function (imageSize) {
var dimensions = imageSize.split('x');
var element = {
"src": image.src,
"width": dimensions[0],
"height": dimensions[1],
"density": image.density,
"type": image.type
};

imageList.push(element);
});

var iconsPath = path.dirname(path.join(projectRoot, icon.src));
var imagePath = path.dirname(path.join(projectRoot, image.src));

downloadImage(imageUrl, iconsPath, icon.src);
});
}
downloadImage(imageUrl, imagePath, image.src);
});
}

return iconList;
return imageList;
}

// Configure Cordova configuration parser
Expand Down Expand Up @@ -230,7 +230,7 @@ function isValidFormat(icon, validFormats) {
return false;
}

function processIconsBySize(platform, manifestIcons, splashScreenSizes, iconSizes, validFormats) {
function processImagesBySize(platform, manifestImages, splashScreenSizes, iconSizes, validFormats) {
// get platform section and create it if it does not exist
var root = config.doc.find('platform[@name=\'' + platform + '\']');
if (!root) {
Expand All @@ -240,7 +240,7 @@ function processIconsBySize(platform, manifestIcons, splashScreenSizes, iconSize

var platformIcons = root.findall('icon');
var platformScreens = root.findall('splash');
manifestIcons.forEach(function (element) {
manifestImages.forEach(function (element) {
if (!isValidFormat(element, validFormats)) {
return;
}
Expand Down Expand Up @@ -286,7 +286,7 @@ function processIconsBySize(platform, manifestIcons, splashScreenSizes, iconSize
});
}

function processIconsByDensity(platform, manifestIcons, screenSizeToDensityMap, iconSizeToDensityMap, dppxToDensityMap, validFormats) {
function processImagesByDensity(platform, manifestImages, screenSizeToDensityMap, iconSizeToDensityMap, dppxToDensityMap, validFormats) {
// get platform section and create it if it does not exist
var root = config.doc.find('platform[@name=\'' + platform + '\']');
if (!root) {
Expand All @@ -296,7 +296,7 @@ function processIconsByDensity(platform, manifestIcons, screenSizeToDensityMap,

var platformIcons = root.findall('icon');
var platformScreens = root.findall('splash');
manifestIcons.forEach(function (element) {
manifestImages.forEach(function (element) {
if (!isValidFormat(element, validFormats)) {
return;
}
Expand Down Expand Up @@ -455,7 +455,7 @@ function processDefaultIconsBySize(platform, screenSizes, iconSizes) {
});
}

function processiOSIcons(manifestIcons) {
function processiOSIcons(manifestIcons, manifestSplashScreens) {
var iconSizes = [
"40x40",
"80x80",
Expand Down Expand Up @@ -487,11 +487,12 @@ function processiOSIcons(manifestIcons) {
"1242x2208"
];

processIconsBySize('ios', manifestIcons, splashScreenSizes, iconSizes);
processImagesBySize('ios', manifestIcons, splashScreenSizes, iconSizes);
processImagesBySize('ios', manifestSplashScreens, splashScreenSizes, []);
processDefaultIconsBySize('ios', splashScreenSizes, iconSizes);
}

function processAndroidIcons(manifestIcons, outputConfiguration, previousIndent) {
function processAndroidIcons(manifestIcons, manifestSplashScreens) {
var iconSizeToDensityMap = {
36: 'ldpi',
48: 'mdpi',
Expand Down Expand Up @@ -540,11 +541,12 @@ function processAndroidIcons(manifestIcons, outputConfiguration, previousIndent)
'image/png'
];

processIconsByDensity('android', manifestIcons, screenSizeToDensityMap, iconSizeToDensityMap, dppxToDensityMap, validFormats);
processImagesByDensity('android', manifestIcons, screenSizeToDensityMap, iconSizeToDensityMap, dppxToDensityMap, validFormats);
processImagesByDensity('android', manifestSplashScreens, screenSizeToDensityMap, [], dppxToDensityMap, validFormats);
processDefaultIconsByDensity('android', screenDensities, iconDensities);
}

function processWindowsIcons(manifestIcons) {
function processWindowsIcons(manifestIcons, manifestSplashScreens) {
var iconSizes = [
"30x30",
"44x44",
Expand All @@ -566,11 +568,12 @@ function processWindowsIcons(manifestIcons) {
"1152x1920"
];

processIconsBySize('windows', manifestIcons, splashScreenSizes, iconSizes);
processImagesBySize('windows', manifestIcons, splashScreenSizes, iconSizes);
processImagesBySize('windows', manifestSplashScreens, splashScreenSizes, []);
processDefaultIconsBySize('windows', splashScreenSizes, iconSizes);
};

function processWindowsPhoneIcons(manifestIcons) {
function processWindowsPhoneIcons(manifestIcons, manifestSplashScreens) {
var iconSizes = [
"62x62",
"173x173"
Expand All @@ -580,7 +583,8 @@ function processWindowsPhoneIcons(manifestIcons) {
"480x800"
];

processIconsBySize('wp8', manifestIcons, splashScreenSizes, iconSizes);
processImagesBySize('wp8', manifestIcons, splashScreenSizes, iconSizes);
processImagesBySize('wp8', manifestSplashScreens, splashScreenSizes, []);
processDefaultIconsBySize('wp8', splashScreenSizes, iconSizes);
};

Expand Down Expand Up @@ -665,16 +669,19 @@ module.exports = function (context) {
// configure access rules
processAccessRules(manifest);

// Obtain and download the icons specified in the manidest
var manifestIcons = getManifestIcons(manifest);
// Obtain and download the icons and splash screens specified in the manifest.
// Currently, splash screens specified in the splash_screens section of the manifest
// take precedence over similarly sized splash screens in the icons section.
var manifestIcons = processImageList(manifest.icons, manifest.start_url);
var manifestSplashScreens = processImageList(manifest.splash_screens, manifest.start_url);

Q.allSettled(pendingTasks).then(function () {

// Configure the icons once all icon files are downloaded
processiOSIcons(manifestIcons);
processAndroidIcons(manifestIcons);
processWindowsIcons(manifestIcons);
processWindowsPhoneIcons(manifestIcons);
processiOSIcons(manifestIcons, manifestSplashScreens);
processAndroidIcons(manifestIcons, manifestSplashScreens);
processWindowsIcons(manifestIcons, manifestSplashScreens);
processWindowsPhoneIcons(manifestIcons, manifestSplashScreens);

// save the updated configuration
config.write();
Expand Down
29 changes: 27 additions & 2 deletions src/android/HostedWebApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.net.Uri;
import android.content.res.AssetManager;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
Expand All @@ -27,6 +28,7 @@
* This class manipulates the Web App W3C manifest.
*/
public class HostedWebApp extends CordovaPlugin {
private static final String LOG_TAG = "HostedWebApp";
private static final String DEFAULT_MANIFEST_FILE = "manifest.json";
private static final String OFFLINE_PAGE = "offline.html";
private static final String OFFLINE_PAGE_TEMPLATE = "<html><body><div style=\"top:50%%;text-align:center;position:absolute\">%s</div></body></html>";
Expand All @@ -35,6 +37,7 @@ public class HostedWebApp extends CordovaPlugin {
private JSONObject manifestObject;

private CordovaActivity activity;
private CordovaPlugin whiteListPlugin;

private LinearLayout rootLayout;
private WebView offlineWebView;
Expand Down Expand Up @@ -181,13 +184,27 @@ else if (id.equals("onPageFinished")) {
return null;
}

@Override
public Boolean shouldAllowRequest(String url) {
CordovaPlugin whiteListPlugin = this.getWhitelistPlugin();

if (whiteListPlugin != null && Boolean.TRUE != whiteListPlugin.shouldAllowRequest(url)) {
Log.w(LOG_TAG, String.format("Whitelist rejection: url='%s'", url));
}

// do not alter default behavior.
return super.shouldAllowRequest(url);
}

@Override
public boolean onOverrideUrlLoading(String url) {
CordovaPlugin whiteListPlugin = this.webView.getPluginManager().getPlugin("Whitelist");
CordovaPlugin whiteListPlugin = this.getWhitelistPlugin();

if (whiteListPlugin != null && Boolean.TRUE != whiteListPlugin.shouldAllowNavigation(url)) {
// If the URL is not in the list URLs to allow navigation, open the URL in the external browser
// (code extracted from CordovaLib/src/org/apache/cordova/CordovaWebViewImpl.java)
Log.w(LOG_TAG, String.format("Whitelist rejection: url='%s'", url));

try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
Expand All @@ -199,7 +216,7 @@ public boolean onOverrideUrlLoading(String url) {
} else {
intent.setData(uri);
}
cordova.getActivity().startActivity(intent);
this.activity.startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
e.printStackTrace();
}
Expand All @@ -214,6 +231,14 @@ public JSONObject getManifest() {
return this.manifestObject;
}

private CordovaPlugin getWhitelistPlugin() {
if (this.whiteListPlugin == null) {
this.whiteListPlugin = this.webView.getPluginManager().getPlugin("Whitelist");
}

return whiteListPlugin;
}

private boolean assetExists(String asset) {
final AssetManager assetManager = this.activity.getResources().getAssets();
try {
Expand Down
2 changes: 1 addition & 1 deletion src/windows/HostedWebAppPluginProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function navigationStartingEvent(evt) {
if (!isInWhitelist) {
evt.stopImmediatePropagation();
evt.preventDefault();
console.log("Popping out URL: " + evt.uri);
console.log("Whitelist rejection: url='" + evt.uri + "'");
Windows.System.Launcher.launchUriAsync(new Windows.Foundation.Uri(evt.uri));
}
}
Expand Down

0 comments on commit cbb235f

Please sign in to comment.