]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java
Cover exception when no preview image view is found
[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.Intent;
4 import android.graphics.Bitmap;
5 import android.support.v7.app.ActionBarActivity;
6 import android.os.Bundle;
7 import android.util.Log;
8 import android.view.Menu;
9 import android.view.MenuItem;
10 import android.view.ViewTreeObserver;
11 import android.widget.ImageView;
12 import android.widget.Toast;
13
14 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
15 import java.io.File;
16
17 public class PreviewActivity extends ActionBarActivity implements ViewTreeObserver.OnGlobalLayoutListener {
18         public static final String TAG = "PreviewActivity";
19         public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
20         public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
21         public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
22
23         private ImageView mPreviewImg;
24         private ViewTreeObserver mPreviewImgObserver;
25         private String mComponentId;
26         private String mComponentRootPath;
27
28         @Override
29         protected void onCreate(Bundle savedInstanceState) {
30                 super.onCreate(savedInstanceState);
31                 setContentView(R.layout.activity_preview);
32                 mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
33                 if (mPreviewImg != null) {
34                         mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
35                         Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
36                         mPreviewImgObserver.addOnGlobalLayoutListener(this);
37                 }
38                 else {
39                         Log.e(TAG, "ImageView for preview image could not be found in the resources.");
40                         mPreviewImgObserver = null;
41                 }
42
43                 Intent intent = getIntent();
44                 mComponentId = intent.getStringExtra("COMPONENT_ID");
45
46                 if (mComponentId != null) {
47                         Log.i(TAG, "Received component id: " + mComponentId);
48                         mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
49                         File rootPath = new File(mComponentRootPath);
50                         if (!rootPath.isDirectory()) {
51                                 Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
52                                 toast.show();
53                                 finish();
54                         }
55                 }
56                 else {
57                         Log.i(TAG, "No component id received");
58                         finish();
59                 }
60         }
61
62         @Override
63         protected void onDestroy() {
64                 super.onDestroy();
65                 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
66                 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
67                         mPreviewImgObserver.removeOnGlobalLayoutListener(this);
68                 }
69         }
70
71         @Override
72         public boolean onCreateOptionsMenu(Menu menu) {
73                 // Inflate the menu; this adds items to the action bar if it is present.
74                 getMenuInflater().inflate(R.menu.menu_preview, menu);
75                 return true;
76         }
77
78         @Override
79         public boolean onOptionsItemSelected(MenuItem item) {
80                 // Handle action bar item clicks here. The action bar will
81                 // automatically handle clicks on the Home/Up button, so long
82                 // as you specify a parent activity in AndroidManifest.xml.
83                 int id = item.getItemId();
84
85                 //noinspection SimplifiableIfStatement
86                 if (id == R.id.action_settings) {
87                         return true;
88                 }
89
90                 return super.onOptionsItemSelected(item);
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                         MuPDFCore.Cookie cookie = core.new Cookie();
101                         int previewW = mPreviewImg.getWidth();
102                         int previewH = mPreviewImg.getHeight();
103                         Bitmap.Config conf = Bitmap.Config.ARGB_8888;
104                         Bitmap previewBitmap = Bitmap.createBitmap(previewW, previewH, conf);
105                         core.updatePage(previewBitmap, 0, previewW, previewH, 0, 0, previewW, previewH, cookie);
106                         mPreviewImg.setImageBitmap(previewBitmap);
107                 } catch (Exception e) {
108                         Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
109                         toast.show();
110                         e.printStackTrace();
111                 }
112         }
113 }