PageRenderTime 106ms CodeModel.GetById 51ms app.highlight 35ms RepoModel.GetById 15ms app.codeStats 0ms

/apdfviewer/src/com/googlecode/apdfviewer/PDFViewerActivity.java

http://nookdevs.googlecode.com/
Java | 465 lines | 378 code | 52 blank | 35 comment | 30 complexity | ffa3e759367434bbc24046cb23068adf MD5 | raw file
  1/*
  2 * Copyright (C) 2009 Li Wenhao <liwenhao.g@gmail.com>
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *      http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16/**
 17 * Modified the apdfviewer to customize it for nook. - Hari.
 18 */
 19package com.googlecode.apdfviewer;
 20
 21import java.io.ObjectInputStream;
 22import java.io.ObjectOutputStream;
 23import java.io.Serializable;
 24
 25import android.content.Context;
 26import android.content.Intent;
 27import android.database.Cursor;
 28import android.media.MediaScannerConnection;
 29import android.media.MediaScannerConnection.MediaScannerConnectionClient;
 30import android.net.Uri;
 31import android.os.Bundle;
 32import android.os.Handler;
 33import android.util.Log;
 34import android.view.KeyEvent;
 35import android.view.View;
 36import android.widget.AdapterView;
 37import android.widget.ImageButton;
 38import android.widget.SeekBar;
 39import android.widget.Spinner;
 40import android.widget.TextView;
 41import android.widget.ViewAnimator;
 42import android.widget.AdapterView.OnItemSelectedListener;
 43import android.widget.SeekBar.OnSeekBarChangeListener;
 44
 45import com.googlecode.apdfviewer.PDFView.StatusListener;
 46import com.nookdevs.common.nookBaseActivity;
 47
 48/**
 49 * @author Li Wenhao
 50 */
 51public class PDFViewerActivity extends nookBaseActivity {
 52    // private final static String TAG = "PDFViewerActivity";
 53
 54    private static final int ABOUT = 1;
 55
 56    PDFView m_pdf_view;
 57    ViewAnimator m_animator;
 58    private SeekBar m_seek_view;
 59    TextView m_text;
 60    // TextView m_name;
 61    boolean m_landscapeleft = false;
 62    boolean m_landscaperight = false;
 63    private static final int SCROLL_PX_Y = 850;
 64    private static final int SCROLL_PX_X = 650;
 65    Handler m_Handler = new Handler();
 66    CurrentPageInfo pageInfo;
 67
 68    @Override
 69    public void onCreate(Bundle savedInstanceState) {
 70        super.onCreate(savedInstanceState);
 71
 72        setContentView(R.layout.main);
 73
 74        // Check the intent for the content to view
 75        Intent intent = getIntent();
 76        if (intent.getData() == null) { return; }
 77
 78        Uri uri = intent.getData();
 79        m_pdf_view = (PDFView) findViewById(R.id.view);
 80        String file = uri.getPath();
 81        new MediaScannerNotifier(file);
 82        initZoomSpinner();
 83        initButtons();
 84        initListener();
 85        m_pdf_view.openUri(uri);
 86        int idx = file.lastIndexOf('/');
 87        file = file.substring(idx + 1) + "userData";
 88        try {
 89            ObjectInputStream inp = new ObjectInputStream(openFileInput(file));
 90            pageInfo = (CurrentPageInfo) inp.readObject();
 91            inp.close();
 92            m_pdf_view.gotoPage(pageInfo.pageno);
 93            if (pageInfo.landscapeleft) {
 94                m_landscapeleft = true;
 95                m_pdf_view.setRotate(90);
 96            } else if (pageInfo.landscaperight) {
 97                m_landscaperight = true;
 98                m_pdf_view.setRotate(270);
 99            }
100            m_pdf_view.scrollTo(pageInfo.scrollX, pageInfo.scrollY);
101        } catch (Exception ex) {
102            Log.e(LOGTAG, ex.getMessage(), ex);
103            pageInfo = new CurrentPageInfo();
104            pageInfo.file = file;
105        }
106        // m_name = (TextView)findViewById(R.id.name);
107        m_animator = (ViewAnimator) findViewById(R.id.viewanim);
108        m_seek_view = (SeekBar) findViewById(R.id.page_picker_seeker);
109        m_seek_view.setMax(m_pdf_view.getPagesCount());
110        m_text = (TextView) findViewById(R.id.page_picker_message);
111        m_seek_view.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
112            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
113                if (progress == 0) {
114                    progress = 1;
115                }
116                m_text.setText(progress + "");
117            }
118
119            public void onStartTrackingTouch(SeekBar seekBar) {
120            }
121
122            public void onStopTrackingTouch(SeekBar seekBar) {
123            }
124        });
125
126        ImageButton close = (ImageButton) findViewById(R.id.page_picker_close);
127        ImageButton plus = (ImageButton) findViewById(R.id.page_picker_plus);
128        plus.setOnClickListener(new View.OnClickListener() {
129            public void onClick(View v) {
130                m_seek_view.incrementProgressBy(1);
131            }
132        });
133        ImageButton minus = (ImageButton) findViewById(R.id.page_picker_minus);
134        minus.setOnClickListener(new View.OnClickListener() {
135            public void onClick(View v) {
136                if (m_seek_view.getProgress() != 0) {
137                    m_seek_view.incrementProgressBy(-1);
138                }
139            }
140        });
141        close.setOnClickListener(new View.OnClickListener() {
142            public void onClick(View v) {
143                if (m_seek_view.getProgress() == 0) {
144                    m_pdf_view.gotoPage(0);
145                } else {
146                    m_pdf_view.gotoPage(m_seek_view.getProgress() - 1);
147                }
148                m_animator.showNext();
149            }
150        });
151    }
152
153    private void initListener() {
154        m_pdf_view.setStatusListener(new StatusListener() {
155
156            public void onError(PDFView v, String msg) {
157                // TODO Auto-generated method stub
158
159            }
160
161            public void onLoadingEnd(PDFView v) {
162                // TODO Auto-generated method stub
163
164            }
165
166            public void onLoadingStart(PDFView v) {
167                // TODO Auto-generated method stub
168
169            }
170
171            public void onPageChanged(PDFView v, int page) {
172                // TODO Auto-generated method stub
173                ImageButton prev = (ImageButton) findViewById(R.id.prev_page);
174                ImageButton next = (ImageButton) findViewById(R.id.next_page);
175                prev.setEnabled(true);
176                next.setEnabled(true);
177                TextView tv = (TextView) findViewById(R.id.page_number_view);
178                tv.setText(page + "/" + v.getPagesCount());
179
180            }
181
182            public void onRenderingEnd(PDFView v) {
183                // TODO Auto-generated method stub
184
185            }
186
187            public void onRenderingStart(PDFView v) {
188                // TODO Auto-generated method stub
189
190            }
191        });
192    }
193
194    private void pageUp() {
195        m_pdf_view.prevPage();
196    }
197
198    private void pageDown() {
199        m_pdf_view.nextPage();
200    }
201
202    private void initButtons() {
203        ImageButton btn = (ImageButton) findViewById(R.id.exit);
204        btn.setOnClickListener(new View.OnClickListener() {
205            public void onClick(View v) {
206                m_pdf_view.close();
207                System.gc();
208                finish();
209            }
210        });
211        btn = (ImageButton) findViewById(R.id.prev_page);
212        btn.setOnClickListener(new View.OnClickListener() {
213            public void onClick(View v) {
214                if (m_landscapeleft) {
215                    pageDown();
216                } else {
217                    pageUp();
218                }
219                saveData();
220            }
221        });
222        btn.setOnLongClickListener(new View.OnLongClickListener() {
223
224            public boolean onLongClick(View v) {
225                m_seek_view.setProgress(m_pdf_view.getCurrentPage() + 1);
226                m_animator.showNext();
227                return true;
228            }
229        });
230
231        btn = (ImageButton) findViewById(R.id.next_page);
232        btn.setOnClickListener(new View.OnClickListener() {
233            public void onClick(View v) {
234                if (m_landscapeleft) {
235                    pageUp();
236                } else {
237                    pageDown();
238                }
239                saveData();
240            }
241        });
242        btn.setOnLongClickListener(new View.OnLongClickListener() {
243
244            public boolean onLongClick(View v) {
245                m_seek_view.setProgress(m_pdf_view.getCurrentPage() + 1);
246                m_animator.showNext();
247                return true;
248            }
249        });
250
251        btn = (ImageButton) findViewById(R.id.switch_page);
252        btn.setOnClickListener(new View.OnClickListener() {
253            public void onClick(View v) {
254                if (m_landscaperight || m_landscapeleft) {
255                    m_pdf_view.setRotate(0);
256                    m_landscaperight = false;
257                    m_landscapeleft = false;
258                } else {
259                    m_pdf_view.setRotate(90);
260                    m_landscapeleft = true;
261                }
262                saveData();
263            }
264        });
265        btn.setOnLongClickListener(new View.OnLongClickListener() {
266            public boolean onLongClick(View v) {
267                if (m_landscaperight || m_landscapeleft) {
268                    m_pdf_view.setRotate(0);
269                    m_landscaperight = false;
270                    m_landscapeleft = false;
271                } else {
272                    m_pdf_view.setRotate(270);
273                    m_landscaperight = true;
274                }
275                saveData();
276                return true;
277            }
278        });
279    }
280
281    private void initZoomSpinner() {
282        Spinner s = (Spinner) findViewById(R.id.zoom);
283
284        s.setOnItemSelectedListener(new OnItemSelectedListener() {
285            public void onNothingSelected(AdapterView<?> view) {
286            }
287
288            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
289                float factor = 1.0F;
290                switch (pos) {
291                    case 1:
292                        factor = 0.25F;
293                        break;
294                    case 2:
295                        factor = 0.50F;
296                        break;
297                    case 3:
298                        factor = 0.75F;
299                        break;
300                    case 4:
301                        factor = 1.00F;
302                        break;
303                    case 5:
304                        factor = 1.25F;
305                        break;
306                    case 6:
307                        factor = 1.5F;
308                        break;
309                    case 7:
310                        factor = 2.0F;
311                        break;
312                    case 8:
313                        factor = 2.5F;
314                        break;
315                    case 9:
316                        factor = 3.0F;
317                        break;
318                    default:
319                        factor = -1.0F;
320                        break;
321                }
322                m_pdf_view.setZoomFactor(factor);
323            }
324        });
325    }
326
327    @Override
328    public void onResume() {
329        super.onResume();
330        updateTitle(NAME);
331    }
332
333    @Override
334    protected void onSaveInstanceState(Bundle outState) {
335        super.onSaveInstanceState(outState);
336    }
337
338    @Override
339    protected void onStop() {
340        super.onStop();
341    }
342
343    @Override
344    protected void onDestroy() {
345        super.onDestroy();
346    }
347
348    /**
349     * @see android.app.Activity#onRestoreInstanceState(android.os.Bundle)
350     */
351    @Override
352    protected void onRestoreInstanceState(Bundle savedInstanceState) {
353        super.onRestoreInstanceState(savedInstanceState);
354    }
355
356    private void saveData() {
357        Runnable run = new Runnable() {
358            public void run() {
359                try {
360                    pageInfo.pageno = m_pdf_view.getCurrentPage();
361                    pageInfo.scrollX = m_pdf_view.getScrollX();
362                    pageInfo.scrollY = m_pdf_view.getScrollY();
363                    pageInfo.landscapeleft = m_landscapeleft;
364                    pageInfo.landscaperight = m_landscaperight;
365                    ObjectOutputStream out =
366                        new ObjectOutputStream(PDFViewerActivity.this.openFileOutput(pageInfo.file,
367                            Context.MODE_PRIVATE));
368                    out.writeObject(pageInfo);
369                    out.close();
370                } catch (Exception ex) {
371                    Log.e(LOGTAG, ex.getMessage(), ex);
372                }
373            }
374        };
375        (new Thread(run)).start();
376
377    }
378
379    @Override
380    public boolean onKeyDown(int keyCode, KeyEvent event) {
381        boolean handled = false;
382        switch (keyCode) {
383            case NOOK_PAGE_UP_KEY_LEFT:
384            case NOOK_PAGE_UP_KEY_RIGHT:
385                pageUp();
386                handled = true;
387                break;
388
389            case NOOK_PAGE_DOWN_KEY_LEFT:
390            case NOOK_PAGE_DOWN_KEY_RIGHT:
391                pageDown();
392                handled = true;
393                break;
394            case NOOK_PAGE_UP_SWIPE:
395                if (m_landscapeleft) {
396                    pageDown();
397                } else {
398                    pageUp();
399                }
400                handled = true;
401                break;
402            case NOOK_PAGE_DOWN_SWIPE:
403                if (m_landscapeleft) {
404                    pageUp();
405                } else {
406                    pageDown();
407                }
408                handled = true;
409                break;
410            default:
411                break;
412        }
413        return handled;
414    }
415
416    class MediaScannerNotifier implements MediaScannerConnectionClient {
417        private MediaScannerConnection mConnection;
418        private String mPath;
419
420        public synchronized void scanFile(String path) {
421            if (path == null) { return; }
422            String mime = "ebook/";
423            String ext = path.substring(path.lastIndexOf(".") + 1).toLowerCase();
424            mime += ext;
425            mConnection.scanFile(path, mime);
426        }
427
428        public MediaScannerNotifier(String path) {
429            mConnection = new MediaScannerConnection(PDFViewerActivity.this, this);
430            mConnection.connect();
431            mPath = path;
432        }
433
434        public void onMediaScannerConnected() {
435            scanFile(mPath);
436        }
437
438        public void onScanCompleted(String path, Uri arg1) {
439            System.out.println("On Scan completed" + path + "  " + arg1);
440            String[] columns = {
441                "title", "authors"
442            };
443            final Cursor dbCursor = getContentResolver().query(arg1, columns, null, null, null);
444            dbCursor.moveToFirst();
445            m_Handler.post(new Runnable() {
446                public void run() {
447                    // m_name.setText(dbCursor.getString(0));
448                    updateTitle(NAME = dbCursor.getString(0));
449                    dbCursor.close();
450                }
451            });
452            mConnection.disconnect();
453        }
454    }
455}
456
457class CurrentPageInfo implements Serializable {
458    int pageno = 1;
459    boolean landscaperight = false;
460    boolean landscapeleft = false;
461    int scrollX = 0;
462    int scrollY = 0;
463    String file;
464
465}