From: Radek Czajka Date: Fri, 13 Jul 2012 12:56:06 +0000 (+0200) Subject: offline version X-Git-Url: https://git.mdrn.pl/wl-mobile.git/commitdiff_plain/91d4143f8e07f469843b2265ce3120bbd64e1fde?hp=4aa83d4ec5f1b6d86b922e022df16de4fe373ea8 offline version --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 9e15f71..10b49e3 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="pl.org.nowoczesnapolska.wloffline" android:versionName="1.1.1" android:versionCode="4"> - + - diff --git a/assets/www/index.html b/assets/www/index.html index fe6e17c..3cd2634 100644 --- a/assets/www/index.html +++ b/assets/www/index.html @@ -6,8 +6,7 @@ - - + diff --git a/assets/www/js/assetcopy.js b/assets/www/js/assetcopy.js new file mode 100644 index 0000000..a9bc86f --- /dev/null +++ b/assets/www/js/assetcopy.js @@ -0,0 +1,23 @@ +/* + * 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]); + }, +}; diff --git a/assets/www/js/catalogue.js b/assets/www/js/catalogue.js index 44f8e80..eed6bd3 100644 --- a/assets/www/js/catalogue.js +++ b/assets/www/js/catalogue.js @@ -3,13 +3,7 @@ * 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', @@ -55,17 +49,6 @@ var Catalogue = new function() { 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); @@ -91,7 +74,6 @@ var Catalogue = new function() { } var done = function() { - FileRepo.clear(); window.localStorage.setItem('db_ver', DB_VER); console.log('db updated'); success && success(); @@ -107,21 +89,23 @@ var Catalogue = new function() { 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", [], @@ -283,93 +267,4 @@ var Catalogue = new function() { }; - 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"); - }); - }); - }; } diff --git a/assets/www/js/dbput.js b/assets/www/js/dbput.js deleted file mode 100644 index 457f613..0000000 --- a/assets/www/js/dbput.js +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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] - ); - } -}; - diff --git a/assets/www/js/downloader.js b/assets/www/js/downloader.js deleted file mode 100644 index 15edf30..0000000 --- a/assets/www/js/downloader.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * - * @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]); - } -}; diff --git a/assets/www/js/filerepo.js b/assets/www/js/filerepo.js index 8ff5a6a..0a30097 100644 --- a/assets/www/js/filerepo.js +++ b/assets/www/js/filerepo.js @@ -6,32 +6,16 @@ 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); @@ -44,81 +28,10 @@ var FileRepo = new function() { }; - 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(); - }); - } - } }; diff --git a/assets/www/js/main.js b/assets/www/js/main.js index f3a1eee..f2c658f 100644 --- a/assets/www/js/main.js +++ b/assets/www/js/main.js @@ -7,7 +7,7 @@ var WL = 'http://www.wolnelektury.pl'; // disable debugging -console.log = function(text) {}; +//console.log = function(text) {}; function onLoad() { diff --git a/gen/pl/org/nowoczesnapolska/wlmobi/R.java b/gen/pl/org/nowoczesnapolska/wlmobi/R.java deleted file mode 100644 index aab3d7c..0000000 --- a/gen/pl/org/nowoczesnapolska/wlmobi/R.java +++ /dev/null @@ -1,26 +0,0 @@ -/* 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; - } -} diff --git a/gen/pl/org/nowoczesnapolska/wloffline/R.java b/gen/pl/org/nowoczesnapolska/wloffline/R.java new file mode 100644 index 0000000..202ac11 --- /dev/null +++ b/gen/pl/org/nowoczesnapolska/wloffline/R.java @@ -0,0 +1,26 @@ +/* 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; + } +} diff --git a/res/xml/plugins.xml b/res/xml/plugins.xml index 2d8ba88..a626ccf 100644 --- a/res/xml/plugins.xml +++ b/res/xml/plugins.xml @@ -17,7 +17,6 @@ - - - + + \ No newline at end of file diff --git a/src/pl/org/nowoczesnapolska/wlmobi/Catalogue.java b/src/pl/org/nowoczesnapolska/wlmobi/Catalogue.java deleted file mode 100644 index 91132b3..0000000 --- a/src/pl/org/nowoczesnapolska/wlmobi/Catalogue.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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 diff --git a/src/pl/org/nowoczesnapolska/wlmobi/DBPut.java b/src/pl/org/nowoczesnapolska/wlmobi/DBPut.java deleted file mode 100644 index d339614..0000000 --- a/src/pl/org/nowoczesnapolska/wlmobi/DBPut.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * 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 diff --git a/src/pl/org/nowoczesnapolska/wlmobi/Downloader.java b/src/pl/org/nowoczesnapolska/wlmobi/Downloader.java deleted file mode 100644 index 459891b..0000000 --- a/src/pl/org/nowoczesnapolska/wlmobi/Downloader.java +++ /dev/null @@ -1,96 +0,0 @@ -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 diff --git a/src/pl/org/nowoczesnapolska/wlmobi/MenuInterface.java b/src/pl/org/nowoczesnapolska/wlmobi/MenuInterface.java deleted file mode 100644 index 064d4de..0000000 --- a/src/pl/org/nowoczesnapolska/wlmobi/MenuInterface.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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); - } -} diff --git a/src/pl/org/nowoczesnapolska/wloffline/AssetCopy.java b/src/pl/org/nowoczesnapolska/wloffline/AssetCopy.java new file mode 100644 index 0000000..07c02c9 --- /dev/null +++ b/src/pl/org/nowoczesnapolska/wloffline/AssetCopy.java @@ -0,0 +1,80 @@ +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 diff --git a/src/pl/org/nowoczesnapolska/wloffline/Catalogue.java b/src/pl/org/nowoczesnapolska/wloffline/Catalogue.java new file mode 100644 index 0000000..3b9c534 --- /dev/null +++ b/src/pl/org/nowoczesnapolska/wloffline/Catalogue.java @@ -0,0 +1,84 @@ +/* + * 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 diff --git a/src/pl/org/nowoczesnapolska/wloffline/MenuInterface.java b/src/pl/org/nowoczesnapolska/wloffline/MenuInterface.java new file mode 100644 index 0000000..43417f9 --- /dev/null +++ b/src/pl/org/nowoczesnapolska/wloffline/MenuInterface.java @@ -0,0 +1,63 @@ +/* + * 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); + } +}