From bae042e93be9e516d950b317349f206cf1921aea Mon Sep 17 00:00:00 2001
From: Matt Holgate <matt@emobix.co.uk>
Date: Tue, 1 Jul 2014 16:51:50 +0100
Subject: Bug #694104 - Allow using MuPDFReaderView from GUI layout tool.

The GUI layout tool instantiates custom controls classes to display a
preview in the IDE. It relies on the 2-argument constructor being
implemented.

Use a different means to get the window manager that works for non-activity
contexts, and avoid creating gesture recognizers in this situation.

Based on a patch supplied by Masaki Muranaka
---
 .../src/com/artifex/mupdfdemo/MuPDFReaderView.java | 23 +++++++++++++++----
 .../src/com/artifex/mupdfdemo/ReaderView.java      | 26 ++++++++++++++++++----
 2 files changed, 41 insertions(+), 8 deletions(-)

(limited to 'platform/android/src')

diff --git a/platform/android/src/com/artifex/mupdfdemo/MuPDFReaderView.java b/platform/android/src/com/artifex/mupdfdemo/MuPDFReaderView.java
index a8f41fb2..383a581c 100644
--- a/platform/android/src/com/artifex/mupdfdemo/MuPDFReaderView.java
+++ b/platform/android/src/com/artifex/mupdfdemo/MuPDFReaderView.java
@@ -4,10 +4,12 @@ import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
+import android.util.AttributeSet;
 import android.util.DisplayMetrics;
 import android.view.MotionEvent;
 import android.view.ScaleGestureDetector;
 import android.view.View;
+import android.view.WindowManager;
 
 public class MuPDFReaderView extends ReaderView {
 	enum Mode {Viewing, Selecting, Drawing}
@@ -30,9 +32,8 @@ public class MuPDFReaderView extends ReaderView {
 		mMode = m;
 	}
 
-	public MuPDFReaderView(Activity act) {
-		super(act);
-		mContext = act;
+	private void setup()
+	{
 		// Get the screen size etc to customise tap margins.
 		// We calculate the size of 1 inch of the screen for tapping.
 		// On some devices the dpi values returned are wrong, so we
@@ -41,7 +42,8 @@ public class MuPDFReaderView extends ReaderView {
 		// dimension I've seen is 480 pixels or so). Then we check
 		// to ensure we are never more than 1/5 of the screen width.
 		DisplayMetrics dm = new DisplayMetrics();
-		act.getWindowManager().getDefaultDisplay().getMetrics(dm);
+		WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
+		wm.getDefaultDisplay().getMetrics(dm);
 		tapPageMargin = (int)dm.xdpi;
 		if (tapPageMargin < 100)
 			tapPageMargin = 100;
@@ -49,6 +51,19 @@ public class MuPDFReaderView extends ReaderView {
 			tapPageMargin = dm.widthPixels/5;
 	}
 
+	public MuPDFReaderView(Context context) {
+		super(context);
+		mContext = context;
+		setup();
+	}
+
+	public MuPDFReaderView(Context context, AttributeSet attrs)
+	{
+		super(context, attrs);
+		mContext = context;
+		setup();
+	}
+
 	public boolean onSingleTapUp(MotionEvent e) {
 		LinkInfo link = null;
 
diff --git a/platform/android/src/com/artifex/mupdfdemo/ReaderView.java b/platform/android/src/com/artifex/mupdfdemo/ReaderView.java
index 774c8dbe..2552e928 100644
--- a/platform/android/src/com/artifex/mupdfdemo/ReaderView.java
+++ b/platform/android/src/com/artifex/mupdfdemo/ReaderView.java
@@ -72,10 +72,23 @@ public class ReaderView
 
 	public ReaderView(Context context, AttributeSet attrs) {
 		super(context, attrs);
-		mGestureDetector = new GestureDetector(this);
-		mScaleGestureDetector = new ScaleGestureDetector(context, this);
-		mScroller        = new Scroller(context);
-		mStepper = new Stepper(this, this);
+		
+		// "Edit mode" means when the View is being displayed in the Android GUI editor. (this class
+		// is instantiated in the IDE, so we need to be a bit careful what we do).
+		if (isInEditMode())
+		{
+			mGestureDetector = null;
+			mScaleGestureDetector = null;
+			mScroller = null;
+			mStepper = null;
+		}
+		else
+		{
+			mGestureDetector = new GestureDetector(this);
+			mScaleGestureDetector = new ScaleGestureDetector(context, this);
+			mScroller        = new Scroller(context);
+			mStepper = new Stepper(this, this);
+		}
 	}
 
 	public ReaderView(Context context, AttributeSet attrs, int defStyle) {
@@ -527,6 +540,11 @@ public class ReaderView
 			int bottom) {
 		super.onLayout(changed, left, top, right, bottom);
 
+		// "Edit mode" means when the View is being displayed in the Android GUI editor. (this class
+		// is instantiated in the IDE, so we need to be a bit careful what we do).
+		if (isInEditMode())
+			return;
+
 		View cv = mChildViews.get(mCurrent);
 		Point cvOffset;
 
-- 
cgit v1.2.3