--- /dev/null
+package cz.cvut.fel.dce.qrscanner;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.graphics.Bitmap;
+import android.net.Uri;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewTreeObserver;
+import android.widget.ImageView;
+import android.widget.Toast;
+
+import java.io.File;
+
+import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFActivity;
+import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
+
+
+public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlobalLayoutListener{
+
+ public static final String TAG = "PreviewActivity";
+ public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
+ public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
+ public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
+ public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
+ public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
+ public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
+ public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
+
+ private ImageView mPreviewImg;
+ private ViewTreeObserver mPreviewImgObserver;
+ private String mComponentId;
+ private String mComponentRootPath;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_preview);
+ mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
+ if (mPreviewImg != null) {
+ mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
+ Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
+ mPreviewImgObserver.addOnGlobalLayoutListener(this);
+ }
+ else {
+ Log.e(TAG, "ImageView for preview image could not be found in the resources.");
+ mPreviewImgObserver = null;
+ }
+
+ Intent intent = getIntent();
+ mComponentId = intent.getStringExtra("COMPONENT_ID");
+
+ if (mComponentId != null) {
+ Log.i(TAG, "Received component id: " + mComponentId);
+ mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
+ File rootPath = new File(mComponentRootPath);
+ if (!rootPath.isDirectory()) {
+ Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
+ toast.show();
+ finish();
+ }
+ }
+ else {
+ Log.i(TAG, "No component id received");
+ finish();
+ }
+ }
+
+ @Override
+ protected void onDestroy() {
+ super.onDestroy();
+ Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
+ if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
+ mPreviewImgObserver.removeOnGlobalLayoutListener(this);
+ }
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ // Inflate the menu; this adds items to the action bar if it is present.
+ getMenuInflater().inflate(R.menu.menu_preview, menu);
+ return true;
+ }
+
+ @Override
+ public void onGlobalLayout() {
+ try {
+ String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
+ Log.i(TAG, "Path to component files: " + picturePath);
+
+ MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
+ MuPDFCore.Cookie cookie = core.new Cookie();
+ int previewW = mPreviewImg.getWidth();
+ int previewH = mPreviewImg.getHeight();
+ Bitmap.Config conf = Bitmap.Config.ARGB_8888;
+ Bitmap previewBitmap = Bitmap.createBitmap(previewW, previewH, conf);
+ core.updatePage(previewBitmap, 0, previewW, previewH, 0, 0, previewW, previewH, cookie);
+ mPreviewImg.setImageBitmap(previewBitmap);
+ } catch (Exception e) {
+ Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
+ toast.show();
+ e.printStackTrace();
+ }
+ }
+
+ /** Called when the user touches the button */
+ public void showContacts(View view) {
+ showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
+ }
+
+ /** Called when the user touches the button */
+ public void showManufacturing(View view) {
+ showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
+ }
+
+ /** Called when the user touches the button */
+ public void showManufactImages(View view) {
+ showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
+ }
+
+ /** Called when the user touches the button */
+ public void showManufactGuide(View view) {
+ showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
+ }
+
+ private void showPDF(String filePath) {
+ Uri uri = Uri.parse(filePath);
+ Intent pdfIntent = new Intent(this,MuPDFActivity.class);
+ pdfIntent.setAction(Intent.ACTION_VIEW);
+ pdfIntent.setData(uri);
+ startActivity(pdfIntent);
+ }
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ // Handle action bar item clicks here. The action bar will
+ // automatically handle clicks on the Home/Up button, so long
+ // as you specify a parent activity in AndroidManifest.xml.
+ int id = item.getItemId();
+
+ //noinspection SimplifiableIfStatement
+ if (id == R.id.action_settings) {
+ return true;
+ }
+
+ return super.onOptionsItemSelected(item);
+ }
+}