use of com.codename1.io.File in project CodenameOne by codenameone.
the class CodenameOneImplementation method setBrowserPageInHierarchy.
/**
* Sets a relative URL from the html hierarchy
*
* @param browserPeer the peer component
* @param url the url relative to the HTML directory
*/
public void setBrowserPageInHierarchy(PeerComponent browserPeer, String url) throws IOException {
installTar();
FileSystemStorage fs = FileSystemStorage.getInstance();
String tardir = fs.getAppHomePath() + "cn1html";
if (tardir.startsWith("/")) {
tardir = "file://" + tardir;
}
if (url.startsWith("/")) {
setBrowserURL(browserPeer, tardir + url);
} else {
setBrowserURL(browserPeer, tardir + "/" + url);
}
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class CodenameOneImplementation method installTar.
/**
* Installs a tar file from the build server into the file system storage so it can be used with respect for hierarchy
*/
public void installTar() throws IOException {
String p = Preferences.get("cn1$InstallKey", null);
String buildKey = Display.getInstance().getProperty("build_key", null);
if (p == null || !p.equals(buildKey)) {
FileSystemStorage fs = FileSystemStorage.getInstance();
String tardir = fs.getAppHomePath() + "cn1html/";
fs.mkdir(tardir);
TarInputStream is = new TarInputStream(Display.getInstance().getResourceAsStream(getClass(), "/html.tar"));
TarEntry t = is.getNextEntry();
byte[] data = new byte[8192];
while (t != null) {
String name = t.getName();
if (t.isDirectory()) {
fs.mkdir(tardir + name);
} else {
String path = tardir + name;
String dir = path.substring(0, path.lastIndexOf('/'));
if (!fs.exists(dir)) {
mkdirs(fs, dir);
}
OutputStream os = fs.openOutputStream(tardir + name);
int count;
while ((count = is.read(data)) != -1) {
os.write(data, 0, count);
}
os.close();
}
t = is.getNextEntry();
}
Util.cleanup(is);
Preferences.set("cn1$InstallKey", buildKey);
}
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class CodenameOneImplementation method downloadImageToCache.
/**
* Downloads an image from a URL to the cache. Platforms
* that support a native image cache {@link #supportsNativeImageCache() } (e.g. Javascript) override this method to defer to the
* platform's handling of cached images. Platforms that have a caches directory ({@link FileSystemStorage#hasCachesDir() }
* will use that directory to cache the image. Other platforms will just download to storage.
*
* @param url The URL of the image to download.
* @param onSuccess Callback on success.
* @param onFail Callback on fail.
*
* @see URLImage#createToCache(com.codename1.ui.EncodedImage, java.lang.String, com.codename1.ui.URLImage.ImageAdapter)
*/
public void downloadImageToCache(String url, SuccessCallback<Image> onSuccess, final FailureCallback<Image> onFail) {
FileSystemStorage fs = FileSystemStorage.getInstance();
if (fs.hasCachesDir()) {
String name = "cn1_image_cache[" + url + "]";
name = StringUtil.replaceAll(name, "/", "_");
name = StringUtil.replaceAll(name, "\\", "_");
name = StringUtil.replaceAll(name, "%", "_");
name = StringUtil.replaceAll(name, "?", "_");
name = StringUtil.replaceAll(name, "*", "_");
name = StringUtil.replaceAll(name, ":", "_");
name = StringUtil.replaceAll(name, "=", "_");
String filePath = fs.getCachesDir() + fs.getFileSystemSeparator() + name;
// We use Util.downloadImageToFileSystem rather than CodenameOneImplementation.downloadImageToFileSystem
// because we want it to try to load from file system first.
Util.downloadImageToFileSystem(url, filePath, onSuccess, onFail);
} else {
// We use Util.downloadImageToStorage rather than CodenameOneImplementation.downloadImageToStorage
// because we want it to try to load from storage first.
Util.downloadImageToStorage(url, "cn1_image_cache[" + url + "]", onSuccess, onFail);
}
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class UIManager method initNamedTheme.
/**
* Same as the initFirstTheme method, but unlike that method this allows specifying the theme resource name
* @param resourceFile the name of the resource file starting with / and without the res extension
* @param resName the name of the theme to use from the file if it contains more than one theme
* @return the resource file or null in case of a failure
*/
public static Resources initNamedTheme(String resourceFile, String resName) {
try {
Resources theme = Resources.openLayered(resourceFile);
UIManager.getInstance().setThemeProps(theme.getTheme(resName));
Resources.setGlobalResources(theme);
return theme;
} catch (IOException e) {
Log.e(e);
}
return null;
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class UIManager method buildTheme.
private void buildTheme(Hashtable themeProps) {
String con = (String) themeProps.get("@includeNativeBool");
if (con != null && con.equalsIgnoreCase("true") && Display.getInstance().hasNativeTheme()) {
boolean a = accessible;
accessible = true;
Display.getInstance().installNativeTheme();
accessible = a;
}
Enumeration e = themeProps.keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
// this is a constant not a theme entry
if (key.startsWith("@")) {
themeConstants.put(key.substring(1, key.length()), themeProps.get(key));
continue;
}
this.themeProps.put(key, themeProps.get(key));
}
if (!this.themeProps.containsKey("PickerButtonBar.derive")) {
// For the button bar (with Cancel and Done) of the Picker interaction dialog in lightweight mode
if (this.themeProps.containsKey("PickerButtonBarNative.derive")) {
this.themeProps.put("PickerButtonBar.derive", "PickerButtonBarNative");
} else {
this.themeProps.put("PickerButtonBar.margin", "0,0,0,0");
this.themeProps.put("PickerButtonBar.border", Border.createCompoundBorder(Border.createLineBorder(1, ColorUtil.rgb(148, 150, 151)), Border.createEmpty(), Border.createEmpty(), Border.createEmpty()));
this.themeProps.put("PickerButtonBar.bgColor", "F0F1F3");
this.themeProps.put("PickerButtonBar.transparency", "255");
}
}
if (!this.themeProps.containsKey("PickerButtonTablet.derive")) {
// For the buttons of the picker in lightweight mode (the Cancel and Done buttons)
if (this.themeProps.containsKey("PickerButtonTabletNative.derive")) {
this.themeProps.put("PickerButtonTablet.derive", "PickerButtonTabletNative");
this.themeProps.put("PickerButtonTablet.sel#derive", "PickerButtonTabletNative.sel");
this.themeProps.put("PickerButtonTablet.press#derive", "PickerButtonTabletNative.press");
} else {
this.themeProps.put("PickerButtonTablet.derive", "Button");
this.themeProps.put("PickerButtonTablet.sel#derive", "Button");
this.themeProps.put("PickerButtonTablet.press#derive", "Button");
}
}
// necessary to clear up the style so we don't get resedue from the previous UI
defaultStyle = new Style();
// create's the default style
defaultStyle = createStyle("", "", false);
defaultSelectedStyle = new Style(defaultStyle);
defaultSelectedStyle = createStyle("", "sel#", true);
String overlayThemes = (String) themeProps.get("@OverlayThemes");
if (overlayThemes != null) {
java.util.List<String> overlayThemesArr = StringUtil.tokenize(overlayThemes, ',');
for (String th : overlayThemesArr) {
th = th.trim();
if (th.length() == 0) {
continue;
}
try {
Resources res = Resources.openLayered("/" + th);
boolean a = accessible;
accessible = true;
addThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
accessible = a;
} catch (Exception ex) {
System.err.println("Failed to load overlay theme file specified by @overlayThemes theme constant: " + th);
Log.e(ex);
}
}
}
}
Aggregations