Skip to content

Commit

Permalink
Add support for saving to custom folder, not tested yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasCz committed May 3, 2015
1 parent b7e5064 commit fdc4be1
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 179 deletions.
382 changes: 261 additions & 121 deletions .idea/workspace.xml

Large diffs are not rendered by default.

39 changes: 19 additions & 20 deletions app/src/main/java/jonas/tool/saveForOffline/DirectoryHelper.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/jonas/tool/saveForOffline/PageSaver.java
Original file line number Diff line number Diff line change
@@ -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);

}
41 changes: 26 additions & 15 deletions app/src/main/java/jonas/tool/saveForOffline/SaveActivity.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
Expand Down
63 changes: 49 additions & 14 deletions app/src/main/java/jonas/tool/saveForOffline/SaveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {

Expand Down Expand Up @@ -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);

Expand All @@ -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.
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand Down
14 changes: 5 additions & 9 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@



<!--<ListPreference
android:key="storage_dir"
android:title="File storage location"
android:defaultValue="external"
android:summary="This option is currently not implemented. Files are stored in /sdcard/Android/data/jonas.tool.saveForOffline/"
android:entries="@array/storage_dir_list"
android:entryValues="@array/storage_dir_list_values"
android:dialogTitle="Choose" />
<CheckBoxPreference
android:key = "is_custom_storage_dir"
android:title="Use custom file storage folder"
android:text="Select this if you want to store files in a custom location" />

<EditTextPreference

android:key="custom_storage_dir"
android:title="Custom storage location"
android:summary="Enter the directory path where you want data to be saved. If it does not exist, it will be created."
android:dialogTitle="Enter directory path (eg. /sdcard/saved-pages/ )"/>-->
android:dialogTitle="Enter directory path (eg. /sdcard/saved-pages/ )"/>

</PreferenceCategory>

Expand Down

0 comments on commit fdc4be1

Please sign in to comment.