Skip to content

Commit c044f11

Browse files
committed
fix exception on hostname detection
1 parent 40536b5 commit c044f11

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
33

44
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
56
<uses-permission android:name="android.permission.INTERNET" />
6-
<uses-permission android:name="android.permission.BLUETOOTH" />
77
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
88
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
99
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

app/src/main/java/de/lolhens/resticui/BackupManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ class BackupManager private constructor(context: Context) {
5757
ResticNameServers.fromContext(context)
5858
_restic = Restic(
5959
ResticStorage.fromContext(context),
60-
hostname = config.hostname ?: HostnameUtil.detectHostname(),
60+
hostname = config.hostname ?: HostnameUtil.detectHostname(context),
6161
nameServers = resticNameServers
6262
)
6363
}
6464

65-
fun setHostname(hostname: String?): String {
65+
fun setHostname(hostname: String?, defaultHostname: () -> String): String {
6666
configure { config ->
6767
config.copy(hostname = hostname)
6868
}
69-
val hostname = hostname ?: HostnameUtil.detectHostname()
69+
val hostname = hostname ?: defaultHostname()
7070
_restic = _restic.withHostname(hostname)
7171
return hostname
7272
}

app/src/main/java/de/lolhens/resticui/ui/settings/SettingsFragment.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.fragment.app.Fragment
88
import de.lolhens.resticui.BackupManager
99
import de.lolhens.resticui.databinding.FragmentSettingsBinding
1010
import de.lolhens.resticui.ui.InputDialogUtil
11+
import de.lolhens.resticui.util.HostnameUtil
1112

1213
class SettingsFragment : Fragment() {
1314
private var _binding: FragmentSettingsBinding? = null
@@ -66,7 +67,9 @@ class SettingsFragment : Fragment() {
6667
binding.textHostname.text = BackupManager.instance(requireContext()).setHostname(
6768
if (hostname.isBlank()) null
6869
else hostname.trim()
69-
)
70+
) {
71+
HostnameUtil.detectHostname(requireContext())
72+
}
7073
}
7174
}
7275

app/src/main/java/de/lolhens/resticui/util/HostnameUtil.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
package de.lolhens.resticui.util
22

3+
import android.Manifest
34
import android.bluetooth.BluetoothAdapter
5+
import android.content.Context
6+
import android.content.pm.PackageManager
7+
import androidx.core.app.ActivityCompat
48

59
object HostnameUtil {
610
private const val DEFAULT_HOSTNAME = "android-device"
711

8-
fun detectHostname(): String {
9-
val blueToothAdapter = BluetoothAdapter.getDefaultAdapter()
12+
fun detectHostname(context: Context): String {
13+
if (ActivityCompat.checkSelfPermission(
14+
context,
15+
Manifest.permission.BLUETOOTH_CONNECT
16+
) == PackageManager.PERMISSION_GRANTED
17+
) {
18+
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
1019

11-
if (blueToothAdapter != null) {
12-
// Some Devices do not have a BluetoothAdapter e.g. the Android Emulator. For this case we use a default
13-
// value
14-
return BluetoothAdapter.getDefaultAdapter().name
20+
// Some Devices do not have a BluetoothAdapter e.g. the Android Emulator
21+
if (bluetoothAdapter != null) {
22+
return bluetoothAdapter.name
23+
}
1524
}
1625

1726
return DEFAULT_HOSTNAME

0 commit comments

Comments
 (0)