This repository has no description
6.0 kB
166 lines
1package us.lassul.assist;
2
3import android.Manifest;
4import android.app.Activity;
5import android.content.Intent;
6import android.content.pm.PackageManager;
7import android.content.res.ColorStateList;
8import android.graphics.Typeface;
9import android.graphics.drawable.GradientDrawable;
10import android.os.Bundle;
11import android.view.Gravity;
12import android.view.Menu;
13import android.view.MenuItem;
14import android.view.View;
15import android.widget.Button;
16import android.widget.LinearLayout;
17import android.widget.ScrollView;
18import android.widget.TextView;
19import android.widget.Toast;
20
21/**
22 * The launcher screen: the current conversation, live during a call, and a Call / Hang up
23 * button. New conversation, the session list and the settings are in the menu.
24 */
25public class MainActivity extends Activity {
26 private static final int NEW_CONVERSATION = 1;
27 private static final int SESSIONS = 2;
28 private static final int SETTINGS = 3;
29
30 private TextView transcript;
31 private ColorStateList textColor;
32 private ScrollView scroll;
33 private TextView status;
34 private Button call;
35 private final Runnable redraw = new Runnable() {
36 @Override
37 public void run() {
38 refresh();
39 }
40 };
41
42 @Override
43 protected void onCreate(Bundle state) {
44 super.onCreate(state);
45 float dp = getResources().getDisplayMetrics().density;
46 LinearLayout layout = new LinearLayout(this);
47 layout.setOrientation(LinearLayout.VERTICAL);
48 Ui.fitBars(this, layout, Ui.dp(this, 16));
49
50 transcript = new TextView(this);
51 transcript.setTextSize(16);
52 transcript.setLineSpacing(0, 1.15f);
53 transcript.setTextIsSelectable(true);
54 textColor = transcript.getTextColors();
55 scroll = new ScrollView(this);
56 scroll.addView(transcript);
57 layout.addView(scroll,
58 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1));
59
60 status = new TextView(this);
61 status.setGravity(Gravity.CENTER);
62 status.setTextColor(Conversation.DIM);
63 status.setPadding(0, (int) (12 * dp), 0, (int) (12 * dp));
64 layout.addView(status);
65
66 call = new Button(this);
67 call.setTextColor(0xffffffff);
68 call.setTextSize(20);
69 call.setTypeface(Typeface.DEFAULT_BOLD);
70 call.setAllCaps(false);
71 call.setOnClickListener(new View.OnClickListener() {
72 @Override
73 public void onClick(View view) {
74 if (CallService.state() != null) {
75 startService(new Intent(MainActivity.this, CallService.class)
76 .setAction(CallService.ACTION_HANG_UP));
77 } else if (!ready()) {
78 startActivity(new Intent(MainActivity.this, SettingsActivity.class));
79 } else {
80 startForegroundService(new Intent(MainActivity.this, CallService.class)
81 .setAction(CallService.ACTION_START));
82 }
83 }
84 });
85 layout.addView(call, new LinearLayout.LayoutParams(
86 LinearLayout.LayoutParams.MATCH_PARENT, (int) (64 * dp)));
87 setContentView(layout);
88 }
89
90 @Override
91 protected void onResume() {
92 super.onResume();
93 Live.watch(redraw);
94 refresh();
95 }
96
97 @Override
98 protected void onPause() {
99 Live.unwatch(redraw);
100 super.onPause();
101 }
102
103 @Override
104 public boolean onCreateOptionsMenu(Menu menu) {
105 menu.add(Menu.NONE, NEW_CONVERSATION, 0, "New conversation");
106 menu.add(Menu.NONE, SESSIONS, 1, "Sessions");
107 menu.add(Menu.NONE, SETTINGS, 2, "Settings");
108 return true;
109 }
110
111 @Override
112 public boolean onOptionsItemSelected(MenuItem item) {
113 switch (item.getItemId()) {
114 case NEW_CONVERSATION:
115 if (CallService.state() != null) {
116 // The running call would save its conversation right back.
117 Toast.makeText(this, "Hang up first", Toast.LENGTH_SHORT).show();
118 } else {
119 Conversation.startNew(this);
120 }
121 return true;
122 case SESSIONS:
123 startActivity(new Intent(this, SessionsActivity.class));
124 return true;
125 case SETTINGS:
126 startActivity(new Intent(this, SettingsActivity.class));
127 return true;
128 default:
129 return super.onOptionsItemSelected(item);
130 }
131 }
132
133 private boolean ready() {
134 return Api.hasToken(this)
135 && checkSelfPermission(Manifest.permission.RECORD_AUDIO)
136 == PackageManager.PERMISSION_GRANTED;
137 }
138
139 private void refresh() {
140 String state = CallService.state();
141 boolean ready = ready();
142 GradientDrawable background = new GradientDrawable();
143 background.setCornerRadius(32 * getResources().getDisplayMetrics().density);
144 background.setColor(state != null ? Conversation.LIVE : Conversation.ACCENT);
145 call.setBackground(background);
146 call.setText(state != null ? "Hang up" : ready ? "Call" : "Set up");
147 status.setText(state != null
148 ? state
149 : ready ? "Hold the headset's voice button, or tap Call" : "Not set up yet");
150
151 Conversation current = Conversation.current(this);
152 if (current == null || current.lines.isEmpty()) {
153 transcript.setText("New conversation. What you and Hermes say shows up here.");
154 transcript.setTextColor(Conversation.DIM);
155 } else {
156 transcript.setText(current.render());
157 transcript.setTextColor(textColor);
158 }
159 scroll.post(new Runnable() {
160 @Override
161 public void run() {
162 scroll.fullScroll(View.FOCUS_DOWN);
163 }
164 });
165 }
166}