Fixed commentview events, long press and click now work as expected.

captchafix
Florens Douwes 11 years ago
parent 93fb5a7327
commit 65d09ba0fb
  1. 5
      Chan/src/org/floens/chan/adapter/PostAdapter.java
  2. 2
      Chan/src/org/floens/chan/loader/LoaderPool.java
  3. 85
      Chan/src/org/floens/chan/model/Post.java
  4. 12
      Chan/src/org/floens/chan/utils/Utils.java
  5. 143
      Chan/src/org/floens/chan/view/PostView.java

@ -29,7 +29,7 @@ public class PostAdapter extends BaseAdapter {
private final List<Post> postList = new ArrayList<Post>();
public PostAdapter(Context activity, ThreadManager threadManager, ListView listView) {
this.context = activity;
context = activity;
this.threadManager = threadManager;
this.listView = listView;
}
@ -131,7 +131,8 @@ public class PostAdapter extends BaseAdapter {
public void scrollToPost(Post post) {
for (int i = 0; i < postList.size(); i++) {
if (postList.get(i).no == post.no) {
listView.smoothScrollToPosition(i);
// listView.smoothScrollToPosition(i); does not work when a view is taller than the container
listView.setSelection(i);
break;
}

@ -6,7 +6,7 @@ import java.util.Map;
import org.floens.chan.model.Loadable;
public class LoaderPool {
private static final String TAG = "LoaderPool";
// private static final String TAG = "LoaderPool";
private static LoaderPool instance;

@ -17,10 +17,12 @@ import org.jsoup.parser.Parser;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
/**
* Contains all data needer to represent a single post.
* Contains all data needed to represent a single post.
*/
public class Post {
public String board;
@ -66,6 +68,12 @@ public class Post {
* The PostView the Post is currently bound to.
*/
public SpannableString subjectSpan;
public SpannableString nameSpan;
public SpannableString tripcodeSpan;
public SpannableString idSpan;
public SpannableString capcodeSpan;
private PostView linkableListener;
private String rawComment;
@ -86,21 +94,28 @@ public class Post {
/**
* Finish up the data
*
* @return false if this data is invalid
*/
public boolean finish(Loadable loadable) {
if (board == null) return false;
if (board == null)
return false;
if (no < 0 || resto < 0 || date == null) return false;
if (no < 0 || resto < 0 || date == null)
return false;
isOP = resto == 0;
if (isOP && replies < 0) return false;
if (isOP && replies < 0)
return false;
if (ext != null) hasImage = true;
if (ext != null) {
hasImage = true;
}
if (hasImage) {
if (filename == null || tim == null || ext == null || imageWidth <= 0 || imageHeight <= 0) return false;
if (filename == null || tim == null || ext == null || imageWidth <= 0 || imageHeight <= 0)
return false;
thumbnailUrl = ChanUrls.getThumbnailUrl(board, tim);
imageUrl = ChanUrls.getImageUrl(board, tim, ext);
@ -122,13 +137,56 @@ public class Post {
e.printStackTrace();
}
parseSpans();
return true;
}
private void parseSpans() {
if (!TextUtils.isEmpty(subject)) {
subjectSpan = new SpannableString(subject);
subjectSpan.setSpan(new ForegroundColorSpan(Color.argb(255, 15, 12, 93)), 0, subjectSpan.length(), 0);
}
if (!TextUtils.isEmpty(name)) {
nameSpan = new SpannableString(name);
nameSpan.setSpan(new ForegroundColorSpan(Color.argb(255, 17, 119, 67)), 0, nameSpan.length(), 0);
}
if (!TextUtils.isEmpty(tripcode)) {
tripcodeSpan = new SpannableString(tripcode);
tripcodeSpan.setSpan(new ForegroundColorSpan(Color.argb(255, 17, 119, 67)), 0, tripcodeSpan.length(), 0);
tripcodeSpan.setSpan(new AbsoluteSizeSpan(10, true), 0, tripcodeSpan.length(), 0);
}
if (!TextUtils.isEmpty(id)) {
idSpan = new SpannableString(" ID: " + id + " ");
// Stolen from the 4chan extension
int hash = id.hashCode();
int r = (hash >> 24) & 0xff;
int g = (hash >> 16) & 0xff;
int b = (hash >> 8) & 0xff;
int idColor = (0xff << 24) + (r << 16) + (g << 8) + b;
int idBgColor = ((r * 0.299f) + (g * 0.587f) + (b * 0.114f)) > 125f ? 0xff636363 : 0x00000000;
idSpan.setSpan(new ForegroundColorSpan(idColor), 0, idSpan.length(), 0);
idSpan.setSpan(new BackgroundColorSpan(idBgColor), 0, idSpan.length(), 0);
idSpan.setSpan(new AbsoluteSizeSpan(10, true), 0, idSpan.length(), 0);
}
if (!TextUtils.isEmpty(capcode)) {
capcodeSpan = new SpannableString("Capcode: " + capcode);
capcodeSpan.setSpan(new ForegroundColorSpan(Color.argb(255, 255, 0, 0)), 0, capcodeSpan.length(), 0);
capcodeSpan.setSpan(new AbsoluteSizeSpan(10, true), 0, capcodeSpan.length(), 0);
}
}
private CharSequence parseComment(String commentRaw, boolean simpleMode) {
if (simpleMode) {
if (simpleMode)
return "";
}
CharSequence total = new SpannableString("");
@ -155,9 +213,6 @@ public class Post {
URL url = new URL(item);
SpannableString link = new SpannableString(url.toString());
// link.setSpan(new ForegroundColorSpan(Color.argb(255, 0, 0, 180)), 0, link.length(), 0);
// linkables.add(new PostLinkable(this, item, item, PostLinkable.Type.LINK));
PostLinkable pl = new PostLinkable(this, item, item, PostLinkable.Type.LINK);
link.setSpan(pl, 0, link.length(), 0);
@ -199,7 +254,8 @@ public class Post {
int id = Integer.parseInt(splitted[1]);
repliesTo.add(id);
}
} catch(NumberFormatException e) {}
} catch (NumberFormatException e) {
}
}
total = TextUtils.concat(total, link);
@ -217,8 +273,3 @@ public class Post {
return total;
}
}

@ -1,5 +1,6 @@
package org.floens.chan.utils;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Handler;
@ -13,14 +14,19 @@ public class Utils {
*/
@SuppressWarnings("deprecation")
public static void setPressedDrawable(View view) {
TypedArray arr = view.getContext().obtainStyledAttributes(
Drawable drawable = Utils.getSelectableBackgroundDrawable(view.getContext());
view.setBackgroundDrawable(drawable);
}
public static Drawable getSelectableBackgroundDrawable(Context context) {
TypedArray arr = context.obtainStyledAttributes(
new int[] {android.R.attr.selectableItemBackground});
Drawable drawable = arr.getDrawable(0);
view.setBackgroundDrawable(drawable);
arr.recycle();
return drawable;
}
/**

@ -13,14 +13,18 @@ import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
@ -41,6 +45,7 @@ public class PostView extends LinearLayout implements View.OnClickListener, View
private boolean isBuild = false;
private LinearLayout full;
private LinearLayout right;
private NetworkImageView imageView;
private TextView titleView;
private TextView commentView;
@ -58,19 +63,16 @@ public class PostView extends LinearLayout implements View.OnClickListener, View
public PostView(Context activity) {
super(activity);
context = (Activity) activity;
init();
}
public PostView(Context activity, AttributeSet attbs) {
super(activity, attbs);
context = (Activity) activity;
init();
}
public PostView(Context activity, AttributeSet attbs, int style) {
super(activity, attbs, style);
context = (Activity) activity;
init();
}
@Override
@ -98,39 +100,24 @@ public class PostView extends LinearLayout implements View.OnClickListener, View
CharSequence total = new SpannableString("");
if (!TextUtils.isEmpty(post.subject)) {
SpannableString subject = new SpannableString(post.subject);
subject.setSpan(new ForegroundColorSpan(Color.argb(255, 15, 12, 93)), 0, subject.length(), 0);
total = TextUtils.concat(total, subject, "\n");
if (post.subjectSpan != null) {
total = TextUtils.concat(total, post.subjectSpan, "\n");
}
if (!TextUtils.isEmpty(post.name)) {
SpannableString name = new SpannableString(post.name);
name.setSpan(new ForegroundColorSpan(Color.argb(255, 17, 119, 67)), 0, name.length(), 0);
total = TextUtils.concat(total, name, " ");
if (post.nameSpan != null) {
total = TextUtils.concat(total, post.nameSpan, " ");
}
if (!TextUtils.isEmpty(post.tripcode)) {
SpannableString tripcode = new SpannableString(post.tripcode);
tripcode.setSpan(new ForegroundColorSpan(Color.argb(255, 17, 119, 67)), 0, tripcode.length(), 0);
tripcode.setSpan(new AbsoluteSizeSpan(10, true), 0, tripcode.length(), 0);
total = TextUtils.concat(total, tripcode, " ");
if (post.tripcodeSpan != null) {
total = TextUtils.concat(total, post.tripcodeSpan, " ");
}
if (!TextUtils.isEmpty(post.id)) {
SpannableString id = new SpannableString(" ID: " + post.id + " ");
IDColor c = computeIDColor(post.id);
id.setSpan(new ForegroundColorSpan(c.color), 0, id.length(), 0);
id.setSpan(new BackgroundColorSpan(c.backgroundColor), 0, id.length(), 0);
id.setSpan(new AbsoluteSizeSpan(10, true), 0, id.length(), 0);
total = TextUtils.concat(total, id, " ");
if (post.idSpan != null) {
total = TextUtils.concat(total, post.idSpan, " ");
}
if (!TextUtils.isEmpty(post.capcode)) {
SpannableString tripcode = new SpannableString("Capcode: " + post.capcode);
tripcode.setSpan(new ForegroundColorSpan(Color.argb(255, 255, 0, 0)), 0, tripcode.length(), 0);
tripcode.setSpan(new AbsoluteSizeSpan(10, true), 0, tripcode.length(), 0);
total = TextUtils.concat(total, tripcode, " ");
if (post.capcodeSpan != null) {
total = TextUtils.concat(total, post.capcodeSpan, " ");
}
CharSequence relativeTime = DateUtils.getRelativeTimeSpanString(post.time * 1000L, System.currentTimeMillis(),
@ -146,20 +133,9 @@ public class PostView extends LinearLayout implements View.OnClickListener, View
if (!TextUtils.isEmpty(post.comment)) {
commentView.setVisibility(View.VISIBLE);
commentView.setText(post.comment);
commentView.setMovementMethod(LinkMovementMethod.getInstance());
commentView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PostView.this.onClick(v);
}
});
commentView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return PostView.this.onLongClick(v);
}
});
commentView.setMovementMethod(new PostViewMovementMethod());
commentView.setOnClickListener(this);
commentView.setOnLongClickListener(this);
if (manager.getLoadable().isThreadMode()) {
post.setLinkableListener(this);
@ -221,24 +197,15 @@ public class PostView extends LinearLayout implements View.OnClickListener, View
countryView.setImageUrl(null, null);
}
if (post.isOP && manager.getLoadable().isBoardMode()) {
full.setClickable(true);
full.setFocusable(true);
full.setOnClickListener(this);
Utils.setPressedDrawable(full);
}
if (post.isSavedReply) {
full.setBackgroundColor(0xFFD6BAD0);
} else {
full.setBackgroundColor(0x00000000);
}
}
private void init() {
setOnClickListener(this);
setOnLongClickListener(this);
if (manager.getLoadable().isBoardMode()) {
Utils.setPressedDrawable(right);
}
}
private void buildView(final Context context) {
@ -278,7 +245,7 @@ public class PostView extends LinearLayout implements View.OnClickListener, View
full.addView(left, wrapMatchParams);
full.setMinimumHeight(imageSize);
LinearLayout right = new LinearLayout(context);
right = new LinearLayout(context);
right.setOrientation(VERTICAL);
LinearLayout header = new LinearLayout(context);
@ -325,20 +292,9 @@ public class PostView extends LinearLayout implements View.OnClickListener, View
full.addView(right, matchWrapParams);
addView(full, matchParams);
}
private IDColor computeIDColor(String id) {
// Stolen from the 4chan extension
int hash = post.id.hashCode();
int r = (hash >> 24) & 0xff;
int g = (hash >> 16) & 0xff;
int b = (hash >> 8) & 0xff;
IDColor c = new IDColor();
c.color = (0xff << 24) + (r << 16) + (g << 8) + b;
c.backgroundColor = ((r * 0.299f) + (g * 0.587f) + (b * 0.114f)) > 125 ? 0xff636363 : 0x00000000;
return c;
full.setOnClickListener(this);
full.setOnLongClickListener(this);
}
public void onLinkableClick(PostLinkable linkable) {
@ -357,9 +313,52 @@ public class PostView extends LinearLayout implements View.OnClickListener, View
return true;
}
private static class IDColor {
public int color;
public int backgroundColor;
private class PostViewMovementMethod extends LinkMovementMethod {
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
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 {
Selection.removeSelection(buffer);
// Changed this to propagate events
PostView.this.onTouchEvent(event);
return true;
}
} else {
PostView.this.onTouchEvent(event);
return true;
}
// return Touch.onTouchEvent(widget, buffer, event);
}
}
}

Loading…
Cancel
Save