1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 18:29:23 +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.view.*;
import org.geometerplus.zlibrary.core.options.Config;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.fbreader.book.SerializerUtil;
@ -48,6 +50,8 @@ public class CancelActivity extends ListActivity {
requestWindowFeature(Window.FEATURE_NO_TITLE);
myCollection = new BookCollectionShadow();
myCollection.bindToService(this, new Runnable() {
public void run() {
Config.Instance().runOnStart(new Runnable() {
public void run() {
final ActionListAdapter adapter = new ActionListAdapter(
new CancelMenuHelper().getActionsList(myCollection)
@ -57,6 +61,8 @@ public class CancelActivity extends ListActivity {
}
});
}
});
}
@Override
protected void onStop() {

View file

@ -182,9 +182,20 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
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();
myShowStatusBarFlag = zlibrary.ShowStatusBarOption.getValue();

View file

@ -29,4 +29,6 @@ interface ConfigInterface {
void setValue(in String group, in String name, in String value);
void unsetValue(in String group, 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) {
//return myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE)
// .getBoolean(name, defaultValue);
final boolean value = myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE)
return myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE)
.getBoolean(name, defaultValue);
System.err.println("SPECIAL GET: (" + name + "," + defaultValue + ") = " + value);
return value;
}
public void setSpecialBooleanValue(String name, boolean value) {
System.err.println("SPECIAL PUT: " + name + " => " + value);
myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE).edit()
.putBoolean(name, value).commit();
}
public String getSpecialStringValue(String name, String defaultValue) {
//return myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE)
// .getString(name, defaultValue);
final String value = myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE)
return myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE)
.getString(name, defaultValue);
System.err.println("SPECIAL GET: (" + name + "," + defaultValue + ") = " + value);
return value;
}
public void setSpecialStringValue(String name, String value) {
System.err.println("SPECIAL PUT: " + name + " => " + value);
myContext.getSharedPreferences("fbreader.ui", Context.MODE_PRIVATE).edit()
.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
public synchronized void onServiceConnected(ComponentName name, IBinder service) {
myInterface = ConfigInterface.Stub.asInterface(service);

View file

@ -19,8 +19,7 @@
package org.geometerplus.android.fbreader.config;
import java.util.List;
import java.util.LinkedList;
import java.util.*;
import android.app.Service;
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
synchronized public String getValue(String group, String name) {
myGetValueStatement.bindString(1, group);

View file

@ -55,6 +55,14 @@ public class PreferenceActivity extends ZLPreferenceActivity {
@Override
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);
final ViewOptions viewOptions = new ViewOptions();

View file

@ -21,20 +21,27 @@ package org.geometerplus.fbreader.fbreader.options;
import java.util.*;
import org.geometerplus.zlibrary.core.options.Config;
import org.geometerplus.zlibrary.core.options.ZLBooleanOption;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.fbreader.book.*;
public class CancelMenuHelper {
private final static String GROUP_NAME = "CancelMenu";
public final ZLBooleanOption ShowLibraryItemOption =
new ZLBooleanOption("CancelMenu", "library", true);
new ZLBooleanOption(GROUP_NAME, "library", true);
public final ZLBooleanOption ShowNetworkLibraryItemOption =
new ZLBooleanOption("CancelMenu", "networkLibrary", true);
new ZLBooleanOption(GROUP_NAME, "networkLibrary", true);
public final ZLBooleanOption ShowPreviousBookItemOption =
new ZLBooleanOption("CancelMenu", "previousBook", false);
new ZLBooleanOption(GROUP_NAME, "previousBook", false);
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 {
library,

View file

@ -41,6 +41,7 @@ public abstract class Config {
private final String myNullString = new String("__NULL__");
private final Map<StringPair,String> myCache =
Collections.synchronizedMap(new HashMap<StringPair,String>());
private final Set<String> myCachedGroups = new HashSet<String>();
// deprecated, used in API server implementation
public final String getValue(String name, String id, String defaultValue) {
@ -50,6 +51,9 @@ public abstract class Config {
public final String getValue(StringPair id, String defaultValue) {
String value = myCache.get(id);
if (value == null) {
if (myCachedGroups.contains(id.Group)) {
value = myNullString;
} else {
try {
value = getValueInternal(id.Group, id.Name);
} catch (NotAvailableException e) {
@ -58,6 +62,7 @@ public abstract class Config {
if (value == null) {
value = myNullString;
}
}
myCache.put(id, value);
}
return value != myNullString ? value : defaultValue;
@ -72,6 +77,24 @@ public abstract class Config {
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) {
myCache.put(id, myNullString);
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 void setValueInternal(String group, String name, String value);
protected abstract void unsetValueInternal(String group, String name);
protected abstract Map<String,String> requestAllValuesForGroupInternal(String group) throws NotAvailableException;
}