Commit 159fdbaa authored by Adrien Béraud's avatar Adrien Béraud
Browse files

conversation: allow to select number to use

Issue: #78218
Change-Id: I476e5aec3b6c22308bdc5256396a8ce051899d52
parent 2bb3cdfd
......@@ -158,7 +158,7 @@ as that of the covered work.
android:label="@string/app_name"
android:parentActivityName=".client.HomeActivity"
android:screenOrientation="portrait"
android:theme="@style/AppThemeWithoutOverlay"
android:theme="@style/AppThemeWithoutOverlayCompat"
android:windowSoftInputMode="adjustResize" >
</activity>
......
/*
* Copyright (C) 2015 Savoir-faire Linux Inc.
*
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package cx.ring.client;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
......@@ -11,18 +30,23 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.util.Log;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import java.text.DateFormat;
......@@ -38,11 +62,12 @@ import cx.ring.model.CallContact;
import cx.ring.model.Conference;
import cx.ring.model.Conversation;
import cx.ring.model.SipCall;
import cx.ring.model.SipUri;
import cx.ring.model.TextMessage;
import cx.ring.model.account.Account;
import cx.ring.service.LocalService;
public class ConversationActivity extends Activity {
public class ConversationActivity extends AppCompatActivity {
private static final String TAG = ConversationActivity.class.getSimpleName();
public static final Uri CONTENT_URI = Uri.withAppendedPath(LocalService.AUTHORITY_URI, "conversations");
......@@ -52,13 +77,14 @@ public class ConversationActivity extends Activity {
private Conversation conversation = null;
private String preferredNumber = null;
private ListView histList = null;
private View msgSendBtn = null;
private EditText msgEditTxt = null;
private ViewGroup bottomPane = null;
private Spinner numberSpinner = null;
private ConversationAdapter adapter = null;
private NumberAdapter numberAdapter = null;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
......@@ -98,7 +124,7 @@ public class ConversationActivity extends Activity {
return;
}
getActionBar().setTitle(conversation.getContact().getDisplayName());
getSupportActionBar().setTitle(conversation.getContact().getDisplayName());
Conference conf = conversation.getCurrentCall();
bottomPane.setVisibility(conf == null ? View.GONE : View.VISIBLE);
......@@ -113,8 +139,27 @@ public class ConversationActivity extends Activity {
}
adapter.updateDataset(conversation.getHistory());
if (conversation.getContact().getPhones().size() > 1) {
numberAdapter = new NumberAdapter(ConversationActivity.this, conversation.getContact());
numberSpinner.setAdapter(numberAdapter);
if (preferredNumber == null || preferredNumber.isEmpty()) {
preferredNumber = CallContact.canonicalNumber(conversation.getLastNumberUsed(conversation.getLastAccountUsed()));
}
numberSpinner.setSelection(getIndex(numberSpinner, preferredNumber));
} else {
numberSpinner.setVisibility(View.GONE);
}
scrolltoBottom();
}
private int getIndex(Spinner spinner, String myString)
{
for (int i=0, n=spinner.getCount();i<n;i++)
if (((CallContact.Phone)spinner.getItemAtPosition(i)).getNumber().equalsIgnoreCase(myString))
return i;
return 0;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
......@@ -142,6 +187,18 @@ public class ConversationActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag_conversation);
msgEditTxt = (EditText) findViewById(R.id.msg_input_txt);
msgEditTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId) {
case EditorInfo.IME_ACTION_SEND:
onSendTextMessage(msgEditTxt.getText().toString());
msgEditTxt.setText("");
return true;
}
return false;
}
});
msgSendBtn = findViewById(R.id.msg_send);
msgSendBtn.setOnClickListener(new View.OnClickListener() {
@Override
......@@ -159,6 +216,8 @@ public class ConversationActivity extends Activity {
histList = (ListView) findViewById(R.id.hist_list);
histList.setAdapter(adapter);
numberSpinner = (Spinner) findViewById(R.id.number_selector);
if (!mBound) {
Log.i(TAG, "onCreate: Binding service...");
Intent intent = new Intent(this, LocalService.class);
......@@ -177,6 +236,67 @@ public class ConversationActivity extends Activity {
});
}
private class NumberAdapter extends BaseAdapter {
final private Context context;
private ArrayList<CallContact.Phone> numbers;
NumberAdapter(Context context, CallContact c) {
this.context = context;
numbers = c.getPhones();
}
public void updateDataset(CallContact c) {
numbers = c.getPhones();
notifyDataSetChanged();
}
@Override
public int getCount() {
return numbers.size();
}
@Override
public Object getItem(int position) {
return numbers.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = LayoutInflater.from(context).inflate(R.layout.item_number_selected, null);
CallContact.Phone number = numbers.get(position);
ImageView numberIcon = (ImageView) convertView.findViewById(R.id.number_icon);
numberIcon.setImageResource(new SipUri(number.getNumber()).isRingId() ? R.drawable.ring_logo_24dp : R.drawable.ic_dialer_sip_black_24dp);
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = LayoutInflater.from(context).inflate(R.layout.item_number, null);
CallContact.Phone number = numbers.get(position);
TextView numberTxt = (TextView) convertView.findViewById(R.id.number_txt);
TextView numberLabelTxt = (TextView) convertView.findViewById(R.id.number_label_txt);
ImageView numberIcon = (ImageView) convertView.findViewById(R.id.number_icon);
numberTxt.setText(number.getNumber());
numberLabelTxt.setText(number.getTypeString(context.getResources()));
numberIcon.setImageResource(new SipUri(number.getNumber()).isRingId() ? R.drawable.ring_logo_24dp : R.drawable.ic_dialer_sip_black_24dp);
return convertView;
}
}
private class ConversationAdapter extends BaseAdapter {
final private Context context;
final private ArrayList<Conversation.ConversationElement> texts = new ArrayList<>();
......@@ -334,20 +454,24 @@ public class ConversationActivity extends Activity {
// overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
}
private void onSendTextMessage(String txt) {
private Pair<Account, String> guess() {
Account a = service.getAccount(conversation.getLastAccountUsed());
String number = numberAdapter == null ? conversation.contact.getPhones().get(0).getNumber() : CallContact.canonicalNumber(((CallContact.Phone) numberSpinner.getSelectedItem()).getNumber());
if (a == null)
a = service.guessAccount(conversation.getContact(), number);
if (number == null || number.isEmpty())
number = conversation.getLastNumberUsed(a.getAccountID());
if (number == null || number.isEmpty())
number = conversation.contact.getPhones().get(0).getNumber();
return new Pair<>(a, number);
}
Conference conf = conversation.getCurrentCall();
private void onSendTextMessage(String txt) {
Conference conf = conversation == null ? null : conversation.getCurrentCall();
if (conf == null || !conf.isOnGoing()) {
String account = conversation.getLastAccountUsed();
if (account == null || account.isEmpty())
account = service.guessAccount(conversation.getContact(), conversation.contact.getPhones().get(0).getNumber()).getAccountID();
String number = preferredNumber;
if (number == null || number.isEmpty())
number = conversation.getLastNumberUsed(account);
if (number == null || number.isEmpty())
number = conversation.contact.getPhones().get(0).getNumber();
Pair<Account, String> g = guess();
try {
service.getRemoteService().sendAccountTextMessage(account, CallContact.canonicalNumber(number), txt);
service.getRemoteService().sendAccountTextMessage(g.first.getAccountID(), g.second, txt);
} catch (RemoteException e) {
e.printStackTrace();
}
......@@ -366,45 +490,10 @@ public class ConversationActivity extends Activity {
startActivity(new Intent(ConversationActivity.this.getApplicationContext(), CallActivity.class).putExtra("conference", conversation.getCurrentCall()));
return;
}
CallContact contact = conversation.getContact();
Pair<Account, String> g = guess();
if (service.getAccounts().isEmpty()) {
//createNotRegisteredDialog().show();
return;
}
Account usedAccount = null;
CallContact contact = null;
if (conversation != null) {
String last_used = conversation.getLastAccountUsed();
Account a = service.getAccount(last_used);
if (a != null/* && a.isEnabled()*/)
usedAccount = a;
else {
Set<String> acc_ids = conversation.getAccountsUsed();
for (Account acc : service.getAccounts()) {
if (acc_ids.contains(acc.getAccountID())) {
usedAccount = acc;
break;
}
}
}
contact = conversation.getContact();
}
String number = preferredNumber;
if (usedAccount != null) {
if (number == null)
number = conversation.getLastNumberUsed(usedAccount.getAccountID());
if (number == null && contact != null)
number = contact.getPhones().get(0).getNumber();
} else {
if (number == null && contact != null)
number = contact.getPhones().get(0).getNumber();
usedAccount = service.guessAccount(contact, number);
}
number = CallContact.canonicalNumber(number);
SipCall call = new SipCall(null, usedAccount.getAccountID(), number, SipCall.Direction.OUTGOING);
SipCall call = new SipCall(null, g.first.getAccountID(), g.second, SipCall.Direction.OUTGOING);
call.setContact(contact);
try {
......
......@@ -49,7 +49,7 @@ public class ContactsLoader extends AsyncTaskLoader<ContactsLoader.Result>
static private final String[] CONTACTS_ID_PROJECTION = new String[] { Contacts._ID };
static private final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.LOOKUP_KEY, Contacts.DISPLAY_NAME, Contacts.PHOTO_ID, Contacts.STARRED};
static private final String[] CONTACTS_SIP_PROJECTION = new String[] { ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.Data.MIMETYPE, SipAddress.SIP_ADDRESS, SipAddress.TYPE };
static private final String[] CONTACTS_SIP_PROJECTION = new String[] { ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.Data.MIMETYPE, SipAddress.SIP_ADDRESS, SipAddress.TYPE, SipAddress.LABEL };
static private final String SELECT = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
private final Uri baseUri;
......@@ -130,6 +130,7 @@ public class ContactsLoader extends AsyncTaskLoader<ContactsLoader.Result>
final int iMime = c.getColumnIndex(ContactsContract.Data.MIMETYPE);
final int iNumber = c.getColumnIndex(SipAddress.SIP_ADDRESS);
final int iType = c.getColumnIndex(SipAddress.TYPE);
final int iLabel = c.getColumnIndex(SipAddress.LABEL);
while (c.moveToNext()) {
long id = c.getLong(iID);
CallContact contact = cache.get(id);
......@@ -145,7 +146,7 @@ public class ContactsLoader extends AsyncTaskLoader<ContactsLoader.Result>
contact.addPhoneNumber(c.getString(iNumber), c.getInt(iType));
} else {
//Log.w(TAG, "SIP Phone for " + id + " :" + cSip.getString(iNumber));
contact.addNumber(c.getString(iNumber), c.getInt(iType), CallContact.NumberType.SIP);
contact.addNumber(c.getString(iNumber), c.getInt(iType), c.getString(iLabel), CallContact.NumberType.SIP);
}
}
c.close();
......
......@@ -83,7 +83,7 @@ public class HistoryLoader extends AsyncTaskLoader<ArrayList<HistoryEntry>> {
if (result.moveToFirst()) {
builder.startNewContact(result.getLong(iID), result.getString(iKey), result.getString(iName), result.getLong(iPhoto));
builder.addPhoneNumber(call.getNumber(), 0);
builder.addPhoneNumber(call.getNumber(), 0, null);
contact = builder.build();
} else {
contact = CallContact.ContactBuilder.buildUnknownContact(call.getNumber());
......
......@@ -36,11 +36,15 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Profile;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.support.annotation.NonNull;
public class CallContact implements Parcelable {
......@@ -80,7 +84,9 @@ public class CallContact implements Parcelable {
this.photo_id = photo_id;
}
public static String canonicalNumber(@NonNull String number) {
public static String canonicalNumber(String number) {
if (number == null || number.isEmpty())
return null;
return new SipUri(number).getRawUriString();
}
......@@ -200,13 +206,13 @@ public class CallContact implements Parcelable {
return this;
}
public ContactBuilder addPhoneNumber(String num, int type) {
phones.add(new Phone(num, type));
public ContactBuilder addPhoneNumber(String num, int type, String label) {
phones.add(new Phone(num, type, label));
return this;
}
public ContactBuilder addSipNumber(String num, int type) {
phones.add(new Phone(num, type, NumberType.SIP));
public ContactBuilder addSipNumber(String num, int type, String label) {
phones.add(new Phone(num, type, label, NumberType.SIP));
return this;
}
......@@ -316,15 +322,22 @@ public class CallContact implements Parcelable {
NumberType ntype;
String number;
int category; // Home, work, custom etc.
String label;
public Phone(String num, int cat) {
this(num, cat, null);
}
public Phone(String num, int cat, String label) {
ntype = NumberType.UNKNOWN;
category = cat;
number = num;
this.label = label;
}
public Phone(String num, int cat, NumberType nty) {
public Phone(String num, int cat, String label, NumberType nty) {
ntype = nty;
number = num;
this.label = label;
category = cat;
}
......@@ -378,14 +391,17 @@ public class CallContact implements Parcelable {
this.number = number;
}
public CharSequence getTypeString(Resources r) {
return ContactsContract.CommonDataKinds.Phone.getTypeLabel(r, category, label);
}
}
public void addPhoneNumber(String tel, int car) {
phones.add(new Phone(tel, car));
}
public void addNumber(String tel, int cat, NumberType type) {
phones.add(new Phone(tel, cat, type));
public void addNumber(String tel, int cat, String label, NumberType type) {
phones.add(new Phone(tel, cat, label, type));
}
......
......@@ -415,11 +415,13 @@ public class LocalService extends Service {
private static final String[] CONTACTS_PHONES_PROJECTION = {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL
};
private static final String[] CONTACTS_SIP_PROJECTION = {
ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS,
ContactsContract.CommonDataKinds.SipAddress.TYPE
ContactsContract.CommonDataKinds.SipAddress.TYPE,
ContactsContract.CommonDataKinds.SipAddress.LABEL
};
private static final String ID_SELECTION = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?";
......@@ -434,8 +436,9 @@ public class LocalService extends Service {
if (cPhones != null) {
final int iNum = cPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
final int iType = cPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
final int iLabel = cPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
while (cPhones.moveToNext()) {
c.addNumber(cPhones.getString(iNum), cPhones.getInt(iType), CallContact.NumberType.TEL);
c.addNumber(cPhones.getString(iNum), cPhones.getInt(iType), cPhones.getString(iLabel), CallContact.NumberType.TEL);
Log.w(TAG,"Phone:"+cPhones.getString(cPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
cPhones.close();
......@@ -450,8 +453,9 @@ public class LocalService extends Service {
if (cSip != null) {
final int iSip = cSip.getColumnIndex(ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS);
final int iType = cSip.getColumnIndex(ContactsContract.CommonDataKinds.SipAddress.TYPE);
final int iLabel = cSip.getColumnIndex(ContactsContract.CommonDataKinds.SipAddress.LABEL);
while (cSip.moveToNext()) {
c.addNumber(cSip.getString(iSip), cSip.getInt(iType), CallContact.NumberType.SIP);
c.addNumber(cSip.getString(iSip), cSip.getInt(iType), cSip.getString(iLabel), CallContact.NumberType.SIP);
Log.w(TAG, "SIP phone:" + cSip.getString(iSip));
}
cSip.close();
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".client.ConversationActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/hist_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"
android:divider="@null"
android:background="#ebeff0"
android:clipToPadding="false"
android:divider="@null"
android:listSelector="@android:color/transparent"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
tools:listitem="@layout/item_textmsg" />
<RelativeLayout
android:id="@+id/ongoingcall_pane"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#e3c1c1"
android:id="@+id/ongoingcall_pane">
android:background="#e3c1c1">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Ongoing call"
android:id="@+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_margin="10dp" />
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:text="@string/ongoing_call"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/text_color_primary" />
</RelativeLayout>
<ImageView
android:id="@+id/divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:id="@+id/divider"
android:layout_gravity="center_horizontal"
android:background="#bdbdbd" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="48dp"
android:background="@android:color/white"
android:elevation="6dp">
android:orientation="horizontal">
<Spinner
android:id="@+id/number_selector"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
tools:listitem="@layout/item_number_selected"
android:visibility="visible" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/msg_input_txt"