aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorpacien2017-11-07 20:38:57 +0100
committerpacien2017-11-07 20:38:57 +0100
commit807a1008cad5c9d100dd7038f3872553576c7be6 (patch)
tree328b5a22a79bd03b53cdd6aecfa62bd2503feabc /app/src/main/java
parent24dbee4690fd102d9b723765fba5799c5790b001 (diff)
downloadtincapp-807a1008cad5c9d100dd7038f3872553576c7be6.tar.gz
Handle unavailable "external" storage ;
Remove network list observer
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt29
-rw-r--r--app/src/main/java/org/pacien/tincapp/context/AppPaths.kt10
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt3
-rw-r--r--app/src/main/java/org/pacien/tincapp/utils/FileObserver.kt16
4 files changed, 24 insertions, 34 deletions
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
16import org.pacien.tincapp.context.AppPaths 16import org.pacien.tincapp.context.AppPaths
17import org.pacien.tincapp.extensions.Android.setElements 17import org.pacien.tincapp.extensions.Android.setElements
18import org.pacien.tincapp.service.TincVpnService 18import org.pacien.tincapp.service.TincVpnService
19import org.pacien.tincapp.utils.FileObserver
20 19
21/** 20/**
22 * @author pacien 21 * @author pacien
@@ -24,14 +23,10 @@ import org.pacien.tincapp.utils.FileObserver
24class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { 23class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
25 24
26 private var networkListAdapter: ArrayAdapter<String>? = null 25 private var networkListAdapter: ArrayAdapter<String>? = null
27 private var confChangeObserver: FileObserver? = null
28 26
29 override fun onCreate(savedInstanceState: Bundle?) { 27 override fun onCreate(savedInstanceState: Bundle?) {
30 super.onCreate(savedInstanceState) 28 super.onCreate(savedInstanceState)
31 29 networkListAdapter = ArrayAdapter(this, R.layout.fragment_list_item)
32 networkListAdapter = ArrayAdapter<String>(this, R.layout.fragment_list_item)
33 confChangeObserver = FileObserver(AppPaths.confDir().absolutePath, FileObserver.CHANGE, { _, _ -> onRefresh() })
34
35 layoutInflater.inflate(R.layout.fragment_list_view, main_content) 30 layoutInflater.inflate(R.layout.fragment_list_view, main_content)
36 list_wrapper.setOnRefreshListener(this) 31 list_wrapper.setOnRefreshListener(this)
37 list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_list_header, list, false), null, false) 32 list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_list_header, list, false), null, false)
@@ -46,7 +41,6 @@ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefr
46 } 41 }
47 42
48 override fun onDestroy() { 43 override fun onDestroy() {
49 confChangeObserver = null
50 networkListAdapter = null 44 networkListAdapter = null
51 super.onDestroy() 45 super.onDestroy()
52 } 46 }
@@ -54,12 +48,6 @@ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefr
54 override fun onStart() { 48 override fun onStart() {
55 super.onRestart() 49 super.onRestart()
56 onRefresh() 50 onRefresh()
57 confChangeObserver?.startWatching()
58 }
59
60 override fun onStop() {
61 super.onStop()
62 confChangeObserver?.stopWatching()
63 } 51 }
64 52
65 override fun onResume() { 53 override fun onResume() {
@@ -68,10 +56,10 @@ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefr
68 } 56 }
69 57
70 override fun onRefresh() { 58 override fun onRefresh() {
71 val networks = AppPaths.confDir().list()?.toList() ?: emptyList() 59 val networks = AppPaths.confDir()?.list()?.toList() ?: emptyList()
72 runOnUiThread { 60 runOnUiThread {
73 networkListAdapter?.setElements(networks) 61 networkListAdapter?.setElements(networks)
74 network_list_placeholder.visibility = if (networkListAdapter?.isEmpty ?: true) View.VISIBLE else View.GONE 62 setPlaceholderVisibility()
75 list_wrapper.isRefreshing = false 63 list_wrapper.isRefreshing = false
76 } 64 }
77 } 65 }
@@ -86,4 +74,15 @@ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefr
86 startActivity(Intent(this, StatusActivity::class.java) 74 startActivity(Intent(this, StatusActivity::class.java)
87 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)) 75 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK))
88 76
77 private fun setPlaceholderVisibility() = if (networkListAdapter?.isEmpty != false) {
78 network_list_placeholder.text = getListPlaceholderText()
79 network_list_placeholder.visibility = View.VISIBLE
80 } else {
81 network_list_placeholder.visibility = View.GONE
82 }
83
84 private fun getListPlaceholderText() =
85 if (!AppPaths.storageAvailable()) getText(R.string.message_storage_unavailable)
86 else getText(R.string.message_no_network_configuration_found)
87
89} 88}
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 @@
1package org.pacien.tincapp.context 1package org.pacien.tincapp.context
2 2
3import android.os.Environment
3import java.io.File 4import java.io.File
4import java.io.FileNotFoundException 5import java.io.FileNotFoundException
5 6
6/** 7/**
7 * @author pacien 8 * @author pacien
8 * * 9 *
9 * @implNote Logs and PID files are stored in the cache directory for easy clean up. 10 * @implNote Logs and PID files are stored in the cache directory for easy clean up.
10 */ 11 */
11object AppPaths { 12object AppPaths {
@@ -23,8 +24,11 @@ object AppPaths {
23 private val NET_DEFAULT_ED25519_PRIVATE_KEY_FILE = "ed25519_key.priv" 24 private val NET_DEFAULT_ED25519_PRIVATE_KEY_FILE = "ed25519_key.priv"
24 private val NET_DEFAULT_RSA_PRIVATE_KEY_FILE = "rsa_key.priv" 25 private val NET_DEFAULT_RSA_PRIVATE_KEY_FILE = "rsa_key.priv"
25 26
26 fun cacheDir() = App.getContext().externalCacheDir!! 27 fun storageAvailable() =
27 fun confDir() = App.getContext().getExternalFilesDir(null)!! 28 Environment.getExternalStorageState().let { it == Environment.MEDIA_MOUNTED && it != Environment.MEDIA_MOUNTED_READ_ONLY }
29
30 fun cacheDir() = App.getContext().externalCacheDir
31 fun confDir() = App.getContext().getExternalFilesDir(null)
28 fun binDir() = File(App.getContext().applicationInfo.nativeLibraryDir) 32 fun binDir() = File(App.getContext().applicationInfo.nativeLibraryDir)
29 33
30 fun confDir(netName: String) = File(confDir(), netName) 34 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() {
44 if (netName.isBlank()) 44 if (netName.isBlank())
45 return reportError(resources.getString(R.string.message_no_network_name_provided), docTopic = "intent-api") 45 return reportError(resources.getString(R.string.message_no_network_name_provided), docTopic = "intent-api")
46 46
47 if (!AppPaths.storageAvailable())
48 return reportError(resources.getString(R.string.message_storage_unavailable))
49
47 if (!AppPaths.confDir(netName).exists()) 50 if (!AppPaths.confDir(netName).exists())
48 return reportError(resources.getString(R.string.message_no_configuration_for_network_format, netName), docTopic = "configuration") 51 return reportError(resources.getString(R.string.message_no_configuration_for_network_format, netName), docTopic = "configuration")
49 52
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 @@
1package org.pacien.tincapp.utils
2
3/**
4 * @author pacien
5 */
6class FileObserver(path: String,
7 mask: Int = android.os.FileObserver.ALL_EVENTS,
8 private val listener: (event: Int, path: String?) -> Unit) : android.os.FileObserver(path, mask) {
9
10 override fun onEvent(event: Int, path: String?) = listener(event, path)
11
12 companion object {
13 val CHANGE = CREATE or DELETE or MODIFY or MOVED_TO or MOVED_FROM
14 }
15
16}