Skip to content

Commit

Permalink
Migrating first/second paths to default/backup(internal/external) paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
MewX committed Jul 16, 2023
1 parent 0067c99 commit 2a59f6d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,11 @@ private void initialApp() {

// create save folder.
// TODO: can stop creating those files and folders.
LightCache.saveFile(GlobalConfig.getFirstStoragePath() + "imgs", ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getSecondStoragePath() + "imgs", ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getFirstStoragePath() + GlobalConfig.customFolderName, ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getSecondStoragePath() + GlobalConfig.customFolderName, ".nomedia", "".getBytes(), false);

GlobalConfig.setFirstStoragePathStatus(LightCache.testFileExist(GlobalConfig.getFirstStoragePath() + "imgs" + File.separator + ".nomedia", true));
// TODO: set status? tell app where is available
LightCache.saveFile(GlobalConfig.getFirstFullSaveFilePath() + "imgs", ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getSecondFullSaveFilePath() + "imgs", ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getDefaultStoragePath() + "imgs", ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getBackupStoragePath() + "imgs", ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getDefaultStoragePath() + GlobalConfig.customFolderName, ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getBackupStoragePath() + GlobalConfig.customFolderName, ".nomedia", "".getBytes(), false);
GlobalConfig.setFirstStoragePathAvailable(LightCache.testFileExist(SaveFileMigration.getExternalStoragePath() + "imgs" + File.separator + ".nomedia", true));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ private void buildChapterList() {

// test does file exist
if (from.equals(FromLocal)
&& !LightCache.testFileExist(GlobalConfig.getFirstStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + ci.cid + ".xml")
&& !LightCache.testFileExist(GlobalConfig.getSecondStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + ci.cid + ".xml")) {
&& !LightCache.testFileExist(GlobalConfig.getDefaultStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + ci.cid + ".xml")
&& !LightCache.testFileExist(GlobalConfig.getBackupStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + ci.cid + ".xml")) {
intent.putExtra("from", "cloud"); // from cloud
}
else {
Expand Down Expand Up @@ -115,8 +115,8 @@ private void buildChapterList() {

// test does file exist
if (from.equals(FromLocal)
&& !LightCache.testFileExist(GlobalConfig.getFirstStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + ci.cid + ".xml")
&& !LightCache.testFileExist(GlobalConfig.getSecondStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + ci.cid + ".xml")) {
&& !LightCache.testFileExist(GlobalConfig.getDefaultStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + ci.cid + ".xml")
&& !LightCache.testFileExist(GlobalConfig.getBackupStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + ci.cid + ".xml")) {
// jump to reader activity
intent.putExtra("from", "cloud"); // from cloud
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ protected void onCreate(Bundle savedInstanceState) {

// hide view and set colors
tvNovelTitle.setText(title);
if(LightCache.testFileExist(GlobalConfig.getFirstStoragePath() + "imgs" + File.separator + aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getFirstStoragePath() + "imgs" + File.separator + aid + ".jpg", ivNovelCover);
else if(LightCache.testFileExist(GlobalConfig.getSecondStoragePath() + "imgs" + File.separator + aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getSecondStoragePath() + "imgs" + File.separator + aid + ".jpg", ivNovelCover);
if(LightCache.testFileExist(GlobalConfig.getDefaultStoragePath() + "imgs" + File.separator + aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getDefaultStoragePath() + "imgs" + File.separator + aid + ".jpg", ivNovelCover);
else if(LightCache.testFileExist(GlobalConfig.getBackupStoragePath() + "imgs" + File.separator + aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getBackupStoragePath() + "imgs" + File.separator + aid + ".jpg", ivNovelCover);
else
ImageLoader.getInstance().displayImage(Wenku8API.getCoverURL(aid), ivNovelCover); // move to onCreateView!
tvLatestChapterNameText.setText(getResources().getText(R.string.novel_item_latest_chapter));
Expand Down Expand Up @@ -498,8 +498,8 @@ private void showDirectJumpToReaderDialog(final int cid) {

// test does file exist
if (from.equals(FromLocal)
&& !LightCache.testFileExist(GlobalConfig.getFirstStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + cid + ".xml")
&& !LightCache.testFileExist(GlobalConfig.getSecondStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + cid + ".xml")) {
&& !LightCache.testFileExist(GlobalConfig.getDefaultStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + cid + ".xml")
&& !LightCache.testFileExist(GlobalConfig.getBackupStoragePath() + GlobalConfig.saveFolderName + File.separator + "novel" + File.separator + cid + ".xml")) {
intent.putExtra("from", "cloud"); // from cloud
} else {
intent.putExtra("from", from); // from "fav"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
viewHolder.tvNovelIntro.setText(mDataset.get(i).intro_short);

// need to solve flicking problem
if(LightCache.testFileExist(GlobalConfig.getFirstStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getFirstStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg", viewHolder.ivNovelCover);
else if(LightCache.testFileExist(GlobalConfig.getSecondStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getSecondStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg", viewHolder.ivNovelCover);
if(LightCache.testFileExist(GlobalConfig.getDefaultStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getDefaultStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg", viewHolder.ivNovelCover);
else if(LightCache.testFileExist(GlobalConfig.getBackupStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getBackupStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg", viewHolder.ivNovelCover);
else
ImageLoader.getInstance().displayImage(Wenku8API.getCoverURL(mDataset.get(i).aid), viewHolder.ivNovelCover);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ else if (mDataset.get(i).latest_chapter.isEmpty()){
viewHolder.tvNovelIntro.setText(mDataset.get(i).latest_chapter);
}

if(LightCache.testFileExist(GlobalConfig.getFirstStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getFirstStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg", viewHolder.ivNovelCover);
else if(LightCache.testFileExist(GlobalConfig.getSecondStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getSecondStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg", viewHolder.ivNovelCover);
if(LightCache.testFileExist(GlobalConfig.getDefaultStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getDefaultStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg", viewHolder.ivNovelCover);
else if(LightCache.testFileExist(GlobalConfig.getBackupStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg"))
ImageLoader.getInstance().displayImage("file://" + GlobalConfig.getBackupStoragePath() + "imgs" + File.separator + mDataset.get(i).aid + ".jpg", viewHolder.ivNovelCover);
else
ImageLoader.getInstance().displayImage(Wenku8API.getCoverURL(mDataset.get(i).aid), viewHolder.ivNovelCover);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ protected void onPreExecute() {
@Override
protected Wenku8Error.ErrorCode doInBackground(Integer... params) {
// covers
File dir = new File(GlobalConfig.getFirstStoragePath() + "imgs");
if(!dir.exists()) dir = new File(GlobalConfig.getSecondStoragePath() + "imgs");
File dir = new File(GlobalConfig.getDefaultStoragePath() + "imgs");
if(!dir.exists()) dir = new File(GlobalConfig.getBackupStoragePath() + "imgs");
File[] childFile = dir.listFiles();
if(childFile != null && childFile.length != 0) {
for (File f : childFile) {
Expand All @@ -200,8 +200,8 @@ protected Wenku8Error.ErrorCode doInBackground(Integer... params) {
}

// cache
dir = new File(GlobalConfig.getFirstStoragePath() + "cache");
if(!dir.exists()) dir = new File(GlobalConfig.getSecondStoragePath() + "cache");
dir = new File(GlobalConfig.getDefaultStoragePath() + "cache");
if(!dir.exists()) dir = new File(GlobalConfig.getBackupStoragePath() + "cache");
childFile = dir.listFiles();
if(childFile != null && childFile.length != 0) {
for (File f : childFile) {
Expand Down Expand Up @@ -256,8 +256,8 @@ protected void onPreExecute() {
@Override
protected Wenku8Error.ErrorCode doInBackground(Integer... params) {
// covers
File dir = new File(GlobalConfig.getFirstStoragePath() + "imgs");
if(!dir.exists()) dir = new File(GlobalConfig.getSecondStoragePath() + "imgs");
File dir = new File(GlobalConfig.getDefaultStoragePath() + "imgs");
if(!dir.exists()) dir = new File(GlobalConfig.getBackupStoragePath() + "imgs");
File[] childFile = dir.listFiles();
if(childFile != null && childFile.length != 0) {
for (File f : childFile) {
Expand All @@ -271,8 +271,8 @@ protected Wenku8Error.ErrorCode doInBackground(Integer... params) {
}

// cache
dir = new File(GlobalConfig.getFirstStoragePath() + "cache");
if(!dir.exists()) dir = new File(GlobalConfig.getSecondStoragePath() + "cache");
dir = new File(GlobalConfig.getDefaultStoragePath() + "cache");
if(!dir.exists()) dir = new File(GlobalConfig.getBackupStoragePath() + "cache");
childFile = dir.listFiles();
if(childFile != null && childFile.length != 0) {
for (File f : childFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.Log;
Expand Down Expand Up @@ -68,7 +68,7 @@ public class GlobalConfig {
private static boolean isInBookshelf = false;
private static boolean isInLatest = false;
private static boolean doLoadImage = true;
private static boolean FirstStoragePathStatus = true;
private static boolean firstStoragePathAvailable = true;
private static Wenku8API.LANG currentLang = Wenku8API.LANG.SC;
public static String pathPickedSave; // dir picker save path

Expand Down Expand Up @@ -132,7 +132,7 @@ else if(temp.equals(Wenku8API.LANG.TC.toString()))

public static void initImageLoader(Context context) {
UnlimitedDiscCache localUnlimitedDiscCache = new UnlimitedDiscCache(
new File(GlobalConfig.getFirstStoragePath() + "cache"),
new File(GlobalConfig.getDefaultStoragePath() + "cache"),
new File(context.getCacheDir() + File.separator + "imgs"));
DisplayImageOptions localDisplayImageOptions = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(true)
Expand Down Expand Up @@ -172,29 +172,28 @@ public static String getOpensourceLicense() {


/**
* 设置第一存储路径的合法性(第一路径可以只有设置
* 设置第一存储路径的合法性(第一路径可以只有设置
* @param a true-合法可以使用; false-不能使用,只能只用第二路径
*/
public static void setFirstStoragePathStatus( boolean a ) {

public static void setFirstStoragePathAvailable(boolean a) {
firstStoragePathAvailable = a;
}

// TODO: get rid of this shortcut.
public static String getFirstStoragePath() {
public static String getDefaultStoragePath() {
// The lookupInternalStorageOnly flag has the highest priority.
if (lookupInternalStorageOnly || !firstStoragePathAvailable) {
return SaveFileMigration.getInternalSavePath();
}
return SaveFileMigration.getExternalStoragePath();
}

// TODO: get rid of this shortcut.
public static String getSecondStoragePath() {
return SaveFileMigration.getInternalSavePath();
public static String getBackupStoragePath() {
String internalPath = SaveFileMigration.getInternalSavePath();
return getDefaultStoragePath().equals(internalPath) ?
SaveFileMigration.getExternalStoragePath() : internalPath;
}

public static String getDefaultStoragePath() {
// TODO: fix with the migration status.
return FirstStoragePathStatus ? getFirstStoragePath() : getSecondStoragePath();
}


public static boolean doCacheImage() {
// for non-image mode
return doLoadImage; // when cache, cache images
Expand Down Expand Up @@ -224,12 +223,13 @@ public static int getTextLoadWay() {
return 2;
}

// TODO: get rid of those ugly shortcuts.
public static String getFirstFullSaveFilePath() {
return getFirstStoragePath() + saveFolderName + File.separator;
return getDefaultStoragePath() + saveFolderName + File.separator;
}

public static String getSecondFullSaveFilePath() {
return getSecondStoragePath() + saveFolderName + File.separator;
return getBackupStoragePath() + saveFolderName + File.separator;
}

public static String getFirstFullUserAccountSaveFilePath() {
Expand Down Expand Up @@ -270,18 +270,18 @@ else if (canStart)
private static String loadFullSaveFileContent(@NonNull String FileName) {
// get full file in file save path
String h = "";
if (LightCache.testFileExist(getFirstStoragePath() + saveFolderName + File.separator + FileName)) {
if (LightCache.testFileExist(getDefaultStoragePath() + saveFolderName + File.separator + FileName)) {
try {
byte[] b = LightCache.loadFile(getFirstStoragePath() + saveFolderName + File.separator + FileName);
byte[] b = LightCache.loadFile(getDefaultStoragePath() + saveFolderName + File.separator + FileName);
if(b == null) return "";
h = new String(b, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

} else if (LightCache.testFileExist(getSecondStoragePath() + saveFolderName + File.separator + FileName)) {
} else if (LightCache.testFileExist(getBackupStoragePath() + saveFolderName + File.separator + FileName)) {
try {
byte[] b = LightCache.loadFile(getSecondStoragePath() + saveFolderName + File.separator + FileName);
byte[] b = LightCache.loadFile(getBackupStoragePath() + saveFolderName + File.separator + FileName);
if(b == null) return "";
h = new String(b, "UTF-8");
} catch (UnsupportedEncodingException e) {
Expand All @@ -302,8 +302,8 @@ private static boolean writeFullSaveFileContent(String FileName, @NonNull String
}

// write save file in save path
if (!LightCache.saveFile(getFirstStoragePath() + saveFolderName + File.separator + path, fileName, s.getBytes(), true)) // if not exist
return LightCache.saveFile(getSecondStoragePath() + saveFolderName
if (!LightCache.saveFile(getDefaultStoragePath() + saveFolderName + File.separator + path, fileName, s.getBytes(), true)) // if not exist
return LightCache.saveFile(getBackupStoragePath() + saveFolderName
+ File.separator + path, fileName, s.getBytes(), true);
return true;
}
Expand Down Expand Up @@ -800,25 +800,6 @@ public static boolean saveNovelContentImage(String url) {
return true; // file exist
}

/**
* removeNovelContentImage:
*
* get image url and delete the corresponding local file
*
* @param url
* : full http url of target image
* @return true if file deleted successfully.
*/
public static boolean removeNovelContentImage(String url) {
String imgFileName = generateImageFileNameByURL(url);

// in fact, one of them deleted is ok, so use "or"
return LightCache.deleteFile(getFirstFullSaveFilePath()
+ imgsSaveFolderName + File.separator, imgFileName)
|| LightCache.deleteFile(getSecondFullSaveFilePath()
+ imgsSaveFolderName + File.separator, imgFileName);
}

/**
* getAvailableNovolContentImagePath:
*
Expand Down

0 comments on commit 2a59f6d

Please sign in to comment.