1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 02:09:35 +02:00

config speed optimization

This commit is contained in:
Nikolay Pultsin 2014-02-08 14:34:44 +01:00
parent 868bfa8185
commit 0689ac720a
8 changed files with 114 additions and 30 deletions

View file

@ -27,6 +27,8 @@ import android.os.Bundle;
import android.widget.*; import android.widget.*;
import android.view.*; import android.view.*;
import org.geometerplus.zlibrary.core.options.Config;
import org.geometerplus.zlibrary.ui.android.R; import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.fbreader.book.SerializerUtil; import org.geometerplus.fbreader.book.SerializerUtil;
@ -49,11 +51,15 @@ public class CancelActivity extends ListActivity {
myCollection = new BookCollectionShadow(); myCollection = new BookCollectionShadow();
myCollection.bindToService(this, new Runnable() { myCollection.bindToService(this, new Runnable() {
public void run() { public void run() {
final ActionListAdapter adapter = new ActionListAdapter( Config.Instance().runOnStart(new Runnable() {
new CancelMenuHelper().getActionsList(myCollection) public void run() {
); final ActionListAdapter adapter = new ActionListAdapter(
setListAdapter(adapter); new CancelMenuHelper().getActionsList(myCollection)
getListView().setOnItemClickListener(adapter); );
setListAdapter(adapter);
getListView().setOnItemClickListener(adapter);
}
});
} }
}); });
} }

View file

@ -182,9 +182,20 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
@Override @Override
protected void onCreate(Bundle icicle) { protected void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this)); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));
final Config config = Config.Instance();
config.runOnStart(new Runnable() {
public void run() {
config.requestAllValuesForGroup("Options");
config.requestAllValuesForGroup("Style");
config.requestAllValuesForGroup("LookNFeel");
config.requestAllValuesForGroup("Fonts");
config.requestAllValuesForGroup("Colors");
config.requestAllValuesForGroup("Files");
}
});
final ZLAndroidLibrary zlibrary = getZLibrary(); final ZLAndroidLibrary zlibrary = getZLibrary();
myShowStatusBarFlag = zlibrary.ShowStatusBarOption.getValue(); myShowStatusBarFlag = zlibrary.ShowStatusBarOption.getValue();

View file

@ -29,4 +29,6 @@ interface ConfigInterface {
void setValue(in String group, in String name, in String value); void setValue(in String group, in String name, in String value);
void unsetValue(in String group, in String name); void unsetValue(in String group, in String name);
void removeGroup(in String name); void removeGroup(in String name);
List<String> requestAllValuesForGroup(in String group);
} }

View file

