Example #1
0
 private void reflowModeSet(boolean reflow) {
   mReflow = reflow;
   mDocView.setAdapter(
       mReflow ? new MuPDFReflowAdapter(this, core) : new MuPDFPageAdapter(this, this, core));
   mReflowButton.setColorFilter(
       mReflow ? Color.argb(0xFF, 172, 114, 37) : Color.argb(0xFF, 255, 255, 255));
   setButtonEnabled(mAnnotButton, !reflow);
   setButtonEnabled(mSearchButton, !reflow);
   if (reflow) setLinkHighlight(false);
   setButtonEnabled(mLinkButton, !reflow);
   setButtonEnabled(mMoreButton, !reflow);
   mDocView.refresh(mReflow);
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout mContentView = (RelativeLayout) findViewById(R.id.content_view);

    try {
      InputStream ins = getResources().getAssets().open("MuPdf.pdf");
      FileOutputStream fos = new FileOutputStream(new File(getExternalCacheDir() + "/1.pdf"));
      byte[] b = new byte[1024];
      while (ins.read(b) != -1) {
        fos.write(b);
      }
      fos.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }

    Uri uri = Uri.parse(getExternalCacheDir() + "/1.pdf");
    core = openFile(uri.getPath());
    if (core != null && core.countPages() == 0) {
      core = null;
    }
    if (core == null || core.countPages() == 0 || core.countPages() == -1) {
      Log.e(TAG, "Document Not Opening");
    }
    if (core != null) {
      MuPDFReaderView mDocView =
          new MuPDFReaderView(this) {
            @Override
            protected void onMoveToChild(int i) {
              if (core == null) return;
              super.onMoveToChild(i);
            }
          };
      mDocView.setAdapter(new MuPDFPageAdapter(this, null, core));
      mContentView.addView(mDocView);
    }
  }
