mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 10:19:33 +02:00
config speed optimization
This commit is contained in:
parent
868bfa8185
commit
0689ac720a
8 changed files with 114 additions and 30 deletions
|
@ -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;
|
||||
|
@ -49,11 +51,15 @@ public class CancelActivity extends ListActivity {
|
|||
myCollection = new BookCollectionShadow();
|
||||
myCollection.bindToService(this, new Runnable() {
|
||||
public void run() {
|
||||
final ActionListAdapter adapter = new ActionListAdapter(
|
||||
new CancelMenuHelper().getActionsList(myCollection)
|
||||
);
|
||||
setListAdapter(adapter);
|
||||
getListView().setOnItemClickListener(adapter);
|
||||
Config.Instance().runOnStart(new Runnable() {
|
||||
public void run() {
|
||||
final ActionListAdapter adapter = new ActionListAdapter(
|
||||
new CancelMenuHelper().getActionsList(myCollection)
|
||||
);
|
||||
setListAdapter(adapter);
|
||||
getListView().setOnItemClickListener(adapter);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,13 +51,17 @@ public abstract class Config {
|
|||
public final String getValue(StringPair id, String defaultValue) {
|
||||
String value = myCache.get(id);
|
||||
if (value == null) {
|
||||
try {
|
||||
value = getValueInternal(id.Group, id.Name);
|
||||
} catch (NotAvailableException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (value == null) {
|
||||
if (myCachedGroups.contains(id.Group)) {
|
||||
value = myNullString;
|
||||
} else {
|
||||
try {
|
||||
value = getValueInternal(id.Group, id.Name);
|
||||
} catch (NotAvailableException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (value == null) {
|
||||
value = myNullString;
|
||||
}
|
||||
}
|
||||
myCache.put(id, value);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue