Repository navigation
Load assets from same folder as JSbundle (Android) #4527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a28bef4
72c7265
670ada0
f345a15
83e628d
8c7da33
8bec103
398f2c3
076649e
682db02
cb808a5
2065ab3
5c72fa2
e43da34
34b5d1b
882533d
4ddb13f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ var AssetRegistry = require('AssetRegistry'); | |
| var PixelRatio = require('PixelRatio'); | ||
| var Platform = require('Platform'); | ||
| var SourceCode = require('NativeModules').SourceCode; | ||
| var assetPathUtils = require('../../local-cli/bundle/assetPathUtils'); | ||
|
|
||
| var _serverURL, _offlinePath; | ||
|
|
||
|
|
@@ -62,18 +63,18 @@ function getOfflinePath() { | |
| * Returns the path at which the asset can be found in the archive | ||
| */ | ||
| function getPathInArchive(asset) { | ||
| var offlinePath = getOfflinePath(); | ||
| if (Platform.OS === 'android') { | ||
| var assetDir = getBasePath(asset); | ||
| if (offlinePath) { | ||
| // E.g. 'file:///sdcard/AwesomeModule/drawable-mdpi/icon.png' | ||
| return 'file://' + offlinePath + getAssetPathInDrawableFolder(asset); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can you structure this block more like the iOS side encapsulating the The android |
||
| // E.g. 'assets_awesomemodule_icon' | ||
| // The Android resource system picks the correct scale. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. quotes: Strings must use singlequote. |
||
| return (assetDir + '/' + asset.name) | ||
| .toLowerCase() | ||
| .replace(/\//g, '_') // Encode folder structure in file name | ||
| .replace(/([^a-z0-9_])/g, '') // Remove illegal chars | ||
| .replace(/^assets_/, ''); // Remove "assets_" prefix | ||
| return assetPathUtils.getAndroidResourceIdentifier(asset); | ||
| } else { | ||
| // E.g. 'assets/AwesomeModule/icon@2x.png' | ||
| return getOfflinePath() + getScaledAssetPath(asset); | ||
| // E.g. '/assets/AwesomeModule/icon@2x.png' | ||
| return offlinePath + getScaledAssetPath(asset); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -86,29 +87,26 @@ function getPathOnDevserver(devServerUrl, asset) { | |
| '&hash=' + asset.hash; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a path like 'assets/AwesomeModule' | ||
| */ | ||
| function getBasePath(asset) { | ||
| // TODO(frantic): currently httpServerLocation is used both as | ||
| // path in http URL and path within IPA. Should we have zipArchiveLocation? | ||
| var path = asset.httpServerLocation; | ||
| if (path[0] === '/') { | ||
| path = path.substr(1); | ||
| } | ||
| return path; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a path like 'assets/AwesomeModule/icon@2x.png' | ||
| */ | ||
| function getScaledAssetPath(asset) { | ||
| var scale = pickScale(asset.scales, PixelRatio.get()); | ||
| var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; | ||
| var assetDir = getBasePath(asset); | ||
| var assetDir = assetPathUtils.getBasePath(asset); | ||
| return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a path like 'drawable-mdpi/icon.png' | ||
| */ | ||
| function getAssetPathInDrawableFolder(asset) { | ||
| var scale = pickScale(asset.scales, PixelRatio.get()); | ||
| var drawbleFolder = assetPathUtils.getAndroidDrawableFolderName(asset, scale); | ||
| var fileName = assetPathUtils.getAndroidResourceIdentifier(asset); | ||
| return drawbleFolder + '/' + fileName + '.' + asset.type; | ||
| } | ||
|
|
||
| function pickScale(scales: Array<number>, deviceScale: number): number { | ||
| // Packager guarantees that `scales` array is sorted | ||
| for (var i = 0; i < scales.length; i++) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /** | ||
| * Copyright (c) 2015-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| */ | ||
| 'use strict'; | ||
|
|
||
| function getAndroidAssetSuffix(scale) { | ||
| switch (scale) { | ||
| case 0.75: return 'ldpi'; | ||
| case 1: return 'mdpi'; | ||
| case 1.5: return 'hdpi'; | ||
| case 2: return 'xhdpi'; | ||
| case 3: return 'xxhdpi'; | ||
| case 4: return 'xxxhdpi'; | ||
| } | ||
| } | ||
|
|
||
| function getAndroidDrawableFolderName(asset, scale) { | ||
| var suffix = getAndroidAssetSuffix(scale); | ||
| if (!suffix) { | ||
| throw new Error( | ||
| 'Don\'t know which android drawable suffix to use for asset: ' + | ||
| JSON.stringify(asset) | ||
| ); | ||
| } | ||
| const androidFolder = 'drawable-' + suffix; | ||
| return androidFolder; | ||
| } | ||
|
|
||
| function getAndroidResourceIdentifier(asset) { | ||
| var folderPath = getBasePath(asset); | ||
| return (folderPath + '/' + asset.name) | ||
| .toLowerCase() | ||
| .replace(/\//g, '_') // Encode folder structure in file name | ||
| .replace(/([^a-z0-9_])/g, '') // Remove illegal chars | ||
| .replace(/^assets_/, ''); // Remove "assets_" prefix | ||
| } | ||
|
|
||
| function getBasePath(asset) { | ||
| var basePath = asset.httpServerLocation; | ||
| if (basePath[0] === '/') { | ||
| basePath = basePath.substr(1); | ||
| } | ||
| return basePath; | ||
| } | ||
|
|
||
| module.exports = { | ||
| getAndroidAssetSuffix: getAndroidAssetSuffix, | ||
| getAndroidDrawableFolderName: getAndroidDrawableFolderName, | ||
| getAndroidResourceIdentifier: getAndroidResourceIdentifier, | ||
| getBasePath: getBasePath | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still not sure about this dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this (assuming it works). Before this change we had two places that compute image file name given set of its properties (platform, scale, etc.). I would recommend extracting this change in a separate PR for easier review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since both resolveAssetSource.js and getAssetDestPath.js is going to depend on this refactor, extracting this change is going to be hard and confusing because then one PR would rely on the other PR getting merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's one of the reasons I hate Github's review flow (Phabricator's is so much better)