diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4834797..49360e5 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -24,9 +24,15 @@ + + + + + @@ -58,18 +64,39 @@ - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1034,11 +1061,15 @@ + - - - + + + @@ -1093,7 +1124,153 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1301,7 +1478,11 @@ 1428859610821 1428859610821 - @@ -1325,8 +1506,8 @@ - - + + @@ -1341,14 +1522,14 @@ - + - + @@ -1361,16 +1542,16 @@ - + - - - + + + @@ -1389,8 +1570,8 @@ - + @@ -1418,55 +1599,25 @@ - - - - - - - - - - - - - - - - - - - - - - + + + - - - - - - - - - - - - - + @@ -1506,29 +1657,20 @@ + + + - - - - - - - - - - - - - + @@ -1568,29 +1710,20 @@ + + + - - - - - - - - - - - - - + @@ -1630,29 +1763,20 @@ + + + - - - - - - - - - - - - - + @@ -1671,14 +1795,14 @@ + + + - - - @@ -1768,14 +1892,14 @@ + + + - - - @@ -1820,25 +1944,17 @@ - - + + + - + - - - - - - - - - - - - + + + @@ -1850,6 +1966,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/jonas/tool/saveForOffline/DirectoryHelper.java b/app/src/main/java/jonas/tool/saveForOffline/DirectoryHelper.java index 626f421..99d732c 100644 --- a/app/src/main/java/jonas/tool/saveForOffline/DirectoryHelper.java +++ b/app/src/main/java/jonas/tool/saveForOffline/DirectoryHelper.java @@ -1,10 +1,13 @@ package jonas.tool.saveForOffline; -import java.io.*; -import android.content.*; -import android.util.*; -import java.text.*; -import java.util.*; -import android.os.*; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Environment; +import android.preference.PreferenceManager; + +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; public class DirectoryHelper { @@ -25,23 +28,19 @@ private static String getStorageDir() { file.mkdirs(); return directory; } - - public static String getThumbnailLocation () { - - String thumbnailLocation = getStorageDir() + createUniqueFilename() + ".png"; - return thumbnailLocation; - } - - public static String getFileLocation () { - String fileLocation = getStorageDir() + createUniqueFilename() + ".mht"; - return fileLocation; - } - public static String getUnpackedDir () { + public static String getDestinationDirectory (Context context) { + + String defaultFileLocation = getStorageDir() + createUniqueFilename() + File.separatorChar; + + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); + + if (sharedPref.getBoolean("is_custom_storage_dir", false)) { + return sharedPref.getString("custom_storage_dir" + createUniqueFilename() + File.separatorChar, defaultFileLocation); + } - String fileLocation = getStorageDir() + createUniqueFilename() + File.separatorChar; - return fileLocation; + return defaultFileLocation; } public static void deleteDirectory(File directory) { diff --git a/app/src/main/java/jonas/tool/saveForOffline/PageSaver.java b/app/src/main/java/jonas/tool/saveForOffline/PageSaver.java new file mode 100644 index 0000000..d5ba8d0 --- /dev/null +++ b/app/src/main/java/jonas/tool/saveForOffline/PageSaver.java @@ -0,0 +1,16 @@ +package jonas.tool.saveForOffline; + +/** + * Created by jonas on 03/05/15. + */ +public class PageSaver { + public PageSaver (ProgressCallback callback) { + + } +} + +interface ProgressCallback { + + public void onProgressChanged (int progress); + +} \ No newline at end of file diff --git a/app/src/main/java/jonas/tool/saveForOffline/SaveActivity.java b/app/src/main/java/jonas/tool/saveForOffline/SaveActivity.java index be6aafb..237665b 100644 --- a/app/src/main/java/jonas/tool/saveForOffline/SaveActivity.java +++ b/app/src/main/java/jonas/tool/saveForOffline/SaveActivity.java @@ -1,16 +1,28 @@ package jonas.tool.saveForOffline; -import android.app.*; -import android.os.*; -import android.webkit.*; -import android.content.*; -import android.graphics.*; -import java.io.*; -import android.widget.*; -import android.view.*; -import android.database.sqlite.*; -import android.transition.*; -import android.preference.*; -import android.util.*; +import android.app.Activity; +import android.content.ContentValues; +import android.content.Intent; +import android.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Picture; +import android.os.AsyncTask; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.view.View; +import android.webkit.WebChromeClient; +import android.webkit.WebView; +import android.webkit.WebViewClient; +import android.widget.Button; +import android.widget.TextView; +import android.widget.Toast; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.util.concurrent.TimeUnit; public class SaveActivity extends Activity @@ -25,9 +37,8 @@ public class SaveActivity extends Activity private boolean hasErrorOccurred = false; private String origurl; - private DirectoryHelper dirHelper= new DirectoryHelper(); - private String filelocation = dirHelper.getFileLocation(); - private String thumbnail = dirHelper.getThumbnailLocation(); + private String filelocation = null; + private String thumbnail = null; private boolean getHasErrorOccurred() { diff --git a/app/src/main/java/jonas/tool/saveForOffline/SaveService.java b/app/src/main/java/jonas/tool/saveForOffline/SaveService.java index caba0b8..ce45bd2 100644 --- a/app/src/main/java/jonas/tool/saveForOffline/SaveService.java +++ b/app/src/main/java/jonas/tool/saveForOffline/SaveService.java @@ -69,13 +69,14 @@ This was originally based on getMeThatPage (https://github.com/PramodKhare/GetMe import java.util.regex.Matcher; import java.util.regex.Pattern; +//this is a mess... + public class SaveService extends Service { private String destinationDirectory; private String thumbnail; private String origurl; - private String uaString; private boolean wasAddedToDb = false; private int failCount = 0; @@ -93,6 +94,28 @@ public class SaveService extends Service { private Message msg; private boolean shouldGoToMainListOnNotificationClick = false; + private String getUserAgent () { + + //fixme, save user agent String somewhere else + + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(SaveService.this); + String ua = sharedPref.getString("user_agent", "mobile"); + + String uaString; + + if (ua.equals("desktop")) { + uaString = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36"; + + } else if (ua.equals("ipad")) { + uaString = "iPad ipad safari"; + + } else { + uaString = "Mozilla/5.0 (Linux; U; Android 4.2.2; en-us; Phone Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"; + } + + return uaString; + } + @Override public void onCreate() { @@ -175,7 +198,6 @@ public void handleMessage(final Message msg) { mNotificationManager.notify(notification_id, mBuilder.build()); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(SaveService.this); - String ua = sharedPref.getString("user_agent", "mobile"); shouldGoToMainListOnNotificationClick = sharedPref.getBoolean("go_to_main_list_on_click", false); @@ -190,21 +212,15 @@ public void handleMessage(final Message msg) { GrabUtility.saveScripts = sharedPref.getBoolean("save_scripts", true); GrabUtility.saveVideo = sharedPref.getBoolean("save_video", true); - if (ua.equals("desktop")) { - uaString = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36"; + //in the beginning, only God and I knew what I was doing. + //now, only God knows. - } else if (ua.equals("ipad")) { - uaString = "iPad ipad safari"; + destinationDirectory = DirectoryHelper.getDestinationDirectory(SaveService.this); - } else { - uaString = "Mozilla/5.0 (Linux; U; Android 4.2.2; en-us; Phone Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"; - } - - - destinationDirectory = DirectoryHelper.getUnpackedDir(); thumbnail = destinationDirectory + "saveForOffline_thumbnail.png"; origurl = intent.getStringExtra("origurl"); + boolean success = grabPage(origurl, destinationDirectory); if (userHasCancelled) { //user cancelled, remove the notification, and delete files. @@ -217,6 +233,13 @@ public void handleMessage(final Message msg) { DirectoryHelper.deleteDirectory(file); return; } + + if (sharedPref.getBoolean("is_custom_storage_dir", false)) { + + File oldFile = new File(destinationDirectory); + oldFile.renameTo(new File(getNewDirectoryName())); + } + notifyProgress("Adding to list...", 100, 97, false); addToDb(); @@ -233,6 +256,14 @@ public void handleMessage(final Message msg) { } + private String getNewDirectoryName () { + String title = GrabUtility.title.replaceAll("[^a-zA-Z0-9-_\\.]", ""); + title = title.substring(0, Math.min(title.length(), 50)); + + File oldFile = new File(destinationDirectory); + return oldFile.getParentFile().getAbsolutePath() + title + File.separatorChar; + } + private String getLastIdFromDb() { DbHelper mHelper = new DbHelper(SaveService.this); SQLiteDatabase dataBase = mHelper.getWritableDatabase(); @@ -253,8 +284,12 @@ private void addToDb() { SQLiteDatabase dataBase = mHelper.getWritableDatabase(); ContentValues values = new ContentValues(); - - values.put(DbHelper.KEY_FILE_LOCATION, destinationDirectory + "index.html"); + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(SaveService.this); + if (sharedPref.getBoolean("is_custom_storage_dir", false)) { + values.put(DbHelper.KEY_FILE_LOCATION, getNewDirectoryName() + "index.html"); + } else { + values.put(DbHelper.KEY_FILE_LOCATION, destinationDirectory + "index.html"); + } values.put(DbHelper.KEY_TITLE, GrabUtility.title); values.put(DbHelper.KEY_THUMBNAIL, thumbnail); diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 0b76c41..2d727b7 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -16,21 +16,17 @@ - + android:dialogTitle="Enter directory path (eg. /sdcard/saved-pages/ )"/>