From 807a1008cad5c9d100dd7038f3872553576c7be6 Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 7 Nov 2017 20:38:57 +0100 Subject: Handle unavailable "external" storage ; Remove network list observer --- .../org/pacien/tincapp/activities/StartActivity.kt | 29 +++++++++++----------- .../java/org/pacien/tincapp/context/AppPaths.kt | 10 +++++--- .../org/pacien/tincapp/service/TincVpnService.kt | 3 +++ .../java/org/pacien/tincapp/utils/FileObserver.kt | 16 ------------ .../res/layout/fragment_network_list_header.xml | 3 +-- app/src/main/res/values/strings.xml | 1 + 6 files changed, 26 insertions(+), 36 deletions(-) delete mode 100644 app/src/main/java/org/pacien/tincapp/utils/FileObserver.kt (limited to 'app/src') diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt index 3163f0a..5926561 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt @@ -16,7 +16,6 @@ import org.pacien.tincapp.R import org.pacien.tincapp.context.AppPaths import org.pacien.tincapp.extensions.Android.setElements import org.pacien.tincapp.service.TincVpnService -import org.pacien.tincapp.utils.FileObserver /** * @author pacien @@ -24,14 +23,10 @@ import org.pacien.tincapp.utils.FileObserver class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { private var networkListAdapter: ArrayAdapter? = null - private var confChangeObserver: FileObserver? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - - networkListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) - confChangeObserver = FileObserver(AppPaths.confDir().absolutePath, FileObserver.CHANGE, { _, _ -> onRefresh() }) - + networkListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) layoutInflater.inflate(R.layout.fragment_list_view, main_content) list_wrapper.setOnRefreshListener(this) list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_list_header, list, false), null, false) @@ -46,7 +41,6 @@ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefr } override fun onDestroy() { - confChangeObserver = null networkListAdapter = null super.onDestroy() } @@ -54,12 +48,6 @@ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefr override fun onStart() { super.onRestart() onRefresh() - confChangeObserver?.startWatching() - } - - override fun onStop() { - super.onStop() - confChangeObserver?.stopWatching() } override fun onResume() { @@ -68,10 +56,10 @@ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefr } override fun onRefresh() { - val networks = AppPaths.confDir().list()?.toList() ?: emptyList() + val networks = AppPaths.confDir()?.list()?.toList() ?: emptyList() runOnUiThread { networkListAdapter?.setElements(networks) - network_list_placeholder.visibility = if (networkListAdapter?.isEmpty ?: true) View.VISIBLE else View.GONE + setPlaceholderVisibility() list_wrapper.isRefreshing = false } } @@ -86,4 +74,15 @@ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefr startActivity(Intent(this, StatusActivity::class.java) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)) + private fun setPlaceholderVisibility() = if (networkListAdapter?.isEmpty != false) { + network_list_placeholder.text = getListPlaceholderText() + network_list_placeholder.visibility = View.VISIBLE + } else { + network_list_placeholder.visibility = View.GONE + } + + private fun getListPlaceholderText() = + if (!AppPaths.storageAvailable()) getText(R.string.message_storage_unavailable) + else getText(R.string.message_no_network_configuration_found) + } diff --git a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt index 94780cc..005cded 100644 --- a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt +++ b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt @@ -1,11 +1,12 @@ package org.pacien.tincapp.context +import android.os.Environment import java.io.File import java.io.FileNotFoundException /** * @author pacien - * * + * * @implNote Logs and PID files are stored in the cache directory for easy clean up. */ object AppPaths { @@ -23,8 +24,11 @@ object AppPaths { private val NET_DEFAULT_ED25519_PRIVATE_KEY_FILE = "ed25519_key.priv" private val NET_DEFAULT_RSA_PRIVATE_KEY_FILE = "rsa_key.priv" - fun cacheDir() = App.getContext().externalCacheDir!! - fun confDir() = App.getContext().getExternalFilesDir(null)!! + fun storageAvailable() = + Environment.getExternalStorageState().let { it == Environment.MEDIA_MOUNTED && it != Environment.MEDIA_MOUNTED_READ_ONLY } + + fun cacheDir() = App.getContext().externalCacheDir + fun confDir() = App.getContext().getExternalFilesDir(null) fun binDir() = File(App.getContext().applicationInfo.nativeLibraryDir) fun confDir(netName: String) = File(confDir(), netName) diff --git a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt index f22e1e1..601ffbb 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt @@ -44,6 +44,9 @@ class TincVpnService : VpnService() { if (netName.isBlank()) return reportError(resources.getString(R.string.message_no_network_name_provided), docTopic = "intent-api") + if (!AppPaths.storageAvailable()) + return reportError(resources.getString(R.string.message_storage_unavailable)) + if (!AppPaths.confDir(netName).exists()) return reportError(resources.getString(R.string.message_no_configuration_for_network_format, netName), docTopic = "configuration") diff --git a/app/src/main/java/org/pacien/tincapp/utils/FileObserver.kt b/app/src/main/java/org/pacien/tincapp/utils/FileObserver.kt deleted file mode 100644 index 0370c47..0000000 --- a/app/src/main/java/org/pacien/tincapp/utils/FileObserver.kt +++ /dev/null @@ -1,16 +0,0 @@ -package org.pacien.tincapp.utils - -/** - * @author pacien - */ -class FileObserver(path: String, - mask: Int = android.os.FileObserver.ALL_EVENTS, - private val listener: (event: Int, path: String?) -> Unit) : android.os.FileObserver(path, mask) { - - override fun onEvent(event: Int, path: String?) = listener(event, path) - - companion object { - val CHANGE = CREATE or DELETE or MODIFY or MOVED_TO or MOVED_FROM - } - -} diff --git a/app/src/main/res/layout/fragment_network_list_header.xml b/app/src/main/res/layout/fragment_network_list_header.xml index 436cfc2..e323046 100644 --- a/app/src/main/res/layout/fragment_network_list_header.xml +++ b/app/src/main/res/layout/fragment_network_list_header.xml @@ -10,7 +10,6 @@ + style="@style/AppTheme.ListBlock.Placeholder"/> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2eac580..2849c8f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -74,6 +74,7 @@ No scanner could be started.\n\nInstall Barcode Scanner? Encrypting/decrypting private keys Could not decrypt private keys:\n\n%1$s + Storage directory is unavailable. none yes -- cgit v1.2.3