Remove Utils
This commit is contained in:
parent
8cc951a759
commit
e3e3fbe33f
3 changed files with 8 additions and 120 deletions
|
@ -1,56 +0,0 @@
|
|||
package com.wbrawner.simplemarkdown.utility;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public static final String KEY_AUTOSAVE = "autosave";
|
||||
|
||||
public static boolean isAutosaveEnabled(Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
return prefs.getBoolean(
|
||||
KEY_AUTOSAVE,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
public static void closeQuietly(Closeable closeable) {
|
||||
if (closeable != null) {
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean canAccessFiles(Context context) {
|
||||
return ContextCompat.checkSelfPermission(
|
||||
context,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||
) == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
public static Handler createSafeHandler(ErrorHandler errorHandler, String name) {
|
||||
HandlerThread handlerThread = new HandlerThread(name);
|
||||
handlerThread.start();
|
||||
handlerThread.setUncaughtExceptionHandler((t, e) -> {
|
||||
errorHandler.reportException(e);
|
||||
t.interrupt();
|
||||
});
|
||||
return new Handler(handlerThread.getLooper());
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ import android.content.pm.PackageManager
|
|||
import android.content.res.Configuration
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.preference.PreferenceManager
|
||||
import android.provider.OpenableColumns
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
|
@ -16,11 +17,11 @@ import android.widget.Toast
|
|||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.wbrawner.simplemarkdown.MarkdownApplication
|
||||
import com.wbrawner.simplemarkdown.R
|
||||
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter
|
||||
import com.wbrawner.simplemarkdown.utility.ErrorHandler
|
||||
import com.wbrawner.simplemarkdown.utility.Utils
|
||||
import com.wbrawner.simplemarkdown.view.adapter.EditPagerAdapter
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.coroutines.*
|
||||
|
@ -63,7 +64,9 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes
|
|||
|
||||
override fun onUserLeaveHint() {
|
||||
super.onUserLeaveHint()
|
||||
if (shouldAutoSave && presenter.markdown.isNotEmpty() && Utils.isAutosaveEnabled(this)) {
|
||||
val isAutoSaveEnabled = PreferenceManager.getDefaultSharedPreferences(this)
|
||||
.getBoolean(KEY_AUTOSAVE, true)
|
||||
if (shouldAutoSave && presenter.markdown.isNotEmpty() && isAutoSaveEnabled) {
|
||||
|
||||
launch {
|
||||
presenter.saveMarkdown("autosave.md", File(filesDir, "autosave.md").outputStream())
|
||||
|
@ -235,8 +238,8 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes
|
|||
}
|
||||
|
||||
private fun requestFileOp(requestType: Int) {
|
||||
if (!Utils.canAccessFiles(this@MainActivity)) {
|
||||
if (Build.VERSION.SDK_INT < 23) return
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
!= PackageManager.PERMISSION_GRANTED && Build.VERSION.SDK_INT > 22) {
|
||||
requestPermissions(
|
||||
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
|
||||
requestType
|
||||
|
@ -291,5 +294,6 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes
|
|||
const val REQUEST_OPEN_FILE = 1
|
||||
const val REQUEST_SAVE_FILE = 2
|
||||
const val REQUEST_DARK_MODE = 4
|
||||
const val KEY_AUTOSAVE = "autosave"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
package com.wbrawner.simplemarkdown;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.wbrawner.simplemarkdown.utility.Utils;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public class UtilsTest {
|
||||
private Context context;
|
||||
private SharedPreferences sharedPreferences;
|
||||
private String rootDir;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
context = RuntimeEnvironment.application.getApplicationContext();
|
||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
sharedPreferences.edit().clear().apply();
|
||||
rootDir = System.getProperty("java.io.tmpdir") + "/SimpleMarkdown";
|
||||
new File(rootDir).mkdir();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
rmdir(new File(rootDir));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAutosaveEnabled() throws Exception {
|
||||
assertTrue(Utils.isAutosaveEnabled(context));
|
||||
sharedPreferences.edit().putBoolean(Utils.KEY_AUTOSAVE, false).apply();
|
||||
assertFalse(Utils.isAutosaveEnabled(context));
|
||||
}
|
||||
|
||||
private void rmdir(File dir) {
|
||||
for (File file : dir.listFiles()) {
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
} else {
|
||||
rmdir(file);
|
||||
}
|
||||
}
|
||||
|
||||
dir.delete();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue