support text selection in direct messages

This commit is contained in:
Mariotaku Lee 2015-05-10 18:05:34 +08:00
parent fa21f99796
commit 248781a524
13 changed files with 145 additions and 137 deletions

View File

@ -43,7 +43,6 @@ import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.util.Pair;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.CardView;
import android.support.v7.widget.FixedLinearLayoutManager;
@ -55,22 +54,16 @@ import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.LayoutParams;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.URLSpan;
import android.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.Gravity;
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.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.ImageView;
import android.widget.Space;
import android.widget.TextView;
@ -93,17 +86,16 @@ import org.mariotaku.twidere.model.SingleResponse;
import org.mariotaku.twidere.text.method.StatusContentMovementMethod;
import org.mariotaku.twidere.util.AsyncTaskUtils;
import org.mariotaku.twidere.util.AsyncTwitterWrapper;
import org.mariotaku.twidere.util.ClipboardUtils;
import org.mariotaku.twidere.util.CompareUtils;
import org.mariotaku.twidere.util.KeyboardShortcutsHandler;
import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback;
import org.mariotaku.twidere.util.LinkCreator;
import org.mariotaku.twidere.util.MediaLoaderWrapper;
import org.mariotaku.twidere.util.MediaLoadingHandler;
import org.mariotaku.twidere.util.MenuUtils;
import org.mariotaku.twidere.util.RecyclerViewNavigationHelper;
import org.mariotaku.twidere.util.RecyclerViewUtils;
import org.mariotaku.twidere.util.SharedPreferencesWrapper;
import org.mariotaku.twidere.util.StatusActionModeCallback;
import org.mariotaku.twidere.util.StatusAdapterLinkClickHandler;
import org.mariotaku.twidere.util.StatusLinkClickHandler;
import org.mariotaku.twidere.util.ThemeUtils;
@ -695,7 +687,6 @@ public class StatusFragment extends BaseSupportFragment implements LoaderCallbac
final long timestamp;
final String source;
final int layoutPosition = getLayoutPosition();
final int linkHighlightingStyle = adapter.getLinkHighlightingStyle();
if (status.is_quote) {
quotedNameView.setText(manager.getUserNickname(status.user_id, status.user_name, false));
quotedScreenNameView.setText("@" + status.user_screen_name);
@ -942,55 +933,6 @@ public class StatusFragment extends BaseSupportFragment implements LoaderCallbac
}
private static class StatusActionModeCallback implements Callback {
private final TextView textView;
private final FragmentActivity activity;
public StatusActionModeCallback(TextView textView, FragmentActivity activity) {
this.textView = textView;
this.activity = activity;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.action_status_text_selection, menu);
mode.setTitle(android.R.string.selectTextMode);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
final int start = textView.getSelectionStart(), end = textView.getSelectionEnd();
final SpannableString string = SpannableString.valueOf(textView.getText());
final URLSpan[] spans = string.getSpans(start, end, URLSpan.class);
final boolean avail = spans.length == 1 && URLUtil.isValidUrl(spans[0].getURL());
MenuUtils.setMenuItemAvailability(menu, android.R.id.copyUrl, avail);
MenuUtils.setMenuItemShowAsActionFlags(menu, android.R.id.copyUrl, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case android.R.id.copyUrl: {
final int start = textView.getSelectionStart(), end = textView.getSelectionEnd();
final SpannableString string = SpannableString.valueOf(textView.getText());
final URLSpan[] spans = string.getSpans(start, end, URLSpan.class);
if (spans.length != 1) return true;
ClipboardUtils.setText(activity, spans[0].getURL());
mode.finish();
return true;
}
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
}
}
static class LoadConversationTask extends AsyncTask<ParcelableStatus, ParcelableStatus,

View File

