Select contact and send message

This commit is contained in:
Christian Basler 2015-11-11 21:03:03 +01:00
parent c44f702ba5
commit 9040026965
13 changed files with 341 additions and 73 deletions

View File

@ -2,11 +2,21 @@ package ch.dissem.apps.abit;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.*; 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.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast;
import ch.dissem.bitmessage.BitmessageContext; import java.util.List;
import ch.dissem.apps.abit.adapter.ContactAdapter;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.BitmessageAddress;
import static ch.dissem.apps.abit.ComposeMessageActivity.EXTRA_IDENTITY; import static ch.dissem.apps.abit.ComposeMessageActivity.EXTRA_IDENTITY;
@ -16,9 +26,11 @@ import static ch.dissem.apps.abit.ComposeMessageActivity.EXTRA_RECIPIENT;
* Compose a new message. * Compose a new message.
*/ */
public class ComposeMessageFragment extends Fragment { public class ComposeMessageFragment extends Fragment {
private BitmessageContext bmCtx;
private BitmessageAddress identity; private BitmessageAddress identity;
private BitmessageAddress recipient; private BitmessageAddress recipient;
private AutoCompleteTextView recipientInput;
private EditText subjectInput;
private EditText bodyInput;
/** /**
* Mandatory empty constructor for the fragment manager to instantiate the * Mandatory empty constructor for the fragment manager to instantiate the
@ -33,10 +45,14 @@ public class ComposeMessageFragment extends Fragment {
if (getArguments() != null) { if (getArguments() != null) {
if (getArguments().containsKey(EXTRA_IDENTITY)) { if (getArguments().containsKey(EXTRA_IDENTITY)) {
identity = (BitmessageAddress) getArguments().getSerializable(EXTRA_IDENTITY); identity = (BitmessageAddress) getArguments().getSerializable(EXTRA_IDENTITY);
} else {
throw new RuntimeException("No identity set for ComposeMessageFragment");
} }
if (getArguments().containsKey(EXTRA_RECIPIENT)) { if (getArguments().containsKey(EXTRA_RECIPIENT)) {
recipient = (BitmessageAddress) getArguments().getSerializable(EXTRA_RECIPIENT); recipient = (BitmessageAddress) getArguments().getSerializable(EXTRA_RECIPIENT);
} }
} else {
throw new RuntimeException("No identity set for ComposeMessageFragment");
} }
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
@ -45,13 +61,32 @@ public class ComposeMessageFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_compose_message, container, false); View rootView = inflater.inflate(R.layout.fragment_compose_message, container, false);
recipientInput = (AutoCompleteTextView) rootView.findViewById(R.id.recipient);
final ContactAdapter adapter = new ContactAdapter(getContext());
recipientInput.setAdapter(adapter);
recipientInput.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
recipient = adapter.getItem(position);
}
});
recipientInput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
recipient = adapter.getItem(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
if (recipient != null) { if (recipient != null) {
EditText recipientInput = (EditText) rootView.findViewById(R.id.recipient);
recipientInput.setText(recipient.toString()); recipientInput.setText(recipient.toString());
} }
EditText body = (EditText) rootView.findViewById(R.id.body); subjectInput = (EditText) rootView.findViewById(R.id.subject);
body.setInputType(EditorInfo.TYPE_TEXT_VARIATION_SHORT_MESSAGE | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE); bodyInput = (EditText) rootView.findViewById(R.id.body);
body.setImeOptions(EditorInfo.IME_ACTION_SEND | EditorInfo.IME_FLAG_NO_ENTER_ACTION); // bodyInput.setInputType(EditorInfo.TYPE_TEXT_VARIATION_SHORT_MESSAGE | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);
// bodyInput.setImeOptions(EditorInfo.IME_ACTION_SEND | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
return rootView; return rootView;
} }
@ -65,7 +100,25 @@ public class ComposeMessageFragment extends Fragment {
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.send: case R.id.send:
Toast.makeText(getActivity(), "TODO: Send", Toast.LENGTH_SHORT).show(); String inputString = recipientInput.getText().toString();
if (recipient == null || !recipient.toString().equals(inputString)) {
try {
recipient = new BitmessageAddress(inputString);
} catch (Exception e) {
List<BitmessageAddress> contacts = Singleton.getAddressRepository(getContext()).getContacts();
for (BitmessageAddress contact : contacts) {
if (inputString.equalsIgnoreCase(contact.getAlias())) {
recipient = contact;
if (inputString.equals(contact.getAlias()))
break;
}
}
}
}
Singleton.getBitmessageContext(getContext()).send(identity, recipient,
subjectInput.getText().toString(),
bodyInput.getText().toString());
getActivity().finish();
return true; return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);

View File

@ -114,6 +114,7 @@ public class MessageListActivity extends AppCompatActivity
private MessageRepository messageRepo; private MessageRepository messageRepo;
private AddressRepository addressRepo; private AddressRepository addressRepo;
private BitmessageAddress selectedIdentity;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -224,38 +225,50 @@ public class MessageListActivity extends AppCompatActivity
} catch (RemoteException e) { } catch (RemoteException e) {
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
} }
} else if (profile instanceof ProfileDrawerItem) {
Object tag = ((ProfileDrawerItem) profile).getTag();
if (tag instanceof BitmessageAddress) {
selectedIdentity = (BitmessageAddress) tag;
}
} }
// false if it should close the drawer // false if it should close the drawer
return false; return false;
} }
}) })
.build(); .build();
if (profiles.size() > 0) {
accountHeader.setActiveProfile(profiles.get(0), true);
}
incomingHandler.updateAccountHeader(accountHeader); incomingHandler.updateAccountHeader(accountHeader);
ArrayList<IDrawerItem> drawerItems = new ArrayList<>(); ArrayList<IDrawerItem> drawerItems = new ArrayList<>();
for (Label label : messageRepo.getLabels()) { for (Label label : messageRepo.getLabels()) {
PrimaryDrawerItem item = new PrimaryDrawerItem().withName(label.toString()).withTag(label); PrimaryDrawerItem item = new PrimaryDrawerItem().withName(label.toString()).withTag(label);
switch (label.getType()) { if (label.getType() == null) {
case INBOX: item.withIcon(CommunityMaterial.Icon.cmd_label);
item.withIcon(GoogleMaterial.Icon.gmd_inbox); } else {
break; switch (label.getType()) {
case DRAFT: case INBOX:
item.withIcon(CommunityMaterial.Icon.cmd_file); item.withIcon(GoogleMaterial.Icon.gmd_inbox);
break; break;
case SENT: case DRAFT:
item.withIcon(CommunityMaterial.Icon.cmd_send); item.withIcon(CommunityMaterial.Icon.cmd_file);
break; break;
case BROADCAST: case SENT:
item.withIcon(CommunityMaterial.Icon.cmd_rss); item.withIcon(CommunityMaterial.Icon.cmd_send);
break; break;
case UNREAD: case BROADCAST:
item.withIcon(GoogleMaterial.Icon.gmd_markunread_mailbox); item.withIcon(CommunityMaterial.Icon.cmd_rss);
break; break;
case TRASH: case UNREAD:
item.withIcon(GoogleMaterial.Icon.gmd_delete); item.withIcon(GoogleMaterial.Icon.gmd_markunread_mailbox);
break; break;
default: case TRASH:
item.withIcon(CommunityMaterial.Icon.cmd_label); item.withIcon(GoogleMaterial.Icon.gmd_delete);
break;
default:
item.withIcon(CommunityMaterial.Icon.cmd_label);
}
} }
drawerItems.add(item); drawerItems.add(item);
} }
@ -273,8 +286,8 @@ public class MessageListActivity extends AppCompatActivity
.withDrawerItems(drawerItems) .withDrawerItems(drawerItems)
.addStickyDrawerItems( .addStickyDrawerItems(
new PrimaryDrawerItem() new PrimaryDrawerItem()
.withName(R.string.subscriptions) .withName(R.string.contacts_and_subscriptions)
.withIcon(CommunityMaterial.Icon.cmd_rss_box), .withIcon(GoogleMaterial.Icon.gmd_contacts),
new PrimaryDrawerItem() new PrimaryDrawerItem()
.withName(R.string.settings) .withName(R.string.settings)
.withIcon(GoogleMaterial.Icon.gmd_settings), .withIcon(GoogleMaterial.Icon.gmd_settings),
@ -313,7 +326,7 @@ public class MessageListActivity extends AppCompatActivity
} else if (item instanceof Nameable<?>) { } else if (item instanceof Nameable<?>) {
Nameable<?> ni = (Nameable<?>) item; Nameable<?> ni = (Nameable<?>) item;
switch (ni.getNameRes()) { switch (ni.getNameRes()) {
case R.string.subscriptions: case R.string.contacts_and_subscriptions:
if (!(getSupportFragmentManager().findFragmentById(R.id.item_list) instanceof SubscriptionListFragment)) { if (!(getSupportFragmentManager().findFragmentById(R.id.item_list) instanceof SubscriptionListFragment)) {
changeList(new SubscriptionListFragment()); changeList(new SubscriptionListFragment());
} else { } else {
@ -416,6 +429,10 @@ public class MessageListActivity extends AppCompatActivity
super.onStop(); super.onStop();
} }
public BitmessageAddress getSelectedIdentity() {
return selectedIdentity;
}
private static class IncomingHandler extends Handler { private static class IncomingHandler extends Handler {
private WeakReference<AccountHeader> accountHeaderRef; private WeakReference<AccountHeader> accountHeaderRef;
@ -423,7 +440,7 @@ public class MessageListActivity extends AppCompatActivity
accountHeaderRef = new WeakReference<>(null); accountHeaderRef = new WeakReference<>(null);
} }
public void updateAccountHeader(AccountHeader accountHeader){ public void updateAccountHeader(AccountHeader accountHeader) {
accountHeaderRef = new WeakReference<>(accountHeader); accountHeaderRef = new WeakReference<>(accountHeader);
} }

View File

@ -113,7 +113,9 @@ public class MessageListFragment extends AbstractItemListFragment<Plaintext> {
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
startActivity(new Intent(getActivity().getApplicationContext(), ComposeMessageActivity.class)); Intent intent = new Intent(getActivity().getApplicationContext(), ComposeMessageActivity.class);
intent.putExtra(ComposeMessageActivity.EXTRA_IDENTITY, ((MessageListActivity)getActivity()).getSelectedIdentity());
startActivity(intent);
} }
}); });

View File

@ -74,7 +74,7 @@ public class SubscriptionDetailFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_subscription_detail, container, false); View rootView = inflater.inflate(R.layout.fragment_contact_detail, container, false);
// Show the dummy content as text in a TextView. // Show the dummy content as text in a TextView.
if (item != null) { if (item != null) {

View File

@ -103,7 +103,7 @@ public class SubscriptionListFragment extends AbstractItemListFragment<Bitmessag
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_subscribtions, container, false); View rootView = inflater.inflate(R.layout.fragment_contact_list, container, false);
return rootView; return rootView;
} }

View File

@ -0,0 +1,124 @@
package ch.dissem.apps.abit.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import ch.dissem.apps.abit.Identicon;
import ch.dissem.apps.abit.R;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.bitmessage.entity.BitmessageAddress;
/**
* An adapter for contacts. Can be filtered by alias or address.
*/
public class ContactAdapter extends BaseAdapter implements Filterable {
private final LayoutInflater inflater;
private final List<BitmessageAddress> originalData;
private List<BitmessageAddress> data;
public ContactAdapter(Context ctx) {
inflater = LayoutInflater.from(ctx);
originalData = Singleton.getAddressRepository(ctx).getContacts();
data = originalData;
}
@Override
public int getCount() {
return data.size();
}
@Override
public BitmessageAddress getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.contact_row, parent, false);
}
BitmessageAddress item = getItem(position);
((ImageView) convertView.findViewById(R.id.avatar)).setImageDrawable(new Identicon(item));
((TextView) convertView.findViewById(R.id.name)).setText(item.toString());
((TextView) convertView.findViewById(R.id.address)).setText(item.getAddress());
return convertView;
}
@Override
public Filter getFilter() {
return new ContactFilter();
}
private class ContactFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (prefix == null || prefix.length() == 0) {
results.values = originalData;
results.count = originalData.size();
} else {
String prefixString = prefix.toString().toLowerCase();
final ArrayList<BitmessageAddress> newValues = new ArrayList<>();
for (int i = 0; i < originalData.size(); i++) {
final BitmessageAddress value = originalData.get(i);
// First match against the whole, non-splitted value
if (value.getAlias() != null) {
String alias = value.getAlias().toLowerCase();
if (alias.startsWith(prefixString)) {
newValues.add(value);
} else {
final String[] words = alias.split(" ");
for (String word : words) {
if (word.startsWith(prefixString)) {
newValues.add(value);
break;
}
}
}
} else {
String address = value.getAddress().toLowerCase();
if (address.contains(prefixString)) {
newValues.add(value);
}
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//noinspection unchecked
data = (List<BitmessageAddress>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}

View File

@ -1,6 +1,7 @@
package ch.dissem.apps.abit.service; package ch.dissem.apps.abit.service;
import android.app.Service; import android.app.Service;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
@ -8,6 +9,7 @@ import android.os.IBinder;
import android.os.Message; import android.os.Message;
import android.os.Messenger; import android.os.Messenger;
import android.os.RemoteException; import android.os.RemoteException;
import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -129,6 +131,9 @@ public class BitmessageService extends Service {
String message = msg.getData().getString(DATA_FIELD_MESSAGE); String message = msg.getData().getString(DATA_FIELD_MESSAGE);
bmc.send((BitmessageAddress) identity, (BitmessageAddress) address, bmc.send((BitmessageAddress) identity, (BitmessageAddress) address,
subject, message); subject, message);
} else {
Context ctx = service.get();
Toast.makeText(ctx, "Could not send", Toast.LENGTH_LONG);
} }
break; break;
} }

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2015 Christian Basler
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/avatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@color/accent"
android:layout_margin="16dp"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:lines="1"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignTop="@+id/avatar"
android:layout_toRightOf="@+id/avatar"
android:layout_toEndOf="@+id/avatar"
android:paddingTop="0dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="0dp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BM-2cW0000000000000000000000000000000"
android:lines="1"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceSmall"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_alignBottom="@+id/avatar"
android:layout_toRightOf="@+id/avatar"
android:layout_toEndOf="@+id/avatar"/>
</RelativeLayout>

