Skip to content

Commit

Permalink
Material Design, AS 3.5 upgrade, formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Shingyx committed Sep 7, 2019
1 parent d341dd9 commit da155dd
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 118 deletions.
9 changes: 0 additions & 9 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions boomswitch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0-alpha09'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ data class BluetoothDeviceInfo(
): Comparable<BluetoothDeviceInfo> {
constructor(device: BluetoothDevice) : this(device.name, device.address)

override fun toString(): String {
return name
}

override fun compareTo(other: BluetoothDeviceInfo): Int {
return name.compareTo(other.name, true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private val READ_STATE_UUID = UUID.fromString("4356a21c-a599-4b94-a1c8-4b91fca02
private const val BOOM_INACTIVE_STATE = 0.toByte()
private const val BOOM_POWER_ON = 1.toByte()
private const val BOOM_STANDBY = 2.toByte()
private const val TIMEOUT = 15000.toLong()
private const val TIMEOUT = 15000L

object BoomClient {
private val lock = ReentrantLock()
Expand Down
107 changes: 62 additions & 45 deletions boomswitch/src/main/java/com/github/shingyx/boomswitch/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package com.github.shingyx.boomswitch

import android.app.AlertDialog
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.os.Handler
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.BaseAdapter
import android.widget.Filter
import android.widget.Filterable
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.android.synthetic.main.activity_main.*

private val TAG = MainActivity::class.java.simpleName
Expand All @@ -26,9 +29,9 @@ class MainActivity : AppCompatActivity() {
private lateinit var bluetoothStateReceiver: BroadcastReceiver

private val bluetoothOffAlertDialog by lazy {
AlertDialog.Builder(this)
MaterialAlertDialogBuilder(this)
.setMessage("Bluetooth is turned off. Please turn Bluetooth on then come back to this app.")
.setPositiveButton("OK", null)
.setPositiveButton(android.R.string.ok, null)
.create()
}

Expand All @@ -39,7 +42,7 @@ class MainActivity : AppCompatActivity() {
Preferences.initialize(this)
handler = Handler()
toaster = Toaster(this, handler)
adapter = BluetoothDeviceAdapter()
adapter = BluetoothDeviceAdapter(this)
bluetoothStateReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == BluetoothAdapter.ACTION_STATE_CHANGED) {
Expand All @@ -48,17 +51,11 @@ class MainActivity : AppCompatActivity() {
}
}

spinner.adapter = adapter
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
Log.v(TAG, "onNothingSelected")
Preferences.bluetoothDeviceInfo = null
}

override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
Log.v(TAG, "onItemSelected $position")
Preferences.bluetoothDeviceInfo = adapter.getItem(position)
}
select_speaker.setAdapter(adapter)
select_speaker.setText(Preferences.bluetoothDeviceInfo.toString())
select_speaker.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ ->
Log.v(TAG, "onItemClick $position")
Preferences.bluetoothDeviceInfo = adapter.getItem(position)
}

button.setOnClickListener {
Expand All @@ -72,7 +69,10 @@ class MainActivity : AppCompatActivity() {
}
}

registerReceiver(bluetoothStateReceiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
registerReceiver(
bluetoothStateReceiver,
IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
)
}

override fun onResume() {
Expand All @@ -86,50 +86,67 @@ class MainActivity : AppCompatActivity() {
}

private fun updateBluetoothDevices() {
var devices = BluetoothAdapter.getDefaultAdapter()
val bondedDevices = BluetoothAdapter.getDefaultAdapter()
?.takeIf { it.isEnabled }
?.bondedDevices
?.map { BluetoothDeviceInfo(it) }
?.sorted()

if (devices != null) {
var devicesInfo = bondedDevices?.map { BluetoothDeviceInfo(it) }?.sorted()

if (devicesInfo != null) {
bluetoothOffAlertDialog.hide()
} else {
bluetoothOffAlertDialog.show()
devices = emptyList()
devicesInfo = emptyList()
}
// TODO reselect current device
adapter.updateItems(devices)

adapter.updateItems(devicesInfo)
}
}

private inner class BluetoothDeviceAdapter : BaseAdapter() {
private var devices = emptyList<BluetoothDeviceInfo>()
private class BluetoothDeviceAdapter(
private val activity: Activity
) : BaseAdapter(), Filterable {
private var devices = emptyList<BluetoothDeviceInfo>()
private val filter = NoFilter()

fun updateItems(items: List<BluetoothDeviceInfo>) {
devices = items
notifyDataSetChanged()
}
fun updateItems(items: List<BluetoothDeviceInfo>) {
devices = items
notifyDataSetChanged()
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = convertView
?: activity.layoutInflater.inflate(R.layout.dropdown_menu_popup_item, parent, false)
(view as TextView).text = devices[position].toString()
return view
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = convertView
?: layoutInflater.inflate(R.layout.spinner_bluetooth_device_info, parent, false)
override fun getItem(position: Int): BluetoothDeviceInfo {
return devices[position]
}

val deviceInfo = devices[position]
view.findViewById<TextView>(R.id.device_name).text = deviceInfo.name
view.findViewById<TextView>(R.id.device_address).text = deviceInfo.address
return view
}
override fun getItemId(position: Int): Long {
return position.toLong()
}

override fun getItem(position: Int): BluetoothDeviceInfo {
return devices[position]
}
override fun getCount(): Int {
return devices.size
}

override fun getFilter(): Filter {
return filter
}

override fun getItemId(position: Int): Long {
return position.toLong()
private inner class NoFilter : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
return FilterResults().apply {
values = devices
count = devices.size
}
}

override fun getCount(): Int {
return devices.size
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
notifyDataSetChanged()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ class MainWidget : AppWidgetProvider() {
private lateinit var handler: Handler
private lateinit var toaster: Toaster

override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
for (appWidgetId in appWidgetIds) {
val intent = Intent(context, this.javaClass)
intent.action = BOOM_SWITCH
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

val views = RemoteViews(context.packageName, R.layout.main_widget)
views.setOnClickPendingIntent(R.id.widgetLayout, pendingIntent)

val intent = Intent(BOOM_SWITCH, null, context, this.javaClass)
views.setOnClickPendingIntent(
R.id.widgetLayout,
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
)

appWidgetManager.updateAppWidget(appWidgetId, views)
}
Expand Down
45 changes: 23 additions & 22 deletions boomswitch/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="32dp"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:labelFor="@id/spinner"
android:text="Select your BOOM"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

<Spinner
android:id="@+id/spinner"
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:paddingStart="48dp"
tools:ignore="RtlSymmetry" />
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:hint="BOOM Speaker">

<Button
<AutoCompleteTextView
android:id="@+id/select_speaker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
tools:ignore="LabelFor" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/button"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="20dp"
android:layout_marginTop="16dp"
android:text="Switch"
android:textAppearance="@style/TextAppearance.AppCompat.Button"
android:textSize="18sp" />
android:textAppearance="@style/TextAppearance.AppCompat.Button" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
7 changes: 7 additions & 0 deletions boomswitch/src/main/res/layout/dropdown_menu_popup_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:padding="16dp"
android:textAppearance="?attr/textAppearanceSubtitle1" />
25 changes: 0 additions & 25 deletions boomswitch/src/main/res/layout/spinner_bluetooth_device_info.xml

This file was deleted.

8 changes: 8 additions & 0 deletions boomswitch/src/main/res/values-night-v8/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">#64B5F6</item>
<item name="colorSecondary">#64B5F6</item>
<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.Dialog.Alert</item>
<item name="android:navigationBarColor">#000</item>
</style>
</resources>
9 changes: 5 additions & 4 deletions boomswitch/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<resources>

<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="android:navigationBarColor">@android:color/black</item>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">#1E88E5</item>
<item name="colorSecondary">#1E88E5</item>
<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.Dialog.Alert</item>
<item name="android:navigationBarColor">#000</item>
</style>

</resources>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.41'
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Loading

0 comments on commit da155dd

Please sign in to comment.