A third source next to voip.ms and Google Voice, while bubble is Android's default SMS app (Settings → SIM card asks for the role). SMS and MMS in and out, group chats as MMS, the phone's existing texts imported, and Android's message store kept as the record so another app still agrees. - Sim: role check, sending (SmsManager, waits for the radio), receiving, chunked import from content://sms and content://mms - Pdu: MMS wire format (notification-ind, retrieve-conf, send-req, ack) - SmsReceiver, MmsReceiver, MmsDownloaded, MmsFiles, RespondService, SendToActivity: what Android requires of a default SMS app - The import no longer shares a lock with Config.get, which froze the screens on the first copy of a long history
251 lines
9.9 KiB
Java
251 lines
9.9 KiB
Java
package me.teafry.voipmessages;
|
|
|
|
import android.content.Context;
|
|
import android.os.Bundle;
|
|
import android.text.Editable;
|
|
import android.text.TextWatcher;
|
|
import android.util.TypedValue;
|
|
import android.view.Gravity;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.AdapterView;
|
|
import android.widget.ArrayAdapter;
|
|
import android.widget.BaseAdapter;
|
|
import android.widget.EditText;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.ListView;
|
|
import android.widget.Spinner;
|
|
import android.widget.TextView;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
|
|
/**
|
|
* Start a conversation: pick the number to send from, then a contact or a
|
|
* typed number. From the Google Voice number, "+ Group" starts a group.
|
|
*/
|
|
public final class NewChatActivity extends BaseActivity {
|
|
private ListView list;
|
|
private EditText to;
|
|
private TextView toLabel, start;
|
|
private LinearLayout chips;
|
|
private String from;
|
|
private boolean fromChosen;
|
|
private final List<String> members = new ArrayList<>();
|
|
private final List<String[]> options = new ArrayList<>(); // [number, name or null, label]
|
|
private Adapter adapter;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle b) {
|
|
super.onCreate(b);
|
|
Config cfg = Config.get(this);
|
|
from = cfg.defaultDid;
|
|
LinearLayout root = new LinearLayout(this);
|
|
root.setOrientation(LinearLayout.VERTICAL);
|
|
root.setPadding(Ui.dp(this, 16), Ui.dp(this, 8), Ui.dp(this, 16), 0);
|
|
|
|
LinearLayout header = new LinearLayout(this);
|
|
header.setGravity(Gravity.CENTER_VERTICAL);
|
|
TextView title = Ui.bold(Ui.text(this, "New chat", Ui.T_TITLE, Ui.TEXT));
|
|
header.addView(title, new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
|
|
start = Ui.button(this, "Start", true);
|
|
start.setVisibility(View.GONE);
|
|
start.setOnClickListener(v -> open(members.size() > 1 ? Phone.groupKey(members) : members.get(0)));
|
|
header.addView(start);
|
|
root.addView(header);
|
|
|
|
if (cfg.dids.size() > 1) {
|
|
LinearLayout fromRow = new LinearLayout(this);
|
|
fromRow.setGravity(Gravity.CENTER_VERTICAL);
|
|
fromRow.addView(Ui.text(this, "From ", Ui.T_SMALL, Ui.MUTED));
|
|
Spinner sp = new Spinner(this);
|
|
List<String> labels = new ArrayList<>();
|
|
int sel = 0;
|
|
for (int i = 0; i < cfg.dids.size(); i++) {
|
|
Config.Did d = cfg.dids.get(i);
|
|
labels.add(Ui.didLabel(this, d));
|
|
if (d.did.equals(from)) sel = i;
|
|
}
|
|
sp.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, labels));
|
|
sp.setSelection(sel);
|
|
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
|
public void onItemSelected(AdapterView<?> p, View v, int pos, long id) {
|
|
if (!cfg.dids.get(pos).did.equals(from)) fromChosen = true;
|
|
from = cfg.dids.get(pos).did;
|
|
if (!cfg.canGroup(from)) {
|
|
members.clear();
|
|
renderChips();
|
|
}
|
|
adapter.notifyDataSetChanged();
|
|
}
|
|
|
|
public void onNothingSelected(AdapterView<?> p) {}
|
|
});
|
|
fromRow.addView(sp);
|
|
root.addView(fromRow);
|
|
}
|
|
|
|
chips = new LinearLayout(this);
|
|
chips.setOrientation(LinearLayout.VERTICAL);
|
|
root.addView(chips);
|
|
|
|
LinearLayout toRow = new LinearLayout(this);
|
|
toRow.setGravity(Gravity.CENTER_VERTICAL);
|
|
toRow.setBackground(Ui.round(Ui.INPUT, Ui.dp(this, 20)));
|
|
toRow.setPadding(Ui.dp(this, 20), 0, Ui.dp(this, 12), 0);
|
|
toLabel = Ui.text(this, "To:", Ui.T_SMALL, Ui.MUTED);
|
|
to = new EditText(this);
|
|
to.setHint("Name or number");
|
|
to.setHintTextColor(Ui.FAINT);
|
|
to.setTextColor(Ui.TEXT);
|
|
to.setTextSize(TypedValue.COMPLEX_UNIT_SP, Ui.T_BODY);
|
|
to.setBackground(null);
|
|
to.setSingleLine();
|
|
toRow.addView(toLabel);
|
|
toRow.addView(to, new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
|
|
LinearLayout.LayoutParams tlp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
tlp.topMargin = Ui.dp(this, 8);
|
|
tlp.bottomMargin = Ui.dp(this, 8);
|
|
root.addView(toRow, tlp);
|
|
|
|
list = new ListView(this);
|
|
list.setDivider(null);
|
|
adapter = new Adapter();
|
|
list.setAdapter(adapter);
|
|
list.setOnItemClickListener((p, v, pos, id) -> pick(options.get(pos)[0]));
|
|
root.addView(list, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1));
|
|
setContentView(root);
|
|
|
|
to.addTextChangedListener(new TextWatcher() {
|
|
public void beforeTextChanged(CharSequence s, int a, int b, int c) {}
|
|
|
|
public void onTextChanged(CharSequence s, int a, int b, int c) {}
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
filter();
|
|
}
|
|
});
|
|
to.setOnEditorActionListener((v, action, e) -> {
|
|
if (!options.isEmpty()) pick(options.get(0)[0]);
|
|
return true;
|
|
});
|
|
filter();
|
|
}
|
|
|
|
@Override
|
|
public void changed() {}
|
|
|
|
private void filter() {
|
|
String q = to.getText().toString().trim().toLowerCase(Locale.ROOT);
|
|
options.clear();
|
|
String digits = Phone.normalize(q);
|
|
boolean typedNumber = q.matches("[\\d\\s()+.-]+") && (digits.length() == 10 || (digits.length() >= 3 && digits.length() <= 6));
|
|
List<Contacts.Entry> all = Contacts.all(this);
|
|
boolean typedIsContact = false;
|
|
for (Contacts.Entry e : all) {
|
|
if (members.contains(e.number)) continue;
|
|
boolean match = q.isEmpty() || e.name.toLowerCase(Locale.ROOT).contains(q) || (digits.length() >= 3 && e.number.contains(digits));
|
|
if (match) {
|
|
options.add(new String[] {e.number, e.name, e.label});
|
|
if (e.number.equals(digits)) typedIsContact = true;
|
|
}
|
|
if (options.size() >= 100) break;
|
|
}
|
|
if (typedNumber && !typedIsContact && !members.contains(digits)) options.add(0, new String[] {digits, null, null});
|
|
adapter.notifyDataSetChanged();
|
|
}
|
|
|
|
private void pick(String number) {
|
|
if (!members.isEmpty()) addMember(number);
|
|
else open(number);
|
|
}
|
|
|
|
private void addMember(String number) {
|
|
if (!members.contains(number)) members.add(number);
|
|
to.setText("");
|
|
renderChips();
|
|
}
|
|
|
|
private void renderChips() {
|
|
chips.removeAllViews();
|
|
for (String n : members) {
|
|
TextView chip = Ui.text(this, Contacts.name(this, n) + " ✕", Ui.T_SMALL, Ui.TEXT);
|
|
chip.setBackground(Ui.round(Ui.INPUT, Ui.dp(this, 24)));
|
|
chip.setPadding(Ui.dp(this, 18), Ui.dp(this, 8), Ui.dp(this, 18), Ui.dp(this, 8));
|
|
chip.setOnClickListener(v -> {
|
|
members.remove(n);
|
|
renderChips();
|
|
filter();
|
|
});
|
|
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
lp.topMargin = Ui.dp(this, 6);
|
|
chips.addView(chip, lp);
|
|
}
|
|
start.setVisibility(members.isEmpty() ? View.GONE : View.VISIBLE);
|
|
toLabel.setText(members.isEmpty() ? "To:" : "Add:");
|
|
adapter.notifyDataSetChanged();
|
|
}
|
|
|
|
private void open(String contact) {
|
|
if (from == null) return;
|
|
startActivity(ChatActivity.intent(this, existingDid(contact), contact));
|
|
finish();
|
|
}
|
|
|
|
/**
|
|
* Reuse an existing conversation with this person: from the chosen number,
|
|
* or from any number if none was chosen.
|
|
*/
|
|
private String existingDid(String contact) {
|
|
if (fromChosen) return from;
|
|
for (Store.Conv c : Store.get(this).conversations(false)) if (c.contact.equals(contact)) return c.did;
|
|
return from;
|
|
}
|
|
|
|
private final class Adapter extends BaseAdapter {
|
|
@Override
|
|
public int getCount() {
|
|
return options.size();
|
|
}
|
|
|
|
@Override
|
|
public Object getItem(int i) {
|
|
return options.get(i);
|
|
}
|
|
|
|
@Override
|
|
public long getItemId(int i) {
|
|
return i;
|
|
}
|
|
|
|
@Override
|
|
public View getView(int i, View cv, ViewGroup parent) {
|
|
Context c = NewChatActivity.this;
|
|
String[] o = options.get(i);
|
|
LinearLayout row = new LinearLayout(c);
|
|
row.setGravity(Gravity.CENTER_VERTICAL);
|
|
row.setPadding(Ui.dp(c, 8), Ui.dp(c, 12), Ui.dp(c, 8), Ui.dp(c, 12));
|
|
View av = new View(c);
|
|
av.setBackground(Ui.avatar(o[0], o[1], false));
|
|
int s = Ui.dp(c, 72);
|
|
LinearLayout.LayoutParams alp = new LinearLayout.LayoutParams(s, s);
|
|
alp.rightMargin = Ui.dp(c, 16);
|
|
row.addView(av, alp);
|
|
LinearLayout main = new LinearLayout(c);
|
|
main.setOrientation(LinearLayout.VERTICAL);
|
|
main.addView(Ui.bold(Ui.text(c, o[1] != null ? o[1] : Phone.format(o[0]), Ui.T_NAME, Ui.TEXT)));
|
|
main.addView(Ui.text(c, o[1] != null ? Phone.format(o[0]) + (o[2] != null ? " · " + o[2] : "") : "Send to this number", Ui.T_SMALL, Ui.MUTED));
|
|
row.addView(main, new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
|
|
if (Config.get(c).canGroup(from) && members.isEmpty()) {
|
|
TextView plus = Ui.text(c, "+ Group", Ui.T_TINY, Ui.MUTED);
|
|
plus.setBackground(Ui.round(Ui.INPUT, Ui.dp(c, 24)));
|
|
plus.setPadding(Ui.dp(c, 16), Ui.dp(c, 10), Ui.dp(c, 16), Ui.dp(c, 10));
|
|
plus.setOnClickListener(v -> addMember(o[0]));
|
|
row.addView(plus);
|
|
}
|
|
return row;
|
|
}
|
|
}
|
|
}
|