]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/glass/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java
5ae8e5954ea8226b66586c58a7560f39ee5c6839
[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.net.Uri;
7 import android.os.Bundle;
8 import android.util.Log;
9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.view.ViewTreeObserver;
13 import android.widget.ImageView;
14 import android.widget.Toast;
15
16 import java.io.File;
17
18 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFActivity;
19 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
20
21
22 public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlobalLayoutListener{
23
24         public static final String TAG = "PreviewActivity";
25         public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
26         public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
27         public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
28         public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
29         public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
30         public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
31         public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
32
33         private ImageView mPreviewImg;
34         private ViewTreeObserver mPreviewImgObserver;
35         private String mComponentId;
36         private String mComponentRootPath;
37
38         @Override
39         protected void onCreate(Bundle savedInstanceState) {
40                 super.onCreate(savedInstanceState);
41                 setContentView(R.layout.activity_preview);
42                 mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
43                 if (mPreviewImg != null) {
44                         mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
45                         Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
46                         mPreviewImgObserver.addOnGlobalLayoutListener(this);
47                 }
48                 else {
49                         Log.e(TAG, "ImageView for preview image could not be found in the resources.");
50                         mPreviewImgObserver = null;
51                 }
52
53                 Intent intent = getIntent();
54                 mComponentId = intent.getStringExtra("COMPONENT_ID");
55
56                 if (mComponentId != null) {
57                         Log.i(TAG, "Received component id: " + mComponentId);
58                         mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
59                         File rootPath = new File(mComponentRootPath);
60                         if (!rootPath.isDirectory()) {
61                                 Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
62                                 toast.show();
63                                 finish();
64                         }
65                 }
66                 else {
67                         Log.i(TAG, "No component id received");
68                         finish();
69                 }
70         }
71
72         @Override
73         protected void onDestroy() {
74                 super.onDestroy();
75                 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
76                 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
77                         mPreviewImgObserver.removeOnGlobalLayoutListener(this);
78                 }
79         }
80
81         @Override
82         public boolean onCreateOptionsMenu(Menu menu) {
83                 // Inflate the menu; this adds items to the action bar if it is present.
84                 getMenuInflater().inflate(R.menu.menu_preview, menu);
85                 return true;
86         }
87
88         @Override
89         public void onGlobalLayout() {
90                 try {
91                         String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
92                         Log.i(TAG, "Path to component files: " + picturePath);
93
94                         MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
95                         MuPDFCore.Cookie cookie = core.new Cookie();
96                         int previewW = mPreviewImg.getWidth();
97                         int previewH = mPreviewImg.getHeight();
98                         Bitmap.Config conf = Bitmap.Config.ARGB_8888;
99                         Bitmap previewBitmap = Bitmap.createBitmap(previewW, previewH, conf);
100                         core.updatePage(previewBitmap, 0, previewW, previewH, 0, 0, previewW, previewH, cookie);
101                         mPreviewImg.setImageBitmap(previewBitmap);
102                 } catch (Exception e) {
103                         Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
104                         toast.show();
105                         e.printStackTrace();
106                 }
107         }
108
109         /** Called when the user touches the button */
110         public void showContacts(View view) {
111                 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
112         }
113
114         /** Called when the user touches the button */
115         public void showManufacturing(View view) {
116                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
117         }
118
119         /** Called when the user touches the button */
120         public void showManufactImages(View view) {
121                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
122         }
123
124         /** Called when the user touches the button */
125         public void showManufactGuide(View view) {
126                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
127         }
128
129         private void showPDF(String filePath) {
130                 Uri uri = Uri.parse(filePath);
131                 Intent pdfIntent = new Intent(this,MuPDFActivity.class);
132                 pdfIntent.setAction(Intent.ACTION_VIEW);
133                 pdfIntent.setData(uri);
134                 startActivity(pdfIntent);
135         }
136         @Override
137         public boolean onOptionsItemSelected(MenuItem item) {
138                 // Handle action bar item clicks here. The action bar will
139                 // automatically handle clicks on the Home/Up button, so long
140                 // as you specify a parent activity in AndroidManifest.xml.
141                 int id = item.getItemId();
142
143                 //noinspection SimplifiableIfStatement
144                 if (id == R.id.action_settings) {
145                         return true;
146                 }
147
148                 return super.onOptionsItemSelected(item);
149         }
150 }