Skip to content

Commit

Permalink
Create dialog to ask use voice assistant and setup
Browse files Browse the repository at this point in the history
  • Loading branch information
WSTxda committed Feb 7, 2024
1 parent 12793ef commit 6f66227
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
26 changes: 26 additions & 0 deletions .idea/appInsightsSettings.xml

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

60 changes: 49 additions & 11 deletions app/src/main/java/com/wstxda/voicegpt/VoiceGptActivity.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
package com.wstxda.voicegpt

import android.app.Activity
import android.app.AlertDialog
import android.content.ComponentName
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast

class VoiceGptActivity : Activity() {
public override fun onResume() {
super.onResume()
try {
val intent = Intent("android.intent.action.MAIN")
intent.addCategory("android.intent.category.LAUNCHER")
intent.component = ComponentName(
"com.openai.chatgpt", "com.openai.voice.assistant.AssistantActivity"
)
finish()

private lateinit var sharedPreferences: SharedPreferences

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedPreferences = getSharedPreferences("VoiceGptPrefs", MODE_PRIVATE)
if (!sharedPreferences.getBoolean("dialogShown", false)) {
showVoiceAssistantDialog()
} else {
openAssistantActivity()
}
}

private fun showVoiceAssistantDialog() {
AlertDialog.Builder(this)
.setTitle(R.string.assistant_dialog_title)
.setMessage(R.string.assistant_dialog_summary)
.setPositiveButton(R.string.assistant_dialog_setup) { _, _ ->
setAppAsVoiceAssistant()
}.setNegativeButton(R.string.assistant_dialog_cancel) { dialog, _ ->
openAssistantActivity()
dialog.dismiss()
sharedPreferences.edit().putBoolean("dialogShown", true).apply()
}.setOnCancelListener {
finish()
}.create().show()
}

private fun setAppAsVoiceAssistant() {
sharedPreferences.edit().putBoolean("dialogShown", true).apply()
startActivity(Intent(Settings.ACTION_VOICE_INPUT_SETTINGS))
finish()
}


private fun openAssistantActivity() {
val intent = Intent().apply {
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
component =
ComponentName("com.openai.chatgpt", "com.openai.voice.assistant.AssistantActivity")
}
try {
startActivity(intent)
} catch (unused: Exception) {
finish()
} catch (e: Exception) {
Toast.makeText(applicationContext, R.string.voice_gpt_not_found, Toast.LENGTH_SHORT)
.show()
finish()
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/color.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_launcher_background">#000000</color>
<color name="color_accent">#7896FF</color>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<resources>
<string name="app_name">VoiceGPT</string>

<string name="assistant_dialog_title">Use voice assistant?</string>
<string name="assistant_dialog_summary">Set VoiceGPT as the default voice assistant to use with gestures and buttons?</string>
<string name="assistant_dialog_setup">Use assistant</string>
<string name="assistant_dialog_cancel">Cancel</string>

<string name="voice_gpt_not_found">Activity not found. Please install or enable ChatGPT</string>
</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<resources>

<style name="Theme.Transparent" parent="@android:style/Theme.Material.Light">
<style name="Theme.Transparent" parent="@android:style/Theme.Material">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:colorBackgroundFloating">@android:color/black</item>
<item name="android:colorAccent">@color/color_accent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
Expand Down

0 comments on commit 6f66227

Please sign in to comment.