1 package cz.cvut.fel.dce.qrscanner;
3 import android.content.Context;
4 import android.content.Intent;
5 import android.graphics.Bitmap;
6 import android.media.AudioManager;
7 import android.net.Uri;
8 import android.os.AsyncTask;
9 import android.support.v7.app.ActionBarActivity;
10 import android.os.Bundle;
11 import android.util.Log;
12 import android.view.Menu;
13 import android.view.MenuItem;
14 import android.view.View;
15 import android.view.ViewTreeObserver;
16 import android.widget.Button;
17 import android.widget.ImageView;
18 import android.widget.RelativeLayout;
19 import android.widget.Toast;
21 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFActivity;
22 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
23 import cz.cvut.fel.dce.qrscanner.pdfviewer.PdfPageView;
27 public class PreviewActivity extends ActionBarActivity implements ViewTreeObserver.OnGlobalLayoutListener {
28 public static final String TAG = "PreviewActivity";
29 public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
30 public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
31 public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
32 public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
33 public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
34 public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
35 public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
37 private ImageView mPreviewImg;
38 private RelativeLayout mProgressContainer;
39 private ViewTreeObserver mPreviewImgObserver;
40 private String mComponentId;
41 private String mComponentRootPath;
42 private PdfPageView mPdfView;
43 private boolean mPdfLoaded;
47 protected void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
49 setContentView(R.layout.activity_preview);
51 mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
52 mProgressContainer = (RelativeLayout) findViewById(R.id.progress_container);
53 if (mProgressContainer == null) {
54 Log.e(TAG, "Progress container was not found.");
58 if (mPreviewImg != null) {
59 mPreviewImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
60 mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
61 Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
62 mPreviewImgObserver.addOnGlobalLayoutListener(this);
64 Log.e(TAG, "ImageView for preview image could not be found in the resources.");
65 mPreviewImgObserver = null;
68 Intent intent = getIntent();
69 mComponentId = intent.getStringExtra("COMPONENT_ID");
71 if (mComponentId != null) {
72 Log.i(TAG, "Received component id: " + mComponentId);
73 mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
74 File rootPath = new File(mComponentRootPath);
75 if (!rootPath.isDirectory()) {
76 Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
81 Log.i(TAG, "No component id received");
87 protected void onDestroy() {
89 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
90 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
91 mPreviewImgObserver.removeOnGlobalLayoutListener(this);
96 public boolean onCreateOptionsMenu(Menu menu) {
97 // Inflate the menu; this adds items to the action bar if it is present.
102 public void onGlobalLayout() {
104 Log.d(TAG, "PDF file already loaded.");
108 String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
109 Log.i(TAG, "Path to component files: " + picturePath);
110 mPdfView = new PdfPageView(getApplicationContext(), picturePath);
112 new LoadPageTask().execute();
116 } catch (Exception e) {
117 Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
124 * Called when the user touches the button
126 public void showContacts(View view) {
127 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
131 * Called when the user touches the button
133 public void showManufacturing(View view) {
134 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
138 * Called when the user touches the button
140 public void showManufactImages(View view) {
141 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
145 * Called when the user touches the button
147 public void showManufactGuide(View view) {
148 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
151 private void showPDF(String filePath) {
152 Uri uri = Uri.parse(filePath);
153 Intent pdfIntent = new Intent(this, MuPDFActivity.class);
154 pdfIntent.setAction(Intent.ACTION_VIEW);
155 pdfIntent.setData(uri);
156 startActivity(pdfIntent);
159 private class LoadPageTask extends AsyncTask<Void, Void, Void> {
162 protected Void doInBackground(Void[] objects) {
168 protected void onPreExecute() {
169 super.onPreExecute();
170 Log.d(TAG, "Starting loading of the PDF page asynchronously.");
171 mProgressContainer.setVisibility(View.VISIBLE);
175 protected void onPostExecute(Void aVoid) {
176 super.onPostExecute(aVoid);
177 mProgressContainer.setVisibility(View.INVISIBLE);
178 mPreviewImg.setImageBitmap(mPdfView.getPageBitmap());
179 mPreviewImg.invalidate();
180 Log.d(TAG, "PDF page loaded.");