mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-17 11:25:34 -04:00
Extracted fileLogger to Utils
This commit is contained in:
parent
fdbeb9ddaf
commit
e8c68a7eb2
@ -20,10 +20,8 @@ package org.kiwix.kiwixmobile.core
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.os.StrictMode
|
||||
import android.os.StrictMode.VmPolicy
|
||||
import android.util.Log
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.multidex.MultiDex
|
||||
import com.jakewharton.threetenabp.AndroidThreeTen
|
||||
@ -31,8 +29,7 @@ import org.kiwix.kiwixmobile.core.data.local.KiwixDatabase
|
||||
import org.kiwix.kiwixmobile.core.di.components.CoreComponent
|
||||
import org.kiwix.kiwixmobile.core.di.components.DaggerCoreComponent
|
||||
import org.kiwix.kiwixmobile.core.downloader.DownloadMonitor
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.writeLogFile
|
||||
import javax.inject.Inject
|
||||
|
||||
abstract class CoreApp : Application() {
|
||||
@ -127,32 +124,4 @@ abstract class CoreApp : Application() {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun writeLogFile() {
|
||||
if (isExternalStorageWritable) {
|
||||
val appDirectory =
|
||||
File(Environment.getExternalStorageDirectory().toString() + "/Kiwix")
|
||||
val logFile = File(appDirectory, "logcat.txt")
|
||||
Log.d("KIWIX", "Writing all logs into [" + logFile.path + "]")
|
||||
// create app folder
|
||||
if (!appDirectory.exists()) {
|
||||
appDirectory.mkdir()
|
||||
}
|
||||
if (logFile.exists() && logFile.isFile) {
|
||||
logFile.delete()
|
||||
}
|
||||
// clear the previous logcat and then write the new one to the file
|
||||
try {
|
||||
logFile.createNewFile()
|
||||
Runtime.getRuntime().exec("logcat -c")
|
||||
Runtime.getRuntime().exec("logcat -f " + logFile.path + " -s kiwix")
|
||||
} catch (e: IOException) {
|
||||
Log.e("KIWIX", "Error while writing logcat.txt", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Checks if external storage is available for read and write */
|
||||
val isExternalStorageWritable: Boolean
|
||||
get() = Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()
|
||||
}
|
||||
|
@ -61,6 +61,8 @@ import org.kiwix.kiwixmobile.core.utils.AlertDialogShower;
|
||||
import org.kiwix.kiwixmobile.core.utils.KiwixDialog;
|
||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil;
|
||||
|
||||
import static org.kiwix.kiwixmobile.core.utils.files.FileUtils.isExternalStorageWritable;
|
||||
|
||||
/**
|
||||
* Created by @author Aditya-Sood (21/05/19) as a part of GSoC 2019
|
||||
*
|
||||
@ -324,7 +326,7 @@ public class AddNoteDialog extends DialogFragment {
|
||||
* "{External Storage}/Kiwix/Notes/ZimFileTitle/ArticleTitle.txt"
|
||||
* */
|
||||
|
||||
if (CoreApp.getInstance().isExternalStorageWritable()) {
|
||||
if (isExternalStorageWritable()) {
|
||||
|
||||
if (ContextCompat.checkSelfPermission(getContext(),
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
||||
|
@ -54,6 +54,7 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil;
|
||||
import static org.kiwix.kiwixmobile.core.utils.ConstantsKt.RESULT_RESTART;
|
||||
import static org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_NIGHT_MODE;
|
||||
import static org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_STORAGE;
|
||||
import static org.kiwix.kiwixmobile.core.utils.files.FileUtils.isExternalStorageWritable;
|
||||
|
||||
public abstract class CorePrefsFragment extends PreferenceFragmentCompat implements
|
||||
SettingsContract.View,
|
||||
@ -217,7 +218,7 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme
|
||||
}
|
||||
|
||||
private void clearAllNotes() {
|
||||
if (CoreApp.getInstance().isExternalStorageWritable()) {
|
||||
if (isExternalStorageWritable()) {
|
||||
if (ContextCompat.checkSelfPermission(getActivity(),
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
|
@ -31,6 +31,7 @@ import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book
|
||||
import org.kiwix.kiwixmobile.core.extensions.get
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.lang.Runtime.getRuntime
|
||||
import java.util.ArrayList
|
||||
|
||||
object FileUtils {
|
||||
@ -219,4 +220,34 @@ object FileUtils {
|
||||
File("$fileName.part").exists() -> "$fileName.part"
|
||||
else -> "${fileName}aa"
|
||||
}
|
||||
|
||||
@JvmStatic fun writeLogFile() {
|
||||
if (isExternalStorageWritable) {
|
||||
val appDirectory =
|
||||
File(Environment.getExternalStorageDirectory().toString() + "/Kiwix")
|
||||
val logFile = File(appDirectory, "logcat.txt")
|
||||
Log.d("KIWIX", "Writing all logs into [" + logFile.path + "]")
|
||||
|
||||
// create a new app folder
|
||||
if (!appDirectory.exists()) {
|
||||
appDirectory.mkdir()
|
||||
}
|
||||
|
||||
if (logFile.exists() && logFile.isFile) {
|
||||
logFile.delete()
|
||||
}
|
||||
// clear the previous logcat and then write the new one to the file
|
||||
try {
|
||||
logFile.createNewFile()
|
||||
getRuntime().exec("logcat -c")
|
||||
Runtime.getRuntime().exec("logcat -f " + logFile.path + " -s kiwix")
|
||||
} catch (e: IOException) {
|
||||
Log.e("KIWIX", "Error while writing logcat.txt", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Checks if external storage is available for read and write */
|
||||
@JvmStatic val isExternalStorageWritable: Boolean
|
||||
get() = Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user