View File

@ -1,51 +1,47 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:orientation="vertical" android:layout_height="match_parent"
android:layout_width="match_parent" android:fitsSystemWindows="true"
android:layout_height="match_parent" android:orientation="vertical">
android:fitsSystemWindows="true">
<android.support.design.widget.TextInputLayout <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp">
<AutoCompleteTextView
android:id="@+id/recipient"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="4dp"> android:hint="@string/to"
android:inputType="textNoSuggestions"
<EditText android:singleLine="true" />
android:id="@+id/recipient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
android:singleLine="true"
android:hint="@string/to"/>
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout <android.support.design.widget.TextInputLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<EditText <EditText
android:id="@+id/subject" android:id="@+id/subject"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:inputType="textEmailSubject" android:hint="@string/subject"
android:textAppearance="?android:attr/textAppearanceLarge" android:inputType="textEmailSubject"
android:hint="@string/subject"/> android:textAppearance="?android:attr/textAppearanceLarge" />
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
<ScrollView <EditText
android:layout_width="match_parent" android:id="@+id/body"
android:layout_height="0dp" android:layout_width="match_parent"
android:layout_weight="1"> android:layout_height="0dp"
android:layout_weight="1"
<EditText android:hint="@string/compose_body_hint"
android:id="@+id/body" android:inputType="textMultiLine|textCapSentences"
android:layout_width="match_parent" android:gravity="top"
android:layout_height="wrap_content" android:isScrollContainer="true" />
android:inputType="textMultiLine"/>
</ScrollView>
</LinearLayout> </LinearLayout>

