summaryrefslogtreecommitdiff
path: root/android/sdremote/src/org/libreoffice/impressremote/PresentationFragment.java
blob: 1793696681a33208cc9f481d20b8e3f8773b0237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package org.libreoffice.impressremote;

import org.libreoffice.impressremote.communication.CommunicationService;
import org.libreoffice.impressremote.communication.SlideShow;

import pl.polidea.coverflow.AbstractCoverFlowImageAdapter;
import pl.polidea.coverflow.CoverFlow;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.TextView;

public class PresentationFragment extends Fragment {

	private CoverFlow mTopView;
	private ImageView mHandle;
	private View mLayout;
	private WebView mNotes;
	private Context mContext;
	private TextView mNumberText;

	private CommunicationService mCommunicationService;
	private SlideShow mSlideShow;

	private float mOriginalCoverflowWidth;
	private float mOriginalCoverflowHeight;

	private float mNewCoverflowWidth = 0;
	private float mNewCoverflowHeight = 0;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	                Bundle savedInstanceState) {
		mContext = container.getContext();

		View v = inflater.inflate(R.layout.fragment_presentation, container,
		                false);

		mNotes = (WebView) v.findViewById(R.id.presentation_notes);

		String summary = "<html><body>This is just a test<br/><ul><li>And item</li><li>And again</li></ul>More text<br/>Blabla<br/>Blabla<br/>blabla<br/>Blabla</body></html>";
		mNotes.loadData(summary, "text/html", null);
		mNotes.setBackgroundColor(Color.TRANSPARENT);

		//		TextView aText = new TextView();
		//		aText.setText

		mTopView = (CoverFlow) v.findViewById(R.id.presentation_coverflow);

		mLayout = v.findViewById(R.id.presentation_layout);

		mNumberText = (TextView) v.findViewById(R.id.presentation_slidenumber);

		mHandle = (ImageView) v.findViewById(R.id.presentation_handle);
		mHandle.setOnTouchListener(new SizeListener());

		// Call again to set things up if necessary.
		setCommunicationService(mCommunicationService);

		// Save the height/width for future reference
		mOriginalCoverflowHeight = mTopView.getImageHeight();
		mOriginalCoverflowWidth = mTopView.getImageWidth();

		if (mNewCoverflowHeight != 0) {
			ThumbnailAdapter aAdapter = (ThumbnailAdapter) mTopView
			                .getAdapter();
			aAdapter.setHeight(mNewCoverflowHeight);
			mTopView.setImageHeight(mNewCoverflowHeight);
			aAdapter.setWidth(mNewCoverflowWidth);
			mTopView.setImageWidth(mNewCoverflowWidth);

			// We need to update the view now
			aAdapter.notifyDataSetChanged();
		}
		return v;
	}

	// -------------------------------------------------- RESIZING LISTENER ----
	private class SizeListener implements OnTouchListener {

		@Override
		public boolean onTouch(View aView, MotionEvent aEvent) {

			switch (aEvent.getAction()) {
			case MotionEvent.ACTION_DOWN:
				mHandle.setImageResource(R.drawable.handle_light);
				break;
			case MotionEvent.ACTION_UP:
				mHandle.setImageResource(R.drawable.handle_default);
				break;
			case MotionEvent.ACTION_MOVE:
				LayoutParams aParams = mTopView.getLayoutParams();
				int aHeight = mTopView.getHeight();
				int aViewSize = mLayout.getHeight();

				final int DRAG_MARGIN = 120;

				// Calculate height change, taking limits into account
				int aDiff = (int) (aEvent.getY());
				System.out.println("Diff1 is :" + aDiff);
				if (aDiff + aHeight < DRAG_MARGIN) {
					aDiff = DRAG_MARGIN - aHeight;
				} else if ((aHeight + aDiff) > (aViewSize - DRAG_MARGIN)) {
					aDiff = (aViewSize - DRAG_MARGIN) - aHeight;
				}

				// Now deal with the internal height
				AbstractCoverFlowImageAdapter aAdapter = (AbstractCoverFlowImageAdapter) mTopView
				                .getAdapter();

				double aRatio = mOriginalCoverflowWidth
				                / mOriginalCoverflowHeight;
				System.out.println("Diff2 is :" + aDiff);
				float aHeightNew = mTopView.getImageHeight() + aDiff;
				float aWidthNew = (float) (aRatio * aHeightNew);

				//				 Too wide -- so scale down
				if (aWidthNew > mLayout.getWidth() - 50) {
					aWidthNew = mLayout.getWidth() - 50;
					aHeightNew = (float) (aWidthNew / aRatio);
					aDiff = (int) (aHeightNew - mTopView.getImageHeight());
				}

				// Set the new settings -- it turns out that changing the
				// internal height now works, and changing the views height
				// is unnecessary / even causes problems.
				//				aParams.height += aDiff;
				//				mTopView.setLayoutParams(aParams);

				mNewCoverflowHeight = aHeightNew;
				mNewCoverflowWidth = aWidthNew;

				aAdapter.setHeight(aHeightNew);
				mTopView.setImageHeight(aHeightNew);
				aAdapter.setWidth(aWidthNew);
				mTopView.setImageWidth(aWidthNew);

				// We need to update the view now
				aAdapter.notifyDataSetChanged();

				break;
			}
			// TODO Auto-generated method stub
			return true;
		}
	}

	// ----------------------------------------------------- CLICK LISTENER ----

	protected class ClickListener implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1,
		                int aPosition, long arg3) {
			if (mCommunicationService != null)
				mCommunicationService.getTransmitter().gotoSlide(aPosition);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}

	// ---------------------------------------------------- MESSAGE HANDLER ----
	public void setCommunicationService(
	                CommunicationService aCommunicationService) {
		mCommunicationService = aCommunicationService;
		if (mCommunicationService == null)
			return;

		mSlideShow = mCommunicationService.getSlideShow();
		if (mTopView != null && mSlideShow != null) {
			mTopView.setAdapter(new ThumbnailAdapter(mContext, mSlideShow));
			mTopView.setSelection(mSlideShow.getCurrentSlide(), true);
			mTopView.setOnItemSelectedListener(new ClickListener());
		}

	}

	public void handleMessage(Message aMessage) {
		Bundle aData = aMessage.getData();
		switch (aMessage.what) {
		case CommunicationService.MSG_SLIDE_CHANGED:
			int aSlide = aData.getInt("slide_number");
			mTopView.setSelection(aSlide, true);

			mNumberText.setText((mSlideShow.getCurrentSlide() + 1) + "/"
			                + mSlideShow.getSize());

			break;
		case CommunicationService.MSG_SLIDE_PREVIEW:
			int aNSlide = aData.getInt("slide_number");
			if (mTopView.getSelectedItemPosition() == aNSlide) {
				// mTopView. // TODO: update the current item
			}
			break;

		}
	}

	// ------------------------------------------------- THUMBNAIL ADAPTER ----
	protected class ThumbnailAdapter extends AbstractCoverFlowImageAdapter {

		private Context mContext;

		private SlideShow mSlideShow;

		public ThumbnailAdapter(Context aContext, SlideShow aSlideShow) {
			mContext = aContext;
			mSlideShow = aSlideShow;
		}

		@Override
		public int getCount() {
			return mSlideShow.getSize();
		}

		@Override
		protected Bitmap createBitmap(int position) {
			Bitmap aBitmap = mSlideShow.getImage(position);
			final int borderWidth = 8;

			Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
			p.setShadowLayer(borderWidth, 0, 0, Color.BLACK);

			RectF aRect = new RectF(borderWidth, borderWidth, borderWidth
			                + aBitmap.getWidth(), borderWidth
			                + aBitmap.getHeight());
			Bitmap aOut = Bitmap.createBitmap(aBitmap.getWidth() + 2
			                * borderWidth, aBitmap.getHeight() + 2
			                * borderWidth, aBitmap.getConfig());
			Canvas canvas = new Canvas(aOut);
			canvas.drawColor(Color.TRANSPARENT);
			canvas.drawRect(aRect, p);
			canvas.drawBitmap(aBitmap, null, aRect, null);

			return aOut;
		}
	}
}