Skip to content

Commit

Permalink
Implemented Voice Search
Browse files Browse the repository at this point in the history
  • Loading branch information
somenath1435 committed Mar 20, 2019
1 parent 3ab8922 commit 313647d
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<application
android:name=".OpenEventGeneral"
Expand All @@ -16,8 +17,8 @@
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustPan"
android:theme="@style/SplashTheme">
android:theme="@style/SplashTheme"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -26,6 +27,6 @@
</activity>
<meta-data
android:name="com.stripe.android.API_KEY"
android:value="${STRIPE_API_TOKEN}"/>
android:value="${STRIPE_API_TOKEN}" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.fossasia.openevent.general.search

import android.Manifest
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.speech.RecognizerIntent
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
Expand All @@ -16,14 +20,19 @@ import androidx.navigation.Navigation
import androidx.navigation.fragment.navArgs
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.fragment_search_location.search
import kotlinx.android.synthetic.main.fragment_search_location.view.voice_text
import kotlinx.android.synthetic.main.fragment_search_location.view.voice_button
import kotlinx.android.synthetic.main.fragment_search_location.view.currentLocation
import kotlinx.android.synthetic.main.fragment_search_location.view.locationProgressBar
import kotlinx.android.synthetic.main.fragment_search_location.view.search
import kotlinx.android.synthetic.main.fragment_search_location.view.currentLocation
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.utils.Utils
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.util.Locale

const val LOCATION_PERMISSION_REQUEST = 1000
const val REQ_CODE_SPEECH_INPUT = 100
const val AUDIO_PERMISSION_REQUEST = 1001

class SearchLocationFragment : Fragment() {
private lateinit var rootView: View
Expand All @@ -46,6 +55,18 @@ class SearchLocationFragment : Fragment() {
rootView.currentLocation.visibility = View.GONE
})


rootView.voice_button.setOnClickListener {
checkAudioPermission()
speechInput()
}

rootView.voice_text.setOnClickListener {
checkAudioPermission()
speechInput()
}


rootView.currentLocation.setOnClickListener {
checkLocationPermission()
geoLocationViewModel.configure()
Expand Down Expand Up @@ -86,6 +107,15 @@ class SearchLocationFragment : Fragment() {
}
}

private fun checkAudioPermission() {
val permission =
ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.RECORD_AUDIO)
if (permission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO,
Manifest.permission.RECORD_AUDIO), AUDIO_PERMISSION_REQUEST)
}
}

private fun redirectToMain() {
val fragmentId = if (safeArgs.fromSearchFragment) R.id.searchFragment else R.id.eventsFragment
Navigation.findNavController(rootView).popBackStack(fragmentId, false)
Expand All @@ -101,6 +131,13 @@ class SearchLocationFragment : Fragment() {
rootView.locationProgressBar.visibility = View.GONE
}
}
AUDIO_PERMISSION_REQUEST -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
speechInput()
} else {
Snackbar.make(rootView, R.string.no_audio_permission, Snackbar.LENGTH_SHORT).show()
}
}
}
}

Expand All @@ -114,4 +151,30 @@ class SearchLocationFragment : Fragment() {
else -> super.onOptionsItemSelected(item)
}
}

private fun speechInput() {
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt))

try {
//Takes Voice and converts to text
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT)
} catch (a: ActivityNotFoundException) {
Snackbar.make(rootView, getString(R.string.not_supported), Snackbar.LENGTH_SHORT).show()
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

when (requestCode) {
REQ_CODE_SPEECH_INPUT -> if (resultCode == Activity.RESULT_OK && null != data) {
val result: ArrayList<String> = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
//Entering output from voice to searchView
rootView.search.setQuery(result.get(0), true)
}
}
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_mic_pink_48dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:tint="#A64CA4"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,14c1.66,0 2.99,-1.34 2.99,-3L15,5c0,-1.66 -1.34,-3 -3,-3S9,3.34 9,5v6c0,1.66 1.34,3 3,3zM17.3,11c0,3 -2.54,5.1 -5.3,5.1S6.7,14 6.7,11L5,11c0,3.41 2.72,6.23 6,6.72L11,21h2v-3.28c3.28,-0.48 6,-3.3 6,-6.72h-1.7z" />
</vector>
25 changes: 25 additions & 0 deletions app/src/main/res/layout/fragment_search_location.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@
app:queryHint="@string/location_hint"
app:searchIcon="@null" />

<LinearLayout
android:id="@+id/voiceLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/voice_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="@dimen/padding_medium"
android:text="@string/use_voice_search"
android:textColor="@color/design_default_color_primary"
android:textSize="@dimen/text_size_extra_large" />

<ImageButton
android:id="@+id/voice_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/ic_mic_pink_48dp" />
</LinearLayout>

<LinearLayout
android:id="@+id/currentLocation"
android:layout_width="match_parent"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values-bn-rIN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,10 @@
<string name="invalid_email_message">অকার্যকর ইমেইল এড্রেস!</string>
<string name="invalid_confirm_password_message">আপনার পাসওয়ার্ড ও কন্ফার্ম পাসওয়ার্ড মিলছে না !</string>
<string name="invalid_password_message">খুব ছোট পাসওয়ার্ড!</string>

<!--Text to Speech-->
<string name="speech_prompt">কিছু বলুন</string>
<string name="not_supported">সমর্থিত নয়</string>
<string name="use_voice_search">ভয়েস অনুসন্ধান ব্যবহার করুন</string>
<string name="no_audio_permission">কোন অডিও অনুমতি নেই</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,9 @@
<string name="timeZone_title">Sử dụng múi giờ của sự kiện.</string>
<string name="timeZone_summary">Múi giờ của bạn sẽ được sử dụng khi khóa lại.</string>
<string name="hide">(giấu)</string>
<!--Text to Speech-->
<string name="speech_prompt">Nói gì đó đi</string>
<string name="not_supported">Không được hỗ trợ</string>
<string name="use_voice_search">Sử dụng Tìm kiếm bằng giọng nói</string>
<string name="no_audio_permission">Không có quyền âm thanh</string>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,10 @@
<string name="invalid_confirm_password_message">Your password and confirmation password do not match!</string>
<string name="invalid_password_message">Password too short!</string>

<!--Text to Speech-->
<string name="speech_prompt">Say something</string>
<string name="not_supported">Not supported</string>
<string name="use_voice_search">Use Voice Search</string>
<string name="no_audio_permission">No Audio Permission</string>

</resources>

0 comments on commit 313647d

Please sign in to comment.