Skip to content

Commit

Permalink
bug fix release 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
teamclouday committed Jun 1, 2021
1 parent 0664c3b commit 386d8d6
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 45 deletions.
4 changes: 2 additions & 2 deletions Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.example.microphone"
minSdkVersion 21
targetSdkVersion 30
versionCode 4
versionName "1.3"
versionCode 5
versionName "1.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
4 changes: 3 additions & 1 deletion Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
android:icon="@drawable/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Microphone">
android:theme="@style/Theme.Microphone"
android:name=".DefaultApp">
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class BackgroundHelper : Service()
const val COMMAND_DISC_USB = 21

const val COMMAND_SET_IP = 30

const val COMMAND_GET_STATUS = 40
}

private var helperBluetooth : BluetoothHelper? = null
Expand Down Expand Up @@ -117,6 +119,7 @@ class BackgroundHelper : Service()
COMMAND_START_USB -> startUSB(msg)
COMMAND_STOP_USB -> stopUSB(msg)
COMMAND_SET_IP -> setIP(msg)
COMMAND_GET_STATUS -> getStatus(msg)
}
}
}
Expand Down Expand Up @@ -177,7 +180,7 @@ class BackgroundHelper : Service()
replyFailed(sender, replyData, COMMAND_START_BLUETOOTH)
return
}
mGlobalData.reset()
// mGlobalData.reset()
mJobBluetooth = mUIScope.launch {
withContext(Dispatchers.Default) {
helperBluetooth?.clean()
Expand Down Expand Up @@ -361,7 +364,7 @@ class BackgroundHelper : Service()
replyFailed(sender, replyData, COMMAND_START_USB)
return
}
mGlobalData.reset()
// mGlobalData.reset()
mJobUSB = mUIScope.launch {
withContext(Dispatchers.Default) {
helperUSB?.clean()
Expand Down Expand Up @@ -483,6 +486,20 @@ class BackgroundHelper : Service()
else replySuccess(sender, replyData, COMMAND_SET_IP)
}

fun getStatus(msg : Message)
{
val sender = msg.replyTo
val replyData = Bundle()
replyData.putBoolean("isBluetoothStarted", mGlobalState.isBluetoothStarted)
replyData.putBoolean("isUSBStarted", mGlobalState.isUSBStarted)
replyData.putBoolean("isAudioStarted", mGlobalState.isAudioStarted)
val reply = Message()
reply.data = replyData
reply.what = COMMAND_GET_STATUS
reply.replyTo = serviceMessenger
sender.send(reply)
}

private fun isConnected() : Boolean
{
return mGlobalState.isBluetoothStarted || mGlobalState.isUSBStarted
Expand Down
45 changes: 45 additions & 0 deletions Android/app/src/main/java/com/example/microphone/DefaultApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.microphone

import android.app.Application
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import android.os.Messenger

class DefaultApp : Application()
{
var mService : Messenger? = null
var mBound = false

private val mConnection = object : ServiceConnection
{
override fun onServiceConnected(name: ComponentName?, service: IBinder?)
{
mService = Messenger(service)
mBound = true
// notify current running activity that service is connected
val notifyIntent = Intent(applicationContext, MainActivity::class.java).apply {
action = Intent.ACTION_VIEW
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra("BackgroundHelperBound", true)
}
startActivity(notifyIntent)
}

override fun onServiceDisconnected(name: ComponentName?)
{
mService = null
mBound = false
}
}

override fun onCreate() {
super.onCreate()
// start and bind to service
val intent = Intent(this, BackgroundHelper::class.java)
// startService(intent)
bindService(intent, mConnection, Context.BIND_AUTO_CREATE)
}
}
147 changes: 108 additions & 39 deletions Android/app/src/main/java/com/example/microphone/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package com.example.microphone
import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.bluetooth.BluetoothAdapter
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.content.pm.PackageManager
import android.os.*
import androidx.appcompat.app.AppCompatActivity
import android.text.InputType
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SwitchCompat
Expand Down Expand Up @@ -48,21 +46,6 @@ class MainActivity : AppCompatActivity()
private var mService : Messenger? = null
private var mBound = false

private val mConnection = object : ServiceConnection
{
override fun onServiceConnected(name: ComponentName?, service: IBinder?)
{
mService = Messenger(service)
mBound = true
}

override fun onServiceDisconnected(name: ComponentName?)
{
mService = null
mBound = false
}
}

private lateinit var handlerThread : HandlerThread
private lateinit var mMessenger : Messenger
private lateinit var mMessengerLooper : Looper
Expand All @@ -74,9 +57,10 @@ class MainActivity : AppCompatActivity()
{
when(msg.what)
{
BackgroundHelper.COMMAND_SET_IP -> handleSetIP(msg)
BackgroundHelper.COMMAND_DISC_BTH -> handleBthDisconnect(msg)
BackgroundHelper.COMMAND_DISC_USB -> handleUSBDisconnect(msg)
BackgroundHelper.COMMAND_SET_IP -> handleSetIP(msg)
BackgroundHelper.COMMAND_DISC_BTH -> handleBthDisconnect(msg)
BackgroundHelper.COMMAND_DISC_USB -> handleUSBDisconnect(msg)
BackgroundHelper.COMMAND_GET_STATUS -> handleGetStatus(msg)
}
if(msg.what < BackgroundHelper.COMMAND_DISC_BTH)
{
Expand All @@ -93,7 +77,6 @@ class MainActivity : AppCompatActivity()
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

mLogTextView = findViewById(R.id.txt_log)
// set action bar title
supportActionBar?.setTitle(R.string.activity_name)
Expand All @@ -113,7 +96,10 @@ class MainActivity : AppCompatActivity()
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}

// first disable all buttons, wait for service reply
//findViewById<Button>(R.id.usb_connect).isClickable = false
//findViewById<Button>(R.id.bth_connect).isClickable = false
//findViewById<SwitchCompat>(R.id.audio_switch).isClickable = false
}

override fun onStart()
Expand All @@ -122,39 +108,56 @@ class MainActivity : AppCompatActivity()
// check for audio permission
if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), 0)
// enable adapter
if(!BluetoothAdapter.getDefaultAdapter().isEnabled)
{
val enableBthIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivity(enableBthIntent)
}
// start handler thread
handlerThread = HandlerThread("MainActivityStartArgs", Process.THREAD_PRIORITY_BACKGROUND)
handlerThread.start()
mMessengerLooper = handlerThread.looper
mMessengerHandler = ReplyHandler(handlerThread.looper)
mMessenger = Messenger(mMessengerHandler)
// start and bind to service
val intent = Intent(this, BackgroundHelper::class.java)
startService(intent)
bindService(intent, mConnection, Context.BIND_AUTO_CREATE)
// get variable from application
refreshAppVariables()
// get status
askForStatus()
}

