]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java
Implement preview by the PdfView widget
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / src / main / java / cz / cvut / fel / dce / qrscanner / PreviewActivity.java
1 package cz.cvut.fel.dce.qrscanner;
2
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;
20
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;
24
25 import java.io.File;
26
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";
36
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;
44
45
46         @Override
47         protected void onCreate(Bundle savedInstanceState) {
48                 super.onCreate(savedInstanceState);
49                 setContentView(R.layout.activity_preview);
50                 mPdfLoaded = false;
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.");
55                         finish();
56                 }
57
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);
63                 } else {
64                         Log.e(TAG, "ImageView for preview image could not be found in the resources.");
65                         mPreviewImgObserver = null;
66                 }
67
68                 Intent intent = getIntent();
69                 mComponentId = intent.getStringExtra("COMPONENT_ID");
70
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);
77                                 toast.show();
78                                 finish();
79                         }
80                 } else {
81                         Log.i(TAG, "No component id received");
82                         finish();
83                 }
84         }
85
86         @Override
87         protected void onDestroy() {
88                 super.onDestroy();
89                 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
90                 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
91                         mPreviewImgObserver.removeOnGlobalLayoutListener(this);
92                 }
93         }
94
95         @Override
96         public boolean onCreateOptionsMenu(Menu menu) {
97                 // Inflate the menu; this adds items to the action bar if it is present.
98                 return true;
99         }
100
101         @Override
102         public void onGlobalLayout() {
103                 if (mPdfLoaded) {
104                         Log.d(TAG, "PDF file already loaded.");
105                         return;
106                 }
107                 try {
108                         String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
109                         Log.i(TAG, "Path to component files: " + picturePath);
110                         mPdfView = new PdfPageView(getApplicationContext(), picturePath);
111                         mPdfView.setPage(0);
112                         new LoadPageTask().execute();
113
114                         mPdfLoaded = true;
115
116                 } catch (Exception e) {
117                         Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
118                         toast.show();
119                         e.printStackTrace();
120                 }
121         }
122
123         /**
124          * Called when the user touches the button
125          */
126         public void showContacts(View view) {
127                 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
128         }
129
130         /**
131          * Called when the user touches the button
132          */
133         public void showManufacturing(View view) {
134                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
135         }
136
137         /**
138          * Called when the user touches the button
139          */
140         public void showManufactImages(View view) {
141                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
142         }
143
144         /**
145          * Called when the user touches the button
146          */
147         public void showManufactGuide(View view) {
148                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
149         }
150
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);
157         }
158
159         private class LoadPageTask extends AsyncTask<Void, Void, Void> {
160
161                 @Override
162                 protected Void doInBackground(Void[] objects) {
163                         mPdfView.loadPage();
164                         return null;
165                 }
166
167                 @Override
168                 protected void onPreExecute() {
169                         super.onPreExecute();
170                         Log.d(TAG, "Starting loading of the PDF page asynchronously.");
171                         mProgressContainer.setVisibility(View.VISIBLE);
172                 }
173
174                 @Override
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.");
181                 }
182         }
183 }