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 if (mPreviewImg != null) {
34 mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
35 Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
36 mPreviewImgObserver.addOnGlobalLayoutListener(this);
39 Log.e(TAG, "ImageView for preview image could not be found in the resources.");
40 mPreviewImgObserver = null;
43 Intent intent = getIntent();
44 mComponentId = intent.getStringExtra("COMPONENT_ID");
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);
57 Log.i(TAG, "No component id received");
63 protected void onDestroy() {
65 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
66 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
67 mPreviewImgObserver.removeOnGlobalLayoutListener(this);
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);
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();
85 //noinspection SimplifiableIfStatement
86 if (id == R.id.action_settings) {
90 return super.onOptionsItemSelected(item);
94 public void onGlobalLayout() {
96 String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
97 Log.i(TAG, "Path to component files: " + picturePath);
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);