This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
not working but it's a good start #89
- Loading branch information
indywidualny
committed
Feb 2, 2016
1 parent
fb9c73a
commit 8bb3092
Showing
10 changed files
with
216 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/main/java/org/indywidualni/fblite/util/Offline.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/org/indywidualni/fblite/util/database/MySQLiteHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
app/src/main/java/org/indywidualni/fblite/util/database/OfflineDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters