]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/glass/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java
dfd00c7afc1dcfedfdbba0d23ddca4ed2ed90c9e
[hornmich/skoda-qr-demo.git] / QRScanner / glass / src / main / java / cz / cvut / fel / dce / qrscanner / PreviewActivity.java
1 package cz.cvut.fel.dce.qrscanner;
2
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;
17
18 import java.io.File;
19
20 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
21
22
23 public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlobalLayoutListener{
24
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";
33
34         private ImageView mPreviewImg;
35         private ViewTreeObserver mPreviewImgObserver;
36         private String mComponentId;
37         private String mComponentRootPath;
38
39         @Override
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);
47
48                 if (mPreviewImg != null) {
49                         mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
50                         Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
51                         mPreviewImgObserver.addOnGlobalLayoutListener(this);
52                 }
53                 else {
54                         Log.e(TAG, "ImageView for preview image could not be found in the resources.");
55                         mPreviewImgObserver = null;
56                 }
57
58                 Intent intent = getIntent();
59                 mComponentId = intent.getStringExtra("COMPONENT_ID");
60
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);
67                                 toast.show();
68                                 finish();
69                         }
70                 }
71                 else {
72                         Log.i(TAG, "No component id received");
73                         finish();
74                 }
75         }
76
77         @Override
78         protected void onDestroy() {
79                 super.onDestroy();
80                 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
81                 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
82                         mPreviewImgObserver.removeOnGlobalLayoutListener(this);
83                 }
84         }
85
86         @Override
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);
90                 return true;
91         }
92
93         @Override
94         public void onGlobalLayout() {
95                 try {
96                         String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
97                         Log.i(TAG, "Path to component files: " + picturePath);
98
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);
112                         toast.show();
113                         e.printStackTrace();
114                 }
115         }
116
117         /** Called when the user touches the button */
118         public void showContacts() {
119                 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
120         }
121
122         /** Called when the user touches the button */
123         public void showManufacturing() {
124                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
125         }
126
127         /** Called when the user touches the button */
128         public void showManufactImages() {
129                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
130         }
131
132         /** Called when the user touches the button */
133         public void showManufactGuide() {
134                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
135         }
136
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);
143         }
144
145         @Override
146         public boolean onKeyDown(int keyCode, KeyEvent event) {
147                 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
148                         openOptionsMenu();
149                         return true;
150                 }
151                 return super.onKeyDown(keyCode, event);
152         }
153
154         @Override
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();
160
161                 if (id == R.id.show_contacts) {
162                         showContacts();
163                         return true;
164                 }
165                 else if (id == R.id.show_manufacturing) {
166                         showManufacturing();
167                         return true;
168                 }
169                 else if (id == R.id.show_pictured_manufacturing) {
170                         showManufactImages();
171                         return true;
172                 }
173                 else if (id == R.id.show_workshop) {
174                         showManufactGuide();
175                         return true;
176                 }
177                 else if (id == R.id.stop) {
178                         finish();
179                         return true;
180                 }
181                 else {
182                         return super.onOptionsItemSelected(item);
183                 }
184         }
185 }