1 package cz.cvut.fel.dce.qrscanner;
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;
14 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
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";
23 private ImageView mPreviewImg;
24 private ViewTreeObserver mPreviewImgObserver;
25 private String mComponentId;
26 private String mComponentRootPath;
29 protected void onCreate(Bundle savedInstanceState) {
30 super.onCreate(savedInstanceState);
31 setContentView(R.layout.activity_preview);
32 mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
33 mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
34 Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
35 mPreviewImgObserver.addOnGlobalLayoutListener(this);
37 Intent intent = getIntent();
38 mComponentId = intent.getStringExtra("COMPONENT_ID");
40 if (mComponentId != null) {
41 Log.i(TAG, "Received component id: " + mComponentId);
42 mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
43 File rootPath = new File(mComponentRootPath);
44 if (!rootPath.isDirectory()) {
45 Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
51 Log.i(TAG, "No component id received");
57 protected void onDestroy() {
59 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
60 if (mPreviewImgObserver.isAlive()) {
61 mPreviewImgObserver.removeOnGlobalLayoutListener(this);
66 public boolean onCreateOptionsMenu(Menu menu) {
67 // Inflate the menu; this adds items to the action bar if it is present.
68 getMenuInflater().inflate(R.menu.menu_preview, menu);
73 public boolean onOptionsItemSelected(MenuItem item) {
74 // Handle action bar item clicks here. The action bar will
75 // automatically handle clicks on the Home/Up button, so long
76 // as you specify a parent activity in AndroidManifest.xml.
77 int id = item.getItemId();
79 //noinspection SimplifiableIfStatement
80 if (id == R.id.action_settings) {
84 return super.onOptionsItemSelected(item);
88 public void onGlobalLayout() {
89 if (mPreviewImg != null) {
91 String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
92 Log.i(TAG, "Path to component files: " + picturePath);
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);
109 Log.e(TAG, "ImageView for preview image could not be found in the resources.");