-
Notifications
You must be signed in to change notification settings - Fork 56
/
OpenActivity.java
628 lines (535 loc) · 16.5 KB
/
OpenActivity.java
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
package com.example.poipptxnew;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import net.pbdavey.awt.Graphics2D;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import and.awt.Dimension;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;
import com.qhm123.slide.GestureDetector;
import com.qhm123.slide.ImageViewTouch;
import com.qhm123.slide.PagerAdapter;
import com.qhm123.slide.ScaleGestureDetector;
import com.qhm123.slide.ViewPager;
public class OpenActivity extends Activity {
private static final String TAG = OpenActivity.class.getSimpleName();
private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
private GestureDetector mGestureDetector;
private ScaleGestureDetector mScaleGestureDetector;
private boolean mPaused;
private boolean mOnScale = false;
private boolean mOnPagerScoll = false;
private int slideCount = 0;
private XSLFSlide[] slide;
private XMLSlideShow ppt;
private Dimension pgsize;
private ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
System.setProperty("javax.xml.stream.XMLInputFactory",
"com.sun.xml.stream.ZephyrParserFactory");
System.setProperty("javax.xml.stream.XMLOutputFactory",
"com.sun.xml.stream.ZephyrWriterFactory");
System.setProperty("javax.xml.stream.XMLEventFactory",
"com.sun.xml.stream.events.ZephyrEventFactory");
Thread.currentThread().setContextClassLoader(
getClass().getClassLoader());
// some test
try {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader reader = inputFactory
.createXMLEventReader(new StringReader(
"<doc att=\"value\">some text</doc>"));
while (reader.hasNext()) {
XMLEvent e = reader.nextEvent();
Log.e("HelloStax", "Event:[" + e + "]");
}
} catch (XMLStreamException e) {
Log.e("HelloStax", "Error parsing XML", e);
}
requestWindowFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
setProgressBarVisibility(true);
setProgressBarIndeterminate(true);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setPageMargin(10);
mViewPager.setPageMarginDrawable(new ColorDrawable(Color.BLACK));
mViewPager.setOffscreenPageLimit(1);
mViewPager.setOnPageChangeListener(mPageChangeListener);
setupOnTouchListeners(mViewPager);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading");
mProgressDialog.setIndeterminate(true);
String path = null;
Intent i = getIntent();
if (i != null) {
Uri uri = i.getData();
if (uri != null) {
Log.d(TAG, "uri.getPath: " + uri.getPath());
path = uri.getPath();
} else {
path = "/sdcard/talkaboutjvm.pptx";
File demoFile = new File(path);
if (!demoFile.exists()) {
InputStream inputStream = getResources().openRawResource(
R.raw.talkaboutjvm);
try {
FileOutputStream fos = new FileOutputStream(path);
byte[] buffer = new byte[512 * 1024];
int count;
while ((count = inputStream.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.flush();
fos.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
try {
setTitle(path);
pptx2png(path);
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
private void pptx2png(final String path) throws IOException,
InvalidFormatException {
mProgressDialog.show();
new AsyncTask<Void, Void, Void>() {
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
mViewPager.setAdapter(mPagerAdapter);
};
@Override
protected Void doInBackground(Void... params) {
System.out.println("Processing " + path);
long time = System.currentTimeMillis();
try {
ppt = new XMLSlideShow(OPCPackage.open(path,
PackageAccess.READ));
pgsize = ppt.getPageSize();
System.out.println("pgsize.width: " + pgsize.getWidth()
+ ", pgsize.height: " + pgsize.getHeight());
slide = ppt.getSlides();
slideCount = slide.length;
} catch (InvalidFormatException e) {
e.printStackTrace();
}
Log.d(TAG, "time: " + (System.currentTimeMillis() - time));
return null;
}
}.execute();
final ExecutorService es = Executors.newSingleThreadExecutor();
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (mViewPager == null) {
return;
}
switch (msg.what) {
case 0: {
Log.d(TAG, "draw finish");
View v = (View) msg.obj;
v.invalidate();
int position = msg.arg1;
if (position == mViewPager.getCurrentItem()) {
setProgress(10000);
}
}
break;
case 1: {
int progress = msg.arg1;
int max = msg.arg2;
int p = (int) ((float) progress / max * 10000);
int position = (Integer) msg.obj;
Log.d(TAG, "update progress: " + progress + ", max: " + max
+ ", p: " + p + ", position: " + position);
if (position == 1) {
setProgressBarIndeterminate(false);
}
if (position == mViewPager.getCurrentItem()) {
if (position != 0 && progress == 0) {
setProgressBarIndeterminate(false);
}
setProgress(p);
}
}
break;
default:
break;
}
}
};
mPagerAdapter = new PagerAdapter() {
@Override
public void destroyItem(View container, int position, Object object) {
ImageViewTouch view = (ImageViewTouch) object;
view.getCanceled().set(true);
Future<?> task = (Future<?>) view.getTag();
task.cancel(false);
((ViewGroup) container).removeView(view);
BitmapDrawable bitmapDrawable = (BitmapDrawable) view
.getDrawable();
if (!bitmapDrawable.getBitmap().isRecycled()) {
bitmapDrawable.getBitmap().recycle();
}
mCache.remove(position);
}
@Override
public Object instantiateItem(View container, final int position) {
if (position == mViewPager.getCurrentItem()) {
setProgressBarIndeterminate(true);
}
final ImageViewTouch imageView = new ImageViewTouch(
OpenActivity.this);
imageView.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageView.setBackgroundColor(Color.BLACK);
imageView.setFocusableInTouchMode(true);
String title = slide[position].getTitle();
System.out.println("Rendering slide " + (position + 1)
+ (title == null ? "" : ": " + title));
Bitmap bmp = Bitmap.createBitmap((int) pgsize.getWidth(),
(int) pgsize.getHeight(), Config.RGB_565);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.WHITE);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
canvas.drawPaint(paint);
final Graphics2D graphics2d = new Graphics2D(canvas);
final AtomicBoolean isCanceled = new AtomicBoolean(false);
Runnable runnable = new Runnable() {
@Override
public void run() {
// render
try {
slide[position].draw(graphics2d, isCanceled,
handler, position);
// slide[position].draw(graphics2d);
handler.sendMessage(Message.obtain(handler, 0,
position, 0, imageView));
} catch (Exception e) {
e.printStackTrace();
}
}
};
Log.d(TAG, "ViewGroup addView");
Future<?> task = es.submit(runnable);
imageView.setTag(task);
imageView.setIsCanceled(isCanceled);
imageView.setImageBitmapResetBase(bmp, true);
((ViewGroup) container).addView(imageView);
Log.d(TAG, "ViewGroup addView");
mCache.put(position, imageView);
return imageView;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public int getCount() {
return slide.length;
}
@Override
public void startUpdate(View container) {
}
@Override
public void finishUpdate(View container) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
};
}
HashMap<Integer, View> mCache = new HashMap<Integer, View>();
public View getView(int position) {
return mCache.get(position);
}
Toast mPreToast;
ViewPager.OnPageChangeListener mPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position, int prePosition) {
ImageViewTouch preImageView = (ImageViewTouch) getView(prePosition);
if (preImageView != null) {
preImageView.setImageBitmapResetBase(
preImageView.mBitmapDisplayed.getBitmap(), true);
}
Log.d(TAG, "onPageSelected: " + position);
if (mPreToast == null) {
mPreToast = Toast.makeText(OpenActivity.this,
String.format("%d/%d", position + 1, slideCount),
Toast.LENGTH_SHORT);
} else {
mPreToast.cancel();
mPreToast.setText(String.format("%d/%d", position + 1,
slideCount));
mPreToast.setDuration(Toast.LENGTH_SHORT);
}
mPreToast.show();
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
mOnPagerScoll = true;
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
mOnPagerScoll = true;
} else if (state == ViewPager.SCROLL_STATE_SETTLING) {
mOnPagerScoll = false;
} else {
mOnPagerScoll = false;
}
}
};
public ImageViewTouch getCurrentImageView() {
return (ImageViewTouch) getView(mViewPager.getCurrentItem());
}
private class MyGestureListener extends
GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// Logger.d(TAG, "gesture onScroll");
if (mOnScale) {
return true;
}
if (mPaused) {
return false;
}
ImageViewTouch imageView = getCurrentImageView();
if (imageView != null) {
imageView.panBy(-distanceX, -distanceY);
// 超出边界效果去掉这个
imageView.center(true, true);
}
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
if (mPaused) {
return false;
}
ImageViewTouch imageView = getCurrentImageView();
// Switch between the original scale and 3x scale.
if (imageView.mBaseZoom < 1) {
if (imageView.getScale() > 2F) {
imageView.zoomTo(1f);
} else {
imageView.zoomToPoint(3f, e.getX(), e.getY());
}
} else {
if (imageView.getScale() > (imageView.mMinZoom + imageView.mMaxZoom) / 2f) {
imageView.zoomTo(imageView.mMinZoom);
} else {
imageView.zoomToPoint(imageView.mMaxZoom, e.getX(),
e.getY());
}
}
return true;
}
}
private class MyOnScaleGestureListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
float currentScale;
float currentMiddleX;
float currentMiddleY;
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
final ImageViewTouch imageView = getCurrentImageView();
Log.d(TAG, "currentScale: " + currentScale + ", maxZoom: "
+ imageView.mMaxZoom);
if (currentScale > imageView.mMaxZoom) {
imageView
.zoomToNoCenterWithAni(currentScale
/ imageView.mMaxZoom, 1, currentMiddleX,
currentMiddleY);
currentScale = imageView.mMaxZoom;
imageView.zoomToNoCenterValue(currentScale, currentMiddleX,
currentMiddleY);
} else if (currentScale < imageView.mMinZoom) {
// imageView.zoomToNoCenterWithAni(currentScale,
// imageView.mMinZoom, currentMiddleX, currentMiddleY);
currentScale = imageView.mMinZoom;
imageView.zoomToNoCenterValue(currentScale, currentMiddleX,
currentMiddleY);
} else {
imageView.zoomToNoCenter(currentScale, currentMiddleX,
currentMiddleY);
}
imageView.center(true, true);
// NOTE: 延迟修正缩放后可能移动问题
imageView.postDelayed(new Runnable() {
@Override
public void run() {
mOnScale = false;
}
}, 300);
// Logger.d(TAG, "gesture onScaleEnd");
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
// Logger.d(TAG, "gesture onScaleStart");
mOnScale = true;
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector, float mx, float my) {
// Logger.d(TAG, "gesture onScale");
ImageViewTouch imageView = getCurrentImageView();
float ns = imageView.getScale() * detector.getScaleFactor();
currentScale = ns;
currentMiddleX = mx;
currentMiddleY = my;
if (detector.isInProgress()) {
imageView.zoomToNoCenter(ns, mx, my);
}
return true;
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent m) {
if (mPaused)
return true;
return super.dispatchTouchEvent(m);
}
private void setupOnTouchListeners(View rootView) {
mGestureDetector = new GestureDetector(this, new MyGestureListener(),
null, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
mScaleGestureDetector = new ScaleGestureDetector(this,
new MyOnScaleGestureListener());
}
OnTouchListener rootListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// NOTE: gestureDetector may handle onScroll..
if (!mOnScale) {
if (!mOnPagerScoll) {
try {
mGestureDetector.onTouchEvent(event);
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
if (!mOnPagerScoll) {
mScaleGestureDetector.onTouchEvent(event);
}
}
ImageViewTouch imageView = getCurrentImageView();
if (!mOnScale && imageView != null
&& imageView.mBitmapDisplayed != null
&& imageView.mBitmapDisplayed.getBitmap() != null) {
Matrix m = imageView.getImageViewMatrix();
RectF rect = new RectF(0, 0, imageView.mBitmapDisplayed
.getBitmap().getWidth(), imageView.mBitmapDisplayed
.getBitmap().getHeight());
m.mapRect(rect);
// Logger.d(TAG, "rect.right: " + rect.right +
// ", rect.left: "
// + rect.left + ", imageView.getWidth(): "
// + imageView.getWidth());
// 图片超出屏幕范围后移动
if (!(rect.right > imageView.getWidth() + 0.1 && rect.left < -0.1)) {
try {
mViewPager.onTouchEvent(event);
} catch (Exception e) {
// why?
e.printStackTrace();
}
}
}
// We do not use the return value of
// mGestureDetector.onTouchEvent because we will not receive
// the "up" event if we return false for the "down" event.
return true;
}
};
rootView.setOnTouchListener(rootListener);
}
@Override
public void onStart() {
super.onStart();
mPaused = false;
}
@Override
public void onStop() {
super.onStop();
mPaused = true;
}
@Override
protected void onDestroy() {
super.onDestroy();
ImageViewTouch imageView = getCurrentImageView();
if (imageView != null) {
imageView.mBitmapDisplayed.recycle();
imageView.clear();
}
ppt = null;
slide = null;
mPagerAdapter = null;
mViewPager = null;
}
}