-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
416 additions
and
27 deletions.
There are no files selected for viewing
190 changes: 190 additions & 0 deletions
190
app/src/main/java/de/felixnuesse/timedsilence/dialogs/BluetoothDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package de.felixnuesse.timedsilence.dialogs | ||
|
||
import android.app.Dialog | ||
import android.content.Context | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.View | ||
import android.view.Window | ||
import android.view.WindowManager | ||
import android.widget.ArrayAdapter | ||
import de.felixnuesse.timedsilence.R | ||
import de.felixnuesse.timedsilence.databinding.DialogBluetoothBinding | ||
import de.felixnuesse.timedsilence.extensions.TAG | ||
import de.felixnuesse.timedsilence.fragments.BluetoothFragment | ||
import de.felixnuesse.timedsilence.handler.calculator.HeadsetHandler | ||
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_LOUD | ||
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_SILENT | ||
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_VIBRATE | ||
import de.felixnuesse.timedsilence.model.data.BluetoothObject | ||
import de.felixnuesse.timedsilence.model.database.DatabaseHandler | ||
import de.felixnuesse.timedsilence.util.WindowUtils | ||
|
||
|
||
/** | ||
* Copyright (C) 2024 Felix Nüsse | ||
* Created on 03.03.2024 | ||
* | ||
* Edited by: Felix Nüsse felix.nuesse(at)t-online.de | ||
* | ||
* | ||
* This program is released under the GPLv3 license | ||
* | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* | ||
* | ||
*/ | ||
class BluetoothDialog(context: Context) : Dialog(context, R.style.AlertDialogCustom) { | ||
|
||
private var fragment: BluetoothFragment? = null | ||
|
||
private lateinit var binding: DialogBluetoothBinding | ||
|
||
private var selectedDeviceName = "" | ||
|
||
constructor(context: Context, fragment: BluetoothFragment) : this(context) { | ||
this.fragment = fragment | ||
} | ||
|
||
private var state: Int = 0 | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
requestWindowFeature(Window.FEATURE_NO_TITLE) | ||
|
||
window?.let { WindowUtils.applyDialogPaddingFixForDarkmode(context, it) } | ||
|
||
binding = DialogBluetoothBinding.inflate(layoutInflater) | ||
val view = binding.root | ||
setContentView(view) | ||
|
||
window!!.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT) | ||
setCanceledOnTouchOutside(true) | ||
|
||
val devices = arrayListOf<String>() | ||
|
||
val existingDevices = HashMap<String, String>() | ||
HeadsetHandler.getPairedDevicesWithChangesInVolume(context).forEach{ | ||
existingDevices[it.name] = it.address | ||
} | ||
|
||
HeadsetHandler.getPairedDevices(context).forEach{ | ||
if(!existingDevices.containsKey(it.name)){ | ||
devices.add(it.name) | ||
} | ||
} | ||
|
||
ArrayAdapter(context, android.R.layout.simple_list_item_1, devices).also { | ||
adapter -> binding.filledExposedDropdown.setAdapter(adapter) | ||
} | ||
|
||
binding.filledExposedDropdown.setOnItemClickListener{ adapter, _, position, _ -> | ||
selectedDeviceName = adapter.adapter.getItem(position).toString() | ||
} | ||
|
||
hideAll() | ||
binding.bluetoothBack.visibility = View.INVISIBLE | ||
binding.bluetoothDialogTitle.text = context.getText(R.string.bluetooth_dialog_title_title) | ||
binding.bluetoothLayout.visibility = View.VISIBLE | ||
|
||
binding.bluetoothNext.setOnClickListener { | ||
Log.e(TAG(), "BluetoothDialog: next!") | ||
|
||
hideAll() | ||
state++ | ||
decideState() | ||
} | ||
|
||
binding.bluetoothBack.setOnClickListener { | ||
Log.e(TAG(), "BluetoothDialog: back!") | ||
|
||
hideAll() | ||
state-- | ||
decideState() | ||
} | ||
|
||
binding.bluetoothCancel.setOnClickListener { | ||
Log.e(TAG(), "BluetoothDialog: cancel!") | ||
this.cancel() | ||
} | ||
|
||
binding.bluetoothSave.setOnClickListener { | ||
Log.e(TAG(), "BluetoothDialog: save!") | ||
|
||
var device = BluetoothObject("", "") | ||
HeadsetHandler.getPairedDevices(context).forEach{ | ||
if(it.name == selectedDeviceName) { | ||
device = BluetoothObject(it.name, it.address) | ||
} | ||
} | ||
|
||
if(device.address.isEmpty()) { | ||
this.cancel() | ||
} | ||
|
||
device.volumeState = getValueForVolumeRadioGroup() | ||
DatabaseHandler(context).addOrUpdateBluetooth(device) | ||
fragment?.notifyChange() | ||
this.cancel() | ||
} | ||
} | ||
|
||
private fun hideAll() { | ||
binding.bluetoothLayout.visibility = View.GONE | ||
binding.bluetoothDialogRbVolume.visibility = View.GONE | ||
} | ||
|
||
private fun getValueForVolumeRadioGroup(): Int{ | ||
when (binding.bluetoothDialogRbVolume.checkedRadioButtonId) { | ||
R.id.keyword_dialog_rb_loud -> return TIME_SETTING_LOUD | ||
R.id.keyword_dialog_rb_silent -> return TIME_SETTING_SILENT | ||
R.id.keyword_dialog_rb_vibrate -> return TIME_SETTING_VIBRATE | ||
} | ||
return TIME_SETTING_VIBRATE | ||
} | ||
|
||
private fun decideState() { | ||
|
||
if(state==0){ | ||
binding.bluetoothBack.visibility = View.INVISIBLE | ||
binding.bluetoothSave.visibility = View.GONE | ||
binding.bluetoothNext.visibility = View.VISIBLE | ||
}else if (state == 1){ | ||
binding.bluetoothSave.visibility = View.VISIBLE | ||
binding.bluetoothBack.visibility = View.VISIBLE | ||
binding.bluetoothNext.visibility = View.GONE | ||
}else { | ||
binding.bluetoothBack.visibility = View.VISIBLE | ||
binding.bluetoothNext.visibility = View.VISIBLE | ||
binding.bluetoothSave.visibility = View.GONE | ||
} | ||
|
||
when (state) { | ||
0 -> { | ||
binding.bluetoothDialogTitle.text = context.getText(R.string.keyword_dialog_title_title) | ||
binding.bluetoothLayout.visibility = View.VISIBLE | ||
} | ||
1 -> { | ||
binding.bluetoothDialogTitle.text = context.getText(R.string.schedule_dialog_title_volume) | ||
binding.bluetoothDialogRbVolume.visibility = View.VISIBLE | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="fill_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:background="?attr/colorPrimaryContainer" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/bluetooth_dialog_title" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:layout_margin="10dp" | ||
android:textSize="15sp" | ||
android:textStyle="bold" /> | ||
</LinearLayout> | ||
|
||
<ScrollView | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
|
||
<LinearLayout | ||
android:id="@+id/bluetooth_layout" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
android:padding="10dp"> | ||
|
||
<com.google.android.material.textfield.TextInputLayout | ||
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:hint="@string/dialog_bluetooth_select_device"> | ||
|
||
<com.google.android.material.textfield.MaterialAutoCompleteTextView | ||
android:id="@+id/filled_exposed_dropdown" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:inputType="none" /> | ||
</com.google.android.material.textfield.TextInputLayout> | ||
</LinearLayout> | ||
|
||
|
||
<RadioGroup | ||
android:id="@+id/bluetooth_dialog_rb_volume" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:padding="10dp"> | ||
|
||
<RadioButton | ||
android:id="@+id/bluetooth_dialog_rb_loud" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:text="@string/volume_setting_loud" /> | ||
|
||
<RadioButton | ||
android:id="@+id/bluetooth_dialog_rb_silent" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:text="@string/volume_setting_silent" /> | ||
|
||
<RadioButton | ||
android:id="@+id/bluetooth_dialog_rb_vibrate" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:checked="true" | ||
android:text="@string/volume_setting_vibrate" /> | ||
</RadioGroup> | ||
|
||
</LinearLayout> | ||
|
||
</ScrollView> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="horizontal" | ||
tools:visibility="visible"> | ||
|
||
<Button | ||
android:id="@+id/bluetoothCancel" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:background="@android:color/transparent" | ||
android:minWidth="0dp" | ||
android:text="@string/cancel" /> | ||
|
||
<Space | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" /> | ||
|
||
<Button | ||
android:id="@+id/bluetoothBack" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:background="@android:color/transparent" | ||
android:minWidth="0dp" | ||
android:text="@string/back" /> | ||
|
||
<Button | ||
android:id="@+id/bluetoothNext" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:background="@android:color/transparent" | ||
android:minWidth="0dp" | ||
android:text="@string/next" /> | ||
|
||
<Button | ||
android:id="@+id/bluetoothSave" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:background="@android:color/transparent" | ||
android:minWidth="0dp" | ||
android:text="@string/calendar_dialog_save" | ||
android:visibility="gone" /> | ||
</LinearLayout> | ||
|
||
|
||
</LinearLayout> |
Oops, something went wrong.