Skip to content

Commit

Permalink
fix exception on hostname detection
Browse files Browse the repository at this point in the history
  • Loading branch information
lhns committed Mar 14, 2023
1 parent 40536b5 commit c044f11
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/de/lolhens/resticui/BackupManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ class BackupManager private constructor(context: Context) {
ResticNameServers.fromContext(context)
_restic = Restic(
ResticStorage.fromContext(context),
hostname = config.hostname ?: HostnameUtil.detectHostname(),
hostname = config.hostname ?: HostnameUtil.detectHostname(context),
nameServers = resticNameServers
)
}

fun setHostname(hostname: String?): String {
fun setHostname(hostname: String?, defaultHostname: () -> String): String {
configure { config ->
config.copy(hostname = hostname)
}
val hostname = hostname ?: HostnameUtil.detectHostname()
val hostname = hostname ?: defaultHostname()
_restic = _restic.withHostname(hostname)
return hostname
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.fragment.app.Fragment
import de.lolhens.resticui.BackupManager
import de.lolhens.resticui.databinding.FragmentSettingsBinding
import de.lolhens.resticui.ui.InputDialogUtil
import de.lolhens.resticui.util.HostnameUtil

class SettingsFragment : Fragment() {
private var _binding: FragmentSettingsBinding? = null
Expand Down Expand Up @@ -66,7 +67,9 @@ class SettingsFragment : Fragment() {
binding.textHostname.text = BackupManager.instance(requireContext()).setHostname(
if (hostname.isBlank()) null
else hostname.trim()
)
) {
HostnameUtil.detectHostname(requireContext())
}
}
}

Expand Down
21 changes: 15 additions & 6 deletions app/src/main/java/de/lolhens/resticui/util/HostnameUtil.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package de.lolhens.resticui.util

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat

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

fun detectHostname(): String {
val blueToothAdapter = BluetoothAdapter.getDefaultAdapter()
fun detectHostname(context: Context): String {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.BLUETOOTH_CONNECT
) == PackageManager.PERMISSION_GRANTED
) {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

if (blueToothAdapter != null) {
// Some Devices do not have a BluetoothAdapter e.g. the Android Emulator. For this case we use a default
// value
return BluetoothAdapter.getDefaultAdapter().name
// Some Devices do not have a BluetoothAdapter e.g. the Android Emulator
if (bluetoothAdapter != null) {
return bluetoothAdapter.name
}
}

return DEFAULT_HOSTNAME
Expand Down

0 comments on commit c044f11

Please sign in to comment.