Code improvement and logging
This commit is contained in:
parent
ced75a9b60
commit
60879351a9
1 changed files with 72 additions and 21 deletions
|
@ -21,6 +21,7 @@ package org.schabi.newpipe.util;
|
||||||
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
@ -29,6 +30,7 @@ import android.support.annotation.Nullable;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.schabi.newpipe.BuildConfig;
|
||||||
import org.schabi.newpipe.MainActivity;
|
import org.schabi.newpipe.MainActivity;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -110,6 +112,7 @@ public class StateSaver {
|
||||||
/**
|
/**
|
||||||
* Try to restore the state from memory and disk, using the {@link StateSaver.WriteRead#readFrom(Queue)} from the writeRead.
|
* Try to restore the state from memory and disk, using the {@link StateSaver.WriteRead#readFrom(Queue)} from the writeRead.
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
private static SavedState tryToRestore(@NonNull SavedState savedState, @NonNull WriteRead writeRead) {
|
private static SavedState tryToRestore(@NonNull SavedState savedState, @NonNull WriteRead writeRead) {
|
||||||
if (MainActivity.DEBUG) {
|
if (MainActivity.DEBUG) {
|
||||||
Log.d(TAG, "tryToRestore() called with: savedState = [" + savedState + "], writeRead = [" + writeRead + "]");
|
Log.d(TAG, "tryToRestore() called with: savedState = [" + savedState + "], writeRead = [" + writeRead + "]");
|
||||||
|
@ -117,7 +120,7 @@ public class StateSaver {
|
||||||
|
|
||||||
FileInputStream fileInputStream = null;
|
FileInputStream fileInputStream = null;
|
||||||
try {
|
try {
|
||||||
Queue<Object> savedObjects = stateObjectsHolder.remove(savedState.prefixFileSaved);
|
Queue<Object> savedObjects = stateObjectsHolder.remove(savedState.getPrefixFileSaved());
|
||||||
if (savedObjects != null) {
|
if (savedObjects != null) {
|
||||||
writeRead.readFrom(savedObjects);
|
writeRead.readFrom(savedObjects);
|
||||||
if (MainActivity.DEBUG) {
|
if (MainActivity.DEBUG) {
|
||||||
|
@ -126,8 +129,13 @@ public class StateSaver {
|
||||||
return savedState;
|
return savedState;
|
||||||
}
|
}
|
||||||
|
|
||||||
File file = new File(savedState.pathFileSaved);
|
File file = new File(savedState.getPathFileSaved());
|
||||||
if (!file.exists()) return null;
|
if (!file.exists()) {
|
||||||
|
if(MainActivity.DEBUG) {
|
||||||
|
Log.d(TAG, "Cache file doesn't exist: " + file.getAbsolutePath());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
fileInputStream = new FileInputStream(file);
|
fileInputStream = new FileInputStream(file);
|
||||||
ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
|
ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
|
||||||
|
@ -139,7 +147,7 @@ public class StateSaver {
|
||||||
|
|
||||||
return savedState;
|
return savedState;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
Log.e(TAG, "Failed to restore state", e);
|
||||||
} finally {
|
} finally {
|
||||||
if (fileInputStream != null) {
|
if (fileInputStream != null) {
|
||||||
try {
|
try {
|
||||||
|
@ -154,10 +162,17 @@ public class StateSaver {
|
||||||
/**
|
/**
|
||||||
* @see #tryToSave(boolean, String, String, WriteRead)
|
* @see #tryToSave(boolean, String, String, WriteRead)
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
public static SavedState tryToSave(boolean isChangingConfig, @Nullable SavedState savedState, Bundle outState, WriteRead writeRead) {
|
public static SavedState tryToSave(boolean isChangingConfig, @Nullable SavedState savedState, Bundle outState, WriteRead writeRead) {
|
||||||
String currentSavedPrefix = savedState == null || TextUtils.isEmpty(savedState.prefixFileSaved)
|
@NonNull
|
||||||
? System.nanoTime() - writeRead.hashCode() + ""
|
String currentSavedPrefix;
|
||||||
: savedState.prefixFileSaved;
|
if (savedState == null || TextUtils.isEmpty(savedState.getPrefixFileSaved())) {
|
||||||
|
// Generate unique prefix
|
||||||
|
currentSavedPrefix = System.nanoTime() - writeRead.hashCode() + "";
|
||||||
|
} else {
|
||||||
|
// Reuse prefix
|
||||||
|
currentSavedPrefix = savedState.getPrefixFileSaved();
|
||||||
|
}
|
||||||
|
|
||||||
savedState = tryToSave(isChangingConfig, currentSavedPrefix, writeRead.generateSuffix(), writeRead);
|
savedState = tryToSave(isChangingConfig, currentSavedPrefix, writeRead.generateSuffix(), writeRead);
|
||||||
if (savedState != null) {
|
if (savedState != null) {
|
||||||
|
@ -173,22 +188,33 @@ public class StateSaver {
|
||||||
* to the file with the name of prefixFileName + suffixFileName, in a cache folder got from the {@link #init(Context)}.
|
* to the file with the name of prefixFileName + suffixFileName, in a cache folder got from the {@link #init(Context)}.
|
||||||
* <p>
|
* <p>
|
||||||
* It checks if the file already exists and if it does, just return the path, so a good way to save is:
|
* It checks if the file already exists and if it does, just return the path, so a good way to save is:
|
||||||
* <li> A fixed prefix for the file
|
* <ul>
|
||||||
* <li> A changing suffix
|
* <li> A fixed prefix for the file</li>
|
||||||
|
* <li> A changing suffix</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @param isChangingConfig
|
||||||
|
* @param prefixFileName
|
||||||
|
* @param suffixFileName
|
||||||
|
* @param writeRead
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
private static SavedState tryToSave(boolean isChangingConfig, final String prefixFileName, String suffixFileName, WriteRead writeRead) {
|
private static SavedState tryToSave(boolean isChangingConfig, final String prefixFileName, String suffixFileName, WriteRead writeRead) {
|
||||||
if (MainActivity.DEBUG) {
|
if (MainActivity.DEBUG) {
|
||||||
Log.d(TAG, "tryToSave() called with: isChangingConfig = [" + isChangingConfig + "], prefixFileName = [" + prefixFileName + "], suffixFileName = [" + suffixFileName + "], writeRead = [" + writeRead + "]");
|
Log.d(TAG, "tryToSave() called with: isChangingConfig = [" + isChangingConfig + "], prefixFileName = [" + prefixFileName + "], suffixFileName = [" + suffixFileName + "], writeRead = [" + writeRead + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
Queue<Object> savedObjects = new LinkedList<>();
|
LinkedList<Object> savedObjects = new LinkedList<>();
|
||||||
writeRead.writeTo(savedObjects);
|
writeRead.writeTo(savedObjects);
|
||||||
|
|
||||||
if (isChangingConfig) {
|
if (isChangingConfig) {
|
||||||
if (savedObjects.size() > 0) {
|
if (savedObjects.size() > 0) {
|
||||||
stateObjectsHolder.put(prefixFileName, savedObjects);
|
stateObjectsHolder.put(prefixFileName, savedObjects);
|
||||||
return new SavedState(prefixFileName, "");
|
return new SavedState(prefixFileName, "");
|
||||||
} else return null;
|
} else {
|
||||||
|
if(MainActivity.DEBUG) Log.d(TAG, "Nothing to save");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FileOutputStream fileOutputStream = null;
|
FileOutputStream fileOutputStream = null;
|
||||||
|
@ -197,8 +223,12 @@ public class StateSaver {
|
||||||
if (!cacheDir.exists()) throw new RuntimeException("Cache dir does not exist > " + cacheDirPath);
|
if (!cacheDir.exists()) throw new RuntimeException("Cache dir does not exist > " + cacheDirPath);
|
||||||
cacheDir = new File(cacheDir, CACHE_DIR_NAME);
|
cacheDir = new File(cacheDir, CACHE_DIR_NAME);
|
||||||
if (!cacheDir.exists()) {
|
if (!cacheDir.exists()) {
|
||||||
boolean mkdirResult = cacheDir.mkdir();
|
if(!cacheDir.mkdir()) {
|
||||||
if (!mkdirResult) return null;
|
if(BuildConfig.DEBUG) {
|
||||||
|
Log.e(TAG, "Failed to create cache directory " + cacheDir.getAbsolutePath());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TextUtils.isEmpty(suffixFileName)) suffixFileName = ".cache";
|
if (TextUtils.isEmpty(suffixFileName)) suffixFileName = ".cache";
|
||||||
|
@ -214,7 +244,9 @@ public class StateSaver {
|
||||||
return name.contains(prefixFileName);
|
return name.contains(prefixFileName);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for (File file1 : files) file1.delete();
|
for (File fileToDelete : files) {
|
||||||
|
fileToDelete.delete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileOutputStream = new FileOutputStream(file);
|
fileOutputStream = new FileOutputStream(file);
|
||||||
|
@ -223,7 +255,7 @@ public class StateSaver {
|
||||||
|
|
||||||
return new SavedState(prefixFileName, file.getAbsolutePath());
|
return new SavedState(prefixFileName, file.getAbsolutePath());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
Log.e(TAG, "Failed to save state", e);
|
||||||
} finally {
|
} finally {
|
||||||
if (fileOutputStream != null) {
|
if (fileOutputStream != null) {
|
||||||
try {
|
try {
|
||||||
|
@ -241,11 +273,11 @@ public class StateSaver {
|
||||||
public static void onDestroy(SavedState savedState) {
|
public static void onDestroy(SavedState savedState) {
|
||||||
if (MainActivity.DEBUG) Log.d(TAG, "onDestroy() called with: savedState = [" + savedState + "]");
|
if (MainActivity.DEBUG) Log.d(TAG, "onDestroy() called with: savedState = [" + savedState + "]");
|
||||||
|
|
||||||
if (savedState != null && !TextUtils.isEmpty(savedState.pathFileSaved)) {
|
if (savedState != null && !TextUtils.isEmpty(savedState.getPathFileSaved())) {
|
||||||
stateObjectsHolder.remove(savedState.prefixFileSaved);
|
stateObjectsHolder.remove(savedState.getPrefixFileSaved());
|
||||||
try {
|
try {
|
||||||
//noinspection ResultOfMethodCallIgnored
|
//noinspection ResultOfMethodCallIgnored
|
||||||
new File(savedState.pathFileSaved).delete();
|
new File(savedState.getPathFileSaved()).delete();
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -271,9 +303,12 @@ public class StateSaver {
|
||||||
// Inner
|
// Inner
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Information about the saved state on the disk
|
||||||
|
*/
|
||||||
public static class SavedState implements Parcelable {
|
public static class SavedState implements Parcelable {
|
||||||
public String prefixFileSaved;
|
private final String prefixFileSaved;
|
||||||
public String pathFileSaved;
|
private final String pathFileSaved;
|
||||||
|
|
||||||
public SavedState(String prefixFileSaved, String pathFileSaved) {
|
public SavedState(String prefixFileSaved, String pathFileSaved) {
|
||||||
this.prefixFileSaved = prefixFileSaved;
|
this.prefixFileSaved = prefixFileSaved;
|
||||||
|
@ -287,7 +322,7 @@ public class StateSaver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return prefixFileSaved + " > " + pathFileSaved;
|
return getPrefixFileSaved() + " > " + getPathFileSaved();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -313,6 +348,22 @@ public class StateSaver {
|
||||||
return new SavedState[size];
|
return new SavedState[size];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the prefix of the saved file
|
||||||
|
* @return the file prefix
|
||||||
|
*/
|
||||||
|
public String getPrefixFileSaved() {
|
||||||
|
return prefixFileSaved;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the path to the saved file
|
||||||
|
* @return the path to the saved file
|
||||||
|
*/
|
||||||
|
public String getPathFileSaved() {
|
||||||
|
return pathFileSaved;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue