-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create dialog to ask use voice assistant and setup
- Loading branch information
Showing
5 changed files
with
85 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
60 changes: 49 additions & 11 deletions
60
app/src/main/java/com/wstxda/voicegpt/VoiceGptActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters