use of twitter4j.URLEntity in project twicalico by moko256.
the class CachedUsersSQLiteOpenHelper method addCachedUserAtTransaction.
private void addCachedUserAtTransaction(User user) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(TABLE_COLUMNS[0], user.getId());
contentValues.put(TABLE_COLUMNS[1], user.getName());
contentValues.put(TABLE_COLUMNS[2], user.getEmail());
contentValues.put(TABLE_COLUMNS[3], user.getScreenName());
contentValues.put(TABLE_COLUMNS[4], user.getLocation());
contentValues.put(TABLE_COLUMNS[5], user.getDescription());
contentValues.put(TABLE_COLUMNS[6], user.isContributorsEnabled());
contentValues.put(TABLE_COLUMNS[7], user.getProfileImageURL());
contentValues.put(TABLE_COLUMNS[8], user.getProfileImageURLHttps());
contentValues.put(TABLE_COLUMNS[9], user.isDefaultProfileImage());
contentValues.put(TABLE_COLUMNS[10], user.getURL());
contentValues.put(TABLE_COLUMNS[11], user.isProtected());
contentValues.put(TABLE_COLUMNS[12], user.getFollowersCount());
contentValues.put(TABLE_COLUMNS[13], user.getProfileBackgroundColor());
contentValues.put(TABLE_COLUMNS[14], user.getProfileTextColor());
contentValues.put(TABLE_COLUMNS[15], user.getProfileLinkColor());
contentValues.put(TABLE_COLUMNS[16], user.getProfileSidebarFillColor());
contentValues.put(TABLE_COLUMNS[17], user.getProfileSidebarBorderColor());
contentValues.put(TABLE_COLUMNS[18], user.isProfileUseBackgroundImage());
contentValues.put(TABLE_COLUMNS[19], user.isDefaultProfile());
contentValues.put(TABLE_COLUMNS[20], user.isShowAllInlineMedia());
contentValues.put(TABLE_COLUMNS[21], user.getFriendsCount());
contentValues.put(TABLE_COLUMNS[22], user.getCreatedAt().getTime());
contentValues.put(TABLE_COLUMNS[23], user.getFavouritesCount());
contentValues.put(TABLE_COLUMNS[24], user.getUtcOffset());
contentValues.put(TABLE_COLUMNS[25], user.getTimeZone());
contentValues.put(TABLE_COLUMNS[26], user.getProfileBackgroundImageURL());
contentValues.put(TABLE_COLUMNS[27], user.getProfileBackgroundImageUrlHttps());
contentValues.put(TABLE_COLUMNS[28], user.getProfileBannerURL() != null ? user.getProfileBannerURL().replaceAll("/web$", "") : null);
contentValues.put(TABLE_COLUMNS[29], user.isProfileBackgroundTiled());
contentValues.put(TABLE_COLUMNS[30], user.getLang());
contentValues.put(TABLE_COLUMNS[31], user.getStatusesCount());
contentValues.put(TABLE_COLUMNS[32], user.isGeoEnabled());
contentValues.put(TABLE_COLUMNS[33], user.isVerified());
contentValues.put(TABLE_COLUMNS[34], user.isTranslator());
contentValues.put(TABLE_COLUMNS[35], user.isFollowRequestSent());
int size = user.getDescriptionURLEntities().length;
String[] texts = new String[size];
String[] URLs = new String[size];
String[] expandedURLs = new String[size];
String[] displaysURLs = new String[size];
String[] starts = new String[size];
String[] ends = new String[size];
for (int i = 0; i < size; i++) {
URLEntity entity = user.getDescriptionURLEntities()[i];
texts[i] = entity.getText();
URLs[i] = entity.getURL();
expandedURLs[i] = entity.getExpandedURL();
displaysURLs[i] = entity.getDisplayURL();
starts[i] = String.valueOf(entity.getStart());
ends[i] = String.valueOf(entity.getEnd());
}
contentValues.put(TABLE_COLUMNS[36], ArrayUtils.toCommaSplitString(texts).toString());
contentValues.put(TABLE_COLUMNS[37], ArrayUtils.toCommaSplitString(URLs).toString());
contentValues.put(TABLE_COLUMNS[38], ArrayUtils.toCommaSplitString(expandedURLs).toString());
contentValues.put(TABLE_COLUMNS[39], ArrayUtils.toCommaSplitString(displaysURLs).toString());
contentValues.put(TABLE_COLUMNS[40], ArrayUtils.toCommaSplitString(starts).toString());
contentValues.put(TABLE_COLUMNS[41], ArrayUtils.toCommaSplitString(ends).toString());
contentValues.put(TABLE_COLUMNS[42], ArrayUtils.toCommaSplitString(user.getWithheldInCountries()).toString());
database.replace(TABLE_NAME, null, contentValues);
}
use of twitter4j.URLEntity in project twicalico by moko256.
the class CachedUsersSQLiteOpenHelper method restoreURLEntities.
private URLEntity[] restoreURLEntities(String[] texts, String[] URLs, String[] expandedURLs, String[] displaysURLs, String[] starts, String[] ends) {
URLEntity[] entities = new URLEntity[texts.length];
for (int i = 0; i < entities.length; i++) {
int finalI = i;
entities[i] = new URLEntity() {
@Override
public String getText() {
return texts[finalI];
}
@Override
public String getURL() {
return URLs[finalI];
}
@Override
public String getExpandedURL() {
return expandedURLs[finalI];
}
@Override
public String getDisplayURL() {
return displaysURLs[finalI];
}
@Override
public int getStart() {
return Integer.valueOf(starts[finalI].trim());
}
@Override
public int getEnd() {
return Integer.valueOf(ends[finalI].trim());
}
};
}
return entities;
}
use of twitter4j.URLEntity in project twicalico by moko256.
the class TwitterStringUtils method getProfileLinkedSequence.
public static CharSequence getProfileLinkedSequence(Context context, User user) {
String description = user.getDescription();
if (GlobalApplication.clientType == Type.MASTODON) {
return convertUrlSpanToCustomTabs(Html.fromHtml(description), context);
}
URLEntity[] urlEntities = user.getDescriptionURLEntities();
if (urlEntities == null || urlEntities.length <= 0 || TextUtils.isEmpty(urlEntities[0].getURL()))
return description;
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(description);
int tweetLength = description.codePointCount(0, description.length());
int sp = 0;
for (URLEntity entity : urlEntities) {
String url = entity.getURL();
String expandedUrl = entity.getDisplayURL();
int urlLength = url.codePointCount(0, url.length());
int displayUrlLength = expandedUrl.codePointCount(0, expandedUrl.length());
if (entity.getStart() <= tweetLength && entity.getEnd() <= tweetLength) {
int dusp = displayUrlLength - urlLength;
spannableStringBuilder.replace(description.offsetByCodePoints(0, entity.getStart()) + sp, description.offsetByCodePoints(0, entity.getEnd()) + sp, expandedUrl);
spannableStringBuilder.setSpan(new ClickableSpan() {
@Override
public void onClick(View view) {
AppCustomTabsKt.launchChromeCustomTabs(context, entity.getExpandedURL());
}
}, description.offsetByCodePoints(0, entity.getStart()) + sp, description.offsetByCodePoints(0, entity.getEnd()) + sp + dusp, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
sp += dusp;
}
}
return spannableStringBuilder;
}
use of twitter4j.URLEntity in project twicalico by moko256.
the class TwitterStringUtils method setLinkedSequenceTo.
@SuppressLint("StaticFieldLeak")
public static void setLinkedSequenceTo(Status item, TextView textView) {
Context context = textView.getContext();
String tweet = item.getText();
if (GlobalApplication.clientType == Type.MASTODON) {
Spanned html = Html.fromHtml(tweet);
int length = html.length();
// Trim unless \n\n made by fromHtml() after post
if (length == 3 && item.getMediaEntities().length > 0 && html.charAt(0) == ".".charAt(0)) {
// If post has media only, context of post from Mastodon is "."
textView.setText("");
return;
} else if (length >= 2) {
html = (Spanned) html.subSequence(0, length - 2);
}
SpannableStringBuilder builder = convertUrlSpanToCustomTabs(html, context);
textView.setText(builder);
List<Emoji> list = ((StatusCacheMap.CachedStatus) item).getEmojis();
if (list != null) {
Matcher matcher = containsEmoji.matcher(builder);
boolean matches = matcher.matches();
int imageSize;
if (matches) {
imageSize = (int) Math.floor((textView.getTextSize() * 1.15) * 2.0);
} else {
imageSize = (int) Math.floor(textView.getTextSize() * 1.15);
}
new AsyncTask<Void, Void, Map<String, Drawable>>() {
@Override
protected Map<String, Drawable> doInBackground(Void... params) {
Map<String, Drawable> map = new ArrayMap<>();
GlideRequests glideRequests = GlideApp.with(context);
for (Emoji emoji : list) {
try {
Drawable value = glideRequests.load(emoji.getUrl()).submit().get();
value.setBounds(0, 0, imageSize, imageSize);
map.put(emoji.getShortCode(), value);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
return map;
}
@Override
protected void onPostExecute(Map<String, Drawable> map) {
if (TextUtils.equals(builder, textView.getText())) {
boolean found = matches || matcher.find();
while (found) {
String shortCode = matcher.group(1);
Drawable drawable = map.get(shortCode);
if (drawable != null) {
builder.setSpan(new ImageSpan(drawable), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
found = matcher.find();
}
textView.setText(builder);
}
}
}.execute();
}
return;
}
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(tweet);
for (SymbolEntity symbolEntity : item.getSymbolEntities()) {
spannableStringBuilder.setSpan(new ClickableSpan() {
@Override
public void onClick(View view) {
context.startActivity(SearchResultActivity.getIntent(context, symbolEntity.getText()));
}
}, tweet.offsetByCodePoints(0, symbolEntity.getStart()), tweet.offsetByCodePoints(0, symbolEntity.getEnd()), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
for (HashtagEntity hashtagEntity : item.getHashtagEntities()) {
spannableStringBuilder.setSpan(new ClickableSpan() {
@Override
public void onClick(View view) {
context.startActivity(SearchResultActivity.getIntent(context, "#" + hashtagEntity.getText()));
}
}, tweet.offsetByCodePoints(0, hashtagEntity.getStart()), tweet.offsetByCodePoints(0, hashtagEntity.getEnd()), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
for (UserMentionEntity userMentionEntity : item.getUserMentionEntities()) {
spannableStringBuilder.setSpan(new ClickableSpan() {
@Override
public void onClick(View view) {
context.startActivity(ShowUserActivity.getIntent(context, userMentionEntity.getScreenName()));
}
}, tweet.offsetByCodePoints(0, userMentionEntity.getStart()), tweet.offsetByCodePoints(0, userMentionEntity.getEnd()), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
boolean hasMedia = item.getMediaEntities().length > 0;
List<URLEntity> urlEntities = new ArrayList<>(item.getURLEntities().length + (hasMedia ? 1 : 0));
urlEntities.addAll(Arrays.asList(item.getURLEntities()));
if (hasMedia) {
urlEntities.add(item.getMediaEntities()[0]);
}
int tweetLength = tweet.codePointCount(0, tweet.length());
int sp = 0;
for (URLEntity entity : urlEntities) {
String url = entity.getURL();
String displayUrl = entity.getDisplayURL();
int urlLength = url.codePointCount(0, url.length());
int displayUrlLength = displayUrl.codePointCount(0, displayUrl.length());
if (entity.getStart() <= tweetLength && entity.getEnd() <= tweetLength) {
int dusp = displayUrlLength - urlLength;
spannableStringBuilder.replace(tweet.offsetByCodePoints(0, entity.getStart()) + sp, tweet.offsetByCodePoints(0, entity.getEnd()) + sp, displayUrl);
spannableStringBuilder.setSpan(new ClickableSpan() {
@Override
public void onClick(View view) {
AppCustomTabsKt.launchChromeCustomTabs(context, entity.getExpandedURL());
}
}, tweet.offsetByCodePoints(0, entity.getStart()) + sp, tweet.offsetByCodePoints(0, entity.getEnd()) + sp + dusp, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
sp += dusp;
}
}
textView.setText(spannableStringBuilder);
}
use of twitter4j.URLEntity in project twicalico by moko256.
the class TwitterStringUtils method getStatusTextSequence.
public static CharSequence getStatusTextSequence(Status item) {
String postText = item.getText();
StringBuilder builder = new StringBuilder(postText);
URLEntity[] urlEntities = item.getURLEntities();
int tweetLength = postText.codePointCount(0, postText.length());
int sp = 0;
for (URLEntity entity : urlEntities) {
String url = entity.getURL();
String displayUrl = entity.getDisplayURL();
int urlLength = url.codePointCount(0, url.length());
int displayUrlLength = displayUrl.codePointCount(0, displayUrl.length());
if (entity.getStart() <= tweetLength && entity.getEnd() <= tweetLength) {
int dusp = displayUrlLength - urlLength;
builder.replace(postText.offsetByCodePoints(0, entity.getStart()) + sp, postText.offsetByCodePoints(0, entity.getEnd()) + sp, displayUrl);
sp += dusp;
}
}
MediaEntity[] mediaEntities = item.getMediaEntities();
if (mediaEntities.length > 0) {
MediaEntity mediaEntity = mediaEntities[0];
int result = builder.indexOf(mediaEntity.getURL(), builder.offsetByCodePoints(0, mediaEntity.getStart()));
if (result != -1) {
builder.replace(result, result + mediaEntity.getURL().length(), "");
}
}
return builder;
}
Aggregations