Example #3
0
  public void createUI(Bundle savedInstanceState) {
    if (core == null) return;

    // Now create the UI.
    // First create the document view
    mDocView =
        new MuPDFReaderView(this) {
          @Override
          protected void onMoveToChild(int i) {
            if (core == null) return;

            mPageNumberView.setText(String.format("%d / %d", i + 1, core.countPages()));
            mPageSlider.setMax((core.countPages() - 1) * mPageSliderRes);
            mPageSlider.setProgress(i * mPageSliderRes);
            super.onMoveToChild(i);
          }

          @Override
          protected void onTapMainDocArea() {
            if (!mButtonsVisible) {
              showButtons();
            } else {
              if (mTopBarMode == TopBarMode.Main) hideButtons();
            }
          }

          @Override
          protected void onDocMotion() {
            hideButtons();
          }

          @Override
          protected void onHit(Hit item) {
            switch (mTopBarMode) {
              case Annot:
                if (item == Hit.Annotation) {
                  showButtons();
                  mTopBarMode = TopBarMode.Delete;
                  mTopBarSwitcher.setDisplayedChild(mTopBarMode.ordinal());
                }
                break;
              case Delete:
                mTopBarMode = TopBarMode.Annot;
                mTopBarSwitcher.setDisplayedChild(mTopBarMode.ordinal());
                // fall through
              default:
                // Not in annotation editing mode, but the pageview will
                // still select and highlight hit annotations, so
                // deselect just in case.
                MuPDFView pageView = (MuPDFView) mDocView.getDisplayedView();
                if (pageView != null) pageView.deselectAnnotation();
                break;
            }
          }
        };
    mDocView.setAdapter(new MuPDFPageAdapter(this, this, core));

    mSearchTask =
        new SearchTask(this, core) {
          @Override
          protected void onTextFound(SearchTaskResult result) {
            SearchTaskResult.set(result);
            // Ask the ReaderView to move to the resulting page
            mDocView.setDisplayedViewIndex(result.pageNumber);
            // Make the ReaderView act on the change to SearchTaskResult
            // via overridden onChildSetup method.
            mDocView.resetupChildren();
          }
        };

    // Make the buttons overlay, and store all its
    // controls in variables
    makeButtonsView();

    // Set up the page slider
    int smax = Math.max(core.countPages() - 1, 1);
    mPageSliderRes = ((10 + smax - 1) / smax) * 2;

    // Set the file-name text
    mFilenameView.setText(mFileName);

    // Activate the seekbar
    mPageSlider.setOnSeekBarChangeListener(
        new SeekBar.OnSeekBarChangeListener() {
          public void onStopTrackingTouch(SeekBar seekBar) {
            mDocView.setDisplayedViewIndex(
                (seekBar.getProgress() + mPageSliderRes / 2) / mPageSliderRes);
          }

          public void onStartTrackingTouch(SeekBar seekBar) {}

          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            updatePageNumView((progress + mPageSliderRes / 2) / mPageSliderRes);
          }
        });

    // Activate the search-preparing button
    mSearchButton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            searchModeOn();
          }
        });

    // Activate the reflow button
    mReflowButton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            toggleReflow();
          }
        });

    if (core.fileFormat().startsWith("PDF")
        && core.isUnencryptedPDF()
        && !core.wasOpenedFromBuffer()) {
      mAnnotButton.setOnClickListener(
          new View.OnClickListener() {
            public void onClick(View v) {
              mTopBarMode = TopBarMode.Annot;
              mTopBarSwitcher.setDisplayedChild(mTopBarMode.ordinal());
            }
          });
    } else {
      mAnnotButton.setVisibility(View.GONE);
    }

    // Search invoking buttons are disabled while there is no text specified
    mSearchBack.setEnabled(false);
    mSearchFwd.setEnabled(false);
    mSearchBack.setColorFilter(Color.argb(255, 128, 128, 128));
    mSearchFwd.setColorFilter(Color.argb(255, 128, 128, 128));

    // React to interaction with the text widget
    mSearchText.addTextChangedListener(
        new TextWatcher() {

          public void afterTextChanged(Editable s) {
            boolean haveText = s.toString().length() > 0;
            setButtonEnabled(mSearchBack, haveText);
            setButtonEnabled(mSearchFwd, haveText);

            // Remove any previous search results
            if (SearchTaskResult.get() != null
                && !mSearchText.getText().toString().equals(SearchTaskResult.get().txt)) {
              SearchTaskResult.set(null);
              mDocView.resetupChildren();
            }
          }

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          public void onTextChanged(CharSequence s, int start, int before, int count) {}
        });

    // React to Done button on keyboard
    mSearchText.setOnEditorActionListener(
        new TextView.OnEditorActionListener() {
          public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) search(1);
            return false;
          }
        });

    mSearchText.setOnKeyListener(
        new View.OnKeyListener() {
          public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER)
              search(1);
            return false;
          }
        });

    // Activate search invoking buttons
    mSearchBack.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            search(-1);
          }
        });
    mSearchFwd.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            search(1);
          }
        });

    mLinkButton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            setLinkHighlight(!mLinkHighlight);
          }
        });

    if (core.hasOutline()) {
      mOutlineButton.setOnClickListener(
          new View.OnClickListener() {
            public void onClick(View v) {
              OutlineItem outline[] = core.getOutline();
              if (outline != null) {
                OutlineActivityData.get().items = outline;
                Intent intent = new Intent(MuPDFActivity.this, OutlineActivity.class);
                intent.putExtra("cp", mDocView.getDisplayedViewIndex());
                startActivityForResult(intent, OUTLINE_REQUEST);
              }
            }
          });
    } else {
      mOutlineButton.setVisibility(View.GONE);
    }

    // Reenstate last state if it was recorded
    SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
    mDocView.setDisplayedViewIndex(prefs.getInt("page" + mFileName, 0));

    if (savedInstanceState == null || !savedInstanceState.getBoolean("ButtonsHidden", false))
      showButtons();

    if (savedInstanceState != null && savedInstanceState.getBoolean("SearchMode", false))
      searchModeOn();

    if (savedInstanceState != null && savedInstanceState.getBoolean("ReflowMode", false))
      reflowModeSet(true);

    // Stick the document view and the buttons overlay into a parent view
    RelativeLayout layout = new RelativeLayout(this);
    layout.addView(mDocView);
    layout.addView(mButtonsView);
    setContentView(layout);

    if (isProofing()) {
      //  go to the current page
      int currentPage = getIntent().getIntExtra("startingPage", 0);
      mDocView.setDisplayedViewIndex(currentPage);
    }
  }