diff options
author | aleksandar-stefanovic <theonewithideas@gmail.com> | 2017-01-14 17:54:45 +0100 |
---|---|---|
committer | Aleksandar Stefanović <theonewithideas@gmail.com> | 2017-01-25 09:16:51 +0000 |
commit | 96b2aa3f820f4cc65904195cde12e305434a732c (patch) | |
tree | 10f5ef5aa5c14736b9499720577812de351f62e4 /android/source | |
parent | d49e497a6bd07e487149d65ab63138ce4aff73a4 (diff) |
Reduce usage of memory-leaking Context object
Huge refactoring of the methods to use the passed instance of the
Context object instead of using the static one. I couldn't completely
remove the static object, because it requires restructuring of the
workflow so that it originates from the activity, and not from some
other random place. The way it was refactored is:
1. Find a place where the static object
(LibreOfficeMainActivity.mAppContext) is used.
2. Add a LibreOfficeMainActivity object to the method signature.
3. Repeat the process with a method that calls it, and repeat until
the LibreOfficeMainActivity object isn't available, so that it can be
passed through all the methods, to the place where the static object
was used.
4. Replace that static object with the parameter of the function.
The commit looks pretty huge, but it's basically just the simple
refactoring explained above. The memory leak isn't completely gone,
but this a progress towards it. Also moved the "global" objects of
Handler and LOKitThread from an Activity to an Application, which
is the correct place for "global" variables. Can someone explain why
Handler and LOKitThread are used? They seem to mostly do nothing, but
steeply increasing the complexity of the application.
Change-Id: Ib2be77fa3adea94d6b7849d0e2afa90bf318d68b
Reviewed-on: https://gerrit.libreoffice.org/33073
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Aleksandar Stefanović <theonewithideas@gmail.com>
Diffstat (limited to 'android/source')
22 files changed, 147 insertions, 143 deletions
diff --git a/android/source/AndroidManifest.xml b/android/source/AndroidManifest.xml index 2c395c12d017..55b71f6879b8 100644 --- a/android/source/AndroidManifest.xml +++ b/android/source/AndroidManifest.xml @@ -9,6 +9,7 @@ <uses-permission android:name="android.permission.INTERNET" /> <application + android:name=".LibreOfficeApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" diff --git a/android/source/src/java/org/libreoffice/InvalidationHandler.java b/android/source/src/java/org/libreoffice/InvalidationHandler.java index aca50b0422cf..4edef18c21d0 100644 --- a/android/source/src/java/org/libreoffice/InvalidationHandler.java +++ b/android/source/src/java/org/libreoffice/InvalidationHandler.java @@ -24,10 +24,12 @@ public class InvalidationHandler implements Document.MessageCallback { private final GeckoLayerClient mLayerClient; private OverlayState mState; private boolean mKeyEvent = false; + private LibreOfficeMainActivity mContext; - public InvalidationHandler(LibreOfficeMainActivity mainActivity) { - mDocumentOverlay = mainActivity.getDocumentOverlay(); - mLayerClient = LibreOfficeMainActivity.getLayerClient(); + public InvalidationHandler(LibreOfficeMainActivity context) { + mContext = context; + mDocumentOverlay = mContext.getDocumentOverlay(); + mLayerClient = mContext.getLayerClient(); mState = OverlayState.NONE; } @@ -72,7 +74,7 @@ public class InvalidationHandler implements Document.MessageCallback { } Intent urlIntent = new Intent(Intent.ACTION_VIEW); urlIntent.setData(Uri.parse(payload)); - LibreOfficeMainActivity.mAppContext.startActivity(urlIntent); + mContext.startActivity(urlIntent); break; case Document.CALLBACK_STATE_CHANGED: stateChanged(payload); @@ -92,25 +94,25 @@ public class InvalidationHandler implements Document.MessageCallback { boolean pressed = Boolean.parseBoolean(value); if (parts[0].equals(".uno:Bold")) { - LOKitShell.getFormattingController().onToggleStateChanged(Document.BOLD, pressed); + mContext.getFormattingController().onToggleStateChanged(Document.BOLD, pressed); } else if (parts[0].equals(".uno:Italic")) { - LOKitShell.getFormattingController().onToggleStateChanged(Document.ITALIC, pressed); + mContext.getFormattingController().onToggleStateChanged(Document.ITALIC, pressed); } else if (parts[0].equals(".uno:Underline")) { - LOKitShell.getFormattingController().onToggleStateChanged(Document.UNDERLINE, pressed); + mContext.getFormattingController().onToggleStateChanged(Document.UNDERLINE, pressed); } else if (parts[0].equals(".uno:Strikeout")) { - LOKitShell.getFormattingController().onToggleStateChanged(Document.STRIKEOUT, pressed); + mContext.getFormattingController().onToggleStateChanged(Document.STRIKEOUT, pressed); } else if (parts[0].equals(".uno:CharFontName")) { - LOKitShell.getFontController().selectFont(value); + mContext.getFontController().selectFont(value); } else if (parts[0].equals(".uno:FontHeight")) { - LOKitShell.getFontController().selectFontSize(value); + mContext.getFontController().selectFontSize(value); } else if (parts[0].equals(".uno:LeftPara")) { - LOKitShell.getFormattingController().onToggleStateChanged(Document.ALIGN_LEFT, pressed); + mContext.getFormattingController().onToggleStateChanged(Document.ALIGN_LEFT, pressed); } else if (parts[0].equals(".uno:CenterPara")) { - LOKitShell.getFormattingController().onToggleStateChanged(Document.ALIGN_CENTER, pressed); + mContext.getFormattingController().onToggleStateChanged(Document.ALIGN_CENTER, pressed); } else if (parts[0].equals(".uno:RightPara")) { - LOKitShell.getFormattingController().onToggleStateChanged(Document.ALIGN_RIGHT, pressed); + mContext.getFormattingController().onToggleStateChanged(Document.ALIGN_RIGHT, pressed); } else if (parts[0].equals(".uno:JustifyPara")) { - LOKitShell.getFormattingController().onToggleStateChanged(Document.ALIGN_JUSTIFY, pressed); + mContext.getFormattingController().onToggleStateChanged(Document.ALIGN_JUSTIFY, pressed); } else { Log.d(LOGTAG, "LOK_CALLBACK_STATE_CHANGED type uncatched: " + payload); } @@ -140,7 +142,7 @@ public class InvalidationHandler implements Document.MessageCallback { int width = Integer.decode(coordinates[2]); int height = Integer.decode(coordinates[3]); - float dpi = LOKitShell.getDpi(); + float dpi = LOKitShell.getDpi(mContext); return new RectF( LOKitTileProvider.twipToPixel(x, dpi), @@ -233,7 +235,7 @@ public class InvalidationHandler implements Document.MessageCallback { newTop = cursorRectangle.bottom - (moveToRect.height() / 2.0f); } - LOKitShell.moveViewportTo(new PointF(newLeft, newTop), null); + LOKitShell.moveViewportTo(mContext, new PointF(newLeft, newTop), null); } /** @@ -360,9 +362,9 @@ public class InvalidationHandler implements Document.MessageCallback { */ private void handleGeneralChangeState(OverlayState previous, OverlayState next) { if (previous == OverlayState.NONE) { - LOKitShell.getToolbarController().switchToEditMode(); + mContext.getToolbarController().switchToEditMode(); } else if (next == OverlayState.NONE) { - LOKitShell.getToolbarController().switchToViewMode(); + mContext.getToolbarController().switchToViewMode(); } } @@ -381,7 +383,7 @@ public class InvalidationHandler implements Document.MessageCallback { mDocumentOverlay.hideSelections(); mDocumentOverlay.hideCursor(); mDocumentOverlay.hideGraphicSelection(); - LibreOfficeMainActivity.mAppContext.hideSoftKeyboard(); + mContext.hideSoftKeyboard(); } /** @@ -397,7 +399,7 @@ public class InvalidationHandler implements Document.MessageCallback { * Handle a transition to OverlayState.CURSOR state. */ private void handleCursorState(OverlayState previous) { - LibreOfficeMainActivity.mAppContext.showSoftKeyboardOrFormattingToolbar(); + mContext.showSoftKeyboardOrFormattingToolbar(); if (previous == OverlayState.TRANSITION) { mDocumentOverlay.showHandle(SelectionHandle.HandleType.MIDDLE); mDocumentOverlay.showCursor(); @@ -424,7 +426,7 @@ public class InvalidationHandler implements Document.MessageCallback { */ private void handleGraphicSelectionState(OverlayState previous) { mDocumentOverlay.showGraphicSelection(); - LibreOfficeMainActivity.mAppContext.hideSoftKeyboard(); + mContext.hideSoftKeyboard(); } /** diff --git a/android/source/src/java/org/libreoffice/LOKitShell.java b/android/source/src/java/org/libreoffice/LOKitShell.java index 6e32b056dc01..3eff5ce8f341 100644 --- a/android/source/src/java/org/libreoffice/LOKitShell.java +++ b/android/source/src/java/org/libreoffice/LOKitShell.java @@ -19,7 +19,6 @@ import android.view.KeyEvent; import org.libreoffice.canvas.SelectionHandle; import org.mozilla.gecko.gfx.ComposedTileLayer; -import org.mozilla.gecko.gfx.LayerView; /** * Common static LOKit functions, functions to send events. @@ -27,77 +26,50 @@ import org.mozilla.gecko.gfx.LayerView; public class LOKitShell { private static final String LOGTAG = LOKitShell.class.getSimpleName(); - public static float getDpi() { - DisplayMetrics metrics = LibreOfficeMainActivity.mAppContext.getResources().getDisplayMetrics(); + public static float getDpi(Context context) { + DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return metrics.density * 160; } // Get a Handler for the main java thread public static Handler getMainHandler() { - return LibreOfficeMainActivity.mAppContext.mMainHandler; + return LibreOfficeApplication.getMainHandler(); } - public static void showProgressSpinner() { + public static void showProgressSpinner(final LibreOfficeMainActivity context) { getMainHandler().post(new Runnable() { @Override public void run() { - LibreOfficeMainActivity.mAppContext.showProgressSpinner(); + context.showProgressSpinner(); } }); } - public static void hideProgressSpinner() { + public static void hideProgressSpinner(final LibreOfficeMainActivity context) { getMainHandler().post(new Runnable() { @Override public void run() { - LibreOfficeMainActivity.mAppContext.hideProgressSpinner(); + context.hideProgressSpinner(); } }); } - public static ToolbarController getToolbarController() { - return LibreOfficeMainActivity.mAppContext.getToolbarController(); - } - - public static FormattingController getFormattingController() { - return LibreOfficeMainActivity.mAppContext.getFormattingController(); - } - - public static FontController getFontController() { - return LibreOfficeMainActivity.mAppContext.getFontController(); - } - public static int getMemoryClass(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); return activityManager.getMemoryClass() * 1024 * 1024; } - public static DisplayMetrics getDisplayMetrics() { - if (LibreOfficeMainActivity.mAppContext == null) { - return null; - } - DisplayMetrics metrics = new DisplayMetrics(); - LibreOfficeMainActivity.mAppContext.getWindowManager().getDefaultDisplay().getMetrics(metrics); - return metrics; - } - public static boolean isEditingEnabled() { return LibreOfficeMainActivity.isExperimentalMode(); } - public static LayerView getLayerView() { - return LibreOfficeMainActivity.getLayerClient().getView(); - } - // EVENTS /** * Make sure LOKitThread is running and send event to it. */ public static void sendEvent(LOEvent event) { - if (LibreOfficeMainActivity.mAppContext != null && LibreOfficeMainActivity.mAppContext.getLOKitThread() != null) { - LibreOfficeMainActivity.mAppContext.getLOKitThread().queueEvent(event); - } + LibreOfficeApplication.getLoKitThread().queueEvent(event); } public static void sendThumbnailEvent(ThumbnailCreator.ThumbnailCreationTask task) { @@ -175,11 +147,11 @@ public class LOKitShell { * Move the viewport to the desired point (top-left), and change the zoom level. * Ensure this runs on the UI thread. */ - public static void moveViewportTo(final PointF position, final Float zoom) { - getLayerView().getLayerClient().post(new Runnable() { + public static void moveViewportTo(final LibreOfficeMainActivity context, final PointF position, final Float zoom) { + context.getLayerClient().post(new Runnable() { @Override public void run() { - getLayerView().getLayerClient().moveTo(position, zoom); + context.getLayerClient().moveTo(position, zoom); } }); } diff --git a/android/source/src/java/org/libreoffice/LOKitThread.java b/android/source/src/java/org/libreoffice/LOKitThread.java index 5927f5fd6e60..7db16059a8ca 100644 --- a/android/source/src/java/org/libreoffice/LOKitThread.java +++ b/android/source/src/java/org/libreoffice/LOKitThread.java @@ -1,5 +1,6 @@ package org.libreoffice; +import android.content.Context; import android.graphics.Bitmap; import android.graphics.PointF; import android.graphics.RectF; @@ -25,7 +26,6 @@ public class LOKitThread extends Thread { private LinkedBlockingQueue<LOEvent> mEventQueue = new LinkedBlockingQueue<LOEvent>(); - private LibreOfficeMainActivity mApplication; private TileProvider mTileProvider; private InvalidationHandler mInvalidationHandler; private ImmutableViewportMetrics mViewportMetrics; @@ -159,22 +159,22 @@ public class LOKitThread extends Thread { */ private void resumeDocument(String filename, int partIndex){ - if (mApplication == null) { - mApplication = LibreOfficeMainActivity.mAppContext; - } - mLayerClient = LibreOfficeMainActivity.getLayerClient(); + LibreOfficeMainActivity mainActivity = LibreOfficeMainActivity.mAppContext; + + + mLayerClient = mainActivity.getLayerClient(); - mInvalidationHandler = new InvalidationHandler(LibreOfficeMainActivity.mAppContext); - mTileProvider = TileProviderFactory.create(mLayerClient, mInvalidationHandler, filename); + mInvalidationHandler = new InvalidationHandler(mainActivity); + mTileProvider = TileProviderFactory.create(mainActivity, mInvalidationHandler, filename); if (mTileProvider.isReady()) { - LOKitShell.showProgressSpinner(); + LOKitShell.showProgressSpinner(mainActivity); mTileProvider.changePart(partIndex); mViewportMetrics = mLayerClient.getViewportMetrics(); mLayerClient.setViewportMetrics(mViewportMetrics.scaleTo(0.9f, new PointF())); refresh(); - LOKitShell.hideProgressSpinner(); + LOKitShell.hideProgressSpinner(mainActivity); } else { closeDocument(); } @@ -186,13 +186,12 @@ public class LOKitThread extends Thread { * Change part of the document. */ private void changePart(int partIndex) { - - LOKitShell.showProgressSpinner(); + LOKitShell.showProgressSpinner(LibreOfficeMainActivity.mAppContext); mTileProvider.changePart(partIndex); mViewportMetrics = mLayerClient.getViewportMetrics(); mLayerClient.setViewportMetrics(mViewportMetrics.scaleTo(0.9f, new PointF())); refresh(); - LOKitShell.hideProgressSpinner(); + LOKitShell.hideProgressSpinner(LibreOfficeMainActivity.mAppContext); } /** @@ -200,19 +199,18 @@ public class LOKitThread extends Thread { * @param filename - filename where the document is located */ private void loadDocument(String filename) { - if (mApplication == null) { - mApplication = LibreOfficeMainActivity.mAppContext; - } + //TODO remove static reference to context (causes memory leaks) + LibreOfficeMainActivity mMainActivity = LibreOfficeMainActivity.mAppContext; - mLayerClient = LibreOfficeMainActivity.getLayerClient(); + mLayerClient = mMainActivity.getLayerClient(); - mInvalidationHandler = new InvalidationHandler(LibreOfficeMainActivity.mAppContext); - mTileProvider = TileProviderFactory.create(mLayerClient, mInvalidationHandler, filename); + mInvalidationHandler = new InvalidationHandler(mMainActivity); + mTileProvider = TileProviderFactory.create(mMainActivity, mInvalidationHandler, filename); if (mTileProvider.isReady()) { - LOKitShell.showProgressSpinner(); + LOKitShell.showProgressSpinner(mMainActivity); refresh(); - LOKitShell.hideProgressSpinner(); + LOKitShell.hideProgressSpinner(mMainActivity); } else { closeDocument(); } diff --git a/android/source/src/java/org/libreoffice/LOKitTileProvider.java b/android/source/src/java/org/libreoffice/LOKitTileProvider.java index dd14621b3f02..a068627711fb 100644 --- a/android/source/src/java/org/libreoffice/LOKitTileProvider.java +++ b/android/source/src/java/org/libreoffice/LOKitTileProvider.java @@ -30,13 +30,13 @@ import java.nio.ByteBuffer; public class LOKitTileProvider implements TileProvider { private static final String LOGTAG = LOKitTileProvider.class.getSimpleName(); private static int TILE_SIZE = 256; - private final GeckoLayerClient mLayerClient; private final float mTileWidth; private final float mTileHeight; private final String mInputFile; private Office mOffice; private Document mDocument; private boolean mIsReady = false; + private LibreOfficeMainActivity mContext; private float mDPI; private float mWidthTwip; @@ -48,14 +48,13 @@ public class LOKitTileProvider implements TileProvider { /** * Initialize LOKit and load the document. - * @param layerClient - layerclient implementation * @param messageCallback - callback for messages retrieved from LOKit * @param input - input path of the document */ - public LOKitTileProvider(GeckoLayerClient layerClient, Document.MessageCallback messageCallback, String input) { - mLayerClient = layerClient; + public LOKitTileProvider(LibreOfficeMainActivity context, Document.MessageCallback messageCallback, String input) { + mContext = context; mMessageCallback = messageCallback; - mDPI = LOKitShell.getDpi(); + mDPI = LOKitShell.getDpi(mContext); mTileWidth = pixelToTwip(TILE_SIZE, mDPI); mTileHeight = pixelToTwip(TILE_SIZE, mDPI); @@ -139,8 +138,8 @@ public class LOKitTileProvider implements TileProvider { if (values == null || values.isEmpty()) return; - LOKitShell.getFontController().parseJson(values); - LOKitShell.getFontController().setupFontViews(); + mContext.getFontController().parseJson(values); + mContext.getFontController().setupFontViews(); } private String getGenericPartName(int i) { diff --git a/android/source/src/java/org/libreoffice/LibreOfficeApplication.java b/android/source/src/java/org/libreoffice/LibreOfficeApplication.java new file mode 100644 index 000000000000..1c72aa89eeba --- /dev/null +++ b/android/source/src/java/org/libreoffice/LibreOfficeApplication.java @@ -0,0 +1,35 @@ +/* + * + * * This file is part of the LibreOffice project. + * * + * * This Source Code Form is subject to the terms of the Mozilla Public + * * License, v. 2.0. If a copy of the MPL was not distributed with this + * * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +package org.libreoffice; + +import android.app.Application; +import android.os.Handler; + +public class LibreOfficeApplication extends Application { + + private static Handler mainHandler; + private static LOKitThread loKitThread; + + public LibreOfficeApplication() { + loKitThread = new LOKitThread(); + loKitThread.start(); + + mainHandler = new Handler(); + } + + public static LOKitThread getLoKitThread() { + return loKitThread; + } + + public static Handler getMainHandler() { + return mainHandler; + } +} diff --git a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java index 2a2c99eadb20..2e1a4ddce8d1 100755 --- a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java +++ b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java @@ -56,16 +56,13 @@ public class LibreOfficeMainActivity extends AppCompatActivity { public static LibreOfficeMainActivity mAppContext; - private static GeckoLayerClient mLayerClient; - private static LOKitThread sLOKitThread; + private GeckoLayerClient mLayerClient; private static boolean mIsExperimentalMode; private int providerId; private URI documentUri; - public Handler mMainHandler; - private DrawerLayout mDrawerLayout; private LOAbout mAbout; @@ -86,7 +83,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity { mAbout = new LOAbout(this, false); } - public static GeckoLayerClient getLayerClient() { + public GeckoLayerClient getLayerClient() { return mLayerClient; } @@ -116,8 +113,6 @@ public class LibreOfficeMainActivity extends AppCompatActivity { sPrefs.edit().putInt(ASSETS_EXTRACTED_PREFS_KEY, BuildConfig.VERSION_CODE).apply(); } } - mMainHandler = new Handler(); - setContentView(R.layout.activity_main); Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar); @@ -169,12 +164,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity { mDrawerList.setOnItemClickListener(new DocumentPartClickListener()); } - if (sLOKitThread == null) { - sLOKitThread = new LOKitThread(); - sLOKitThread.start(); - } else { - sLOKitThread.clearQueue(); - } + LibreOfficeApplication.getLoKitThread().clearQueue(); mLayerClient = new GeckoLayerClient(this); mLayerClient.setZoomConstraints(new ZoomConstraints(true)); @@ -194,7 +184,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity { }); // create TextCursorLayer - mDocumentOverlay = new DocumentOverlay(mAppContext, layerView); + mDocumentOverlay = new DocumentOverlay(this, layerView); mToolbarController.setupToolbars(); } @@ -391,7 +381,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity { } }; - AlertDialog.Builder builder = new AlertDialog.Builder(mAppContext); + AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(R.string.save_alert_dialog_title) .setPositiveButton(R.string.save_document, dialogClickListener) .setNegativeButton(R.string.cancel_save_document, dialogClickListener) @@ -400,10 +390,6 @@ public class LibreOfficeMainActivity extends AppCompatActivity { } - public LOKitThread getLOKitThread() { - return sLOKitThread; - } - public List<DocumentPartView> getDocumentPartView() { return mDocumentPartView; } diff --git a/android/source/src/java/org/libreoffice/SearchController.java b/android/source/src/java/org/libreoffice/SearchController.java index 03a98d993340..14782a0a95ed 100644 --- a/android/source/src/java/org/libreoffice/SearchController.java +++ b/android/source/src/java/org/libreoffice/SearchController.java @@ -27,8 +27,8 @@ class SearchController implements View.OnClickListener { addProperty(rootJson, "SearchItem.SearchString", "string", searchString); addProperty(rootJson, "SearchItem.Backward", "boolean", direction == SearchDirection.DOWN ? "true" : "false"); - addProperty(rootJson, "SearchItem.SearchStartPointX", "long", String.valueOf((long) UnitConverter.pixelToTwip(x, LOKitShell.getDpi()))); - addProperty(rootJson, "SearchItem.SearchStartPointY", "long", String.valueOf((long) UnitConverter.pixelToTwip(y, LOKitShell.getDpi()))); + addProperty(rootJson, "SearchItem.SearchStartPointX", "long", String.valueOf((long) UnitConverter.pixelToTwip(x, LOKitShell.getDpi(mActivity)))); + addProperty(rootJson, "SearchItem.SearchStartPointY", "long", String.valueOf((long) UnitConverter.pixelToTwip(y, LOKitShell.getDpi(mActivity)))); addProperty(rootJson, "SearchItem.Command", "long", String.valueOf(0)); // search all == 1 LOKitShell.sendEvent(new LOEvent(LOEvent.UNO_COMMAND, ".uno:ExecuteSearch", rootJson.toString())); diff --git a/android/source/src/java/org/libreoffice/ThumbnailCreator.java b/android/source/src/java/org/libreoffice/ThumbnailCreator.java index 5532d7f90926..52870b67a5b9 100644 --- a/android/source/src/java/org/libreoffice/ThumbnailCreator.java +++ b/android/source/src/java/org/libreoffice/ThumbnailCreator.java @@ -108,7 +108,7 @@ public class ThumbnailCreator { public void applyBitmap(final Bitmap bitmap) { // run on UI thread - LibreOfficeMainActivity.mAppContext.mMainHandler.post(new Runnable() { + LibreOfficeApplication.getMainHandler().post(new Runnable() { @Override public void run() { changeBitmap(bitmap); diff --git a/android/source/src/java/org/libreoffice/TileProviderFactory.java b/android/source/src/java/org/libreoffice/TileProviderFactory.java index 45b10a9b2f1a..a090bd337eb2 100644 --- a/android/source/src/java/org/libreoffice/TileProviderFactory.java +++ b/android/source/src/java/org/libreoffice/TileProviderFactory.java @@ -24,8 +24,8 @@ public class TileProviderFactory { LibreOfficeKit.initializeLibrary(); } - public static TileProvider create(GeckoLayerClient layerClient, InvalidationHandler invalidationHandler, String filename) { - return new LOKitTileProvider(layerClient, invalidationHandler, filename); + public static TileProvider create(LibreOfficeMainActivity context, InvalidationHandler invalidationHandler, String filename) { + return new LOKitTileProvider(context, invalidationHandler, filename); } } diff --git a/android/source/src/java/org/libreoffice/canvas/GraphicSelection.java b/android/source/src/java/org/libreoffice/canvas/GraphicSelection.java index 30a328d88c9c..8d773b2ea208 100644 --- a/android/source/src/java/org/libreoffice/canvas/GraphicSelection.java +++ b/android/source/src/java/org/libreoffice/canvas/GraphicSelection.java @@ -15,6 +15,7 @@ import android.graphics.PointF; import android.graphics.RectF; import org.libreoffice.LOKitShell; +import org.libreoffice.LibreOfficeMainActivity; import org.mozilla.gecko.gfx.LayerView; import static org.libreoffice.canvas.GraphicSelectionHandle.HandlePosition; @@ -35,11 +36,13 @@ public class GraphicSelection extends CommonCanvasElement { private GraphicSelectionHandle mHandles[] = new GraphicSelectionHandle[8]; private GraphicSelectionHandle mDragHandle = null; private boolean mTriggerSinglePress = false; + private LibreOfficeMainActivity mContext; /** * Construct the graphic selection. */ - public GraphicSelection() { + public GraphicSelection(LibreOfficeMainActivity context) { + mContext = context; // Create the paint, which is needed at drawing mPaintStroke = new Paint(); mPaintStroke.setStyle(Paint.Style.STROKE); @@ -243,7 +246,7 @@ public class GraphicSelection extends CommonCanvasElement { */ private void sendGraphicSelection(String type, PointF screenPosition) { - LayerView layerView = LOKitShell.getLayerView(); + LayerView layerView = mContext.getLayerClient().getView(); if (layerView != null) { // Position is in screen coordinates. We need to convert them to // document coordinates. diff --git a/android/source/src/java/org/libreoffice/canvas/SelectionHandle.java b/android/source/src/java/org/libreoffice/canvas/SelectionHandle.java index 38b5dc121932..ddd16fe5eb4b 100644 --- a/android/source/src/java/org/libreoffice/canvas/SelectionHandle.java +++ b/android/source/src/java/org/libreoffice/canvas/SelectionHandle.java @@ -4,6 +4,7 @@ import android.graphics.Bitmap; import android.graphics.PointF; import org.libreoffice.LOKitShell; +import org.libreoffice.LibreOfficeMainActivity; import org.mozilla.gecko.gfx.ImmutableViewportMetrics; /** @@ -17,8 +18,11 @@ public abstract class SelectionHandle extends BitmapHandle { private final PointF mDragDocumentPosition = new PointF(); private long mLastTime = 0; - public SelectionHandle(Bitmap bitmap) { + private LibreOfficeMainActivity mContext; + + public SelectionHandle(LibreOfficeMainActivity context, Bitmap bitmap) { super(bitmap); + mContext = context; } /** @@ -52,7 +56,7 @@ public abstract class SelectionHandle extends BitmapHandle { * Signal to move the handle to a new position to LO. */ private void signalHandleMove(float newX, float newY) { - ImmutableViewportMetrics viewportMetrics = LOKitShell.getLayerView().getLayerClient().getViewportMetrics(); + ImmutableViewportMetrics viewportMetrics = mContext.getLayerClient().getViewportMetrics(); float zoom = viewportMetrics.zoomFactor; float deltaX = (newX - mDragStartPoint.x) / zoom; diff --git a/android/source/src/java/org/libreoffice/canvas/SelectionHandleEnd.java b/android/source/src/java/org/libreoffice/canvas/SelectionHandleEnd.java index f0b68eea2f10..b85b80fc9579 100644 --- a/android/source/src/java/org/libreoffice/canvas/SelectionHandleEnd.java +++ b/android/source/src/java/org/libreoffice/canvas/SelectionHandleEnd.java @@ -1,6 +1,6 @@ package org.libreoffice.canvas; -import android.content.Context; +import org.libreoffice.LibreOfficeMainActivity; import org.libreoffice.R; @@ -8,8 +8,8 @@ import org.libreoffice.R; * Selection handle for showing and manipulating the end of a selection. */ public class SelectionHandleEnd extends SelectionHandle { - public SelectionHandleEnd(Context context) { - super(getBitmapForDrawable(context, R.drawable.handle_alias_end)); + public SelectionHandleEnd(LibreOfficeMainActivity context) { + super(context, getBitmapForDrawable(context, R.drawable.handle_alias_end)); } /** diff --git a/android/source/src/java/org/libreoffice/canvas/SelectionHandleMiddle.java b/android/source/src/java/org/libreoffice/canvas/SelectionHandleMiddle.java index 409c3f222da5..76bdf9110acc 100644 --- a/android/source/src/java/org/libreoffice/canvas/SelectionHandleMiddle.java +++ b/android/source/src/java/org/libreoffice/canvas/SelectionHandleMiddle.java @@ -1,6 +1,6 @@ package org.libreoffice.canvas; -import android.content.Context; +import org.libreoffice.LibreOfficeMainActivity; import org.libreoffice.R; @@ -8,8 +8,8 @@ import org.libreoffice.R; * Selection handle that is used to manipulate the cursor. */ public class SelectionHandleMiddle extends SelectionHandle { - public SelectionHandleMiddle(Context context) { - super(getBitmapForDrawable(context, R.drawable.handle_alias_middle)); + public SelectionHandleMiddle(LibreOfficeMainActivity context) { + super(context, getBitmapForDrawable(context, R.drawable.handle_alias_middle)); } /** diff --git a/android/source/src/java/org/libreoffice/canvas/SelectionHandleStart.java b/android/source/src/java/org/libreoffice/canvas/SelectionHandleStart.java index 66b109f86e13..ad28826f6456 100644 --- a/android/source/src/java/org/libreoffice/canvas/SelectionHandleStart.java +++ b/android/source/src/java/org/libreoffice/canvas/SelectionHandleStart.java @@ -1,6 +1,6 @@ package org.libreoffice.canvas; -import android.content.Context; +import org.libreoffice.LibreOfficeMainActivity; import org.libreoffice.R; @@ -8,8 +8,8 @@ import org.libreoffice.R; * Selection handle for showing and manipulating the start of a selection. */ public class SelectionHandleStart extends SelectionHandle { - public SelectionHandleStart(Context context) { - super(getBitmapForDrawable(context, R.drawable.handle_alias_start)); + public SelectionHandleStart(LibreOfficeMainActivity context) { + super(context, getBitmapForDrawable(context, R.drawable.handle_alias_start)); } /** diff --git a/android/source/src/java/org/libreoffice/overlay/DocumentOverlay.java b/android/source/src/java/org/libreoffice/overlay/DocumentOverlay.java index f2c4c8f47725..8d90ef09fa4a 100644 --- a/android/source/src/java/org/libreoffice/overlay/DocumentOverlay.java +++ b/android/source/src/java/org/libreoffice/overlay/DocumentOverlay.java @@ -8,7 +8,7 @@ */ package org.libreoffice.overlay; -import android.app.Activity; +import org.libreoffice.LibreOfficeMainActivity; import android.graphics.RectF; import android.util.Log; @@ -64,7 +64,7 @@ public class DocumentOverlay { } } - public DocumentOverlay(Activity context, LayerView layerView) { + public DocumentOverlay(LibreOfficeMainActivity context, LayerView layerView) { mDocumentOverlayView = (DocumentOverlayView) context.findViewById(R.id.text_cursor_view); mDocumentOverlayLayer = new DocumentOverlayLayer(); if (mDocumentOverlayView == null) { diff --git a/android/source/src/java/org/libreoffice/overlay/DocumentOverlayView.java b/android/source/src/java/org/libreoffice/overlay/DocumentOverlayView.java index ce600dc984ae..48b98b4e7ca5 100644 --- a/android/source/src/java/org/libreoffice/overlay/DocumentOverlayView.java +++ b/android/source/src/java/org/libreoffice/overlay/DocumentOverlayView.java @@ -18,6 +18,7 @@ import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; +import org.libreoffice.LibreOfficeMainActivity; import org.libreoffice.canvas.Cursor; import org.libreoffice.canvas.GraphicSelection; import org.libreoffice.canvas.SelectionHandle; @@ -88,14 +89,14 @@ public class DocumentOverlayView extends View implements View.OnTouchListener { mSelectionPaint.setAlpha(50); mSelectionsVisible = false; - mGraphicSelection = new GraphicSelection(); + mGraphicSelection = new GraphicSelection((LibreOfficeMainActivity) getContext()); mGraphicSelection.setVisible(false); postDelayed(cursorAnimation, CURSOR_BLINK_TIME); - mHandleMiddle = new SelectionHandleMiddle(getContext()); - mHandleStart = new SelectionHandleStart(getContext()); - mHandleEnd = new SelectionHandleEnd(getContext()); + mHandleMiddle = new SelectionHandleMiddle((LibreOfficeMainActivity) getContext()); + mHandleStart = new SelectionHandleStart((LibreOfficeMainActivity) getContext()); + mHandleEnd = new SelectionHandleEnd((LibreOfficeMainActivity) getContext()); mInitialized = true; } diff --git a/android/source/src/java/org/mozilla/gecko/gfx/DisplayPortCalculator.java b/android/source/src/java/org/mozilla/gecko/gfx/DisplayPortCalculator.java index f2e164b5ba89..838286b669a8 100644 --- a/android/source/src/java/org/mozilla/gecko/gfx/DisplayPortCalculator.java +++ b/android/source/src/java/org/mozilla/gecko/gfx/DisplayPortCalculator.java @@ -11,6 +11,7 @@ import android.util.Log; import org.json.JSONArray; import org.libreoffice.LOKitShell; +import org.libreoffice.LibreOfficeMainActivity; import org.mozilla.gecko.util.FloatUtils; import java.util.Map; @@ -338,7 +339,8 @@ final class DisplayPortCalculator { VelocityBiasStrategy(Map<String, Integer> prefs) { SIZE_MULTIPLIER = getFloatPref(prefs, PREF_DISPLAYPORT_VB_MULTIPLIER, 2000); - VELOCITY_THRESHOLD = LOKitShell.getDpi() * getFloatPref(prefs, PREF_DISPLAYPORT_VB_VELOCITY_THRESHOLD, 32); + //TODO remove static reference to context + VELOCITY_THRESHOLD = LOKitShell.getDpi(LibreOfficeMainActivity.mAppContext) * getFloatPref(prefs, PREF_DISPLAYPORT_VB_VELOCITY_THRESHOLD, 32); REVERSE_BUFFER = getFloatPref(prefs, PREF_DISPLAYPORT_VB_REVERSE_BUFFER, 200); DANGER_ZONE_BASE_X_MULTIPLIER = getFloatPref(prefs, PREF_DISPLAYPORT_VB_DANGER_X_BASE, 1000); DANGER_ZONE_BASE_Y_MULTIPLIER = getFloatPref(prefs, PREF_DISPLAYPORT_VB_DANGER_Y_BASE, 1000); @@ -452,7 +454,8 @@ final class DisplayPortCalculator { // The velocity above which we start zooming out the display port to keep up // with the panning. - private static final float VELOCITY_EXPANSION_THRESHOLD = LOKitShell.getDpi() / 16f; + //TODO remove static reference to context + private static final float VELOCITY_EXPANSION_THRESHOLD = LOKitShell.getDpi(LibreOfficeMainActivity.mAppContext) / 16f; // How much we increase the display port based on velocity. Assuming no friction and // splitting (see below), this should be the number of frames (@60fps) between us @@ -653,7 +656,8 @@ final class DisplayPortCalculator { private int mMaxFramesToDraw; // maximum number of frames we take to draw PredictionBiasStrategy(Map<String, Integer> prefs) { - VELOCITY_THRESHOLD = LOKitShell.getDpi() * getFloatPref(prefs, PREF_DISPLAYPORT_PB_VELOCITY_THRESHOLD, 16); + //TODO remove static reference to context + VELOCITY_THRESHOLD = LOKitShell.getDpi(LibreOfficeMainActivity.mAppContext) * getFloatPref(prefs, PREF_DISPLAYPORT_PB_VELOCITY_THRESHOLD, 16); resetPageState(); } diff --git a/android/source/src/java/org/mozilla/gecko/gfx/GeckoLayerClient.java b/android/source/src/java/org/mozilla/gecko/gfx/GeckoLayerClient.java index 389a50f07b1e..fdcdb1ba51f0 100644 --- a/android/source/src/java/org/mozilla/gecko/gfx/GeckoLayerClient.java +++ b/android/source/src/java/org/mozilla/gecko/gfx/GeckoLayerClient.java @@ -138,7 +138,7 @@ public class GeckoLayerClient implements PanZoomTarget { /* Informs Gecko that the screen size has changed. */ private void sendResizeEventIfNecessary() { - DisplayMetrics metrics = LOKitShell.getDisplayMetrics(); + DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); IntSize newScreenSize = new IntSize(metrics.widthPixels, metrics.heightPixels); if (mScreenSize.equals(newScreenSize)) { diff --git a/android/source/src/java/org/mozilla/gecko/gfx/JavaPanZoomController.java b/android/source/src/java/org/mozilla/gecko/gfx/JavaPanZoomController.java index 159f91e000a6..3987b00b6773 100644 --- a/android/source/src/java/org/mozilla/gecko/gfx/JavaPanZoomController.java +++ b/android/source/src/java/org/mozilla/gecko/gfx/JavaPanZoomController.java @@ -34,7 +34,6 @@ public class JavaPanZoomController { private static final String LOGTAG = "GeckoPanZoomController"; - // Animation stops if the velocity is below this value when overscrolled or panning. private static final float STOPPED_THRESHOLD = 4.0f; @@ -43,7 +42,7 @@ public class JavaPanZoomController // The distance the user has to pan before we recognize it as such (e.g. to avoid 1-pixel pans // between the touch-down and touch-up of a click). In units of density-independent pixels. - public static final float PAN_THRESHOLD = 1/16f * LOKitShell.getDpi(); + private final float PAN_THRESHOLD; // Angle from axis within which we stay axis-locked private static final double AXIS_LOCK_ANGLE = Math.PI / 6.0; // 30 degrees @@ -52,7 +51,7 @@ public class JavaPanZoomController private static final float MAX_ZOOM = 4.0f; // The maximum amount we would like to scroll with the mouse - private static final float MAX_SCROLL = 0.075f * LOKitShell.getDpi(); + private final float MAX_SCROLL; private enum PanZoomState { NOTHING, /* no touch-start events received */ @@ -93,6 +92,8 @@ public class JavaPanZoomController private boolean mWaitForDoubleTap; public JavaPanZoomController(PanZoomTarget target, View view) { + PAN_THRESHOLD = 1/16f * LOKitShell.getDpi(view.getContext()); + MAX_SCROLL = 0.075f * LOKitShell.getDpi(view.getContext()); mTarget = target; mSubscroller = new SubdocumentScrollHelper(); mX = new AxisX(mSubscroller); diff --git a/android/source/src/java/org/mozilla/gecko/gfx/LayerView.java b/android/source/src/java/org/mozilla/gecko/gfx/LayerView.java index 62dc997dbd89..ea2db576a224 100644 --- a/android/source/src/java/org/mozilla/gecko/gfx/LayerView.java +++ b/android/source/src/java/org/mozilla/gecko/gfx/LayerView.java @@ -56,6 +56,8 @@ public class LayerView extends FrameLayout { private Listener mListener; private OnInterceptTouchListener mTouchIntercepter; + //TODO static beacuse of registerCxxCompositor() function, should be fixed in the future + private static LibreOfficeMainActivity mContext; /* Flags used to determine when to show the painted surface. The integer * order must correspond to the order in which these states occur. */ @@ -83,6 +85,7 @@ public class LayerView extends FrameLayout { public LayerView(Context context, AttributeSet attrs) { super(context, attrs); + mContext = (LibreOfficeMainActivity) context; if (shouldUseTextureView()) { mTextureView = new TextureView(context); @@ -342,7 +345,7 @@ public class LayerView extends FrameLayout { /** This function is invoked by Gecko (compositor thread) via JNI; be careful when modifying signature. */ public static GLController registerCxxCompositor() { try { - LayerView layerView = LibreOfficeMainActivity.getLayerClient().getView(); + LayerView layerView = mContext.getLayerClient().getView(); layerView.mListener.compositorCreated(); return layerView.getGLController(); } catch (Exception e) { diff --git a/android/source/src/java/org/mozilla/gecko/gfx/PanZoomController.java b/android/source/src/java/org/mozilla/gecko/gfx/PanZoomController.java index 829ffd555feb..dd0de0caf898 100644 --- a/android/source/src/java/org/mozilla/gecko/gfx/PanZoomController.java +++ b/android/source/src/java/org/mozilla/gecko/gfx/PanZoomController.java @@ -9,12 +9,7 @@ import android.graphics.PointF; import android.view.MotionEvent; import android.view.View; -import org.libreoffice.LOKitShell; - public interface PanZoomController { - // The distance the user has to pan before we recognize it as such (e.g. to avoid 1-pixel pans - // between the touch-down and touch-up of a click). In units of density-independent pixels. - public static final float PAN_THRESHOLD = 1/16f * LOKitShell.getDpi(); static class Factory { static PanZoomController create(PanZoomTarget target, View view) { |