This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

superconfig / tools / hermes-voice / android / src / us / lassul / assist / SettingsActivity.java
3.9 kB 110 lines
1package us.lassul.assist; 2 3import android.Manifest; 4import android.app.Activity; 5import android.content.pm.PackageManager; 6import android.os.Bundle; 7import android.text.InputType; 8import android.view.View; 9import android.widget.Button; 10import android.widget.EditText; 11import android.widget.LinearLayout; 12import android.widget.ScrollView; 13import android.widget.TextView; 14 15/** 16 * Setup: the app token (`clan vars get coaxmetal hermes-voice-app/token`) and the microphone and 17 * notification permissions. 18 */ 19public class SettingsActivity extends Activity { 20 private TextView status; 21 private EditText token; 22 private Button permissions; 23 24 @Override 25 protected void onCreate(Bundle state) { 26 super.onCreate(state); 27 getActionBar().setDisplayHomeAsUpEnabled(true); 28 int pad = (int) (16 * getResources().getDisplayMetrics().density); 29 LinearLayout layout = new LinearLayout(this); 30 layout.setOrientation(LinearLayout.VERTICAL); 31 32 status = new TextView(this); 33 status.setPadding(0, 0, 0, pad); 34 layout.addView(status); 35 36 token = new EditText(this); 37 token.setHint("App token"); 38 token.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 39 // A password manager's popup over the field swallows the tap on Save. 40 token.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO); 41 layout.addView(token); 42 layout.addView(button("Save token", new View.OnClickListener() { 43 @Override 44 public void onClick(View view) { 45 if (token.getText().toString().trim().isEmpty()) { 46 return; 47 } 48 Api.saveToken(SettingsActivity.this, token.getText().toString()); 49 token.setText(""); 50 refresh(); 51 } 52 })); 53 54 permissions = button("Allow microphone and notifications", new View.OnClickListener() { 55 @Override 56 public void onClick(View view) { 57 requestPermissions(new String[] { 58 Manifest.permission.RECORD_AUDIO, Manifest.permission.POST_NOTIFICATIONS 59 }, 0); 60 } 61 }); 62 layout.addView(permissions); 63 64 TextView help = new TextView(this); 65 help.setPadding(0, pad, 0, 0); 66 help.setTextColor(Conversation.DIM); 67 help.setText("The token: clan vars get coaxmetal hermes-voice-app/token\n\n" 68 + "Hold the headset's voice button to talk to Hermes, a short press hangs up;" 69 + " a tone says it listens. Pick Assist when Android asks which app handles voice" 70 + " commands. On GrapheneOS, Assist also needs the Network permission."); 71 layout.addView(help); 72 73 ScrollView scroll = new ScrollView(this); 74 scroll.addView(layout); 75 Ui.fitBars(this, scroll, pad); 76 setContentView(scroll); 77 } 78 79 @Override 80 protected void onResume() { 81 super.onResume(); 82 refresh(); 83 } 84 85 @Override 86 public void onRequestPermissionsResult(int code, String[] permissions, int[] results) { 87 refresh(); 88 } 89 90 @Override 91 public boolean onNavigateUp() { 92 finish(); 93 return true; 94 } 95 96 private void refresh() { 97 boolean mic = checkSelfPermission(Manifest.permission.RECORD_AUDIO) 98 == PackageManager.PERMISSION_GRANTED; 99 status.setText("Token: " + (Api.hasToken(this) ? "set" : "missing") 100 + "\nMicrophone: " + (mic ? "allowed" : "not allowed")); 101 permissions.setVisibility(mic ? View.GONE : View.VISIBLE); 102 } 103 104 private Button button(String label, View.OnClickListener action) { 105 Button button = new Button(this); 106 button.setText(label); 107 button.setOnClickListener(action); 108 return button; 109 } 110}