调整下载信息存储,记录阅读位置

This commit is contained in:
seven332 committed 2014-10-05 02:37:51 +08:00
1 parent 0c3a7301cc
commit d56c4e5386
10 files changed
+111 -47

No files matched your search

+2 -2
View File
@@ -16,8 +16,8 @@
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hippo.ehviewer"
android:versionCode="25"
android:versionName="0.6.4" >
android:versionCode="26"
android:versionName="0.6.5" >
<uses-sdk
android:minSdkVersion="14"
+47 -1
View File
@@ -17,6 +17,7 @@
package com.hippo.ehviewer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
@@ -32,9 +33,11 @@ import com.hippo.ehviewer.ehclient.EhInfo;
import com.hippo.ehviewer.ehclient.ExDownloaderManager;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.Crash;
import com.hippo.ehviewer.util.EhUtils;
import com.hippo.ehviewer.util.Favorite;
import com.hippo.ehviewer.util.Log;
import com.hippo.ehviewer.util.Ui;
import com.hippo.ehviewer.util.Utils;
import com.hippo.ehviewer.widget.MaterialToast;
public class AppContext extends Application implements UncaughtExceptionHandler {
@@ -85,10 +88,53 @@ public class AppContext extends Application implements UncaughtExceptionHandler
} catch (IOException e) {}
}
// Fix <=22 error
// Fix <=22 login error
if (Config.getVersionCode() <= 22)
EhInfo.getInstance(this).logout();
// Fix <=25 Update
if (Config.getVersionCode() <= 25) {
File downloadDir = new File(Config.getDownloadPath());
String cachePath = getExternalCacheDir() != null
? getExternalCacheDir().getPath()
: getCacheDir().getPath();
File oldEdInfoDir = new File(cachePath, "ExDownloaderManager");
String[] list = downloadDir.list();
if (list != null && list.length != 0) {
for (String str : list) {
// Change download dir name from gid-title to gid
File oldDir = new File(downloadDir, str);
if (!new File(oldDir, EhUtils.EH_DOWNLOAD_FILENAME).exists())
continue;
int index = str.indexOf('-');
if (index == -1)
continue;
String gid = str.substring(0, index);
File newDir = new File(downloadDir, gid);
if (!oldDir.renameTo(newDir)) {
// if delete error, might same gid and different title
Utils.deleteDirInThread(oldDir);
continue;
}
// Move download info file
File oldInfoFile = new File(oldEdInfoDir, gid);
File newInfoFile = new File(newDir, EhUtils.EH_DOWNLOAD_FILENAME);
try {
FileOutputStream fos = new FileOutputStream(newInfoFile);
// Add last page
byte[] bs = {'0', '0', '0', '0', '0', '0', '0', '0', '\n'};
fos.write(bs);
Utils.copy(new FileInputStream(oldInfoFile), fos);
} catch (Throwable e) {
continue;
}
}
}
// Delete old exdownload info dir
Utils.deleteDirInThread(oldEdInfoDir);
}
// Update version code
try {
PackageInfo pi= getPackageManager().getPackageInfo(getPackageName(), 0);
@@ -20,10 +20,10 @@ import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;
@@ -36,6 +36,7 @@ import android.os.Process;
import com.hippo.ehviewer.AppContext;
import com.hippo.ehviewer.network.HttpHelper;
import com.hippo.ehviewer.util.AutoExpandArray;
import com.hippo.ehviewer.util.BgThread;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.EhUtils;
import com.hippo.ehviewer.util.Log;
@@ -97,6 +98,8 @@ public class ExDownloader implements Runnable {
private volatile HashSet<Integer> mDownloadIndexSet;
private volatile Object mDownloadLock = new Object();
private int mCurReadIndex = 0;
/**
* Do no work in UI thread
* @author Hippo
@@ -132,13 +135,8 @@ public class ExDownloader implements Runnable {
mControlorArray = new HttpHelper.DownloadControlor[mWorkerNum];
// Make sure dir
mDir = EhUtils.getGalleryDir(mGid, mTitle);
mDir = EhUtils.getGalleryDir(mGid);
Utils.ensureDir(mDir, true);
// Create mark file
File makeFile = new File(mDir, EhUtils.EH_DOWNLOAD_FILENAME);
try {
new FileOutputStream(makeFile).close();
} catch (IOException e) {}
}
public void setListenerForImageSet(ListenerForImageSet l) {
@@ -244,6 +242,15 @@ public class ExDownloader implements Runnable {
ensureWorkers();
}
public void setCurReadIndex(final int index) {
new BgThread() {
@Override
public void run() {
writeCurReadIndex(index);
}
}.start();
}
public int getMaxEnsureIndex() {
if (mImageNum != -1) {
return mImageNum - 1;
@@ -288,7 +295,7 @@ public class ExDownloader implements Runnable {
}
private File getExDownloadInfoFile() {
return new File(mManager.getExDownloadInfoDir(), String.valueOf(mGid));
return new File(mDir, EhUtils.EH_DOWNLOAD_FILENAME);
}
private boolean checkMode(int samlpe, int target) {
@@ -300,6 +307,7 @@ public class ExDownloader implements Runnable {
* This function parser the download info file.<br>
* The file look like this:<br>
* <code>
* 00000000<br>
* 728874<br>
* 306429c222<br>
* 1<br>
@@ -317,7 +325,7 @@ public class ExDownloader implements Runnable {
* @param ediFile
* @return
*/
private boolean parserEdiFile(File ediFile) {
private synchronized boolean parserEdiFile(File ediFile) {
if (!ediFile.exists() || !ediFile.isFile() || !ediFile.canRead()
|| !ediFile.canWrite())
return false;
@@ -326,6 +334,8 @@ public class ExDownloader implements Runnable {
try {
is = new BufferedInputStream(new FileInputStream(ediFile),
IO_BUFFER_SIZE);
// skip read index
Utils.readAsciiLine(is);
if (mGid != Integer.valueOf(Utils.readAsciiLine(is)) ||
!mToken.equals(Utils.readAsciiLine(is)))
return false;
@@ -377,9 +387,11 @@ public class ExDownloader implements Runnable {
}
}
private void writeEdiFile(File ediFile) {
private synchronized void writeEdiFile(File ediFile) {
try {
FileWriter writer = new FileWriter(ediFile);
writer.write(String.format("%08x", mCurReadIndex));
writer.write("\n");
writer.write(String.valueOf(mGid));
writer.write("\n");
writer.write(mToken);
@@ -406,6 +418,27 @@ public class ExDownloader implements Runnable {
} catch (IOException e) {}
}
private synchronized void writeCurReadIndex(int index) {
mCurReadIndex = index;
try {
RandomAccessFile raf = new RandomAccessFile(getExDownloadInfoFile(), "rw");
raf.write(String.format("%08x", mCurReadIndex).getBytes());
raf.close();
} catch (Throwable e) {}
}
public static int readCurReadIndex(int gid) {
try {
RandomAccessFile raf = new RandomAccessFile(new File(EhUtils.getGalleryDir(gid),
EhUtils.EH_DOWNLOAD_FILENAME), "r");
int index = Integer.parseInt(raf.readLine(), 16);
raf.close();
return index;
} catch (Throwable e) {
return 0;
}
}
private void getDetailInfo(int pageIndex, File ediFile, boolean needPreviewInfo) throws Exception {
HttpHelper hh = new HttpHelper(mContext);
EdDetailParser edp = new EdDetailParser();
@@ -478,7 +511,7 @@ public class ExDownloader implements Runnable {
try {
File ediFile = getExDownloadInfoFile();
if (!parserEdiFile(ediFile) || mPreviewPerPage == -1 ||
(mMode == EhClient.MODE_EX && mImageNum == -1)) {
((mMode == EhClient.MODE_EX || mMode == EhClient.MODE_G) && mImageNum == -1)) {
getDetailInfo(0, ediFile, true);
}
@@ -16,8 +16,6 @@
package com.hippo.ehviewer.ehclient;
import java.io.File;
import android.content.Context;
import android.util.SparseArray;
@@ -29,7 +27,6 @@ public class ExDownloaderManager {
private final Context mContext;
private final SparseArray<ExDownloader> mExDownloadList;
private final File mExDownloadInfoDir;
private static ExDownloaderManager sInstance;
@@ -44,18 +41,6 @@ public class ExDownloaderManager {
private ExDownloaderManager() {
mContext = AppContext.getInstance();
mExDownloadList = new SparseArray<ExDownloader>();
// Init download info dir
String cachePath = mContext.getExternalCacheDir() != null
? mContext.getExternalCacheDir().getPath()
: mContext.getCacheDir().getPath();
mExDownloadInfoDir = new File(cachePath, TAG);
if (!mExDownloadInfoDir.exists()) {
mExDownloadInfoDir.mkdirs();
}
}
public File getExDownloadInfoDir() {
return mExDownloadInfoDir;
}
public synchronized ExDownloader getExDownloader(int gid, String token, String title, int mode) {
@@ -767,6 +767,7 @@ public class GalleryView extends GLView
setState();
loadImage(mCurIndex-1);
mImageSet.setCurReadIndex(mCurIndex);
if (mGalleryViewListener != null) {
mGalleryViewListener.onPageChanged(mCurIndex);
}
@@ -805,6 +806,7 @@ public class GalleryView extends GLView
setState();
loadImage(mCurIndex+1);
mImageSet.setCurReadIndex(mCurIndex);
if (mGalleryViewListener != null) {
mGalleryViewListener.onPageChanged(mCurIndex);
}
@@ -844,6 +846,7 @@ public class GalleryView extends GLView
loadImage(mCurIndex);
loadImage(mCurIndex+1);
mImageSet.setCurReadIndex(mCurIndex);
if (mGalleryViewListener != null) {
mGalleryViewListener.onPageChanged(mCurIndex);
}
@@ -88,7 +88,7 @@ public class ImageSet implements ExDownloader.ListenerForImageSet {
public ImageSet(int gid, String token, String title, int startIndex) {
mDir = EhUtils.getGalleryDir(gid, title);
mDir = EhUtils.getGalleryDir(gid);
Utils.ensureDir(mDir, true);
// Init ExDownloader
@@ -128,6 +128,10 @@ public class ImageSet implements ExDownloader.ListenerForImageSet {
mExDownloader.setStartIndex(startIndex);
}
public void setCurReadIndex(int index) {
mExDownloader.setCurReadIndex(index);
}
public Object getImage(int index) {
// Try to get file name
String filename = mImageFilenameArray.get(index);
@@ -229,7 +229,7 @@ public class DownloadActivity extends AbsActivity {
intent.putExtra(GalleryActivity.KEY_GID, gi.gid);
intent.putExtra(GalleryActivity.KEY_TOKEN, gi.token);
intent.putExtra(GalleryActivity.KEY_TITLE, gi.title);
intent.putExtra(GalleryActivity.KEY_START_INDEX, 0);
intent.putExtra(GalleryActivity.KEY_START_INDEX, -1);
startActivity(intent);
}
});
@@ -360,7 +360,7 @@ public class DownloadActivity extends AbsActivity {
mServiceConn.getService().delete(mDownloadInfo);
// Delete dir
GalleryInfo gi = mDownloadInfo.galleryInfo;
Utils.deleteDirInThread(EhUtils.getGalleryDir(gi.gid, gi.title));
Utils.deleteDirInThread(EhUtils.getGalleryDir(gi.gid));
}
return true;
}
@@ -34,6 +34,7 @@ import android.widget.TextView;
import com.hippo.ehviewer.AppHandler;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.app.MaterialAlertDialog;
import com.hippo.ehviewer.ehclient.ExDownloader;
import com.hippo.ehviewer.gallery.GalleryView;
import com.hippo.ehviewer.gallery.data.ImageSet;
import com.hippo.ehviewer.gallery.ui.GLRootView;
@@ -199,6 +200,8 @@ public class GalleryActivity extends AbsActivity
String token = intent.getStringExtra(KEY_TOKEN);
mTitle = intent.getStringExtra(KEY_TITLE);
int startIndex = intent.getIntExtra(KEY_START_INDEX, 0);
if (startIndex == -1)
startIndex = ExDownloader.readCurReadIndex(mGid);
if (mGid == -1 || token == null || mTitle == null) {
new MaterialAlertDialog.Builder(this).setTitle(R.string.error)
@@ -214,6 +217,7 @@ public class GalleryActivity extends AbsActivity
} else {
mImageSet = new ImageSet(mGid, token, mTitle, startIndex);
mGalleryView = new GalleryView(getApplicationContext(), mImageSet, startIndex);
GLRootView glrv= (GLRootView)findViewById(R.id.gl_root_view);
glrv.setContentPane(mGalleryView);
@@ -302,6 +306,7 @@ public class GalleryActivity extends AbsActivity
public void onSizeUpdate(int size) {
updateTitle();
mPageSeeker.setMax(size - 1);
mPageSeeker.setProgress(mGalleryView.getCurIndex() + 1);
}
private void showSeekBubble() {
@@ -57,7 +57,6 @@ import com.hippo.ehviewer.app.MaterialAlertDialog;
import com.hippo.ehviewer.data.Data;
import com.hippo.ehviewer.data.DownloadInfo;
import com.hippo.ehviewer.ehclient.EhInfo;
import com.hippo.ehviewer.ehclient.ExDownloaderManager;
import com.hippo.ehviewer.network.HttpHelper;
import com.hippo.ehviewer.preference.ListPreference;
import com.hippo.ehviewer.util.BgThread;
@@ -599,9 +598,6 @@ public class SettingsActivity extends AbsPreferenceActivity {
for (int i = 0; i < targetDirList.size(); i++) {
File dir = targetDirList.get(i);
Utils.deleteFile(dir);
// Delete gid file
new File(ExDownloaderManager.getInstance().getExDownloadInfoDir(),
String.valueOf(EhUtils.getGidFromDirname(dir.getName()))).delete();
handler.post(new CleanResponder(CleanResponder.STATE_DOING, targetDirList.size(), i + 1));
}
// Close windows
+3 -11
View File
@@ -18,6 +18,7 @@ package com.hippo.ehviewer.util;
import java.io.File;
public final class EhUtils {
public static final String EH_DOWNLOAD_FILENAME = ".ehviewer";
@@ -25,20 +26,11 @@ public final class EhUtils {
/**
* Get gallery for read and download
* @param gid
* @param title
* @return
*/
public static File getGalleryDir(int gid, String title) {
public static File getGalleryDir(int gid) {
return new File(Config.getDownloadPath(),
Utils.standardizeFilename(gid + "-" + title));
}
public static int getGidFromDirname(String dirname) {
int index = dirname.indexOf('-');
if (index == -1)
return 0;
else
return Utils.parseIntSafely(dirname.substring(0, index), 0);
Utils.standardizeFilename(Integer.toString(gid)));
}
/**