@@ -2,6 +2,7 @@ package io.github.xei.police.joystick
22
33
44import android.app.Activity
5+ import android.bluetooth.BluetoothAdapter
56import android.content.ActivityNotFoundException
67import android.content.Intent
78import android.os.Bundle
@@ -14,6 +15,7 @@ import android.widget.ImageButton
1415import android.widget.Toast
1516
1617import io.github.xei.police.R
18+ import io.github.xei.police.exception.BluetoothNotSupportException
1719
1820
1921/* *
@@ -24,7 +26,8 @@ import io.github.xei.police.R
2426class JoystickFragment : Fragment (), JoystickContract.View, View.OnClickListener {
2527
2628 companion object {
27- private const val REQUEST_CODE_SPEECH_RECOGNIZE = 100
29+ private const val REQUEST_CODE_ENABLE_BLUETOOTH = 100
30+ private const val REQUEST_CODE_SPEECH_RECOGNIZE = 200
2831
2932 fun newInstance () = JoystickFragment ()
3033 }
@@ -33,31 +36,50 @@ class JoystickFragment : Fragment(), JoystickContract.View, View.OnClickListener
3336 override val isActive: Boolean
3437 get() = isAdded
3538
39+ private lateinit var mUpArrowKeyImageButton: ImageButton
3640 private lateinit var mVoiceCommandImageButton: ImageButton
3741
38- override fun onCreateView (inflater : LayoutInflater ? , container : ViewGroup ? ,
42+ 43+ override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? ,
3944 savedInstanceState : Bundle ? ): View ? {
4045
41- val rootView = inflater!! .inflate(R .layout.fragment_joystick, container, false )
46+ val rootView = inflater.inflate(R .layout.fragment_joystick, container, false )
4247
4348 findViews(rootView)
4449 setOnClickListeners()
4550
51+ presenter.start()
52+ 4653 return rootView
4754 }
4855
4956 override fun findViews (rootView : View ) {
57+ mUpArrowKeyImageButton = rootView.findViewById(R .id.fragmentJoystick_imageButton_upArrowButton)
5058 mVoiceCommandImageButton = rootView.findViewById(R .id.layoutFragmentJoystickCenter_imageButton_voiceCommand)
5159 }
5260
5361 override fun setOnClickListeners () {
62+ mUpArrowKeyImageButton.setOnClickListener(this )
5463 mVoiceCommandImageButton.setOnClickListener(this )
5564 }
5665
5766 override fun showToast (text : String ) {
5867 Toast .makeText(context, text, Toast .LENGTH_SHORT ).show()
5968 }
6069
70+ override fun isBluetoothEnabled (): Boolean {
71+ try {
72+ return BluetoothAdapter .getDefaultAdapter().isEnabled
73+ } catch (npe: NullPointerException ) {
74+ throw BluetoothNotSupportException ()
75+ }
76+ }
77+ 78+ override fun startEnableBluetoothActivityForResult () {
79+ val intent = Intent (BluetoothAdapter .ACTION_REQUEST_ENABLE )
80+ startActivityForResult(intent, REQUEST_CODE_ENABLE_BLUETOOTH )
81+ }
82+ 6183 private fun startVoiceRecognitionActivityForResult () {
6284 val intent = Intent (RecognizerIntent .ACTION_RECOGNIZE_SPEECH )
6385 intent.putExtra(RecognizerIntent .EXTRA_CALLING_PACKAGE , javaClass.`package`.name)
@@ -84,6 +106,12 @@ class JoystickFragment : Fragment(), JoystickContract.View, View.OnClickListener
84106 super .onActivityResult(requestCode, resultCode, data)
85107
86108 when (requestCode) {
109+ 110+ REQUEST_CODE_ENABLE_BLUETOOTH -> if (resultCode == Activity .RESULT_OK ) {
111+ // TODO: connect to device
112+ // TODO: change LED to green
113+ }
114+ 87115 REQUEST_CODE_SPEECH_RECOGNIZE -> if (resultCode == Activity .RESULT_OK && data != null ) {
88116 val recognizedCommand = data.getStringArrayListExtra(RecognizerIntent .EXTRA_RESULTS )
89117 presenter.performVoiceCommand(recognizedCommand[0 ])
0 commit comments