@ -32,10 +32,12 @@ import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.mariotaku.twidere.R;
import org.mariotaku.twidere.api.twitter.Twitter;
import org.mariotaku.twidere.api.twitter.TwitterException;
import org.mariotaku.twidere.api.twitter.model.TranslationResult;
import org.mariotaku.twidere.model.ParcelableStatus;
import org.mariotaku.twidere.model.SingleResponse;
import org.mariotaku.twidere.util.TwitterAPIUtils;
@ -43,17 +45,12 @@ import org.mariotaku.twidere.util.Utils;
import org.mariotaku.twidere.view.holder.StatusViewHolder;
import org.mariotaku.twidere.view.holder.StatusViewHolder.DummyStatusHolderAdapter;
import org.mariotaku.twidere.api.twitter.model.TranslationResult;
import org.mariotaku.twidere.api.twitter.Twitter;
import org.mariotaku.twidere.api.twitter.TwitterException;
public class StatusTranslateDialogFragment extends BaseSupportDialogFragment implements
LoaderCallbacks<SingleResponse<TranslationResult>> {
private StatusViewHolder mHolder;
private DummyStatusHolderAdapter mAdapter;
private ProgressBar mProgressBar;
private View mProgress;
private TextView mMessageView;
private View mProgressContainer;
private View mStatusContainer;
@ -70,8 +67,8 @@ public class StatusTranslateDialogFragment extends BaseSupportDialogFragment imp
dismiss();
return;
}
mAdapter = new DummyStatusHolderAdapter(getActivity());
mHolder = new StatusViewHolder(mAdapter, mStatusContainer);
DummyStatusHolderAdapter adapter = new DummyStatusHolderAdapter(getActivity());
mHolder = new StatusViewHolder(adapter, mStatusContainer);
getLoaderManager().initLoader(0, args, this);
}
@ -80,7 +77,7 @@ public class StatusTranslateDialogFragment extends BaseSupportDialogFragment imp
final ParcelableStatus status = args.getParcelable(EXTRA_STATUS);
mStatusContainer.setVisibility(View.GONE);
mProgressContainer.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
mProgress.setVisibility(View.VISIBLE);
mMessageView.setVisibility(View.VISIBLE);
mMessageView.setText(R.string.please_wait);
final long statusId;
@ -96,7 +93,7 @@ public class StatusTranslateDialogFragment extends BaseSupportDialogFragment imp
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mProgressContainer = view.findViewById(R.id.progress_container);
mProgressBar = (ProgressBar) mProgressContainer.findViewById(android.R.id.progress);
mProgress = mProgressContainer.findViewById(R.id.load_progress);
mMessageView = (TextView) mProgressContainer.findViewById(android.R.id.message);
mStatusContainer = view.findViewById(R.id.status_container);
}
@ -123,7 +120,7 @@ public class StatusTranslateDialogFragment extends BaseSupportDialogFragment imp
} else {
mStatusContainer.setVisibility(View.GONE);
mProgressContainer.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
mProgress.setVisibility(View.GONE);
mMessageView.setVisibility(View.VISIBLE);
mMessageView.setText(Utils.getErrorMessage(getActivity(), data.getException()));
}

View File

