Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
not working but it's a good start #89
Browse files Browse the repository at this point in the history
  • Loading branch information
indywidualny committed Feb 2, 2016
1 parent fb9c73a commit 8bb3092
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 7 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Expand All @@ -31,6 +31,5 @@ dependencies {
compile 'net.grandcentrix.tray:tray:0.9.2'
compile 'org.jsoup:jsoup:1.8.3'
compile 'ch.acra:acra:4.7.0'
compile 'org.piwik.sdk:piwik-sdk:0.0.4'
compile project(':saxrssreader')
}
Binary file added app/libs/piwik.jar
Binary file not shown.
5 changes: 3 additions & 2 deletions app/src/main/java/org/indywidualni/fblite/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void onCreate() {
/**
* Piwik dry run. Uncomment these lines during app development.
*/
//Piwik.getInstance(this).setDryRun(true);
//Piwik.getInstance(this).setDebug(true);
Piwik.getInstance(this).setDryRun(true);
Piwik.getInstance(this).setDebug(true);

/**
* Count app downloads. Fired only after new installation or upgrade.
Expand Down Expand Up @@ -75,6 +75,7 @@ public synchronized Tracker getTracker() {
try {
mPiwikTracker = Piwik.getInstance(this).newTracker("http://indywidualni.org/analytics/piwik.php", 1);
mPiwikTracker.setUserId(Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID));
mPiwikTracker.setDispatchTimeout(30000);
mPiwikTracker.setDispatchInterval(-1);
} catch (MalformedURLException e) {
Log.w("Piwik", "url is malformed", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.database.SQLException;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
Expand All @@ -35,6 +36,7 @@
import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
Expand All @@ -54,6 +56,7 @@
import org.indywidualni.fblite.util.Dimension;
import org.indywidualni.fblite.util.DownloadManagerResolver;
import org.indywidualni.fblite.util.Miscellany;
import org.indywidualni.fblite.util.Offline;
import org.indywidualni.fblite.webview.MyWebViewClient;

import java.io.File;
Expand Down Expand Up @@ -215,6 +218,17 @@ else if (getResources().getConfiguration().orientation == Configuration.ORIENTAT
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);

// since API 18 cache quota is managed automatically
if (Build.VERSION.SDK_INT < 18) {
//noinspection deprecation
webView.getSettings().setAppCacheMaxSize(5 * 1024 * 1024); // 5 MB
}

// enable caching
webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // load online by default

// get user agent
USER_AGENT_DEFAULT = webView.getSettings().getUserAgentString();
trayPreferences.put("webview_user_agent", USER_AGENT_DEFAULT);
Expand Down Expand Up @@ -281,14 +295,15 @@ else if (getResources().getConfiguration().orientation == Configuration.ORIENTAT
} catch (Exception ignored) {}

// notify when there is no internet connection
if (!Connectivity.isConnected(getApplicationContext()))
if (!Connectivity.isConnected(this))
Toast.makeText(getApplicationContext(), getString(R.string.no_network), Toast.LENGTH_SHORT).show();

// set webview clients
mWebChromeClient = new MyWebChromeClient();
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(mWebChromeClient);

// load url in webView
// load url in a webView
webView.loadUrl(webViewUrl);

// OnLongClickListener for detecting long clicks on links and images
Expand Down
71 changes: 71 additions & 0 deletions app/src/main/java/org/indywidualni/fblite/util/Offline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.indywidualni.fblite.util;

import android.content.Context;
import android.database.SQLException;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import net.grandcentrix.tray.TrayAppPreferences;

import org.indywidualni.fblite.MyApplication;
import org.indywidualni.fblite.util.database.OfflineDataSource;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Offline {

private static String userAgent;
private static Context context;
private OfflineDataSource dataSource;

public Offline() {
context = MyApplication.getContextOfApplication();
TrayAppPreferences trayPreferences = new TrayAppPreferences(context);
userAgent = trayPreferences.getString("webview_user_agent", System.getProperty("http.agent"));
dataSource = OfflineDataSource.getInstance();
syncCookies();
}

public OfflineDataSource getDataSource() {
return dataSource;
}

public void savePage(String url) throws SQLException {
new SaveTask().execute(url);
}

private class SaveTask extends AsyncTask<String, Void, Void> {

@Override
protected Void doInBackground(String... args) throws SQLException {
try {
final Connection.Response response = Jsoup.connect(args[0]).userAgent(userAgent)
.cookie("https://m.facebook.com", CookieManager.getInstance().getCookie("https://m.facebook.com")).execute();
final Document doc = response.parse();
dataSource.insertPage(args[0], doc.outerHtml());
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Problem saving the current page");
e.printStackTrace();
}
return null;
}

}

/** CookieSyncManager was deprecated in API level 21.
* We need it for API level lower than 21 though.
* In API level >= 21 it's done automatically.
*/
@SuppressWarnings("deprecation")
private void syncCookies() {
if (Build.VERSION.SDK_INT < 21) {
CookieSyncManager.createInstance(context);
CookieSyncManager.getInstance().sync();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.indywidualni.fblite.util.database;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import org.indywidualni.fblite.MyApplication;

public class MySQLiteHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "offline.db";
private static final int DATABASE_VERSION = 1;

public MySQLiteHelper() {
super(MyApplication.getContextOfApplication(), DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {
Log.v("SQLiteDatabase", "Creating database");
database.execSQL("CREATE TABLE Pages (" +
"url TEXT NOT NULL PRIMARY KEY, " +
"html TEXT" +
");");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS Pages;");
onCreate(db);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.indywidualni.fblite.util.database;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class OfflineDataSource {

private static volatile OfflineDataSource instance;
private static final int MAX_PAGES = 50;

private SQLiteDatabase database;
private MySQLiteHelper dbHelper;

private OfflineDataSource() {}

public static OfflineDataSource getInstance() {
if (instance == null) {
synchronized (OfflineDataSource.class) {
if (instance == null)
instance = new OfflineDataSource();
}
}
return instance;
}

private void open() throws SQLException {
if (dbHelper == null) {
dbHelper = new MySQLiteHelper();
database = dbHelper.getWritableDatabase();
}
}

private void close() {
if (database != null) {
dbHelper.close();
dbHelper = null;
database = null;
}
}

// todo: trim the database during every app start when offline mode is enabled to store MAX_PAGES values
// todo: or better trim it every onStop() to avoid (relatively) huge storage consumption

public synchronized void insertPage(String url, String html) throws SQLException {
open();
database.rawQuery("INSERT or REPLACE INTO Pages (url, html) " +
"values (?, ?);", new String[] { url, html });
close();
}

public synchronized String getPage(String url) throws SQLException {
open();
Cursor cursor = null;
// TODO: make it a resource string
String html = "<center><h1>This page was not found in offline database</h1></center>";

try {
cursor = database.rawQuery("SELECT html FROM Pages WHERE url=?", new String[] { url });
if(cursor.getCount() > 0) {
cursor.moveToFirst();
html = cursor.getString(cursor.getColumnIndex("html"));
}
} finally {
if (cursor != null)
cursor.close();
}

close();
return html;
}

}
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@
<string name="file_logger_needs_permission">No access to external storage. Log cannot be saved</string>
<string name="save_img">Save image</string>
<string name="downloading_img">Downloading image&#8230;</string>

<string name="download_manager_disabled">DownloadManager is disabled. Please enable it.</string>
<string name="cannot_access_storage">No access to external storage</string>
<string name="file_cannot_be_saved">A file cannot be saved</string>
<string name="camera_exception">Problems with a camera</string>

<string name="offline_mode">Offline mode</string>
<string name="offline_mode_description">When there is no Internet connection try to load a page from cache and a database.</string>

</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/xml-v21/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
android:summary="@string/load_extra_description"
android:defaultValue="false"/>

<SwitchPreference
android:key="offline_mode"
android:title="@string/offline_mode"
android:summary="@string/offline_mode_description"
android:defaultValue="false"/>

</PreferenceCategory>


Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
android:summary="@string/load_extra_description"
android:defaultValue="false"/>

<CheckBoxPreference
android:key="offline_mode"
android:title="@string/offline_mode"
android:summary="@string/offline_mode_description"
android:defaultValue="false"/>

</PreferenceCategory>


Expand Down

0 comments on commit 8bb3092

Please sign in to comment.