1 package cz.cvut.fel.dce.qrscanner;
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.graphics.Bitmap;
6 import android.graphics.Color;
7 import android.net.Uri;
8 import android.os.Bundle;
9 import android.util.Log;
10 import android.view.KeyEvent;
11 import android.view.Menu;
12 import android.view.MenuItem;
13 import android.view.View;
14 import android.view.ViewTreeObserver;
15 import android.widget.ImageView;
16 import android.widget.Toast;
20 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
23 public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlobalLayoutListener{
25 public static final String TAG = "PreviewActivity";
26 public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
27 public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
28 public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
29 public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
30 public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
31 public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
32 public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
34 private ImageView mPreviewImg;
35 private ViewTreeObserver mPreviewImgObserver;
36 private String mComponentId;
37 private String mComponentRootPath;
40 protected void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42 setContentView(R.layout.activity_preview);
43 mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
44 mPreviewImg.setMaxWidth(640);
45 mPreviewImg.setMaxHeight(360);
46 mPreviewImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
48 if (mPreviewImg != null) {
49 mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
50 Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
51 mPreviewImgObserver.addOnGlobalLayoutListener(this);
54 Log.e(TAG, "ImageView for preview image could not be found in the resources.");
55 mPreviewImgObserver = null;
58 Intent intent = getIntent();
59 mComponentId = intent.getStringExtra("COMPONENT_ID");
61 if (mComponentId != null) {
62 Log.i(TAG, "Received component id: " + mComponentId);
63 mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
64 File rootPath = new File(mComponentRootPath);
65 if (!rootPath.isDirectory()) {
66 Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
72 Log.i(TAG, "No component id received");
78 protected void onDestroy() {
80 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
81 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
82 mPreviewImgObserver.removeOnGlobalLayoutListener(this);
87 public boolean onCreateOptionsMenu(Menu menu) {
88 // Inflate the menu; this adds items to the action bar if it is present.
89 getMenuInflater().inflate(R.menu.menu_preview, menu);
94 public void onGlobalLayout() {
96 String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
97 Log.i(TAG, "Path to component files: " + picturePath);
99 MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
100 Log.d(TAG, "numpages: "+ core.countPages());
101 MuPDFCore.Cookie cookie = core.new Cookie();
102 int pageW = (int)core.getPageSize(0).x;
103 int pageH = (int)core.getPageSize(0).y;
104 Log.d(TAG, "page size: " + pageW + ", " + pageH);
105 Bitmap.Config conf = Bitmap.Config.ARGB_8888;
106 Bitmap previewBitmap = Bitmap.createBitmap(pageW, pageH, conf);
107 core.drawPage(previewBitmap, 0, pageW, pageH,0 , 0, pageW, pageH, cookie);
108 mPreviewImg.setImageBitmap(previewBitmap);
109 mPreviewImg.invalidate();
110 } catch (Exception e) {
111 Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
117 /** Called when the user touches the button */
118 public void showContacts() {
119 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
122 /** Called when the user touches the button */
123 public void showManufacturing() {
124 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
127 /** Called when the user touches the button */
128 public void showManufactImages() {
129 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
132 /** Called when the user touches the button */
133 public void showManufactGuide() {
134 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
137 private void showPDF(String filePath) {
138 Uri uri = Uri.parse(filePath);
139 Intent pdfIntent = new Intent("com.artifex.mupdfdemo.VIEW");
140 //pdfIntent.setAction(Intent.ACTION_VIEW);
141 pdfIntent.setData(uri);
142 startActivity(pdfIntent);
146 public boolean onKeyDown(int keyCode, KeyEvent event) {
147 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
151 return super.onKeyDown(keyCode, event);
155 public boolean onOptionsItemSelected(MenuItem item) {
156 // Handle action bar item clicks here. The action bar will
157 // automatically handle clicks on the Home/Up button, so long
158 // as you specify a parent activity in AndroidManifest.xml.
159 int id = item.getItemId();
161 if (id == R.id.show_contacts) {
165 else if (id == R.id.show_manufacturing) {
169 else if (id == R.id.show_pictured_manufacturing) {
170 showManufactImages();
173 else if (id == R.id.show_workshop) {
177 else if (id == R.id.stop) {
182 return super.onOptionsItemSelected(item);