Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: location issue with sending badges #603

Merged
merged 1 commit into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

<uses-feature android:name="android.hardware.location.gps" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.content.FileProvider
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
Expand All @@ -33,7 +32,7 @@ class SavedBadgesFragment : BaseFragment() {
companion object {
@JvmStatic
fun newInstance() =
SavedBadgesFragment()
SavedBadgesFragment()
}

private var recyclerAdapter: SaveAdapter? = null
Expand Down Expand Up @@ -94,10 +93,10 @@ class SavedBadgesFragment : BaseFragment() {
recyclerAdapter = SaveAdapter(requireContext(), files, object : OnSavedItemSelected {
override fun onEdit(item: ConfigInfo?) {
startActivity(
Intent(requireContext(), EditBadgeActivity::class.java).apply {
putExtra("badgeJSON", item?.badgeJSON)
putExtra("fileName", item?.fileName)
}
Intent(requireContext(), EditBadgeActivity::class.java).apply {
putExtra("badgeJSON", item?.badgeJSON)
putExtra("fileName", item?.fileName)
}
)
setPreviewNull()
recyclerAdapter?.resetSelectedItem()
Expand All @@ -112,13 +111,12 @@ class SavedBadgesFragment : BaseFragment() {
}

override fun export(item: ConfigInfo) {
if (bluetoothAdapter.isOn()) {
if (bluetoothAdapter.isTurnedOn(requireContext())) {
Toast.makeText(requireContext(), getString(R.string.sending_data), Toast.LENGTH_LONG).show()
SendingUtils.sendMessage(requireContext(), getSendData())
} else {
showAlertDialog()
}
}

override fun onSelected(item: ConfigInfo?) {
if (item != null)
setPreview(item.badgeJSON)
Expand Down Expand Up @@ -162,45 +160,29 @@ class SavedBadgesFragment : BaseFragment() {
builder.create().show()
}

private fun showAlertDialog() {
val dialogMessage = getString(R.string.enable_bluetooth)
val builder = android.app.AlertDialog.Builder(requireContext())
builder.setIcon(resources.getDrawable(R.drawable.ic_caution))
builder.setTitle(getString(R.string.permission_required))
builder.setMessage(dialogMessage)
builder.setPositiveButton("OK") { _, _ ->
bluetoothAdapter.turnBluetoothOn()
Toast.makeText(context, R.string.bluetooth_enabled, Toast.LENGTH_SHORT).show()
}
builder.setNegativeButton("CANCEL") { _, _ ->
Toast.makeText(context, R.string.enable_bluetooth, Toast.LENGTH_SHORT).show()
}
builder.create().show()
}

private fun setPreviewNull() {
preview_badge.setValue(
Converters.convertTextToLEDHex(
" ",
false
).second,
false,
false,
Speed.ONE,
Mode.LEFT
Converters.convertTextToLEDHex(
" ",
false
).second,
ifMar = false,
ifFla = false,
speed = Speed.ONE,
mode = Mode.LEFT
)
}

private fun setPreview(badgeJSON: String) {
val badgeConfig = SendingUtils.getBadgeFromJSON(badgeJSON)

preview_badge.setValue(
Converters.fixLEDHex(
badgeConfig?.hexStrings ?: listOf(), badgeConfig?.isInverted ?: false),
badgeConfig?.isMarquee ?: false,
badgeConfig?.isFlash ?: false,
badgeConfig?.speed ?: Speed.ONE,
badgeConfig?.mode ?: Mode.LEFT
Converters.fixLEDHex(
badgeConfig?.hexStrings ?: listOf(), badgeConfig?.isInverted ?: false),
badgeConfig?.isMarquee ?: false,
badgeConfig?.isFlash ?: false,
badgeConfig?.speed ?: Speed.ONE,
badgeConfig?.mode ?: Mode.LEFT
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ package org.fossasia.badgemagic.ui.fragments

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.VectorDrawable
import android.location.LocationManager
import android.os.AsyncTask
import android.os.Bundle
import android.provider.Settings
import android.text.Editable
import android.text.SpannableStringBuilder
import android.text.TextWatcher
Expand Down Expand Up @@ -122,49 +118,30 @@ class TextArtFragment : BaseFragment() {

transfer_button.setOnClickListener {
if (textViewMainText.text.trim().toString() != "") {
if (bluetoothAdapter.isOn()) {
if (scanLocationPermissions()) {
// Easter egg
Toast.makeText(requireContext(), getString(R.string.sending_data), Toast.LENGTH_LONG).show()

transfer_button.visibility = View.GONE
send_progress.visibility = View.VISIBLE

val buttonTimer = Timer()
buttonTimer.schedule(object : TimerTask() {
override fun run() {
activity?.runOnUiThread {
transfer_button.visibility = View.VISIBLE
send_progress.visibility = View.GONE
}
if (bluetoothAdapter.isTurnedOn(requireContext())) {
// Easter egg
Toast.makeText(requireContext(), getString(R.string.sending_data), Toast.LENGTH_LONG).show()

transfer_button.visibility = View.GONE
send_progress.visibility = View.VISIBLE

val buttonTimer = Timer()
buttonTimer.schedule(object : TimerTask() {
override fun run() {
activity?.runOnUiThread {
transfer_button.visibility = View.VISIBLE
send_progress.visibility = View.GONE
}
}, SCAN_TIMEOUT_MS)
}
}, SCAN_TIMEOUT_MS)

SendingUtils.sendMessage(requireContext(), getSendData())
}
} else
showAlertDialog()
SendingUtils.sendMessage(requireContext(), getSendData())
}
} else
Toast.makeText(requireContext(), getString(R.string.empty_text_to_send), Toast.LENGTH_LONG).show()
}
}

private fun scanLocationPermissions(): Boolean {
val lm = requireContext().getSystemService(Context.LOCATION_SERVICE)
if (lm is LocationManager) {
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
AlertDialog.Builder(requireContext())
.setMessage(R.string.no_gps_enabled)
.setPositiveButton("OK") { _, _ -> requireContext().startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) }
.setNegativeButton("Cancel", null)
.show()
return false
}
return true
}
return false
}

private fun startSaveFile() {
textViewMainText.hideKeyboard()
showSaveFileDialog()
Expand All @@ -190,22 +167,6 @@ class TextArtFragment : BaseFragment() {
true
}

private fun showAlertDialog() {
val dialogMessage = getString(R.string.enable_bluetooth)
val builder = AlertDialog.Builder(requireContext())
builder.setIcon(resources.getDrawable(R.drawable.ic_caution))
builder.setTitle(getString(R.string.permission_required))
builder.setMessage(dialogMessage)
builder.setPositiveButton("OK") { _, _ ->
bluetoothAdapter.turnBluetoothOn()
Toast.makeText(context, R.string.bluetooth_enabled, Toast.LENGTH_SHORT).show()
}
builder.setNegativeButton("CANCEL") { _, _ ->
Toast.makeText(context, R.string.enable_bluetooth, Toast.LENGTH_SHORT).show()
}
builder.create().show()
}

private val textChangedListener = object : TextWatcher {
var startPos = -1
var endPos = -1
Expand Down
53 changes: 49 additions & 4 deletions app/src/main/java/org/fossasia/badgemagic/util/BluetoothAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,62 @@
package org.fossasia.badgemagic.util

import android.app.AlertDialog
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.location.LocationManager
import android.provider.Settings
import android.widget.Toast
import androidx.core.content.ContextCompat
import org.fossasia.badgemagic.R

class BluetoothAdapter(val context: Context) {
private val btManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
class BluetoothAdapter(appContext: Context) {
private val btManager = appContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
private val btAdapter = btManager.adapter!!

fun isOn(): Boolean = btAdapter.isEnabled
fun isTurnedOn(context: Context): Boolean = when {
btAdapter.isEnabled -> scanLocationPermissions(context)
else -> {
showAlertDialog(context)
false
}
}

fun turnBluetoothOn() {
private fun turnBluetoothOn() {
if (btAdapter.disable()) {
btAdapter.enable()
}
}

private fun scanLocationPermissions(context: Context): Boolean {
val lm = context.getSystemService(Context.LOCATION_SERVICE)
if (lm is LocationManager) {
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
AlertDialog.Builder(context)
.setMessage(R.string.no_gps_enabled)
.setPositiveButton("OK") { _, _ -> context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) }
.setNegativeButton("Cancel", null)
.show()
return false
}
return true
}
return false
}

private fun showAlertDialog(context: Context) {
val dialogMessage = context.getString(R.string.enable_bluetooth)
val builder = AlertDialog.Builder(context)
builder.setIcon(ContextCompat.getDrawable(context, R.drawable.ic_caution))
builder.setTitle(context.getString(R.string.permission_required))
builder.setMessage(dialogMessage)
builder.setPositiveButton("OK") { _, _ ->
turnBluetoothOn()
Toast.makeText(context, R.string.bluetooth_enabled, Toast.LENGTH_SHORT).show()
}
builder.setNegativeButton("CANCEL") { _, _ ->
Toast.makeText(context, R.string.enable_bluetooth, Toast.LENGTH_SHORT).show()
}
builder.create().show()
}
}