@ -29,6 +29,7 @@ import android.text.method.MovementMethod;
import android.text.style.ClickableSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.TextView;
/**
@ -60,7 +61,7 @@ public class StatusContentMovementMethod extends ArrowKeyMovementMethod {
@Override
public boolean onTouchEvent(@NonNull final TextView widget, @NonNull final Spannable buffer, @NonNull final MotionEvent event) {
final int action = event.getAction();
final int action = event.getActionMasked();
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
@ -78,16 +79,22 @@ public class StatusContentMovementMethod extends ArrowKeyMovementMethod {
final ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(widget);
} else {
if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
return true;
} else {
if ((!widget.isLongClickable() || (event.getEventTime() - event.getDownTime()) < ViewConfiguration.getLongPressTimeout())) {
link[0].onClick(widget);
}
Selection.removeSelection(buffer);
return true;
}
return true;
} else {
Selection.removeSelection(buffer);
}
}
return super.onTouchEvent(widget, buffer, event);

View File

@ -0,0 +1,84 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.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, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util;
import android.content.Context;
import android.support.v4.view.MenuItemCompat;
import android.text.SpannableString;
import android.text.style.URLSpan;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.URLUtil;
import android.widget.TextView;
import org.mariotaku.twidere.R;
/**
* Created by mariotaku on 15/5/10.
*/
public class StatusActionModeCallback implements ActionMode.Callback {
private final TextView textView;
private final Context context;
public StatusActionModeCallback(TextView textView, Context context) {
this.textView = textView;
this.context = context;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.action_status_text_selection, menu);
mode.setTitle(android.R.string.selectTextMode);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
final int start = textView.getSelectionStart(), end = textView.getSelectionEnd();
final SpannableString string = SpannableString.valueOf(textView.getText());
final URLSpan[] spans = string.getSpans(start, end, URLSpan.class);
final boolean avail = spans.length == 1 && URLUtil.isValidUrl(spans[0].getURL());
MenuUtils.setMenuItemAvailability(menu, android.R.id.copyUrl, avail);
MenuUtils.setMenuItemShowAsActionFlags(menu, android.R.id.copyUrl, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case android.R.id.copyUrl: {
final int start = textView.getSelectionStart(), end = textView.getSelectionEnd();
final SpannableString string = SpannableString.valueOf(textView.getText());
final URLSpan[] spans = string.getSpans(start, end, URLSpan.class);
if (spans.length != 1) return true;
ClipboardUtils.setText(context, spans[0].getURL());
mode.finish();
return true;
}
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
}

View File

@ -23,7 +23,6 @@ import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatTextView;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ClickableSpan;
@ -32,6 +31,8 @@ import android.view.MotionEvent;
public class HandleSpanClickTextView extends AppCompatTextView {
private boolean mLongClickPerformed;
public HandleSpanClickTextView(final Context context) {
super(context);
}
@ -68,19 +69,27 @@ public class HandleSpanClickTextView extends AppCompatTextView {
if (links.length != 0 && x <= lineWidth) {
final ClickableSpan link = links[0];
if (action == MotionEvent.ACTION_UP) {
Selection.removeSelection(buffer);
setClickable(false);
link.onClick(this);
if (!mLongClickPerformed) {
link.onClick(this);
}
return true;
} else {
Selection.setSelection(buffer, buffer.getSpanStart(link), buffer.getSpanEnd(link));
mLongClickPerformed = false;
setClickable(true);
}
} else {
Selection.removeSelection(buffer);
setClickable(false);
}
}
return super.onTouchEvent(event);
}
@Override
public boolean performLongClick() {
final boolean result = super.performLongClick();
mLongClickPerformed = true;
return result;
}
}

View File

