@Override
  protected void onStart() {
    super.onStart();

    mIsActivityVisible = true;

    if (mAllShareService == null) {
      System.out.println("mAllShareService == null");
    }
    if (mAllShareServiceListener == null) {
      System.out.println("mAllShareServiceListener == null");
    }
    if (mDevicePicker == null) {
      System.out.println("mDevicePicker == null");
      mDevicePicker = new DevicePicker();
      //   mDevicePicker = (DevicePicker)
      // getSupportFragmentManager().findFragmentById(R.id.devicePicker);
    }
    if (mDevicePicker == null) {
      System.out.println("mDevicePicker == null");
      //  mDevicePicker = new DevicePicker();
    }

    mAllShareService.registerListener(mAllShareServiceListener);
    mAllShareService.init(getApplicationContext());
  }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   mAllShareService.stop();
   if (mTmpFile != null) {
     mTmpFile.delete();
     mTmpFile = null;
   }
 }
  /** Abstracts call to ViewController.zoom() method. */
  private void scaleTv(float scaleFactorDelta, float scalePointX, float scalePointY) {
    if (mAllShareService.isImageReady()) {
      int tvScalePointX = (int) scalePointX;
      int tvScalePointY = (int) scalePointY;
      int tvScaleFactorDelta = (int) (scaleFactorDelta * 100);

      int rotation = mPinchToZoomTransformation.getRotation();

      mAllShareService
          .getViewController()
          .zoom(
              tvScalePointX,
              tvScalePointY,
              tvScaleFactorDelta,
              rotation,
              mImageViewWidth,
              mImageViewHeight);
    }
  }
  private void showPage(int page, float zoom) throws Exception {
    // long startTime = System.currentTimeMillis();
    // long middleTime = startTime;
    try {
      // free memory from previous page
      mGraphView.setPageBitmap(null);
      mGraphView.updateImage();

      // Only load the page if it's a different page (i.e. not just changing the zoom level)
      if (mPdfPage == null || mPdfPage.getPageNumber() != page) {
        mPdfPage = mPdfFile.getPage(page, true);
      }
      // int num = mPdfPage.getPageNumber();
      // int maxNum = mPdfFile.getNumPages();
      float width = mPdfPage.getWidth();
      float height = mPdfPage.getHeight();
      // String pageInfo= new File(pdffilename).getName() + " - " + num +"/"+maxNum+ ": " + width +
      // "x" + height;
      // mGraphView.showText(pageInfo);
      // Log.i(TAG, pageInfo);
      RectF clip = null;
      // middleTime = System.currentTimeMillis();
      Bitmap bi = mPdfPage.getImage((int) (width * zoom), (int) (height * zoom), clip, true, true);
      mGraphView.setPageBitmap(bi);
      mGraphView.updateImage();

      writeTempFileToDisc(bi);
      //   bi.recycle();//ADDED FOR EFFICIENCY

      File dir = Environment.getExternalStorageDirectory();
      File file = new File(dir, IMAGE_PATH + mPage + ".jpg");
      mImageUri = Uri.fromFile(file);
      System.out.println("Displaying " + mImageUri.getPath());
      mAllShareService.start(mImageUri.getPath());

      if (progress != null) progress.dismiss();
    } catch (Throwable e) {
      Log.e(TAG, e.getMessage(), e);
      mGraphView.showText("Exception: " + e.getMessage());
    }
    // long stopTime = System.currentTimeMillis();
    // mGraphView.pageParseMillis = middleTime-startTime;
    // mGraphView.pageRenderMillis = stopTime-middleTime;
  }
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.i(TAG, "onCreate");

    // progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page");
    /*closeNavigationHandler = new Handler();
    closeNavigationThread = new Thread(new Runnable() {

    	public void run() {
    		navigationPanel.startAnimation(AnimationUtils.loadAnimation(PdfViewerActivity.this,
    				R.anim.slide_out));
    		navigationPanel.setVisibility(View.GONE);
    	}
    });*/

    /*if (navigationPanel == null) {
    	navigationPanel = ((ViewStub) findViewById(R.id.stub_navigation)).inflate();
    	navigationPanel.setVisibility(View.GONE);
    	ImageButton previous = (ImageButton)navigationPanel.findViewById(R.id.navigation_previous);
    	previous.setBackgroundDrawable(null);
    }*/

    uiHandler = new Handler();
    restoreInstance();

    mAllShareService = new AllShareService(getApplicationContext());

    if (mOldGraphView != null) {

      System.out.println("mOldGraphView != null");
      mGraphView = new GraphView(this);
      // mGraphView.fileMillis = mOldGraphView.fileMillis;
      mGraphView.mBi = mOldGraphView.mBi;
      // mGraphView.mLine1 = mOldGraphView.mLine1;
      // mGraphView.mLine2 = mOldGraphView.mLine2;
      // mGraphView.mLine3 = mOldGraphView.mLine3;
      // mGraphView.mText = mOldGraphView.mText;
      // mGraphView.pageParseMillis= mOldGraphView.pageParseMillis;
      // mGraphView.pageRenderMillis= mOldGraphView.pageRenderMillis;
      mOldGraphView = null;
      mGraphView.mImageView.setImageBitmap(mGraphView.mBi);
      mGraphView.updateTexts();

      setContentView(mGraphView);

      // *********************************ALLSHARE****************************

      findUiElements();

      // checks if image to show exists; if not: create it
      copyAssets();

      // set the image to be shown
      File dir = Environment.getExternalStorageDirectory();
      File file = new File(dir, IMAGE_PATH);
      mImageUri = Uri.fromFile(file);

      if (IMAGE_PATH != null) {
        System.out.println("PDFFILE IMAGE_PATH " + IMAGE_PATH);
      }
      if (mImageUri != null) {
        System.out.println("\n mImageURI " + mImageUri);
      } else {
        System.out.println("MIMAGEURI IS NULL");
      }

      System.out.println("DIR " + dir.getAbsolutePath());
      if (file != null) {
        System.out.println("\n file " + file.getAbsolutePath());
      }

      mImageView = (ImageView) findViewById(R.id.pdf_image);
      if (mImageView == null) {
        System.out.println("IMAGEVIEW NULL");
      } else {
        System.out.println("IMAGE VIEW NOT NULL");
      }
      mImageView.setImageBitmap(decodeImage(file));

      mAllShareService = AllShareService.getInstance();

      if (mAllShareService == null) {
        System.out.println("mAllShareService == null");
      }

      mAllShareService.init(getApplicationContext());

      // listens for layout measurements finish
      ViewTreeObserver viewTreeObserver = mImageView.getViewTreeObserver();
      if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
              @Override
              public void onGlobalLayout() {
                initialize();
                System.out.println("GlobalLayout Listener");
                scaleTv(0, mImageViewWidth / 2.0f, mImageViewHeight / 2.0f);
              }
            });
      }

      // *********************************ALLSHARE****************************

    } else {
      System.out.println("mOldGraphView == null");
      mGraphView = new GraphView(this);
      Intent intent = getIntent();
      Log.i(TAG, "" + intent);

      boolean showImages =
          getIntent()
              .getBooleanExtra(
                  PdfViewerActivity.EXTRA_SHOWIMAGES, PdfViewerActivity.DEFAULTSHOWIMAGES);
      PDFImage.sShowImages = showImages;
      boolean antiAlias =
          getIntent()
              .getBooleanExtra(
                  PdfViewerActivity.EXTRA_ANTIALIAS, PdfViewerActivity.DEFAULTANTIALIAS);
      PDFPaint.s_doAntiAlias = antiAlias;
      boolean useFontSubstitution =
          getIntent()
              .getBooleanExtra(
                  PdfViewerActivity.EXTRA_USEFONTSUBSTITUTION,
                  PdfViewerActivity.DEFAULTUSEFONTSUBSTITUTION);
      PDFFont.sUseFontSubstitution = useFontSubstitution;
      boolean keepCaches =
          getIntent()
              .getBooleanExtra(
                  PdfViewerActivity.EXTRA_KEEPCACHES, PdfViewerActivity.DEFAULTKEEPCACHES);
      HardReference.sKeepCaches = keepCaches;

      if (intent != null) {
        if ("android.intent.action.VIEW".equals(intent.getAction())) {
          pdffilename = storeUriContentToFile(intent.getData());
        } else {
          pdffilename = getIntent().getStringExtra(PdfViewerActivity.EXTRA_PDFFILENAME);
        }
      }

      if (pdffilename == null) pdffilename = "no file selected";

      mPage = STARTPAGE;
      mZoom = STARTZOOM;

      setContent(null);
    }
  }
 @Override
 protected void onStop() {
   super.onStop();
   mAllShareService.disconnect();
   cleanupTempFiles();
 }