1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| public class ErView extends Activity implements OnTouchListener {
private ArrayList<WebView> wv = new ArrayList<WebView>();
private ViewFlipper flipper;
private Book book;
private String file = "testbook";
private String FILEPATH ="/mnt/sdcard/";
private File oppath1;
private String sb="";
private ArrayList<String> vImageName = new ArrayList<String>();
private int vContentsSize = 0;
private int vSpineSize = 0;
private int vTocSize = 0;
String abspath = FILEPATH+file;
private float xAtDown;
private float xAtUp;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGetEpubFile();
setLayout(vContentsSize);
mWebViewSetting();
mEpubFileRead();
flipper.setOnTouchListener(this);
}
public void setLayout(int size) {
LinearLayout vLinearLayout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
vLinearLayout.setLayoutParams(params);
flipper= new ViewFlipper(this);
for(int i=0; i < size; i++) {
wv.add(new WebView(this));
}
for( WebView w : wv) {
w.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 700));
flipper.addView(w);
}
setContentView(flipper);
}
public void mWebViewSetting() {
for(int i =0; i < wv.size(); i++) {
wv.get(i).getSettings().setJavaScriptEnabled(true);
wv.get(i).getSettings().setBuiltInZoomControls(true);
wv.get(i).setWebViewClient(new WebViewClientClass());
wv.get(i).setOnTouchListener(this);
}
}
public void mGetEpubFile() {
File filePath = new File(abspath+".epub");
try {
InputStream epubInputStream = new BufferedInputStream(new FileInputStream(filePath));
book = (new EpubReader()).readEpub(epubInputStream);
vContentsSize = book.getContents().size();
vSpineSize = book.getSpine().size();
vTocSize = book.getTableOfContents().size();
} catch (Exception e) {
e.printStackTrace();
}
}
public void mEpubFileRead() {
try {
int pos = abspath.lastIndexOf('/');
DownloadResource(abspath.substring(0, pos));
for(int i = 0; i< vContentsSize; i++) {
InputStream is = book.getSpine().getSpineReferences().get(i).getResource().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
sb="";
while ((line = reader.readLine()) != null)
{
sb = sb+"\n"+line;
Log.d("display line", sb);
}
is.close();
wv.get(i).loadDataWithBaseURL("file:///android_asset/books/testbook.epub"+"/", sb, "text/html", "utf-8", null);
}
}
catch (IOException e) {
Log.e("epublib", e.getMessage());
}
}
private class WebViewClientClass extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public boolean onTouch(View v, MotionEvent event) {
for(int i =0; i<wv.size(); i++) {
if( v!=wv.get(i) && v!=flipper) {
return false;
}
}
if(event.getAction() == MotionEvent.ACTION_DOWN){
xAtDown=event.getX();
}
else if(event.getAction() == MotionEvent.ACTION_UP){
xAtUp = event.getX();
if(xAtDown > xAtUp){
flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
count++;
if(count < vContentsSize) {
flipper.showNext();
} else {
Toast.makeText(this, "마지막 페이지 입니다.", Toast.LENGTH_SHORT).show();
count--;
}
} else if(xAtDown < xAtUp) {
flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_out));
count--;
if(count >- 1) {
flipper.showPrevious();
} else {
Log.d("Downlod path", abspath);
Toast.makeText(this, "첫번째 페이지 입니다.", Toast.LENGTH_SHORT).show();
count++;
}
}
}
return true;
}
private void DownloadResource(String directory) {
try {
nl.siegmann.epublib.domain.Resources rst = book.getResources();
Collection<Resource> clrst = rst.getAll();
Iterator<Resource> itr = clrst.iterator();
Log.d("Downlod path", directory);
while (itr.hasNext()) {
Resource rs = itr.next();
if ((rs.getMediaType() == MediatypeService.JPG) || (rs.getMediaType() == MediatypeService.PNG) || (rs.getMediaType() == MediatypeService.GIF) || rs.getMediaType() == MediatypeService.CSS) {
File oppath1 = new File(directory+File.separator+rs.getHref());
Log.d("Oppath - ", oppath1.getAbsolutePath());
Log.d("Oppath - ", oppath1.getName().toString());
Log.d("Resource Name - ", rs.getHref());
oppath1.createNewFile();
String s = oppath1.getAbsolutePath();
vImageName.add(s);
Log.d("##########", "vImageName "+ s);
Log.d("File Checking - ", "Exists - "+oppath1.exists()+" & Write - "+oppath1.canWrite());
FileOutputStream fos1 = new FileOutputStream(oppath1);
fos1.write(rs.getData());
fos1.close();
}
}
}
catch (IOException e) {
Log.e("error", e.getMessage());
}
} |
Partager