| 
									
										
										
										
											2016-01-19 20:50:58 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright 2016 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.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  | package ch.dissem.apps.abit;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  | import android.content.Intent;
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  | import android.os.Bundle;
 | 
					
						
							|  |  |  | import android.support.v4.app.Fragment;
 | 
					
						
							| 
									
										
										
										
											2015-11-17 22:19:11 +01:00
										 |  |  | import android.text.util.Linkify;
 | 
					
						
							|  |  |  | import android.text.util.Linkify.TransformFilter;
 | 
					
						
							|  |  |  | import android.view.LayoutInflater;
 | 
					
						
							|  |  |  | import android.view.Menu;
 | 
					
						
							|  |  |  | import android.view.MenuInflater;
 | 
					
						
							|  |  |  | import android.view.MenuItem;
 | 
					
						
							|  |  |  | import android.view.View;
 | 
					
						
							|  |  |  | import android.view.ViewGroup;
 | 
					
						
							| 
									
										
										
										
											2015-08-28 13:49:53 +02:00
										 |  |  | import android.widget.ImageView;
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  | import android.widget.TextView;
 | 
					
						
							| 
									
										
										
										
											2015-10-23 22:40:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-17 22:19:11 +01:00
										 |  |  | import com.mikepenz.google_material_typeface_library.GoogleMaterial;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import java.util.Iterator;
 | 
					
						
							|  |  |  | import java.util.regex.Matcher;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 07:06:34 +01:00
										 |  |  | import ch.dissem.apps.abit.listener.ActionBarListener;
 | 
					
						
							| 
									
										
										
										
											2015-08-28 13:49:53 +02:00
										 |  |  | import ch.dissem.apps.abit.service.Singleton;
 | 
					
						
							| 
									
										
										
										
											2015-10-18 13:40:17 +02:00
										 |  |  | import ch.dissem.apps.abit.util.Drawables;
 | 
					
						
							| 
									
										
										
										
											2015-08-28 13:49:53 +02:00
										 |  |  | import ch.dissem.bitmessage.entity.BitmessageAddress;
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  | import ch.dissem.bitmessage.entity.Plaintext;
 | 
					
						
							| 
									
										
										
										
											2015-08-28 13:49:53 +02:00
										 |  |  | import ch.dissem.bitmessage.entity.valueobject.Label;
 | 
					
						
							| 
									
										
										
										
											2015-10-23 22:40:09 +02:00
										 |  |  | import ch.dissem.bitmessage.ports.MessageRepository;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-17 22:19:11 +01:00
										 |  |  | import static android.text.util.Linkify.WEB_URLS;
 | 
					
						
							| 
									
										
										
										
											2016-09-12 11:00:00 +02:00
										 |  |  | import static ch.dissem.apps.abit.ComposeMessageActivity.EXTRA_CONTENT;
 | 
					
						
							| 
									
										
										
										
											2016-01-29 18:05:43 +01:00
										 |  |  | import static ch.dissem.apps.abit.ComposeMessageActivity.EXTRA_IDENTITY;
 | 
					
						
							|  |  |  | import static ch.dissem.apps.abit.ComposeMessageActivity.EXTRA_RECIPIENT;
 | 
					
						
							|  |  |  | import static ch.dissem.apps.abit.ComposeMessageActivity.EXTRA_SUBJECT;
 | 
					
						
							| 
									
										
										
										
											2015-11-17 22:19:11 +01:00
										 |  |  | import static ch.dissem.apps.abit.util.Constants.BITMESSAGE_ADDRESS_PATTERN;
 | 
					
						
							|  |  |  | import static ch.dissem.apps.abit.util.Constants.BITMESSAGE_URL_SCHEMA;
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * A fragment representing a single Message detail screen.
 | 
					
						
							| 
									
										
										
										
											2015-11-22 12:28:31 +01:00
										 |  |  |  * This fragment is either contained in a {@link MainActivity}
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  |  * in two-pane mode (on tablets) or a {@link MessageDetailActivity}
 | 
					
						
							|  |  |  |  * on handsets.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | public class MessageDetailFragment extends Fragment {
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * The fragment argument representing the item ID that this fragment
 | 
					
						
							|  |  |  |      * represents.
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public static final String ARG_ITEM = "item";
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |      * The content this fragment is presenting.
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  |      */
 | 
					
						
							|  |  |  |     private Plaintext item;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Mandatory empty constructor for the fragment manager to instantiate the
 | 
					
						
							|  |  |  |      * fragment (e.g. upon screen orientation changes).
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public MessageDetailFragment() {
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Override
 | 
					
						
							|  |  |  |     public void onCreate(Bundle savedInstanceState) {
 | 
					
						
							|  |  |  |         super.onCreate(savedInstanceState);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (getArguments().containsKey(ARG_ITEM)) {
 | 
					
						
							|  |  |  |             // Load the dummy content specified by the fragment
 | 
					
						
							|  |  |  |             // arguments. In a real-world scenario, use a Loader
 | 
					
						
							|  |  |  |             // to load content from a content provider.
 | 
					
						
							|  |  |  |             item = (Plaintext) getArguments().getSerializable(ARG_ITEM);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |         setHasOptionsMenu(true);
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Override
 | 
					
						
							|  |  |  |     public View onCreateView(LayoutInflater inflater, ViewGroup container,
 | 
					
						
							|  |  |  |                              Bundle savedInstanceState) {
 | 
					
						
							|  |  |  |         View rootView = inflater.inflate(R.layout.fragment_message_detail, container, false);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Show the dummy content as text in a TextView.
 | 
					
						
							|  |  |  |         if (item != null) {
 | 
					
						
							| 
									
										
										
										
											2015-08-28 13:49:53 +02:00
										 |  |  |             ((TextView) rootView.findViewById(R.id.subject)).setText(item.getSubject());
 | 
					
						
							|  |  |  |             BitmessageAddress sender = item.getFrom();
 | 
					
						
							| 
									
										
										
										
											2016-01-19 20:50:58 +01:00
										 |  |  |             ((ImageView) rootView.findViewById(R.id.avatar)).setImageDrawable(new Identicon
 | 
					
						
							| 
									
										
										
										
											2016-09-12 11:00:00 +02:00
										 |  |  |                 (sender));
 | 
					
						
							| 
									
										
										
										
											2015-08-28 13:49:53 +02:00
										 |  |  |             ((TextView) rootView.findViewById(R.id.sender)).setText(sender.toString());
 | 
					
						
							|  |  |  |             if (item.getTo() != null) {
 | 
					
						
							|  |  |  |                 ((TextView) rootView.findViewById(R.id.recipient)).setText(item.getTo().toString());
 | 
					
						
							|  |  |  |             } else if (item.getType() == Plaintext.Type.BROADCAST) {
 | 
					
						
							|  |  |  |                 ((TextView) rootView.findViewById(R.id.recipient)).setText(R.string.broadcast);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2015-11-17 22:19:11 +01:00
										 |  |  |             TextView messageBody = (TextView) rootView.findViewById(R.id.text);
 | 
					
						
							|  |  |  |             messageBody.setText(item.getText());
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Linkify.addLinks(messageBody, WEB_URLS);
 | 
					
						
							|  |  |  |             Linkify.addLinks(messageBody, BITMESSAGE_ADDRESS_PATTERN, BITMESSAGE_URL_SCHEMA, null,
 | 
					
						
							| 
									
										
										
										
											2016-09-12 11:00:00 +02:00
										 |  |  |                 new TransformFilter() {
 | 
					
						
							|  |  |  |                     public final String transformUrl(final Matcher match, String url) {
 | 
					
						
							|  |  |  |                         return match.group();
 | 
					
						
							|  |  |  |                     }
 | 
					
						
							|  |  |  |                 });
 | 
					
						
							| 
									
										
										
										
											2015-11-17 22:19:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             messageBody.setLinksClickable(true);
 | 
					
						
							|  |  |  |             messageBody.setTextIsSelectable(true);
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 20:50:58 +01:00
										 |  |  |             boolean removed = false;
 | 
					
						
							|  |  |  |             Iterator<Label> labels = item.getLabels().iterator();
 | 
					
						
							|  |  |  |             while (labels.hasNext()) {
 | 
					
						
							|  |  |  |                 if (labels.next().getType() == Label.Type.UNREAD) {
 | 
					
						
							|  |  |  |                     labels.remove();
 | 
					
						
							|  |  |  |                     removed = true;
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             if (removed) {
 | 
					
						
							| 
									
										
										
										
											2016-02-23 07:06:34 +01:00
										 |  |  |                 if (getActivity() instanceof ActionBarListener) {
 | 
					
						
							|  |  |  |                     ((ActionBarListener) getActivity()).updateUnread();
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							| 
									
										
										
										
											2016-01-19 20:50:58 +01:00
										 |  |  |                 Singleton.getMessageRepository(inflater.getContext()).save(item);
 | 
					
						
							| 
									
										
										
										
											2015-08-28 13:49:53 +02:00
										 |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  |         return rootView;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @Override
 | 
					
						
							|  |  |  |     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
 | 
					
						
							|  |  |  |         inflater.inflate(R.menu.message, menu);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Drawables.addIcon(getActivity(), menu, R.id.reply, GoogleMaterial.Icon.gmd_reply);
 | 
					
						
							|  |  |  |         Drawables.addIcon(getActivity(), menu, R.id.delete, GoogleMaterial.Icon.gmd_delete);
 | 
					
						
							| 
									
										
										
										
											2016-01-19 20:50:58 +01:00
										 |  |  |         Drawables.addIcon(getActivity(), menu, R.id.mark_unread, GoogleMaterial.Icon
 | 
					
						
							| 
									
										
										
										
											2016-09-12 11:00:00 +02:00
										 |  |  |             .gmd_markunread);
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |         Drawables.addIcon(getActivity(), menu, R.id.archive, GoogleMaterial.Icon.gmd_archive);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super.onCreateOptionsMenu(menu, inflater);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Override
 | 
					
						
							|  |  |  |     public boolean onOptionsItemSelected(MenuItem menuItem) {
 | 
					
						
							| 
									
										
										
										
											2015-10-23 22:40:09 +02:00
										 |  |  |         MessageRepository messageRepo = Singleton.getMessageRepository(getContext());
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |         switch (menuItem.getItemId()) {
 | 
					
						
							|  |  |  |             case R.id.reply:
 | 
					
						
							| 
									
										
										
										
											2016-01-19 20:50:58 +01:00
										 |  |  |                 Intent replyIntent = new Intent(getActivity().getApplicationContext(),
 | 
					
						
							| 
									
										
										
										
											2016-09-12 11:00:00 +02:00
										 |  |  |                     ComposeMessageActivity.class);
 | 
					
						
							| 
									
										
										
										
											2016-01-29 18:05:43 +01:00
										 |  |  |                 replyIntent.putExtra(EXTRA_RECIPIENT, item.getFrom());
 | 
					
						
							|  |  |  |                 replyIntent.putExtra(EXTRA_IDENTITY, item.getTo());
 | 
					
						
							| 
									
										
										
										
											2016-02-06 13:24:25 +01:00
										 |  |  |                 String prefix;
 | 
					
						
							|  |  |  |                 if (item.getSubject().length() >= 3 && item.getSubject().substring(0, 3)
 | 
					
						
							| 
									
										
										
										
											2016-09-12 11:00:00 +02:00
										 |  |  |                     .equalsIgnoreCase("RE:")) {
 | 
					
						
							| 
									
										
										
										
											2016-02-06 13:24:25 +01:00
										 |  |  |                     prefix = "";
 | 
					
						
							|  |  |  |                 } else {
 | 
					
						
							|  |  |  |                     prefix = "RE: ";
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |                 replyIntent.putExtra(EXTRA_SUBJECT, prefix + item.getSubject());
 | 
					
						
							| 
									
										
										
										
											2016-09-12 11:00:00 +02:00
										 |  |  |                 replyIntent.putExtra(EXTRA_CONTENT,
 | 
					
						
							|  |  |  |                     "\n\n------------------------------------------------------\n"
 | 
					
						
							|  |  |  |                         + item.getText());
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |                 startActivity(replyIntent);
 | 
					
						
							|  |  |  |                 return true;
 | 
					
						
							|  |  |  |             case R.id.delete:
 | 
					
						
							|  |  |  |                 if (isInTrash(item)) {
 | 
					
						
							| 
									
										
										
										
											2015-10-23 22:40:09 +02:00
										 |  |  |                     messageRepo.remove(item);
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |                 } else {
 | 
					
						
							|  |  |  |                     item.getLabels().clear();
 | 
					
						
							| 
									
										
										
										
											2015-10-23 22:40:09 +02:00
										 |  |  |                     item.addLabels(messageRepo.getLabels(Label.Type.TRASH));
 | 
					
						
							|  |  |  |                     messageRepo.save(item);
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |                 }
 | 
					
						
							| 
									
										
										
										
											2015-09-11 07:59:39 +02:00
										 |  |  |                 getActivity().onBackPressed();
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |                 return true;
 | 
					
						
							|  |  |  |             case R.id.mark_unread:
 | 
					
						
							| 
									
										
										
										
											2015-10-23 22:40:09 +02:00
										 |  |  |                 item.addLabels(messageRepo.getLabels(Label.Type.UNREAD));
 | 
					
						
							|  |  |  |                 messageRepo.save(item);
 | 
					
						
							| 
									
										
										
										
											2016-02-23 07:06:34 +01:00
										 |  |  |                 if (getActivity() instanceof ActionBarListener) {
 | 
					
						
							|  |  |  |                     ((ActionBarListener) getActivity()).updateUnread();
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |                 return true;
 | 
					
						
							|  |  |  |             case R.id.archive:
 | 
					
						
							| 
									
										
										
										
											2016-02-23 07:06:34 +01:00
										 |  |  |                 if (item.isUnread() && getActivity() instanceof ActionBarListener) {
 | 
					
						
							|  |  |  |                     ((ActionBarListener) getActivity()).updateUnread();
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |                 item.getLabels().clear();
 | 
					
						
							| 
									
										
										
										
											2015-10-23 22:40:09 +02:00
										 |  |  |                 messageRepo.save(item);
 | 
					
						
							| 
									
										
										
										
											2015-09-06 17:45:21 +02:00
										 |  |  |                 return true;
 | 
					
						
							|  |  |  |             default:
 | 
					
						
							|  |  |  |                 return false;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private boolean isInTrash(Plaintext item) {
 | 
					
						
							|  |  |  |         for (Label label : item.getLabels()) {
 | 
					
						
							|  |  |  |             if (label.getType() == Label.Type.TRASH) {
 | 
					
						
							|  |  |  |                 return true;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         return false;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2015-08-05 19:48:41 +02:00
										 |  |  | }
 |