Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added res/drawable/delete.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable/edit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 46 additions & 5 deletions res/layout/script_list_item.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:textSize="16sp" >
android:layout_height="wrap_content"
android:orientation="horizontal" >

</TextView>
<CheckBox
android:id="@+id/sli_active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">

<TextView
android:id="@+id/sli_name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/sli_description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageButton
android:id="@+id/sli_edit"
android:layout_width="48dip"
android:layout_height="48dip"
android:src="@drawable/edit" />
<ImageButton
android:id="@+id/sli_delete"
android:layout_width="48dip"
android:layout_height="48dip"
android:src="@drawable/delete" />
</LinearLayout>


</LinearLayout>

</LinearLayout>
88 changes: 61 additions & 27 deletions src/at/pardus/android/webview/gm/WebViewGmImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.view.MenuItem;
import android.view.Window;
import at.pardus.android.webview.gm.model.ScriptId;
import at.pardus.android.webview.gm.store.ScriptStore;
import at.pardus.android.webview.gm.store.ScriptStoreSQLite;
import at.pardus.android.webview.gm.store.ui.ScriptBrowser;
import at.pardus.android.webview.gm.store.ui.ScriptEditor;
Expand All @@ -42,52 +43,84 @@ public class WebViewGmImpl extends ScriptManagerActivity {
private Stack<Integer> placeHistory = new Stack<Integer>();

private SharedPreferences preferences;

private boolean startBrowser;

private static final Integer LIST = 1;

private static final Integer BROWSER = 2;

public WebViewGmImpl() {
startBrowser=true;
}

public WebViewGmImpl(boolean startBrowser) {
this.startBrowser=startBrowser;
}

@Override
public void openScriptList() {
if (scriptList == null) {
if (scriptStore == null) {
scriptStore = new ScriptStoreSQLite(this);
scriptStore.open();
}
scriptList = new ScriptList(this, scriptStore);
}
getScriptList();
setTitle(R.string.webviewgm_impl_app_name);
setContentView(scriptList.getScriptList());
setContentView(getScriptList ().getScriptList());
placeHistory.push(LIST);
}

protected final ScriptList getScriptList() {
if (scriptList == null)
scriptList = createScriptList();
return scriptList;
}

protected ScriptList createScriptList() {
return new ScriptList(this, getScriptStore());
}

@Override
public void openScriptEditor(ScriptId scriptId) {
if (scriptEditor == null) {
if (scriptStore == null) {
scriptStore = new ScriptStoreSQLite(this);
scriptStore.open();
}
scriptEditor = new ScriptEditor(this, scriptStore);
}
setContentView(scriptEditor.getEditForm(scriptId));
setContentView(getScriptEditor().getEditForm(scriptId));
placeHistory.push(null);
}

protected final ScriptEditor getScriptEditor() {
if (scriptEditor == null)
scriptEditor = createScriptEditor ();
return scriptEditor;
}

protected ScriptEditor createScriptEditor() {
return new ScriptEditor(this, getScriptStore());
}

@Override
public void openScriptBrowser() {
if (scriptBrowser == null) {
if (scriptStore == null) {
scriptStore = new ScriptStoreSQLite(this);
scriptStore.open();
}
scriptBrowser = new ScriptBrowser(this, scriptStore,
preferences.getString("lastUrl", "http://userscripts.org/"));
}
setContentView(scriptBrowser.getBrowser());
setContentView(getScriptBrowser().getBrowser());
placeHistory.push(BROWSER);
}


protected final ScriptBrowser getScriptBrowser() {
if (scriptBrowser == null)
scriptBrowser = createScriptBrowser();
return scriptBrowser;
}

protected ScriptBrowser createScriptBrowser() {
return new ScriptBrowser(this, getScriptStore(),
preferences.getString("lastUrl", "http://userscripts.org/"));
}

protected final ScriptStore getScriptStore() {
if (scriptStore == null) {
scriptStore = createScriptStore();
scriptStore.open();
}
return scriptStore != null ? scriptStore : createScriptStore();
}

protected ScriptStoreSQLite createScriptStore() {
return new ScriptStoreSQLite(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
Expand All @@ -113,7 +146,8 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
preferences = getSharedPreferences("P", Context.MODE_PRIVATE);
openScriptBrowser();
if(startBrowser)
openScriptBrowser();
}

@Override
Expand Down
9 changes: 9 additions & 0 deletions src/at/pardus/android/webview/gm/store/ScriptStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ public interface ScriptStore {
*/
public void delete(ScriptId id);

/**
* Tells if an user script is enabled.
*
* @param id
* the ID of the script
* @return <tt>true</tt> if enabled
*/
public boolean isEnabled(ScriptId id);

/**
* Gets all names of values stored by a user script.
*
Expand Down
55 changes: 45 additions & 10 deletions src/at/pardus/android/webview/gm/store/ScriptStoreSQLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class ScriptStoreSQLite implements ScriptStore {

@Override
public Script[] get(String url) {
Script[] scripts = cache.get(url);
ScriptData[] scripts = cache.get(url);
if (scripts == null) {
if (dbHelper == null) {
Log.w(TAG, "Cannot get user scripts (database not available)");
Expand Down Expand Up @@ -107,6 +107,19 @@ public void enable(ScriptId id) {
dbHelper.updateScriptEnabled(id, true);
initCache();
}

@Override
public boolean isEnabled(ScriptId id) {
if (dbHelper == null) {
Log.e(TAG, "Cannot get user script (database not available)");
return false;
}
ScriptData[] scripts = dbHelper.selectScripts(new ScriptId[] { id }, null);
if (scripts.length == 0) {
return false;
}
return scripts[0].enabled;
}

@Override
public void disable(ScriptId id) {
Expand Down Expand Up @@ -334,15 +347,15 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
* @return an array of matching script objects; an empty array if none
* found
*/
public Script[] selectScripts(ScriptId[] ids, Boolean enabled) {
public ScriptData[] selectScripts(ScriptId[] ids, Boolean enabled) {
String selectionStr = null, selectionIdStr = null;
String[] selectionArgsArr = null, selectionIdArgsArr = null;
if (ids != null || enabled != null) {
StringBuilder selection = new StringBuilder();
List<String> selectionArgs = new ArrayList<String>();
if (ids != null) {
if (ids.length == 0) {
return new Script[0];
return new ScriptData[0];
}
makeScriptIdSelectionArgs(ids, selection, selectionArgs);
selectionIdStr = selection.toString();
Expand All @@ -368,7 +381,7 @@ public Script[] selectScripts(ScriptId[] ids, Boolean enabled) {
selectionIdStr, selectionIdArgsArr);
Cursor cursor = db.query(TBL_SCRIPT, COLS_SCRIPT, selectionStr,
selectionArgsArr, null, null, null);
Script[] scriptsArr = new Script[cursor.getCount()];
ScriptData[] scriptsArr = new ScriptData[cursor.getCount()];
int i = 0;
while (cursor.moveToNext()) {
String name = cursor.getString(0);
Expand All @@ -392,11 +405,12 @@ public Script[] selectScripts(ScriptId[] ids, Boolean enabled) {
int unwrap = cursor.getInt(8);
String version = cursor.getString(9);
String content = cursor.getString(10);
boolean isEnabled = cursor.getInt(11) != 0;
// TODO add require and resource data
scriptsArr[i] = new Script(name, namespace, excludeArr,
scriptsArr[i] = new ScriptData(name, namespace, excludeArr,
includeArr, matchArr, description, downloadurl,
updateurl, installurl, icon, runat, unwrap == 1,
version, content);
version, content, isEnabled);
i++;
}
cursor.close();
Expand Down Expand Up @@ -760,6 +774,27 @@ public void deleteValue(ScriptId id, String name) {
}

}

/**
* An entry in the cache script. It contains some status information as
* well as the script itself (which is meant to be immutable).
*/
private static class ScriptData extends Script {

public boolean enabled;

public ScriptData(String name, String namespace, String[] exclude,
String[] include, String[] match, String description,
String downloadurl, String updateurl, String installurl,
String icon, String runAt, boolean unwrap, String version,
String content, boolean enabled) {
super(name, namespace, exclude, include, match, description,
downloadurl, updateurl, installurl, icon, runAt, unwrap, version,
content);
this.enabled = enabled;
}

}

/**
* Cache of user scripts matching most recently accessed URLs and all
Expand All @@ -769,14 +804,14 @@ private static class ScriptCache {

private static final int CACHE_SIZE = 62;

private LinkedHashMap<String, Script[]> urlScripts = new LinkedHashMap<String, Script[]>(
private LinkedHashMap<String, ScriptData[]> urlScripts = new LinkedHashMap<String, ScriptData[]>(
CACHE_SIZE + 2, 1.0f, true) {

private static final long serialVersionUID = 1L;

@Override
protected boolean removeEldestEntry(
Map.Entry<String, Script[]> eldest) {
Map.Entry<String, ScriptData[]> eldest) {
return size() > CACHE_SIZE;
}

Expand All @@ -792,7 +827,7 @@ protected boolean removeEldestEntry(
* @return if the URL is cached either the found user scripts or an
* empty array; if the URL is not cached then null
*/
public synchronized Script[] get(String url) {
public synchronized ScriptData[] get(String url) {
return urlScripts.get(url);
}

Expand All @@ -804,7 +839,7 @@ public synchronized Script[] get(String url) {
* @param scripts
* the user scripts to execute at that URL
*/
public synchronized void put(String url, Script[] scripts) {
public synchronized void put(String url, ScriptData[] scripts) {
urlScripts.put(url, scripts);
}

Expand Down
Loading