override fun onStop() {
super.onStop()
stopService(intent)
if(mBound)
{
unbindService(mConnection)
mBound = false
}
mMessengerLooper.quitSafely()
ignore { handlerThread.join(1000) }
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
val extras = intent?.extras
if(extras?.getBoolean("BackgroundHelperBound") == true)
{
// get variable from application
refreshAppVariables()
// get status
askForStatus()
}
}

private fun refreshAppVariables()
{
mService = (application as DefaultApp).mService
mBound = (application as DefaultApp).mBound
}

// onclick for bluetooth button
fun onButtonBluetooth(view : View)
{
if(!mBound)
{
Toast.makeText(this, "Service not available", Toast.LENGTH_SHORT).show()
return
}
// enable adapter
if(!BluetoothAdapter.getDefaultAdapter().isEnabled)
{
val enableBthIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivity(enableBthIntent)
}
(view as Button).isClickable = false // lock button
if(isBluetoothStarted)
{
Expand All @@ -175,6 +178,11 @@ class MainActivity : AppCompatActivity()
// onclick for USB button
fun onButtonUSB(view : View)
{
if(!mBound)
{
Toast.makeText(this, "Service not available", Toast.LENGTH_SHORT).show()
return
}
(view as Button).isClickable = false // lock button
if(isUSBStarted)
{
Expand All @@ -196,6 +204,11 @@ class MainActivity : AppCompatActivity()
// basically similar to bluetooth function
fun onSwitchMic(view : View)
{
if(!mBound)
{
Toast.makeText(this, "Service not available", Toast.LENGTH_SHORT).show()
return
}
(view as SwitchCompat).isClickable = false // lock switch
if(isAudioStarted)
{
Expand All @@ -213,6 +226,57 @@ class MainActivity : AppCompatActivity()
}
}

private fun askForStatus()
{
if(!mBound) return
val reply = Message.obtain(null, BackgroundHelper.COMMAND_GET_STATUS, 0, 0)
reply.replyTo = mMessenger
mService?.send(reply)
}

fun handleGetStatus(msg : Message)
{
isBluetoothStarted = msg.data.getBoolean("isBluetoothStarted")
isUSBStarted = msg.data.getBoolean("isUSBStarted")
isAudioStarted = msg.data.getBoolean("isAudioStarted")
val bthButton = findViewById<Button>(R.id.bth_connect)
val usbButton = findViewById<Button>(R.id.usb_connect)
val aioSwitch = findViewById<SwitchCompat>(R.id.audio_switch)
runOnUiThread {
if(isBluetoothStarted)
{
bthButton.setText(R.string.turn_off_bth)
bthButton.isClickable = true

}
else
{
bthButton.setText(R.string.turn_on_bth)
bthButton.isClickable = true
}
if(isUSBStarted)
{
usbButton.setText(R.string.turn_off_usb)
usbButton.isClickable = true
}
else
{
usbButton.setText(R.string.turn_on_usb)
usbButton.isClickable = true
}
if(isAudioStarted)
{
aioSwitch.isChecked = true
aioSwitch.isClickable = true
}
else
{
aioSwitch.isChecked = false
aioSwitch.isClickable = true
}
}
}

fun handleSuccess(msg : Message)
{
val reply = msg.data.getString("reply")
Expand Down Expand Up @@ -441,11 +505,16 @@ class MainActivity : AppCompatActivity()

private fun showNotification()
{
val onTap = Intent(this, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
}
val pendingIntent = PendingIntent.getActivity(this, 0, onTap, 0)
val builder = NotificationCompat.Builder(this, "AndroidMic")
.setSmallIcon(R.drawable.icon)
.setContentTitle("AndroidMic")
.setContentText("Microphone is in use")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setOngoing(true)
with(NotificationManagerCompat.from(this))
{
Expand Down
2 changes: 1 addition & 1 deletion Windows/AndroidMic/AndroidMic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<ProductName>AndroidMic</ProductName>
<PublisherName>Teamclouday</PublisherName>
<ApplicationRevision>1</ApplicationRevision>
<ApplicationVersion>1.3.0.%2a</ApplicationVersion>
<ApplicationVersion>1.4.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
Expand Down

0 comments on commit 386d8d6

Please sign in to comment.