@ -71,7 +71,6 @@ public class HomeActionButton extends FrameLayout implements IHomeActionButton {
private final EffectViewHelper mHelper;
private final ImageView mIconView;
private final ProgressBar mProgressBar;
public HomeActionButton(final Context context) {
this(context, null);
@ -98,7 +97,6 @@ public class HomeActionButton extends FrameLayout implements IHomeActionButton {
this);
}
mIconView = (ImageView) findViewById(android.R.id.icon);
mProgressBar = (ProgressBar) findViewById(android.R.id.progress);
ViewSupport.setOutlineProvider(this, new HomeActionButtonOutlineProvider());
setClipToOutline(true);
setButtonColor(Color.WHITE);
@ -127,12 +125,10 @@ public class HomeActionButton extends FrameLayout implements IHomeActionButton {
@Override
public void setIconColor(int color, Mode mode) {
mIconView.setColorFilter(color, mode);
mProgressBar.setIndeterminateTintList(ColorStateList.valueOf(color));
}
@Override
public void setShowProgress(final boolean showProgress) {
mProgressBar.setVisibility(showProgress ? View.VISIBLE : View.GONE);
mIconView.setVisibility(showProgress ? View.GONE : View.VISIBLE);
}

View File

@ -36,7 +36,6 @@ import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import org.mariotaku.twidere.R;
import org.mariotaku.twidere.activity.iface.IThemedActivity;
@ -47,7 +46,6 @@ import org.mariotaku.twidere.view.iface.IHomeActionButton;
public class HomeActionButtonCompat extends FrameLayout implements IHomeActionButton {
private final ImageView mIconView;
private final ProgressBar mProgressBar;
private final FloatingActionDrawable mBackground;
public HomeActionButtonCompat(final Context context) {
@ -72,7 +70,6 @@ public class HomeActionButtonCompat extends FrameLayout implements IHomeActionBu
this);
}
mIconView = (ImageView) findViewById(android.R.id.icon);
mProgressBar = (ProgressBar) findViewById(android.R.id.progress);
final Resources resources = getResources();
final int radius = resources.getDimensionPixelSize(R.dimen.element_spacing_small);
mBackground = new FloatingActionDrawable(this, radius);
@ -82,19 +79,6 @@ public class HomeActionButtonCompat extends FrameLayout implements IHomeActionBu
@Override
public void setButtonColor(int color) {
mBackground.setColor(color);
final View child = getChildAt(0);
if (child instanceof FrameLayout) {
final Drawable foreground = ((FrameLayout) child).getForeground();
if (foreground != null) {
final Resources resources = getResources();
// final int colorDark = resources.getColor(R.color.action_icon_dark);
// final int colorLight = resources.getColor(R.color.action_icon_light);
// final int contrastColor = TwidereColorUtils.getContrastYIQ(color,
// ThemeUtils.ACCENT_COLOR_THRESHOLD, colorDark, colorLight);
// foreground.setColorFilter(contrastColor, Mode.MULTIPLY);
}
}
}
@Override
@ -119,7 +103,6 @@ public class HomeActionButtonCompat extends FrameLayout implements IHomeActionBu
@Override
public void setShowProgress(final boolean showProgress) {
mProgressBar.setVisibility(showProgress ? View.VISIBLE : View.GONE);
mIconView.setVisibility(showProgress ? View.GONE : View.VISIBLE);
}

View File

@ -26,6 +26,7 @@ import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.Html;
import android.text.method.ArrowKeyMovementMethod;
import android.view.View;
import android.widget.TextView;
@ -35,6 +36,7 @@ import org.mariotaku.twidere.adapter.MessageConversationAdapter;
import org.mariotaku.twidere.model.ParcelableDirectMessage.CursorIndices;
import org.mariotaku.twidere.model.ParcelableMedia;
import org.mariotaku.twidere.util.MediaLoaderWrapper;
import org.mariotaku.twidere.util.StatusActionModeCallback;
import org.mariotaku.twidere.util.ThemeUtils;
import org.mariotaku.twidere.util.TwidereColorUtils;
import org.mariotaku.twidere.util.TwidereLinkify;
@ -45,12 +47,13 @@ import org.mariotaku.twidere.view.CardMediaContainer.OnMediaClickListener;
public class MessageViewHolder extends ViewHolder implements OnMediaClickListener {
public final CardMediaContainer mediaContainer;
public final TextView text, time;
public final TextView textView, time;
private final MessageBubbleView messageContent;
protected final MessageConversationAdapter adapter;
private final int textColorPrimary, textColorPrimaryInverse, textColorSecondary, textColorSecondaryInverse;
private final StatusActionModeCallback callback;
public MessageViewHolder(final MessageConversationAdapter adapter, final View itemView) {
@ -66,10 +69,11 @@ public class MessageViewHolder extends ViewHolder implements OnMediaClickListene
textColorSecondaryInverse = a.getColor(3, 0);
a.recycle();
messageContent = (MessageBubbleView) itemView.findViewById(R.id.message_content);
text = (TextView) itemView.findViewById(R.id.text);
textView = (TextView) itemView.findViewById(R.id.text);
time = (TextView) itemView.findViewById(R.id.time);
mediaContainer = (CardMediaContainer) itemView.findViewById(R.id.media_preview_container);
mediaContainer.setStyle(adapter.getMediaPreviewStyle());
callback = new StatusActionModeCallback(textView, adapter.getContext());
}
public void displayMessage(Cursor cursor, CursorIndices indices) {
@ -80,12 +84,15 @@ public class MessageViewHolder extends ViewHolder implements OnMediaClickListene
final long accountId = cursor.getLong(indices.account_id);
final long timestamp = cursor.getLong(indices.message_timestamp);
final ParcelableMedia[] media = ParcelableMedia.fromSerializedJson(cursor.getString(indices.media));
text.setText(Html.fromHtml(cursor.getString(indices.text)));
linkify.applyAllLinks(text, accountId, false);
text.setMovementMethod(null);
textView.setText(Html.fromHtml(cursor.getString(indices.text)));
linkify.applyAllLinks(textView, accountId, false);
time.setText(Utils.formatToLongTimeString(context, timestamp));
mediaContainer.setVisibility(media != null && media.length > 0 ? View.VISIBLE : View.GONE);
mediaContainer.displayMedia(media, loader, accountId, true, this, adapter.getMediaLoadingHandler());
textView.setTextIsSelectable(true);
textView.setMovementMethod(ArrowKeyMovementMethod.getInstance());
textView.setCustomSelectionActionModeCallback(callback);
}
@Override
@ -115,13 +122,13 @@ public class MessageViewHolder extends ViewHolder implements OnMediaClickListene
ThemeUtils.ACCENT_COLOR_THRESHOLD, textPrimaryDark, textPrimaryLight);
final int textContrastSecondary = TwidereColorUtils.getContrastYIQ(color,
ThemeUtils.ACCENT_COLOR_THRESHOLD, textSecondaryDark, textSecondaryLight);
text.setTextColor(textContrastPrimary);
text.setLinkTextColor(textContrastSecondary);
textView.setTextColor(textContrastPrimary);
textView.setLinkTextColor(textContrastSecondary);
time.setTextColor(textContrastSecondary);
}
public void setTextSize(final float textSize) {
text.setTextSize(textSize);
textView.setTextSize(textSize);
time.setTextSize(textSize * 0.75f);
}

View File

@ -26,15 +26,6 @@
android:background="?selectableItemBackground"
android:duplicateParentState="true"/>
<ProgressBar
android:id="@android:id/progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/element_spacing_normal"
android:visibility="gone"/>
<ImageView
android:id="@android:id/icon"
android:layout_width="match_parent"

View File

@ -25,14 +25,6 @@
android:layout_gravity="center"
android:foreground="@drawable/btn_home_actions_compat">
<ProgressBar
android:id="@android:id/progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"/>
<ImageView
android:id="@android:id/icon"
android:layout_width="wrap_content"

View File

@ -45,6 +45,7 @@
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:textIsSelectable="true"
tools:text="@string/sample_status_text" />
<TextView

View File

@ -34,11 +34,10 @@
android:visibility="visible"
tools:visibility="gone">
<ProgressBar
android:id="@android:id/progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<include
layout="@layout/layout_progress_wheel_medium"
android:layout_width="@dimen/element_size_normal"
android:layout_height="@dimen/element_size_normal"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp" />

View File

@ -24,6 +24,8 @@
android:animateLayoutChanges="true">
<FrameLayout
android:visibility="gone"
tools:visibility="visible"
android:id="@+id/list_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -62,13 +64,11 @@
<FrameLayout
android:id="@+id/progress_container"
android:layout_width="match_parent"
android:visibility="visible"
tools:visibility="gone"
android:layout_height="match_parent">
<ProgressBar
android:id="@android:id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<include layout="@layout/layout_progress_wheel_medium" />
</FrameLayout>
</FrameLayout>