1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

startup optimization

Conflicts:

	src/org/geometerplus/android/fbreader/FBReader.java
	src/org/geometerplus/zlibrary/core/application/ZLApplication.java
	src/org/geometerplus/zlibrary/ui/android/library/ZLAndroidActivity.java
This commit is contained in:
Nikolay Pultsin 2012-01-29 03:14:25 +00:00
parent 1783b9ad7a
commit ab72a293df
8 changed files with 150 additions and 85 deletions

View file

@ -103,6 +103,7 @@ public final class FBReader extends ZLAndroidActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
System.err.println("+FBReader.onCreate");
DictionaryUtil.init(this);
final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance();
@ -153,6 +154,7 @@ public final class FBReader extends ZLAndroidActivity {
fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_REVERSE_PORTRAIT, new SetScreenOrientationAction(this, fbReader, ZLibrary.SCREEN_ORIENTATION_REVERSE_PORTRAIT));
fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_REVERSE_LANDSCAPE, new SetScreenOrientationAction(this, fbReader, ZLibrary.SCREEN_ORIENTATION_REVERSE_LANDSCAPE));
}
System.err.println("-FBReader.onCreate");
}
@Override
@ -222,8 +224,12 @@ public final class FBReader extends ZLAndroidActivity {
@Override
public void onStart() {
System.err.println("+FBReader.onStart");
super.onStart();
System.err.println("++FBReader.onStart");
final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary)ZLibrary.Instance();
System.err.println("FBReader.onStart 1");
final int fullScreenFlag =
zlibrary.ShowStatusBarOption.getValue() ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
@ -231,8 +237,10 @@ public final class FBReader extends ZLAndroidActivity {
finish();
startActivity(new Intent(this, getClass()));
}
System.err.println("FBReader.onStart 2");
SetScreenOrientationAction.setOrientation(this, zlibrary.OrientationOption.getValue());
System.err.println("FBReader.onStart 3");
final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance();
final RelativeLayout root = (RelativeLayout)findViewById(R.id.root_view);
@ -240,6 +248,7 @@ public final class FBReader extends ZLAndroidActivity {
((PopupPanel)fbReader.getPopupById(NavigationPopup.ID)).createControlPanel(this, root, PopupWindow.Location.Bottom);
((PopupPanel)fbReader.getPopupById(SelectionPopup.ID)).createControlPanel(this, root, PopupWindow.Location.Floating);
System.err.println("FBReader.onStart 4");
synchronized (myPluginActions) {
int index = 0;
while (index < myPluginActions.size()) {
@ -247,6 +256,7 @@ public final class FBReader extends ZLAndroidActivity {
}
myPluginActions.clear();
}
System.err.println("FBReader.onStart 5");
sendOrderedBroadcast(
new Intent(PluginApi.ACTION_REGISTER),
@ -257,32 +267,52 @@ public final class FBReader extends ZLAndroidActivity {
null,
null
);
System.err.println("FBReader.onStart 6");
final TipsManager manager = TipsManager.Instance();
switch (manager.requiredAction()) {
case Initialize:
startActivity(new Intent(TipsActivity.INITIALIZE_ACTION, null, this, TipsActivity.class));
break;
case Show:
startActivity(new Intent(TipsActivity.SHOW_TIP_ACTION, null, this, TipsActivity.class));
break;
case Download:
manager.startDownloading();
break;
case None:
break;
System.err.println("FBReader.onStart 7");
new TipRunner().start();
System.err.println("FBReader.onStart 8");
System.err.println("-FBReader.onStart");
}
private class TipRunner extends Thread {
TipRunner() {
setPriority(MIN_PRIORITY);
}
public void run() {
final TipsManager manager = TipsManager.Instance();
switch (manager.requiredAction()) {
case Initialize:
startActivity(new Intent(
TipsActivity.INITIALIZE_ACTION, null, FBReader.this, TipsActivity.class
));
break;
case Show:
startActivity(new Intent(
TipsActivity.SHOW_TIP_ACTION, null, FBReader.this, TipsActivity.class
));
break;
case Download:
manager.startDownloading();
break;
case None:
break;
}
}
}
@Override
public void onResume() {
super.onResume();
System.err.println("+FBReader.onResume");
try {
sendBroadcast(new Intent(getApplicationContext(), KillerCallback.class));
} catch (Throwable t) {
}
PopupPanel.restoreVisibilities(FBReaderApp.Instance());
ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_OPENED);
System.err.println("-FBReader.onResume");
}
@Override
@ -293,11 +323,11 @@ public final class FBReader extends ZLAndroidActivity {
}
@Override
protected FBReaderApp createApplication(ZLFile file) {
protected FBReaderApp createApplication() {
if (SQLiteBooksDatabase.Instance() == null) {
new SQLiteBooksDatabase(this, "READER");
}
return new FBReaderApp(file != null ? file.getPath() : null);
return new FBReaderApp();
}
@Override

View file

@ -123,7 +123,8 @@ public abstract class TreeActivity extends ListActivity {
public void run() {
openTreeInternal(tree, treeToSelect, storeInHistory);
}
}
},
true
);
} else {
tree.waitForOpening();

View file

@ -22,8 +22,9 @@ package org.geometerplus.android.util;
import java.util.Queue;
import java.util.LinkedList;
import android.content.Context;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
@ -43,23 +44,42 @@ public abstract class UIUtil {
}
};
private static final Queue<Pair> ourTaskQueue = new LinkedList<Pair>();
private static final Handler ourProgressHandler = new Handler() {
public void handleMessage(Message message) {
try {
synchronized (ourMonitor) {
if (ourTaskQueue.isEmpty()) {
ourProgress.dismiss();
ourProgress = null;
} else {
ourProgress.setMessage(ourTaskQueue.peek().Message);
}
ourMonitor.notify();
}
} catch (Exception e) {
}
private static volatile Handler ourProgressHandler;
private static boolean init() {
if (ourProgressHandler != null) {
return true;
}
};
try {
ourProgressHandler = new Handler() {
public void handleMessage(Message message) {
try {
synchronized (ourMonitor) {
if (ourTaskQueue.isEmpty()) {
ourProgress.dismiss();
ourProgress = null;
} else {
ourProgress.setMessage(ourTaskQueue.peek().Message);
}
ourMonitor.notify();
}
} catch (Exception e) {
}
}
};
return true;
} catch (Throwable t) {
t.printStackTrace();
return false;
}
}
public static void wait(String key, Runnable action, Context context) {
if (!init()) {
action.run();
return;
}
synchronized (ourMonitor) {
final String message =
ZLResource.resource("dialog").getResource("waitMessage").getResource(key).getValue();
@ -88,26 +108,32 @@ public abstract class UIUtil {
}).start();
}
public static void runWithMessage(Context context, String key, final Runnable action, final Runnable postAction) {
public static void runWithMessage(final Activity activity, String key, final Runnable action, final Runnable postAction, final boolean minPriority) {
final String message =
ZLResource.resource("dialog").getResource("waitMessage").getResource(key).getValue();
final ProgressDialog progress = ProgressDialog.show(context, null, message, true, false);
final Handler handler = new Handler() {
public void handleMessage(Message message) {
progress.dismiss();
postAction.run();
}
};
final Thread runner = new Thread(new Runnable() {
activity.runOnUiThread(new Runnable() {
public void run() {
action.run();
handler.sendEmptyMessage(0);
final ProgressDialog progress = ProgressDialog.show(activity, null, message, true, false);
final Thread runner = new Thread() {
public void run() {
action.run();
activity.runOnUiThread(new Runnable() {
public void run() {
progress.dismiss();
if (postAction != null) {
postAction.run();
}
}
});
}
};
if (minPriority) {
runner.setPriority(Thread.MIN_PRIORITY);
}
runner.start();
}
});
runner.setPriority(Thread.MIN_PRIORITY);
runner.start();
}
public static void showMessageText(Context context, String text) {

View file

@ -103,11 +103,7 @@ public final class FBReaderApp extends ZLApplication {
public volatile BookModel Model;
private final String myArg0;
public FBReaderApp(String arg) {
myArg0 = arg;
public FBReaderApp() {
addAction(ActionCode.INCREASE_FONT, new ChangeFontSizeAction(this, +2));
addAction(ActionCode.DECREASE_FONT, new ChangeFontSizeAction(this, -2));
@ -139,34 +135,27 @@ public final class FBReaderApp extends ZLApplication {
setView(BookTextView);
}
public void initWindow() {
super.initWindow();
wait("loadingBook", new Runnable() {
public void run() {
Book book = createBookForFile(ZLFile.createFileByPath(myArg0));
if (book == null) {
book = Library.Instance().getRecentBook();
}
if ((book == null) || !book.File.exists()) {
public void openBook(Book book, final Bookmark bookmark) {
if (book == null) {
if (Model == null) {
book = Library.Instance().getRecentBook();
if (book == null || !book.File.exists()) {
book = Book.getByFile(Library.getHelpFile());
}
openBookInternal(book, null);
}
});
}
public void openBook(final Book book, final Bookmark bookmark) {
if (book == null) {
return;
if (book == null) {
return;
}
}
if (Model != null) {
if (bookmark == null & book.File.getPath().equals(Model.Book.File.getPath())) {
return;
}
}
wait("loadingBook", new Runnable() {
final Book bookToOpen = book;
runWithMessage("loadingBook", new Runnable() {
public void run() {
openBookInternal(book, bookmark);
openBookInternal(bookToOpen, bookmark);
}
});
}
@ -301,10 +290,7 @@ public final class FBReaderApp extends ZLApplication {
@Override
public void openFile(ZLFile file) {
final Book book = createBookForFile(file);
if (book != null) {
openBook(book, null);
}
openBook(createBookForFile(file), null);
}
public void onWindowClosing() {

View file

@ -64,7 +64,7 @@ public abstract class ZLApplication {
myWindow = window;
}
public void initWindow() {
public final void initWindow() {
setView(myView);
}
@ -74,9 +74,9 @@ public abstract class ZLApplication {
}
}
protected void wait(String key, Runnable runnable) {
protected void runWithMessage(String key, Runnable runnable) {
if (myWindow != null) {
myWindow.wait(key, runnable);
myWindow.runWithMessage(key, runnable);
}
}

View file

@ -34,7 +34,7 @@ abstract public class ZLApplicationWindow {
}
abstract protected void setTitle(String title);
abstract protected void wait(String key, Runnable runnable);
abstract protected void runWithMessage(String key, Runnable runnable);
abstract protected void refreshMenu();

View file

@ -90,11 +90,11 @@ public final class ZLAndroidApplicationWindow extends ZLApplicationWindow {
}
@Override
public void wait(String key, Runnable action) {
public void runWithMessage(String key, Runnable action) {
final Activity activity =
((ZLAndroidLibrary)ZLAndroidLibrary.Instance()).getActivity();
if (activity != null) {
UIUtil.wait(key, action, activity);
UIUtil.runWithMessage(activity, key, action, null, false);
} else {
action.run();
}

View file

@ -36,7 +36,7 @@ import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.zlibrary.ui.android.application.ZLAndroidApplicationWindow;
public abstract class ZLAndroidActivity extends Activity {
protected abstract ZLApplication createApplication(ZLFile file);
protected abstract ZLApplication createApplication();
private static final String REQUESTED_ORIENTATION_KEY = "org.geometerplus.zlibrary.ui.android.library.androidActiviy.RequestedOrientation";
private static final String ORIENTATION_CHANGE_COUNTER_KEY = "org.geometerplus.zlibrary.ui.android.library.androidActiviy.ChangeCounter";
@ -82,26 +82,46 @@ public abstract class ZLAndroidActivity extends Activity {
@Override
public void onCreate(Bundle state) {
System.err.println("+onCreate");
super.onCreate(state);
System.err.println("onCreate 1");
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));
System.err.println("onCreate 2");
requestWindowFeature(Window.FEATURE_NO_TITLE);
System.err.println("onCreate 5");
setContentView(R.layout.main);
System.err.println("onCreate 6");
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
System.err.println("onCreate 7");
getLibrary().setActivity(this);
System.err.println("onCreate 8");
final ZLFile fileToOpen = fileFromIntent(getIntent());
final ZLAndroidApplication androidApplication = (ZLAndroidApplication)getApplication();
System.err.println("onCreate 9");
if (androidApplication.myMainWindow == null) {
final ZLApplication application = createApplication(fileToOpen);
final ZLApplication application = createApplication();
System.err.println("onCreate 10");
androidApplication.myMainWindow = new ZLAndroidApplicationWindow(application);
System.err.println("onCreate 11");
application.initWindow();
} else {
ZLApplication.Instance().openFile(fileToOpen);
System.err.println("onCreate 12");
}
System.err.println("onCreate 13");
new Thread() {
public void run() {
System.err.println("+openFile");
ZLApplication.Instance().openFile(fileFromIntent(getIntent()));
System.err.println("-openFile");
}
}.start();
System.err.println("onCreate 14");
ZLApplication.Instance().getViewWidget().repaint();
System.err.println("-onCreate");
}
private PowerManager.WakeLock myWakeLock;
@ -145,6 +165,7 @@ public abstract class ZLAndroidActivity extends Activity {
@Override
public void onResume() {
System.err.println("+onResume");
super.onResume();
switchWakeLock(
getLibrary().BatteryLevelToTurnScreenOffOption.getValue() <
@ -163,6 +184,7 @@ public abstract class ZLAndroidActivity extends Activity {
}
registerReceiver(myBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
System.err.println("-onResume");
}
@Override