Abit/app/src/main/java/ch/dissem/apps/abit/MessageDetailFragment.java

201 lines
7.5 KiB
Java
Raw Normal View History

/*
* 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.
*/
package ch.dissem.apps.abit;
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.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
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;
2015-11-17 22:19:11 +01:00
2016-02-23 07:06:34 +01:00
import ch.dissem.apps.abit.listener.ActionBarListener;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.util.Assets;
import ch.dissem.apps.abit.util.Drawables;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.valueobject.Label;
import ch.dissem.bitmessage.ports.MessageRepository;
2015-11-17 22:19:11 +01:00
import static android.text.util.Linkify.WEB_URLS;
import static ch.dissem.apps.abit.util.Constants.BITMESSAGE_ADDRESS_PATTERN;
import static ch.dissem.apps.abit.util.Constants.BITMESSAGE_URL_SCHEMA;
/**
* A fragment representing a single Message detail screen.
* This fragment is either contained in a {@link MainActivity}
* 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.
*/
private Plaintext item;
2015-09-06 17:45:21 +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);
}
@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) {
((TextView) rootView.findViewById(R.id.subject)).setText(item.getSubject());
ImageView status = (ImageView) rootView.findViewById(R.id.status);
status.setImageResource(Assets.getStatusDrawable(item.getStatus()));
status.setContentDescription(getString(Assets.getStatusString(item.getStatus())));
BitmessageAddress sender = item.getFrom();
((ImageView) rootView.findViewById(R.id.avatar))
.setImageDrawable(new Identicon(sender));
((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,
new Linkify.TransformFilter() {
@Override
public String transformUrl(Matcher match, String url) {
return match.group();
}
}
);
2015-11-17 22:19:11 +01:00
messageBody.setLinksClickable(true);
messageBody.setTextIsSelectable(true);
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();
}
Singleton.getMessageRepository(inflater.getContext()).save(item);
}
}
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);
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) {
MessageRepository messageRepo = Singleton.getMessageRepository(getContext());
2015-09-06 17:45:21 +02:00
switch (menuItem.getItemId()) {
case R.id.reply:
ComposeMessageActivity.launchReplyTo(this, item);
2015-09-06 17:45:21 +02:00
return true;
case R.id.delete:
if (isInTrash(item)) {
messageRepo.remove(item);
2015-09-06 17:45:21 +02:00
} else {
item.getLabels().clear();
item.addLabels(messageRepo.getLabels(Label.Type.TRASH));
messageRepo.save(item);
2015-09-06 17:45:21 +02:00
}
getActivity().onBackPressed();
2015-09-06 17:45:21 +02:00
return true;
case R.id.mark_unread:
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();
messageRepo.save(item);
2015-09-06 17:45:21 +02:00
return true;
default:
return false;
}
}
public static boolean isInTrash(Plaintext item) {
2015-09-06 17:45:21 +02:00
for (Label label : item.getLabels()) {
if (label.getType() == Label.Type.TRASH) {
return true;
}
}
return false;
}
}