Konvert AppConfig
This commit is contained in:
1 parent
31e7943d22
commit
d74b5bff7b
8 files changed
+63
-168
No files matched your search
@@ -13,165 +13,63 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.hippo.ehviewer
|
||||
|
||||
package com.hippo.ehviewer;
|
||||
import android.os.Environment
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
import com.hippo.ehviewer.util.ReadableTime
|
||||
import com.hippo.ehviewer.yorozuya.FileUtils
|
||||
import splitties.init.appCtx
|
||||
import java.io.File
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
fun File.ensureDirectory() = if (exists()) isDirectory else mkdirs()
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
object AppConfig {
|
||||
const val APP_DIRNAME = "EhViewer"
|
||||
private const val DOWNLOAD = "download"
|
||||
private const val TEMP = "temp"
|
||||
private const val COPY = "copy"
|
||||
private const val PARSE_ERROR = "parse_error"
|
||||
private const val CRASH = "crash"
|
||||
|
||||
import com.hippo.ehviewer.client.exception.ParseException;
|
||||
import com.hippo.ehviewer.util.ReadableTime;
|
||||
import com.hippo.ehviewer.yorozuya.FileUtils;
|
||||
import com.hippo.ehviewer.yorozuya.IOUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class AppConfig {
|
||||
|
||||
public static final String APP_DIRNAME = "EhViewer";
|
||||
|
||||
private static final String DOWNLOAD = "download";
|
||||
private static final String TEMP = "temp";
|
||||
private static final String PARSE_ERROR = "parse_error";
|
||||
private static final String CRASH = "crash";
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static Context sContext;
|
||||
|
||||
public static void initialize(Context context) {
|
||||
sContext = context.getApplicationContext();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalAppDir() {
|
||||
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
|
||||
File dir = sContext.getExternalFilesDir(null);
|
||||
return FileUtils.ensureDirectory(dir) ? dir : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* mkdirs and get
|
||||
*/
|
||||
@Nullable
|
||||
public static File getDirInExternalAppDir(String filename) {
|
||||
File appFolder = getExternalAppDir();
|
||||
if (appFolder != null) {
|
||||
File dir = new File(appFolder, filename);
|
||||
return FileUtils.ensureDirectory(dir) ? dir : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getFileInExternalAppDir(String filename) {
|
||||
File appFolder = getExternalAppDir();
|
||||
if (appFolder != null) {
|
||||
File file = new File(appFolder, filename);
|
||||
return FileUtils.ensureFile(file) ? file : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getDefaultDownloadDir() {
|
||||
return getDirInExternalAppDir(DOWNLOAD);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalTempDir() {
|
||||
File dir = sContext.getExternalCacheDir();
|
||||
File file;
|
||||
if (null != dir && FileUtils.ensureDirectory(file = new File(dir, TEMP))) {
|
||||
return file;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalCopyTempDir() {
|
||||
File dir = sContext.getExternalCacheDir();
|
||||
File file;
|
||||
if (null != dir && FileUtils.ensureDirectory(file = new File(dir, "copy"))) {
|
||||
return file;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalParseErrorDir() {
|
||||
return getDirInExternalAppDir(PARSE_ERROR);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalCrashDir() {
|
||||
return getDirInExternalAppDir(CRASH);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getTempDir() {
|
||||
File dir = sContext.getCacheDir();
|
||||
File file;
|
||||
if (null != dir && FileUtils.ensureDirectory(file = new File(dir, TEMP))) {
|
||||
return file;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File createTempFile() {
|
||||
return FileUtils.createTempFile(getTempDir(), null);
|
||||
}
|
||||
|
||||
public static void saveParseErrorBody(@NonNull ParseException e) {
|
||||
File dir = getExternalParseErrorDir();
|
||||
if (null == dir) {
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".txt");
|
||||
OutputStream os = null;
|
||||
try {
|
||||
os = new FileOutputStream(file);
|
||||
String message = e.getMessage();
|
||||
String body = e.getBody();
|
||||
if (null != message) {
|
||||
os.write(message.getBytes(StandardCharsets.UTF_8));
|
||||
os.write('\n');
|
||||
private val externalAppDir: File?
|
||||
get() {
|
||||
if (Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()) {
|
||||
return appCtx.getExternalFilesDir(null)?.takeIf { it.ensureDirectory() }
|
||||
}
|
||||
os.write(body.getBytes(StandardCharsets.UTF_8));
|
||||
os.flush();
|
||||
} catch (IOException e1) {
|
||||
// Ignore
|
||||
} finally {
|
||||
IOUtils.closeQuietly(os);
|
||||
return null
|
||||
}
|
||||
|
||||
private fun getDirInExternalAppDir(filename: String) = externalAppDir?.run { File(this, filename).takeIf { it.ensureDirectory() } }
|
||||
|
||||
val defaultDownloadDir: File?
|
||||
get() = getDirInExternalAppDir(DOWNLOAD)
|
||||
|
||||
val externalTempDir: File?
|
||||
get() = appCtx.externalCacheDir?.run { File(this, TEMP).takeIf { it.ensureDirectory() } }
|
||||
|
||||
val externalCopyTempDir: File?
|
||||
get() = appCtx.externalCacheDir?.run { File(this, COPY).takeIf { it.ensureDirectory() } }
|
||||
|
||||
val externalParseErrorDir: File?
|
||||
get() = getDirInExternalAppDir(PARSE_ERROR)
|
||||
val externalCrashDir: File?
|
||||
get() = getDirInExternalAppDir(CRASH)
|
||||
val tempDir: File?
|
||||
get() = appCtx.cacheDir.run { File(this, TEMP).takeIf { it.ensureDirectory() } }
|
||||
|
||||
fun createTempFile(): File? {
|
||||
return FileUtils.createTempFile(tempDir, null)
|
||||
}
|
||||
|
||||
fun saveParseErrorBody(e: ParseException) {
|
||||
val dir = externalParseErrorDir ?: return
|
||||
File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".txt").writer().use {
|
||||
it.write(e.message)
|
||||
it.appendLine()
|
||||
it.append(e.body)
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getFilesDir(String name) {
|
||||
File dir = sContext.getFilesDir();
|
||||
if (dir == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
dir = new File(dir, name);
|
||||
if (dir.isDirectory() || dir.mkdirs()) {
|
||||
return dir;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
fun getFilesDir(name: String) = File(appCtx.filesDir, name).takeIf { it.ensureDirectory() }
|
||||
}
|
||||
@@ -74,7 +74,7 @@ object Crash {
|
||||
}
|
||||
|
||||
fun saveCrashLog(t: Throwable) {
|
||||
val dir = AppConfig.getExternalCrashDir() ?: return
|
||||
val dir = AppConfig.externalCrashDir ?: return
|
||||
val nowString = ReadableTime.getFilenamableTime(System.currentTimeMillis())
|
||||
val fileName = "crash-$nowString.log"
|
||||
val file = File(dir, fileName)
|
||||
|
||||
@@ -69,7 +69,6 @@ class EhApplication : Application(), ImageLoaderFactory {
|
||||
super.onCreate()
|
||||
System.loadLibrary("ehviewer")
|
||||
ReadableTime.initialize(this)
|
||||
AppConfig.initialize(this)
|
||||
AppCompatDelegate.setDefaultNightMode(Settings.theme)
|
||||
launchIO {
|
||||
launchIO {
|
||||
@@ -107,11 +106,11 @@ class EhApplication : Application(), ImageLoaderFactory {
|
||||
}
|
||||
|
||||
private fun clearTempDir() {
|
||||
var dir = AppConfig.getTempDir()
|
||||
var dir = AppConfig.tempDir
|
||||
if (null != dir) {
|
||||
FileUtils.deleteContent(dir)
|
||||
}
|
||||
dir = AppConfig.getExternalTempDir()
|
||||
dir = AppConfig.externalTempDir
|
||||
if (null != dir) {
|
||||
FileUtils.deleteContent(dir)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ package com.hippo.ehviewer
|
||||
import com.hippo.ehviewer.client.hosts
|
||||
import com.hippo.ehviewer.client.hostsDsl
|
||||
|
||||
// TODO: Documentation
|
||||
|
||||
val builtInHosts = hostsDsl {
|
||||
hosts("ehgt.org", "gt0.ehgt.org", "gt1.ehgt.org", "gt2.ehgt.org", "gt3.ehgt.org") {
|
||||
"37.48.89.44" blockedInCN false
|
||||
|
||||
@@ -1248,7 +1248,7 @@ var downloadLocation: UniFile
|
||||
encodedQuery(downloadQuery)
|
||||
encodedFragment(downloadFragment)
|
||||
}.build(),
|
||||
) ?: UniFile.fromFile(AppConfig.getDefaultDownloadDir())!!
|
||||
) ?: UniFile.fromFile(AppConfig.defaultDownloadDir)!!
|
||||
}
|
||||
set(value) = with(value.uri) {
|
||||
Settings.edit {
|
||||
|
||||
@@ -111,13 +111,13 @@ class AdvancedFragment : BasePreferenceFragment() {
|
||||
e.printStackTrace()
|
||||
}
|
||||
try {
|
||||
val zipFile = File(AppConfig.getExternalTempDir(), "logs.zip")
|
||||
val zipFile = File(AppConfig.externalTempDir, "logs.zip")
|
||||
if (zipFile.exists()) {
|
||||
zipFile.delete()
|
||||
}
|
||||
val files = ArrayList<File>()
|
||||
AppConfig.getExternalParseErrorDir()?.listFiles()?.let { files.addAll(it) }
|
||||
AppConfig.getExternalCrashDir()?.listFiles()?.let { files.addAll(it) }
|
||||
AppConfig.externalParseErrorDir?.listFiles()?.let { files.addAll(it) }
|
||||
AppConfig.externalCrashDir?.listFiles()?.let { files.addAll(it) }
|
||||
var finished = false
|
||||
var origin: BufferedInputStream? = null
|
||||
var out: ZipOutputStream? = null
|
||||
|
||||
@@ -88,7 +88,7 @@ class DownloadFragment : BasePreferenceFragment() {
|
||||
.setMessage(file.uri.toString())
|
||||
.setPositiveButton(R.string.pick_new_download_location) { _, _ -> openDirPickerL() }
|
||||
.setNeutralButton(R.string.reset_download_location) { _, _ ->
|
||||
val uniFile = UniFile.fromFile(AppConfig.getDefaultDownloadDir())
|
||||
val uniFile = UniFile.fromFile(AppConfig.defaultDownloadDir)
|
||||
if (uniFile != null) {
|
||||
downloadLocation = uniFile
|
||||
lifecycleScope.launchNonCancellable {
|
||||
|
||||
@@ -131,7 +131,7 @@ class ReaderActivity : EhActivity() {
|
||||
CreateDocument("todo/todo"),
|
||||
) { uri ->
|
||||
if (uri != null) {
|
||||
val filepath = AppConfig.getExternalTempDir().toString() + File.separator + mCacheFileName
|
||||
val filepath = AppConfig.externalTempDir.toString() + File.separator + mCacheFileName
|
||||
val cachefile = File(filepath)
|
||||
lifecycleScope.launchIO {
|
||||
try {
|
||||
@@ -374,7 +374,7 @@ class ReaderActivity : EhActivity() {
|
||||
return
|
||||
}
|
||||
|
||||
val dir = AppConfig.getExternalTempDir()
|
||||
val dir = AppConfig.externalTempDir
|
||||
if (null == dir) {
|
||||
Toast.makeText(this, R.string.error_cant_create_temp_file, Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
@@ -432,7 +432,7 @@ class ReaderActivity : EhActivity() {
|
||||
return
|
||||
}
|
||||
|
||||
val dir = AppConfig.getExternalCopyTempDir()
|
||||
val dir = AppConfig.externalCopyTempDir
|
||||
if (null == dir) {
|
||||
Toast.makeText(this, R.string.error_cant_create_temp_file, Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
@@ -542,7 +542,7 @@ class ReaderActivity : EhActivity() {
|
||||
if (null == mGalleryProvider) {
|
||||
return
|
||||
}
|
||||
val dir = AppConfig.getExternalTempDir()
|
||||
val dir = AppConfig.externalTempDir
|
||||
if (null == dir) {
|
||||
Toast.makeText(this, R.string.error_cant_create_temp_file, Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user