From d74b5bff7b67448ce55fc8771bd923da7805314e Mon Sep 17 00:00:00 2001 From: tarsin Date: Sat, 3 Jun 2023 15:40:01 +0800 Subject: [PATCH] Konvert AppConfig --- .../main/java/com/hippo/ehviewer/AppConfig.kt | 204 +++++------------- app/src/main/java/com/hippo/ehviewer/Crash.kt | 2 +- .../java/com/hippo/ehviewer/EhApplication.kt | 5 +- .../java/com/hippo/ehviewer/EhHostsConfig.kt | 2 - .../ehviewer/download/DownloadManager.kt | 2 +- .../ehviewer/ui/fragment/AdvancedFragment.kt | 6 +- .../ehviewer/ui/fragment/DownloadFragment.kt | 2 +- .../tachiyomi/ui/reader/ReaderActivity.kt | 8 +- 8 files changed, 63 insertions(+), 168 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/AppConfig.kt b/app/src/main/java/com/hippo/ehviewer/AppConfig.kt index b361f1eac..ebda0695e 100644 --- a/app/src/main/java/com/hippo/ehviewer/AppConfig.kt +++ b/app/src/main/java/com/hippo/ehviewer/AppConfig.kt @@ -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() } } diff --git a/app/src/main/java/com/hippo/ehviewer/Crash.kt b/app/src/main/java/com/hippo/ehviewer/Crash.kt index f49d277e9..83eadf630 100644 --- a/app/src/main/java/com/hippo/ehviewer/Crash.kt +++ b/app/src/main/java/com/hippo/ehviewer/Crash.kt @@ -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) diff --git a/app/src/main/java/com/hippo/ehviewer/EhApplication.kt b/app/src/main/java/com/hippo/ehviewer/EhApplication.kt index 062bea982..b4ba29ac6 100644 --- a/app/src/main/java/com/hippo/ehviewer/EhApplication.kt +++ b/app/src/main/java/com/hippo/ehviewer/EhApplication.kt @@ -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) } diff --git a/app/src/main/java/com/hippo/ehviewer/EhHostsConfig.kt b/app/src/main/java/com/hippo/ehviewer/EhHostsConfig.kt index 201e8920a..51c6c3b2b 100644 --- a/app/src/main/java/com/hippo/ehviewer/EhHostsConfig.kt +++ b/app/src/main/java/com/hippo/ehviewer/EhHostsConfig.kt @@ -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 diff --git a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt index 78286570e..0d1f733e9 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt @@ -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 { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.kt b/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.kt index 3c0a4e0f4..416eff80e 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.kt @@ -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() - 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 diff --git a/app/src/main/java/com/hippo/ehviewer/ui/fragment/DownloadFragment.kt b/app/src/main/java/com/hippo/ehviewer/ui/fragment/DownloadFragment.kt index 8deb2241d..25cd6cc76 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/fragment/DownloadFragment.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/fragment/DownloadFragment.kt @@ -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 { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index eb2e2dd20..1390f48f2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -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