<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:windowSoftInputMode="adjustPan"
- package="pl.org.nowoczesnapolska.wlmobi" android:versionName="1.1.1" android:versionCode="4">
+ package="pl.org.nowoczesnapolska.wloffline" android:versionName="1.1.1" android:versionCode="4">
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:anyDensity="true"
/>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+ <!-- uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> -->
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="false">
- <activity android:name=".Catalogue"
+ <activity android:name="pl.org.nowoczesnapolska.wloffline.Catalogue"
android:label="@string/app_name" android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<script type="text/javascript" charset="utf-8" src="js/phonegap-1.0.0.js"></script>
- <script type="text/javascript" charset="utf-8" src="js/dbput.js"></script>
- <script type="text/javascript" charset="utf-8" src="js/downloader.js"></script>
+ <script type="text/javascript" charset="utf-8" src="js/assetcopy.js"></script>
<script type="text/javascript" charset="utf-8" src="js/menuinterface.js"></script>
<script type="text/javascript" charset="utf-8" src="js/main.js"></script>
--- /dev/null
+/*
+ * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
+ * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+ */
+
+/**
+ *
+ * @return Object literal singleton instance of AssetCopy
+ */
+var AssetCopy = {
+ /**
+ * @param asset Path to the asset (relative to assets dir)
+ * @param target Path to DB file (relative to app db files dir)
+ * @param overwrite
+ * @param win Success callback
+ * @param fail Error callback
+ */
+ copy: function(asset, target, overwrite, win, fail) {
+ if(overwrite==false) overwrite="false";
+ else overwrite="true";
+ PhoneGap.exec(win, fail, "AssetCopy", "copy", [asset, target, overwrite]);
+ },
+};
* Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
*/
-var DB_VER = '0.9.15';
-
-var WL_INITIAL = WL + '/media/api/mobile/initial/initial.db';
-var WL_UPDATE = WL + '/api/changes/SINCE.json?book_fields=author,html,parent,parent_number,sort_key,title' +
- '&tag_fields=books,category,name,sort_key' +
- '&tag_categories=author,epoch,genre,kind';
-
+var DB_VER = 'o0.2';
var categories = {'author': 'autor',
self.updateDB(function() {
self.db = window.openDatabase("wolnelektury", "1.0", "WL Catalogue", 1);
if (self.db) {
- /*var regexp = {
- onFunctionCall: function(val) {
- var re = new RegExp(val.getString(0));
- if (val.getString(1).match(re))
- return 1;
- else
- return 0;
- }
- };
- self.db.createFunction("REGEXP", 2, regexp);*/
-
success && success();
} else {
error && error('Nie mogę otworzyć bazy danych: ' + err);
}
var done = function() {
- FileRepo.clear();
window.localStorage.setItem('db_ver', DB_VER);
console.log('db updated');
success && success();
console.log('upload db for Android 2.x+');
var dbname = "wolnelektury";
- var db = window.openDatabase(dbname, "1.0", "WL Catalogue", 500000);
- if (db) {
- console.log('db created successfully');
- DBPut.fetch(WL_INITIAL, function(data) {
- console.log('db fetch successful');
- success && success();
+ window.AssetCopy.copy("initial/Databases.db",
+ "/data/data/pl.org.nowoczesnapolska.wloffline/app_database/Databases.db", true,
+ function(data) {
+ console.log('db descriptor upload successful');
+ window.AssetCopy.copy("initial/0000000000000001.db",
+ "/data/data/pl.org.nowoczesnapolska.wloffline/app_database/file__0/0000000000000001.db", true,
+ function(data) {
+ console.log('db upload successful');
+ success && success();
+ }, function(data) {
+ error && error("database upload error: " + data);
+ });
}, function(data) {
- error && error('Błąd podczas pobierania bazy danych: ' + data);
+ error && error("database descriptor upload error: " + data);
});
- } else {
- error && error('Błąd podczas inicjowania bazy danych: ' + data);
- }
};
-
-
+
this.withState = function(callback) {
self.db.transaction(function(tx) {
tx.executeSql("SELECT * FROM state", [],
};
- self.update = function(data, success, error) {
- var addBookSql = new Sql("\
- INSERT OR REPLACE INTO book \
- (id, title, html_file, html_file_size, parent, parent_number, sort_key, pretty_size, authors) \
- VALUES \
- ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}')");
- var addTagSql = new Sql("INSERT OR REPLACE INTO tag (id, category, name, sort_key, books) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}')");
-
- var sqls = [];
-
- if (data.deleted) {
- for (i in data.deleted.books) {
- var book_id = data.deleted.books[i];
- sqls.push("DELETE FROM book WHERE id=" + book_id);
- FileRepo.deleteIfExists(book_id);
- }
-
- for (i in data.deleted.tags) {
- var tag_id = data.deleted.tags[i];
- sqls.push("DELETE FROM tag WHERE id=" + tag_id);
- }
- }
-
- if (data.updated) {
- for (i in data.updated.books) {
- var book = data.updated.books[i];
- if (!book.html) book.html = {};
- if (!book.html.url) book.html.url = '';
- if (!book.html.size) book.html.size = '';
- if (!book.parent) book.parent = '';
- if (!book.parent_number) book.parent_number = '';
- var pretty_size = prettySize(book.html.size);
- sqls.push(addBookSql.prepare(
- book.id, book.title, book.html.url, book.html.size,
- book.parent, book.parent_number, book.sort_key, pretty_size, book.author
- ));
- FileRepo.deleteIfExists(book.id);
- }
-
- for (i in data.updated.tags) {
- var tag = data.updated.tags[i];
- var category = categories[tag.category];
- var books = tag.books.join(',');
- sqls.push(addTagSql.prepare(tag.id, category, tag.name, tag.sort_key, books));
- }
- }
-
- sqls.push("UPDATE state SET last_checked=" + data.time_checked);
-
- self.chainSqls(sqls, success, error);
- };
-
-
- this.sync = function(success, error) {
- self.withState(function(state) {
- var url = WL_UPDATE.replace("SINCE", state.last_checked);
- console.log('sync: ' + url);
- var xhr = new XMLHttpRequest();
- xhr.open("GET", url);
- xhr.onload = function() {
- console.log('sync: fetched by ajax: ' + url);
- self.update(JSON.parse(xhr.responseText), success, error);
- }
- xhr.onerror = function(e) {
- error && error("Błąd aktualizacji bazy danych." + e);
- }
- xhr.send();
- });
- };
-
- this.updateLocal = function() {
- FileRepo.withLocal(function(local) {
- self.db.transaction(function(tx) {
- tx.executeSql("UPDATE book SET _local=0", [], function(tx, results) {
- ll = local.length;
- var ids = [];
- for (var i = 0; i < ll; i ++) {
- ids.push(local[i].name);
- }
- ids = ids.join(',');
- tx.executeSql("UPDATE book SET _local=1 where id in ("+ids+")");
- });
- });
- }, function() {
- self.db.transaction(function(tx) {
- tx.executeSql("UPDATE book SET _local=0");
- });
- });
- };
}
+++ /dev/null
-/*
- * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
- * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
- */
-
-/**
- *
- * @return Object literal singleton instance of DBPut
- */
-var DBPut = {
- /**
- * @param asset Path to the asset (relative to assets dir)
- * @param target Path to DB file (relative to app db files dir)
- * @param overwrite
- * @param win Success callback
- * @param fail Error callback
- */
- put: function(asset, target, overwrite, win, fail) {
- if (overwrite==false) overwrite="false";
- else overwrite="true";
- return PhoneGap.exec(
- win,
- fail,
- "DBPut",
- "put",
- [asset, target, overwrite]
- );
- },
- fetch: function(url, win, fail) {
- return PhoneGap.exec(
- win,
- fail,
- "DBPut",
- "fetch",
- [url]
- );
- }
-};
-
+++ /dev/null
-/**
- *
- * @return Object literal singleton instance of Downloader
- */
-var Downloader = {
- /**
- * @param fileUrl
- * @param dirName
- * @param fileName
- * @param overwrite
- * @param win
- * @param fail
- */
- downloadFile: function(fileUrl,dirName,fileName,overwrite,win,fail) {
- if(overwrite==false) overwrite="false";
- else overwrite="true";
- return PhoneGap.exec(win, fail, "Downloader", "downloadFile", [fileUrl,dirName,fileName,overwrite]);
- }
-};
var FileRepo = new function() {
/* API for files repository */
var self = this;
- this.root = null;
this.init = function(success, error) {
- self.initRoot(success);
+ success();
};
- this.initRoot = function(success) {
- // fs size is irrelevant, PERSISTENT is futile (on Android, at least)
- window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs) {
- console.log('local fs found: ' + fs.root.fullPath);
- self.root = fs.root;
- success && success();
- }, function() {
- console.log('local fs not found');
- success && success();
- });
- };
-
-
this.withLocalHtml = function(book_id, success, error) {
console.log('info:withLocalHtml: id:' + book_id);
View.spinner('Otwieranie treści utworu');
- if (!self.root)
- error && error('info:withLocalHtml: no local html: no usable filesystem');
- var url = "file://" + self.root.fullPath + "/html/" + book_id;
+ var url = "html/" + book_id + '.html';
console.log('info:withLocalHtml: local ajax: ' + url);
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
};
- this.withLocal = function(win, fail) {
- if (self.root) {
- self.root.getDirectory('html', {}, function(dir) {
- var reader = dir.createReader();
- reader.readEntries(win, fail);
- });
- }
- else {
- win && win([]);
- }
- }
-
-
- // downloads HTML file from server, saves it in cache and calls success with file contents
- this.withHtmlFromServer = function(book_id, success, error) {
- console.log('info:withHtmlFromServer: id:' + book_id);
- // read file from WL
- Catalogue.withBook(book_id, function(book) {
- var url = WL + book.html_file;
- console.log('info:withHtmlFromServer: fetching url: ' + url);
-
- View.spinner("Pobieranie treści utworu z sieci");
-
- if (self.root) {
- Downloader.downloadFile(url, self.root.fullPath + "/html/", ""+book_id, true,
- function(data){
- console.log('info:withHtmlFromServer: loaded file from WL');
- self.withLocalHtml(book_id, success, error);
- }, function(data) {
- console.log('error downloading file!')
- error && error("error: " + data);
- });
- }
- else {
- // there's no big fs, so we'll just get the text from AJAX
- console.log('info:withHtmlFromServer: ajax: ' + url);
- var xhr = new XMLHttpRequest();
- xhr.open("GET", url);
- xhr.onload = function() {
- console.log('info:withHtmlFromServer: fetched by ajax: ' + url);
- success && success(xhr.responseText);
- }
- xhr.onerror = function() {
- console.log('error downloading file!')
- error && error("error: " + data);
- }
- xhr.send();
- }
- });
- };
-
// calls the callback with contents of the HTML file for a given book,
- // loaded from the server and cached locally
+ // loaded from app data
this.withHtml = function(id, success, error) {
console.log('info:withHtml: id:' + id);
- self.withLocalHtml(id, success, function() {
- self.withHtmlFromServer(id, success, error);
- });
- };
-
-
- this.clear = function() {
- FileRepo.withLocal(function(local) {
- for (i in local) {
- local[i].remove();
- }
- });
+ self.withLocalHtml(id, success);
};
-
-
- this.deleteIfExists = function(id) {
- if (self.root) {
- self.root.getFile('html/' + id, {create: false}, function(f) {
- f.remove();
- });
- }
- }
};
// disable debugging
-console.log = function(text) {};
+//console.log = function(text) {};
function onLoad() {
+++ /dev/null
-/* AUTO-GENERATED FILE. DO NOT MODIFY.
- *
- * This class was automatically generated by the
- * aapt tool from the resource data it found. It
- * should not be modified by hand.
- */
-
-package pl.org.nowoczesnapolska.wlmobi;
-
-public final class R {
- public static final class attr {
- }
- public static final class drawable {
- public static final int icon=0x7f020000;
- }
- public static final class layout {
- public static final int main=0x7f030000;
- }
- public static final class string {
- public static final int app_name=0x7f050001;
- public static final int hello=0x7f050000;
- }
- public static final class xml {
- public static final int plugins=0x7f040000;
- }
-}
--- /dev/null
+/* AUTO-GENERATED FILE. DO NOT MODIFY.
+ *
+ * This class was automatically generated by the
+ * aapt tool from the resource data it found. It
+ * should not be modified by hand.
+ */
+
+package pl.org.nowoczesnapolska.wloffline;
+
+public final class R {
+ public static final class attr {
+ }
+ public static final class drawable {
+ public static final int icon=0x7f020000;
+ }
+ public static final class layout {
+ public static final int main=0x7f030000;
+ }
+ public static final class string {
+ public static final int app_name=0x7f050001;
+ public static final int hello=0x7f050000;
+ }
+ public static final class xml {
+ public static final int plugins=0x7f040000;
+ }
+}
<plugin name="FileTransfer" value="com.phonegap.FileTransfer"/>
<plugin name="Capture" value="com.phonegap.Capture"/>
- <plugin name="Downloader" value="pl.org.nowoczesnapolska.wlmobi.Downloader" />
- <plugin name="DBPut" value="pl.org.nowoczesnapolska.wlmobi.DBPut" />
- <plugin name="MenuInterface" value="pl.org.nowoczesnapolska.wlmobi.MenuInterface" />
+ <plugin name="AssetCopy" value="pl.org.nowoczesnapolska.wloffline.AssetCopy" />
+ <plugin name="MenuInterface" value="pl.org.nowoczesnapolska.wloffline.MenuInterface" />
</plugins>
\ No newline at end of file
+++ /dev/null
-/*
- * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
- * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
- */
-
-package pl.org.nowoczesnapolska.wlmobi;
-
-import android.os.Bundle;
-import android.view.KeyEvent;
-import android.view.Menu;
-import android.view.MenuItem;
-import android.webkit.WebSettings;
-
-import com.phonegap.*;
-
-public class Catalogue extends DroidGap {
-
- String infoLabel = "Proszę czekać";
- Boolean infoEnabled = false;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
-
- super.loadUrl("file:///android_asset/www/index.html");
-
- MenuInterface.view = appView;
-
- WebSettings settings = this.appView.getSettings();
- settings.setSupportZoom(true);
- settings.setBuiltInZoomControls(true);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(Menu.NONE, 1, 1, "Początek");
- menu.add(Menu.NONE, 3, 2, "Dodaj zakładkę");
- menu.add(Menu.NONE, 2, 3, MenuInterface.infoLabel);
- menu.add(Menu.NONE, 4, 4, "Tryb nocny");
- //MenuInflater inflater = getMenuInflater();
- //inflater.inflate(R.menu.game_menu, menu);
- return super.onCreateOptionsMenu(menu);
- }
-
- @Override
- public boolean onPrepareOptionsMenu(Menu menu) {
- MenuItem mi = menu.getItem(2);
- mi.setTitle(MenuInterface.infoLabel);
- mi.setEnabled(MenuInterface.infoEnabled);
- return super.onPrepareOptionsMenu(menu);
- }
-
-
- @Override
- public boolean onKeyDown(int i,KeyEvent e){
- if (e.getKeyCode() == KeyEvent.KEYCODE_MENU) {
- return false;
- }
- return super.onKeyDown(i, e);
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case 1:
- this.appView.loadUrl("javascript:Menu.start();");
- break;
- case 2:
- this.appView.loadUrl("javascript:Menu.info();");
- break;
- case 3:
- this.appView.loadUrl("javascript:Menu.bookmark();");
- break;
- case 4:
- this.appView.loadUrl("javascript:Menu.toggleNightMode();");
- break;
- default:
- return super.onOptionsItemSelected(item);
- }
- return true;
- }
-}
\ No newline at end of file
+++ /dev/null
-/*
- * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
- * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
- */
-
-package pl.org.nowoczesnapolska.wlmobi;
-
-import org.json.JSONArray;
-import org.json.JSONException;
-
-import android.util.Log;
-import android.content.res.AssetManager;
-
-import com.phonegap.api.Plugin;
-import com.phonegap.api.PluginResult;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.io.IOException;
-import pl.org.nowoczesnapolska.wlmobi.Downloader;
-
-public class DBPut extends Plugin{
-
- @Override
- public PluginResult execute(String action, JSONArray args, String callbackId) {
- if (action.equals("put")) {
- try {
- return this.put(args.getString(0), args.getString(1), args.getString(2));
- } catch (JSONException e) {
- return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
- }
- }
- else if (action.equals("fetch")) {
- try {
- return this.fetch(args.getString(0));
- } catch (JSONException e) {
- return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
- }
- }
- else {
- return new PluginResult(PluginResult.Status.INVALID_ACTION);
- }
- }
-
- private PluginResult fetch(String url) {
- String fileName = "0000000000000001.db";
- String targetPath = "/data/data/" + this.ctx.getPackageName() + "/app_database/file__0/";
-
- Log.d("DBPut", "database path: " + targetPath + " / " + fileName);
-
- Downloader d = new Downloader();
- return d.downloadUrl(url, targetPath, fileName, "true");
- }
-
-
- private PluginResult put(String assetPath, String targetPath, String overwrite) {
- // this hard-coding is kinda creepy, should probably create the db and use getDatabasePath instead
- String absoluteTargetPath = "/data/data/" + this.ctx.getPackageName() + "/app_database/" + targetPath;
- int index = absoluteTargetPath.lastIndexOf('/');
- String targetDir = absoluteTargetPath.substring(0, index);
-
- try {
- File dir = new File(targetDir);
- if(!dir.exists()) {
- Log.d("DBPut", "directory " + targetDir + " created");
- dir.mkdirs();
- }
-
- File fout = new File(absoluteTargetPath);
-
- if(overwrite.equals("false") && fout.exists()) {
- Log.d("DBPut", "File already exists");
- return new PluginResult(PluginResult.Status.OK, "exist");
- }
-
- FileOutputStream fos = new FileOutputStream(fout);
-
- AssetManager assetManager = this.ctx.getResources().getAssets();
- InputStream is = assetManager.open(assetPath);
-
- byte[] buffer = new byte[1024];
- int len1 = 0;
-
- while ( (len1 = is.read(buffer)) > 0 ) {
- fos.write(buffer,0, len1);
- }
-
- fos.close();
-
- Log.d("DBPut", "Copied to " + absoluteTargetPath);
- } catch (IOException e) {
- Log.d("DBPut", "Error: " + e);
- return new PluginResult(PluginResult.Status.ERROR, "Error: " + e);
- }
- return new PluginResult(PluginResult.Status.OK, absoluteTargetPath);
- }
-}
\ No newline at end of file
+++ /dev/null
-package pl.org.nowoczesnapolska.wlmobi;
-
-/*
- @author Mauro Rocco http://www.toforge.com
-
- Radek Czajka: don't prepend /sdcard/
-*/
-
-import org.json.JSONArray;
-import org.json.JSONException;
-
-import android.util.Log;
-
-import com.phonegap.api.Plugin;
-import com.phonegap.api.PluginResult;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-
-public class Downloader extends Plugin{
-
- @Override
- public PluginResult execute(String action, JSONArray args, String callbackId) {
- if (action.equals("downloadFile")) {
- try {
- return this.downloadUrl(args.getString(0),args.getString(1),args.getString(2),args.getString(3));
- } catch (JSONException e) {
- return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
- }
- }
- else {
- return new PluginResult(PluginResult.Status.INVALID_ACTION);
- }
-
- }
-
- PluginResult downloadUrl(String fileUrl, String dirName, String fileName, String overwrite){
- try{
- Log.d("DownloaderPlugin", "DIRECTORY CALLED "+dirName+" created");
- File dir = new File(dirName);
- if(!dir.exists()){
- Log.d("DownloaderPlugin", "directory "+dirName+" created");
- dir.mkdirs();
- }
-
- File file = new File(dirName+fileName);
-
- if(overwrite.equals("false") && file.exists()){
- Log.d("DownloaderPlugin", "File already exist");
- return new PluginResult(PluginResult.Status.OK, "exist");
- }
-
- URL url = new URL(fileUrl);
- Log.d("DownloaderPlugin", "connecting to server for downloading " + url);
- HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
- ucon.setRequestMethod("GET");
- ucon.setDoOutput(true);
- ucon.connect();
-
- Log.d("DownloaderPlugin", "download begining");
-
- Log.d("DownloaderPlugin", "download url:" + url);
-
- InputStream is = ucon.getInputStream();
-
- byte[] buffer = new byte[1024];
-
- int len1 = 0;
-
- FileOutputStream fos = new FileOutputStream(file);
-
- while ( (len1 = is.read(buffer)) > 0 ) {
- fos.write(buffer, 0, len1);
- //new String(buffer, "ISO8859_1").getBytes("UTF-8"), 0, len1);
- }
-
- fos.close();
-
- Log.d("DownloaderPlugin", "Download complete in" + fileName);
-
- } catch (IOException e) {
-
- Log.d("DownloaderPlugin", "Error: " + e);
- return new PluginResult(PluginResult.Status.ERROR, "Error: " + e);
-
- }
-
- return new PluginResult(PluginResult.Status.OK, fileName);
-
- }
-
-}
\ No newline at end of file
+++ /dev/null
-/*
- * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
- * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
- */
-
-package pl.org.nowoczesnapolska.wlmobi;
-
-import org.json.JSONArray;
-import org.json.JSONException;
-
-import android.graphics.Color;
-import android.webkit.WebView;
-
-import com.phonegap.api.Plugin;
-import com.phonegap.api.PluginResult;
-
-public class MenuInterface extends Plugin{
-
- public static String infoLabel = "Proszę czekać...";
- public static Boolean infoEnabled = false;
- public static Boolean nightEnabled = false;
- public static WebView view;
-
- @Override
- public PluginResult execute(String action, JSONArray args, String callbackId) {
- if (action.equals("setInfoButton")) {
- try {
- return this.setInfoButton(args.getString(0), args.getString(1));
- } catch (JSONException e) {
- return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
- }
- }
- else if (action.equals("setNightMode")) {
- try {
- return this.setNightMode(args.getString(0));
- } catch (JSONException e) {
- return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
- }
- }
- else {
- return new PluginResult(PluginResult.Status.INVALID_ACTION);
- }
- }
-
- private PluginResult setInfoButton(String label, String enabled) {
- infoLabel = label;
- infoEnabled = enabled.equals("true");
-
- return new PluginResult(PluginResult.Status.OK);
- }
-
- private PluginResult setNightMode(String enabled) {
- nightEnabled = enabled.equals("true");
- if (nightEnabled) {
- view.setBackgroundColor(0x222222ff);
- }
- else {
- view.setBackgroundColor(Color.WHITE);
- }
-
- return new PluginResult(PluginResult.Status.OK);
- }
-}
--- /dev/null
+package pl.org.nowoczesnapolska.wloffline;
+
+/*
+ @author Radek Czajka
+ */
+
+import org.json.JSONArray;
+import org.json.JSONException;
+
+import android.util.Log;
+import android.content.res.AssetManager;
+
+import com.phonegap.api.Plugin;
+import com.phonegap.api.PluginResult;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+
+public class AssetCopy extends Plugin{
+
+ @Override
+ public PluginResult execute(String action, JSONArray args, String callbackId) {
+ if (action.equals("copy")) {
+ try {
+ return this.copy(args.getString(0), args.getString(1), args.getString(2));
+ } catch (JSONException e) {
+ return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
+ }
+ }
+ else {
+ return new PluginResult(PluginResult.Status.INVALID_ACTION);
+ }
+ }
+
+ private PluginResult copy(String assetPath, String targetPath, String overwrite) {
+ int index = targetPath.lastIndexOf('/');
+ String targetDir = targetPath.substring(0, index);
+
+ try {
+ File dir = new File(targetDir);
+ if(!dir.exists()) {
+ Log.d("AssetCopy", "directory " + targetDir + " created");
+ dir.mkdirs();
+ }
+
+ Log.d("AssetCopy", "1");
+ File fout = new File(targetPath);
+ Log.d("AssetCopy", "2");
+
+ if(overwrite.equals("false") && fout.exists()) {
+ Log.d("AssetCopy", "File already exists");
+ return new PluginResult(PluginResult.Status.OK, "exist");
+ }
+ Log.d("AssetCopy", "3");
+
+ FileOutputStream fos = new FileOutputStream(fout);
+ Log.d("AssetCopy", "4");
+
+ AssetManager assetManager = this.ctx.getResources().getAssets();
+ InputStream is = assetManager.open(assetPath);
+
+ byte[] buffer = new byte[1024];
+ int len1 = 0;
+
+ while ( (len1 = is.read(buffer)) > 0 ) {
+ fos.write(buffer,0, len1);
+ }
+
+ fos.close();
+
+ Log.d("AssetCopy", "Copied to " + targetPath);
+ } catch (IOException e) {
+ Log.d("AssetCopy", "Error: " + e);
+ return new PluginResult(PluginResult.Status.ERROR, "Error: " + e);
+ }
+ return new PluginResult(PluginResult.Status.OK, targetPath);
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
+ * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+ */
+
+package pl.org.nowoczesnapolska.wloffline;
+
+import android.os.Bundle;
+import android.view.KeyEvent;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.webkit.WebSettings;
+
+import com.phonegap.*;
+
+public class Catalogue extends DroidGap {
+
+ String infoLabel = "Proszę czekać";
+ Boolean infoEnabled = false;
+
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+
+ super.loadUrl("file:///android_asset/www/index.html");
+
+ MenuInterface.view = appView;
+
+ WebSettings settings = this.appView.getSettings();
+ settings.setSupportZoom(true);
+ settings.setBuiltInZoomControls(true);
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ menu.add(Menu.NONE, 1, 1, "Początek");
+ menu.add(Menu.NONE, 3, 2, "Dodaj zakładkę");
+ menu.add(Menu.NONE, 2, 3, MenuInterface.infoLabel);
+ menu.add(Menu.NONE, 4, 4, "Tryb nocny");
+ //MenuInflater inflater = getMenuInflater();
+ //inflater.inflate(R.menu.game_menu, menu);
+ return super.onCreateOptionsMenu(menu);
+ }
+
+ @Override
+ public boolean onPrepareOptionsMenu(Menu menu) {
+ MenuItem mi = menu.getItem(2);
+ mi.setTitle(MenuInterface.infoLabel);
+ mi.setEnabled(MenuInterface.infoEnabled);
+ return super.onPrepareOptionsMenu(menu);
+ }
+
+
+ @Override
+ public boolean onKeyDown(int i,KeyEvent e){
+ if (e.getKeyCode() == KeyEvent.KEYCODE_MENU) {
+ return false;
+ }
+ return super.onKeyDown(i, e);
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ switch (item.getItemId()) {
+ case 1:
+ this.appView.loadUrl("javascript:Menu.start();");
+ break;
+ case 2:
+ this.appView.loadUrl("javascript:Menu.info();");
+ break;
+ case 3:
+ this.appView.loadUrl("javascript:Menu.bookmark();");
+ break;
+ case 4:
+ this.appView.loadUrl("javascript:Menu.toggleNightMode();");
+ break;
+ default:
+ return super.onOptionsItemSelected(item);
+ }
+ return true;
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
+ * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+ */
+
+package pl.org.nowoczesnapolska.wloffline;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+
+import android.graphics.Color;
+import android.webkit.WebView;
+
+import com.phonegap.api.Plugin;
+import com.phonegap.api.PluginResult;
+
+public class MenuInterface extends Plugin{
+
+ public static String infoLabel = "Proszę czekać...";
+ public static Boolean infoEnabled = false;
+ public static Boolean nightEnabled = false;
+ public static WebView view;
+
+ @Override
+ public PluginResult execute(String action, JSONArray args, String callbackId) {
+ if (action.equals("setInfoButton")) {
+ try {
+ return this.setInfoButton(args.getString(0), args.getString(1));
+ } catch (JSONException e) {
+ return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
+ }
+ }
+ else if (action.equals("setNightMode")) {
+ try {
+ return this.setNightMode(args.getString(0));
+ } catch (JSONException e) {
+ return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
+ }
+ }
+ else {
+ return new PluginResult(PluginResult.Status.INVALID_ACTION);
+ }
+ }
+
+ private PluginResult setInfoButton(String label, String enabled) {
+ infoLabel = label;
+ infoEnabled = enabled.equals("true");
+
+ return new PluginResult(PluginResult.Status.OK);
+ }
+
+ private PluginResult setNightMode(String enabled) {
+ nightEnabled = enabled.equals("true");
+ if (nightEnabled) {
+ view.setBackgroundColor(0x222222ff);
+ }
+ else {
+ view.setBackgroundColor(Color.WHITE);
+ }
+
+ return new PluginResult(PluginResult.Status.OK);
+ }
+}