Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. package com.artifex.mupdf;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Environment;
  6. import android.view.*;
  7. import android.view.View.OnClickListener;
  8. import android.widget.*;
  9. import android.widget.LinearLayout.*;
  10. import java.io.File;
  11.  
  12. import com.artifex.mupdf.PixmapView;
  13.  
  14. public class MuPDFActivity extends Activity
  15. {
  16.         /* The core rendering instance */
  17.         private MuPDFCore core;
  18.  
  19.         private MuPDFCore openFile()
  20.         {
  21.                 String storageState = Environment.getExternalStorageState();
  22.                 File path, file;
  23.                 MuPDFCore core;
  24.  
  25.                 if (Environment.MEDIA_MOUNTED.equals(storageState))
  26.                 {
  27.                         System.out.println("Media mounted read/write");
  28.                 }
  29.                 else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(storageState))
  30.                 {
  31.                         System.out.println("Media mounted read only");
  32.                 }
  33.                 else
  34.                 {
  35.                         System.out.println("No media at all! Bale!\n");
  36.                         return null;
  37.                 }
  38.                 path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
  39.                 file = new File(path, "test.pdf");
  40.                 System.out.println("Trying to open "+file.toString());
  41.                 try
  42.                 {
  43.                         core = new MuPDFCore(file.toString());
  44.                 }
  45.                 catch (Exception e)
  46.                 {
  47.                         System.out.println(e);
  48.                         return null;
  49.                 }
  50.                 return core;
  51.         }
  52.  
  53.         /** Called when the activity is first created. */
  54.         @Override
  55.         public void onCreate(Bundle savedInstanceState)
  56.         {
  57.                 PixmapView pixmapView;
  58.  
  59.                 if (core == null) {
  60.                         core = (MuPDFCore)getLastNonConfigurationInstance();
  61.                 }
  62.                 if (core == null) {
  63.                         core = openFile();
  64.                 }
  65.                 if (core == null)
  66.                 {
  67.                         /* FIXME: Error handling here! */
  68.                         return;
  69.                 }
  70.  
  71.                 pixmapView = new PixmapView(this, core);
  72.                 super.onCreate(savedInstanceState);
  73.  
  74.                 /* Now create the UI */
  75.                 RelativeLayout layout;
  76.                 LinearLayout bar;
  77.                 MyButtonHandler bh = new MyButtonHandler(pixmapView);
  78.  
  79.                 bar = new LinearLayout(this);
  80.                 bar.setOrientation(LinearLayout.HORIZONTAL);
  81.                 bh.buttonStart = new Button(this);
  82.                 bh.buttonStart.setText("<<");
  83.                 bh.buttonStart.setOnClickListener(bh);
  84.                 bar.addView(bh.buttonStart);
  85.                 bh.buttonPrev = new Button(this);
  86.                 bh.buttonPrev.setText("<");
  87.                 bh.buttonPrev.setOnClickListener(bh);
  88.                 bar.addView(bh.buttonPrev);
  89.                 bh.buttonNext = new Button(this);
  90.                 bh.buttonNext.setText(">");
  91.                 bh.buttonNext.setOnClickListener(bh);
  92.                 bar.addView(bh.buttonNext);
  93.                 bh.buttonEnd = new Button(this);
  94.                 bh.buttonEnd.setText(">>");
  95.                 bh.buttonEnd.setOnClickListener(bh);
  96.                 bar.addView(bh.buttonEnd);
  97.  
  98.                 layout = new RelativeLayout(this);
  99.                 layout.setLayoutParams(new RelativeLayout.LayoutParams(
  100.                                         RelativeLayout.LayoutParams.FILL_PARENT,
  101.                                         RelativeLayout.LayoutParams.FILL_PARENT));
  102.                 layout.setGravity(Gravity.FILL);
  103.  
  104.                 RelativeLayout.LayoutParams barParams =
  105.                         new RelativeLayout.LayoutParams(
  106.                                         RelativeLayout.LayoutParams.FILL_PARENT,
  107.                                         RelativeLayout.LayoutParams.WRAP_CONTENT);
  108.                 barParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  109.                 bar.setId(100);
  110.                 layout.addView(bar, barParams);
  111.  
  112.                 RelativeLayout.LayoutParams pixmapParams =
  113.                         new RelativeLayout.LayoutParams(
  114.                                         RelativeLayout.LayoutParams.FILL_PARENT,
  115.                                         RelativeLayout.LayoutParams.FILL_PARENT);
  116.                 pixmapParams.addRule(RelativeLayout.ABOVE,100);
  117.                 layout.addView(pixmapView, pixmapParams);
  118.  
  119.                 setContentView(layout);
  120.         }
  121.  
  122.         public Object onRetainNonConfigurationInstance()
  123.         {
  124.                 MuPDFCore mycore = core;
  125.                 core = null;
  126.                 return mycore;
  127.         }
  128.  
  129.         public void onDestroy()
  130.         {
  131.                 if (core != null)
  132.                         core.onDestroy();
  133.                 core = null;
  134.                 super.onDestroy();
  135.         }
  136.  
  137.         private class MyButtonHandler implements OnClickListener
  138.         {
  139.                 Button buttonStart;
  140.                 Button buttonPrev;
  141.                 Button buttonNext;
  142.                 Button buttonEnd;
  143.                 PixmapView pixmapView;
  144.  
  145.                 public MyButtonHandler(PixmapView pixmapView)
  146.                 {
  147.                         this.pixmapView = pixmapView;
  148.                 }
  149.  
  150.                 public void onClick(View v)
  151.                 {
  152.                         if (v == buttonStart)
  153.                                 pixmapView.changePage(Integer.MIN_VALUE);
  154.                         else if (v == buttonPrev)
  155.                                 pixmapView.changePage(-1);
  156.                         else if (v == buttonNext)
  157.                                 pixmapView.changePage(+1);
  158.                         else if (v == buttonEnd)
  159.                                 pixmapView.changePage(Integer.MAX_VALUE);
  160.                 }
  161.         }
  162. }
  163.