View File

@ -70,7 +70,7 @@
<Switch <Switch
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/enabled" android:text="@string/subscribed"
android:id="@+id/active" android:id="@+id/active"
android:paddingTop="16dp" android:paddingTop="16dp"
android:paddingLeft="16dp" android:paddingLeft="16dp"

View File

@ -46,4 +46,7 @@
<string name="proof_of_work_title">Proof of Work</string> <string name="proof_of_work_title">Proof of Work</string>
<string name="error_invalid_sync_host">Synchronisation fehlgeschlagen: der vertrauenswürdige Knoten konnte nicht erreicht werden.</string> <string name="error_invalid_sync_host">Synchronisation fehlgeschlagen: der vertrauenswürdige Knoten konnte nicht erreicht werden.</string>
<string name="error_invalid_sync_port">Ungültiger Port in den Synchronisationseinstellungen: %s</string> <string name="error_invalid_sync_port">Ungültiger Port in den Synchronisationseinstellungen: %s</string>
<string name="compose_body_hint">Nachricht schreiben</string>
<string name="contacts_and_subscriptions">Kontakte</string>
<string name="subscribed">Abonniert</string>
</resources> </resources>

View File

@ -46,4 +46,7 @@
<string name="proof_of_work_text">Warning: This might heat your device until the battery\'s dead.</string> <string name="proof_of_work_text">Warning: This might heat your device until the battery\'s dead.</string>
<string name="error_invalid_sync_port">Invalid port in synchronization settings: %s</string> <string name="error_invalid_sync_port">Invalid port in synchronization settings: %s</string>
<string name="error_invalid_sync_host">Synchronization failed: Trusted node could not be reached.</string> <string name="error_invalid_sync_host">Synchronization failed: Trusted node could not be reached.</string>
<string name="compose_body_hint">Write message</string>
<string name="contacts_and_subscriptions">Contacts</string>
<string name="subscribed">Subscribed</string>
</resources> </resources>