@ -104,31 +104,21 @@ public final class ConfigShadow extends Config implements ServiceConnection {
} }
public boolean getSpecialBooleanValue(String name, boolean defaultValue) { public boolean getSpecialBooleanValue(String name, boolean defaultValue) {
//return myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE) return myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE)
// .getBoolean(name, defaultValue);
final boolean value = myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE)
.getBoolean(name, defaultValue); .getBoolean(name, defaultValue);
System.err.println("SPECIAL GET: (" + name + "," + defaultValue + ") = " + value);
return value;
} }
public void setSpecialBooleanValue(String name, boolean value) { public void setSpecialBooleanValue(String name, boolean value) {
System.err.println("SPECIAL PUT: " + name + " => " + value);
myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE).edit() myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE).edit()
.putBoolean(name, value).commit(); .putBoolean(name, value).commit();
} }
public String getSpecialStringValue(String name, String defaultValue) { public String getSpecialStringValue(String name, String defaultValue) {
//return myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE) return myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE)
// .getString(name, defaultValue);
final String value = myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE)
.getString(name, defaultValue); .getString(name, defaultValue);
System.err.println("SPECIAL GET: (" + name + "," + defaultValue + ") = " + value);
return value;
} }
public void setSpecialStringValue(String name, String value) { public void setSpecialStringValue(String name, String value) {
System.err.println("SPECIAL PUT: " + name + " => " + value);
myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE).edit() myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE).edit()
.putString(name, value).commit(); .putString(name, value).commit();
} }
@ -165,6 +155,25 @@ public final class ConfigShadow extends Config implements ServiceConnection {
} }
} }
@Override
protected Map<String,String> requestAllValuesForGroupInternal(String group) throws NotAvailableException {
if (myInterface == null) {
throw new NotAvailableException("Config is not initialized for " + group);
}
try {
final Map<String,String> values = new HashMap<String,String>();
for (String pair : myInterface.requestAllValuesForGroup(group)) {
final String[] split = pair.split("\000");
if (split.length == 2) {
values.put(split[0], split[1]);
}
}
return values;
} catch (RemoteException e) {
throw new NotAvailableException("RemoteException for " + group);
}
}
// method from ServiceConnection interface // method from ServiceConnection interface
public synchronized void onServiceConnected(ComponentName name, IBinder service) { public synchronized void onServiceConnected(ComponentName name, IBinder service) {
myInterface = ConfigInterface.Stub.asInterface(service); myInterface = ConfigInterface.Stub.asInterface(service);

View file

@ -19,8 +19,7 @@
package org.geometerplus.android.fbreader.config; package org.geometerplus.android.fbreader.config;
import java.util.List; import java.util.*;
import java.util.LinkedList;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
@ -110,6 +109,24 @@ final class SQLiteConfig extends ConfigInterface.Stub {
} }
} }
@Override
synchronized public List<String> requestAllValuesForGroup(String group) {
try {
final List<String> pairs = new LinkedList<String>();
final Cursor cursor = myDatabase.rawQuery(
"SELECT name,value FROM config WHERE groupName = ?",
new String[] { group }
);
while (cursor.moveToNext()) {
pairs.add(cursor.getString(0) + "\000" + cursor.getString(1));
}
cursor.close();
return pairs;
} catch (SQLException e) {
return Collections.emptyList();
}
}
@Override @Override
synchronized public String getValue(String group, String name) { synchronized public String getValue(String group, String name) {
myGetValueStatement.bindString(1, group); myGetValueStatement.bindString(1, group);

View file

@ -55,6 +55,14 @@ public class PreferenceActivity extends ZLPreferenceActivity {
@Override @Override
protected void init(Intent intent) { protected void init(Intent intent) {
final Config config = Config.Instance();
config.requestAllValuesForGroup("Style");
config.requestAllValuesForGroup("Options");
config.requestAllValuesForGroup("LookNFeel");
config.requestAllValuesForGroup("Fonts");
config.requestAllValuesForGroup("Files");
config.requestAllValuesForGroup("Scrolling");
config.requestAllValuesForGroup("Colors");
setResult(FBReader.RESULT_REPAINT); setResult(FBReader.RESULT_REPAINT);
final ViewOptions viewOptions = new ViewOptions(); final ViewOptions viewOptions = new ViewOptions();

View file

@ -21,20 +21,27 @@ package org.geometerplus.fbreader.fbreader.options;
import java.util.*; import java.util.*;
import org.geometerplus.zlibrary.core.options.Config;
import org.geometerplus.zlibrary.core.options.ZLBooleanOption; import org.geometerplus.zlibrary.core.options.ZLBooleanOption;
import org.geometerplus.zlibrary.core.resources.ZLResource; import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.fbreader.book.*; import org.geometerplus.fbreader.book.*;
public class CancelMenuHelper { public class CancelMenuHelper {
private final static String GROUP_NAME = "CancelMenu";
public final ZLBooleanOption ShowLibraryItemOption = public final ZLBooleanOption ShowLibraryItemOption =
new ZLBooleanOption("CancelMenu", "library", true); new ZLBooleanOption(GROUP_NAME, "library", true);
public final ZLBooleanOption ShowNetworkLibraryItemOption = public final ZLBooleanOption ShowNetworkLibraryItemOption =
new ZLBooleanOption("CancelMenu", "networkLibrary", true); new ZLBooleanOption(GROUP_NAME, "networkLibrary", true);
public final ZLBooleanOption ShowPreviousBookItemOption = public final ZLBooleanOption ShowPreviousBookItemOption =
new ZLBooleanOption("CancelMenu", "previousBook", false); new ZLBooleanOption(GROUP_NAME, "previousBook", false);
public final ZLBooleanOption ShowPositionItemsOption = public final ZLBooleanOption ShowPositionItemsOption =
new ZLBooleanOption("CancelMenu", "positions", true); new ZLBooleanOption(GROUP_NAME, "positions", true);
public CancelMenuHelper() {
Config.Instance().requestAllValuesForGroup(GROUP_NAME);
}
public static enum ActionType { public static enum ActionType {
library, library,

View file

@ -41,6 +41,7 @@ public abstract class Config {
private final String myNullString = new String("__NULL__"); private final String myNullString = new String("__NULL__");
private final Map<StringPair,String> myCache = private final Map<StringPair,String> myCache =
Collections.synchronizedMap(new HashMap<StringPair,String>()); Collections.synchronizedMap(new HashMap<StringPair,String>());
private final Set<String> myCachedGroups = new HashSet<String>();
// deprecated, used in API server implementation // deprecated, used in API server implementation
public final String getValue(String name, String id, String defaultValue) { public final String getValue(String name, String id, String defaultValue) {
@ -50,13 +51,17 @@ public abstract class Config {
public final String getValue(StringPair id, String defaultValue) { public final String getValue(StringPair id, String defaultValue) {
String value = myCache.get(id); String value = myCache.get(id);
if (value == null) { if (value == null) {
try { if (myCachedGroups.contains(id.Group)) {
value = getValueInternal(id.Group, id.Name);
} catch (NotAvailableException e) {
return defaultValue;
}
if (value == null) {
value = myNullString; value = myNullString;
} else {
try {
value = getValueInternal(id.Group, id.Name);
} catch (NotAvailableException e) {
return defaultValue;
}
if (value == null) {
value = myNullString;
}
} }
myCache.put(id, value); myCache.put(id, value);
} }
@ -72,6 +77,24 @@ public abstract class Config {
setValueInternal(id.Group, id.Name, value); setValueInternal(id.Group, id.Name, value);
} }
public final void requestAllValuesForGroup(String group) {
synchronized (myCachedGroups) {
if (myCachedGroups.contains(group)) {
return;
}
final Map<String,String> values;
try {
values = requestAllValuesForGroupInternal(group);
} catch (NotAvailableException e) {
return;
}
for (Map.Entry<String,String> entry : values.entrySet()) {
setToCache(group, entry.getKey(), entry.getValue());
}
myCachedGroups.add(group);
}
}
public final void unsetValue(StringPair id) { public final void unsetValue(StringPair id) {
myCache.put(id, myNullString); myCache.put(id, myNullString);
unsetValueInternal(id.Group, id.Name); unsetValueInternal(id.Group, id.Name);
@ -96,4 +119,5 @@ public abstract class Config {
protected abstract String getValueInternal(String group, String name) throws NotAvailableException; protected abstract String getValueInternal(String group, String name) throws NotAvailableException;
protected abstract void setValueInternal(String group, String name, String value); protected abstract void setValueInternal(String group, String name, String value);
protected abstract void unsetValueInternal(String group, String name); protected abstract void unsetValueInternal(String group, String name);
protected abstract Map<String,String> requestAllValuesForGroupInternal(String group) throws NotAvailableException;
} }