mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-24 02:39:55 +01:00
Move all files to natural position.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package org.thoughtcrime.securesms.giph.model;
|
||||
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.bumptech.glide.load.Key;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Conversions;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public class ChunkedImageUrl implements Key {
|
||||
|
||||
public static final long SIZE_UNKNOWN = -1;
|
||||
|
||||
private final String url;
|
||||
private final long size;
|
||||
|
||||
public ChunkedImageUrl(@NonNull String url) {
|
||||
this(url, SIZE_UNKNOWN);
|
||||
}
|
||||
|
||||
public ChunkedImageUrl(@NonNull String url, long size) {
|
||||
if (url == null) throw new RuntimeException();
|
||||
this.url = url;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
|
||||
messageDigest.update(url.getBytes());
|
||||
messageDigest.update(Conversions.longToByteArray(size));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null || !(other instanceof ChunkedImageUrl)) return false;
|
||||
|
||||
ChunkedImageUrl that = (ChunkedImageUrl)other;
|
||||
|
||||
return this.url.equals(that.url) && this.size == that.size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return url.hashCode() ^ (int)size;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package org.thoughtcrime.securesms.giph.model;
|
||||
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
public class GiphyImage {
|
||||
|
||||
@JsonProperty
|
||||
private ImageTypes images;
|
||||
|
||||
public String getGifUrl() {
|
||||
ImageData data = getGifData();
|
||||
return data != null ? data.url : null;
|
||||
}
|
||||
|
||||
public long getGifSize() {
|
||||
ImageData data = getGifData();
|
||||
return data != null ? data.size : 0;
|
||||
}
|
||||
|
||||
public String getGifMmsUrl() {
|
||||
ImageData data = getGifMmsData();
|
||||
return data != null ? data.url : null;
|
||||
}
|
||||
|
||||
public long getMmsGifSize() {
|
||||
ImageData data = getGifMmsData();
|
||||
return data != null ? data.size : 0;
|
||||
}
|
||||
|
||||
public float getGifAspectRatio() {
|
||||
return (float)images.downsized.width / (float)images.downsized.height;
|
||||
}
|
||||
|
||||
public int getGifWidth() {
|
||||
ImageData data = getGifData();
|
||||
return data != null ? data.width : 0;
|
||||
}
|
||||
|
||||
public int getGifHeight() {
|
||||
ImageData data = getGifData();
|
||||
return data != null ? data.height : 0;
|
||||
}
|
||||
|
||||
public String getStillUrl() {
|
||||
ImageData data = getStillData();
|
||||
return data != null ? data.url : null;
|
||||
}
|
||||
|
||||
public long getStillSize() {
|
||||
ImageData data = getStillData();
|
||||
return data != null ? data.size : 0;
|
||||
}
|
||||
|
||||
private @Nullable ImageData getGifData() {
|
||||
return getFirstNonEmpty(images.downsized, images.downsized_medium, images.fixed_height, images.fixed_width);
|
||||
}
|
||||
|
||||
private @Nullable ImageData getGifMmsData() {
|
||||
return getFirstNonEmpty(images.fixed_height_downsampled, images.fixed_width_downsampled);
|
||||
}
|
||||
|
||||
private @Nullable ImageData getStillData() {
|
||||
return getFirstNonEmpty(images.downsized_still, images.fixed_height_still, images.fixed_width_still);
|
||||
}
|
||||
|
||||
private static @Nullable ImageData getFirstNonEmpty(ImageData... data) {
|
||||
for (ImageData image : data) {
|
||||
if (!TextUtils.isEmpty(image.url)) {
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class ImageTypes {
|
||||
@JsonProperty
|
||||
private ImageData fixed_height;
|
||||
@JsonProperty
|
||||
private ImageData fixed_height_still;
|
||||
@JsonProperty
|
||||
private ImageData fixed_height_downsampled;
|
||||
@JsonProperty
|
||||
private ImageData fixed_width;
|
||||
@JsonProperty
|
||||
private ImageData fixed_width_still;
|
||||
@JsonProperty
|
||||
private ImageData fixed_width_downsampled;
|
||||
@JsonProperty
|
||||
private ImageData fixed_width_small;
|
||||
@JsonProperty
|
||||
private ImageData downsized_medium;
|
||||
@JsonProperty
|
||||
private ImageData downsized;
|
||||
@JsonProperty
|
||||
private ImageData downsized_still;
|
||||
}
|
||||
|
||||
public static class ImageData {
|
||||
@JsonProperty
|
||||
private String url;
|
||||
|
||||
@JsonProperty
|
||||
private int width;
|
||||
|
||||
@JsonProperty
|
||||
private int height;
|
||||
|
||||
@JsonProperty
|
||||
private int size;
|
||||
|
||||
@JsonProperty
|
||||
private String mp4;
|
||||
|
||||
@JsonProperty
|
||||
private String webp;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.thoughtcrime.securesms.giph.model;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GiphyResponse {
|
||||
|
||||
@JsonProperty
|
||||
private List<GiphyImage> data;
|
||||
|
||||
public List<GiphyImage> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.thoughtcrime.securesms.giph.net;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class GiphyGifLoader extends GiphyLoader {
|
||||
|
||||
public GiphyGifLoader(@NonNull Context context, @Nullable String searchString) {
|
||||
super(context, searchString);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTrendingUrl() {
|
||||
return "https://api.giphy.com/v1/gifs/trending?api_key=3o6ZsYH6U6Eri53TXy&offset=%d&limit=" + PAGE_SIZE;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSearchUrl() {
|
||||
return "https://api.giphy.com/v1/gifs/search?api_key=3o6ZsYH6U6Eri53TXy&offset=%d&limit=" + PAGE_SIZE + "&q=%s";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.thoughtcrime.securesms.giph.net;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
|
||||
|
||||
import org.thoughtcrime.securesms.giph.model.GiphyImage;
|
||||
import org.thoughtcrime.securesms.giph.model.GiphyResponse;
|
||||
import org.thoughtcrime.securesms.net.ContentProxySelector;
|
||||
import org.thoughtcrime.securesms.util.AsyncLoader;
|
||||
import org.thoughtcrime.securesms.util.JsonUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public abstract class GiphyLoader extends AsyncLoader<List<GiphyImage>> {
|
||||
|
||||
private static final String TAG = GiphyLoader.class.getSimpleName();
|
||||
|
||||
public static int PAGE_SIZE = 100;
|
||||
|
||||
@Nullable private String searchString;
|
||||
|
||||
private final OkHttpClient client;
|
||||
|
||||
protected GiphyLoader(@NonNull Context context, @Nullable String searchString) {
|
||||
super(context);
|
||||
this.searchString = searchString;
|
||||
this.client = new OkHttpClient.Builder().proxySelector(new ContentProxySelector()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GiphyImage> loadInBackground() {
|
||||
return loadPage(0);
|
||||
}
|
||||
|
||||
public @NonNull List<GiphyImage> loadPage(int offset) {
|
||||
try {
|
||||
String url;
|
||||
|
||||
if (TextUtils.isEmpty(searchString)) url = String.format(getTrendingUrl(), offset);
|
||||
else url = String.format(getSearchUrl(), offset, Uri.encode(searchString));
|
||||
|
||||
Request request = new Request.Builder().url(url).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
throw new IOException("Unexpected code " + response);
|
||||
}
|
||||
|
||||
GiphyResponse giphyResponse = JsonUtils.fromJson(response.body().byteStream(), GiphyResponse.class);
|
||||
List<GiphyImage> results = Stream.of(giphyResponse.getData())
|
||||
.filterNot(g -> TextUtils.isEmpty(g.getGifUrl()))
|
||||
.filterNot(g -> TextUtils.isEmpty(g.getGifMmsUrl()))
|
||||
.filterNot(g -> TextUtils.isEmpty(g.getStillUrl()))
|
||||
.toList();
|
||||
|
||||
if (results == null) return new LinkedList<>();
|
||||
else return results;
|
||||
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, e);
|
||||
return new LinkedList<>();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract String getTrendingUrl();
|
||||
protected abstract String getSearchUrl();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.thoughtcrime.securesms.giph.net;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class GiphyStickerLoader extends GiphyLoader {
|
||||
|
||||
public GiphyStickerLoader(@NonNull Context context, @Nullable String searchString) {
|
||||
super(context, searchString);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTrendingUrl() {
|
||||
return "https://api.giphy.com/v1/stickers/trending?api_key=3o6ZsYH6U6Eri53TXy&offset=%d&limit=" + PAGE_SIZE;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSearchUrl() {
|
||||
return "https://api.giphy.com/v1/stickers/search?q=cat&api_key=3o6ZsYH6U6Eri53TXy&offset=%d&limit=" + PAGE_SIZE + "&q=%s";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Twitter, Inc.
|
||||
*
|
||||
* 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 org.thoughtcrime.securesms.giph.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.appcompat.widget.AppCompatImageView;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
/**
|
||||
* AspectRatioImageView maintains an aspect ratio by adjusting the width or height dimension. The
|
||||
* aspect ratio (width to height ratio) and adjustment dimension can be configured.
|
||||
*/
|
||||
public class AspectRatioImageView extends AppCompatImageView {
|
||||
|
||||
private static final float DEFAULT_ASPECT_RATIO = 1.0f;
|
||||
private static final int DEFAULT_ADJUST_DIMENSION = 0;
|
||||
// defined by attrs.xml enum
|
||||
static final int ADJUST_DIMENSION_HEIGHT = 0;
|
||||
static final int ADJUST_DIMENSION_WIDTH = 1;
|
||||
|
||||
private double aspectRatio; // width to height ratio
|
||||
private int dimensionToAdjust; // ADJUST_DIMENSION_HEIGHT or ADJUST_DIMENSION_WIDTH
|
||||
|
||||
public AspectRatioImageView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public AspectRatioImageView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
// final TypedArray a = context.obtainStyledAttributes(attrs,
|
||||
// R.styleable.tw__AspectRatioImageView);
|
||||
// try {
|
||||
// aspectRatio = a.getFloat(R.styleable.tw__AspectRatioImageView_tw__image_aspect_ratio,
|
||||
// DEFAULT_ASPECT_RATIO);
|
||||
// dimensionToAdjust
|
||||
// = a.getInt(R.styleable.tw__AspectRatioImageView_tw__image_dimension_to_adjust,
|
||||
// DEFAULT_ADJUST_DIMENSION);
|
||||
// } finally {
|
||||
// a.recycle();
|
||||
// }
|
||||
}
|
||||
|
||||
public double getAspectRatio() {
|
||||
return aspectRatio;
|
||||
}
|
||||
|
||||
public int getDimensionToAdjust() {
|
||||
return dimensionToAdjust;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the aspect ratio that should be respected during measurement.
|
||||
*
|
||||
* @param aspectRatio desired width to height ratio
|
||||
*/
|
||||
public void setAspectRatio(final double aspectRatio) {
|
||||
this.aspectRatio = aspectRatio;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the size to 0.
|
||||
*/
|
||||
public void resetSize() {
|
||||
if (getMeasuredWidth() == 0 && getMeasuredHeight() == 0) {
|
||||
return;
|
||||
}
|
||||
measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY),
|
||||
MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY));
|
||||
layout(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
int width = getMeasuredWidth();
|
||||
int height = getMeasuredHeight();
|
||||
if (dimensionToAdjust == ADJUST_DIMENSION_HEIGHT) {
|
||||
height = calculateHeight(width, aspectRatio);
|
||||
} else {
|
||||
width = calculateWidth(height, aspectRatio);
|
||||
}
|
||||
setMeasuredDimension(width, height);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the height that will satisfy the width to height aspect ratio, keeping the given
|
||||
* width fixed.
|
||||
*/
|
||||
int calculateHeight(int width, double ratio) {
|
||||
if (ratio == 0) {
|
||||
return 0;
|
||||
}
|
||||
return (int) Math.round(width / ratio);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the width that will satisfy the width to height aspect ratio, keeping the given
|
||||
* height fixed.
|
||||
*/
|
||||
int calculateWidth(int height, double ratio) {
|
||||
return (int) Math.round(height * ratio);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package org.thoughtcrime.securesms.giph.ui;
|
||||
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import com.google.android.material.tabs.TabLayout;
|
||||
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentPagerAdapter;
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import android.view.View;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.providers.BlobProvider;
|
||||
import org.thoughtcrime.securesms.util.DynamicDarkToolbarTheme;
|
||||
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
||||
import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
|
||||
import org.thoughtcrime.securesms.util.DynamicTheme;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class GiphyActivity extends PassphraseRequiredActionBarActivity
|
||||
implements GiphyActivityToolbar.OnLayoutChangedListener,
|
||||
GiphyActivityToolbar.OnFilterChangedListener,
|
||||
GiphyAdapter.OnItemClickListener
|
||||
{
|
||||
|
||||
private static final String TAG = GiphyActivity.class.getSimpleName();
|
||||
|
||||
public static final String EXTRA_IS_MMS = "extra_is_mms";
|
||||
public static final String EXTRA_WIDTH = "extra_width";
|
||||
public static final String EXTRA_HEIGHT = "extra_height";
|
||||
public static final String EXTRA_COLOR = "extra_color";
|
||||
|
||||
private final DynamicTheme dynamicTheme = new DynamicDarkToolbarTheme();
|
||||
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
||||
|
||||
private GiphyGifFragment gifFragment;
|
||||
private GiphyStickerFragment stickerFragment;
|
||||
private boolean forMms;
|
||||
|
||||
private GiphyAdapter.GiphyViewHolder finishingImage;
|
||||
|
||||
@Override
|
||||
public void onPreCreate() {
|
||||
dynamicTheme.onCreate(this);
|
||||
dynamicLanguage.onCreate(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle bundle, boolean ready) {
|
||||
setContentView(R.layout.giphy_activity);
|
||||
|
||||
initializeToolbar();
|
||||
initializeResources();
|
||||
}
|
||||
|
||||
private void initializeToolbar() {
|
||||
|
||||
GiphyActivityToolbar toolbar = ViewUtil.findById(this, R.id.giphy_toolbar);
|
||||
toolbar.setOnFilterChangedListener(this);
|
||||
toolbar.setOnLayoutChangedListener(this);
|
||||
toolbar.setPersistence(GiphyActivityToolbarTextSecurePreferencesPersistence.fromContext(this));
|
||||
|
||||
final int conversationColor = getConversationColor();
|
||||
toolbar.setBackgroundColor(conversationColor);
|
||||
setStatusBarColor(conversationColor);
|
||||
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
|
||||
getSupportActionBar().setDisplayShowTitleEnabled(false);
|
||||
}
|
||||
|
||||
private void initializeResources() {
|
||||
ViewPager viewPager = ViewUtil.findById(this, R.id.giphy_pager);
|
||||
TabLayout tabLayout = ViewUtil.findById(this, R.id.tab_layout);
|
||||
|
||||
this.gifFragment = new GiphyGifFragment();
|
||||
this.stickerFragment = new GiphyStickerFragment();
|
||||
this.forMms = getIntent().getBooleanExtra(EXTRA_IS_MMS, false);
|
||||
|
||||
gifFragment.setClickListener(this);
|
||||
stickerFragment.setClickListener(this);
|
||||
|
||||
viewPager.setAdapter(new GiphyFragmentPagerAdapter(this, getSupportFragmentManager(),
|
||||
gifFragment, stickerFragment));
|
||||
tabLayout.setupWithViewPager(viewPager);
|
||||
tabLayout.setBackgroundColor(getConversationColor());
|
||||
}
|
||||
|
||||
private @ColorInt int getConversationColor() {
|
||||
return getIntent().getIntExtra(EXTRA_COLOR, ActivityCompat.getColor(this, R.color.signal_primary));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFilterChanged(String filter) {
|
||||
this.gifFragment.setSearchString(filter);
|
||||
this.stickerFragment.setSearchString(filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLayoutChanged(boolean gridLayout) {
|
||||
gifFragment.setLayoutManager(gridLayout);
|
||||
stickerFragment.setLayoutManager(gridLayout);
|
||||
}
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
@Override
|
||||
public void onClick(final GiphyAdapter.GiphyViewHolder viewHolder) {
|
||||
if (finishingImage != null) finishingImage.gifProgress.setVisibility(View.GONE);
|
||||
finishingImage = viewHolder;
|
||||
finishingImage.gifProgress.setVisibility(View.VISIBLE);
|
||||
|
||||
new AsyncTask<Void, Void, Uri>() {
|
||||
@Override
|
||||
protected Uri doInBackground(Void... params) {
|
||||
try {
|
||||
byte[] data = viewHolder.getData(forMms);
|
||||
|
||||
return BlobProvider.getInstance()
|
||||
.forData(data)
|
||||
.withMimeType(MediaUtil.IMAGE_GIF)
|
||||
.createForSingleSessionOnDisk(GiphyActivity.this);
|
||||
} catch (InterruptedException | ExecutionException | IOException e) {
|
||||
Log.w(TAG, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected void onPostExecute(@Nullable Uri uri) {
|
||||
if (uri == null) {
|
||||
Toast.makeText(GiphyActivity.this, R.string.GiphyActivity_error_while_retrieving_full_resolution_gif, Toast.LENGTH_LONG).show();
|
||||
} else if (viewHolder == finishingImage) {
|
||||
Intent intent = new Intent();
|
||||
intent.setData(uri);
|
||||
intent.putExtra(EXTRA_WIDTH, viewHolder.image.getGifWidth());
|
||||
intent.putExtra(EXTRA_HEIGHT, viewHolder.image.getGifHeight());
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
} else {
|
||||
Log.w(TAG, "Resolved Uri is no longer the selected element...");
|
||||
}
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
private static class GiphyFragmentPagerAdapter extends FragmentPagerAdapter {
|
||||
|
||||
private final Context context;
|
||||
private final GiphyGifFragment gifFragment;
|
||||
private final GiphyStickerFragment stickerFragment;
|
||||
|
||||
private GiphyFragmentPagerAdapter(@NonNull Context context,
|
||||
@NonNull FragmentManager fragmentManager,
|
||||
@NonNull GiphyGifFragment gifFragment,
|
||||
@NonNull GiphyStickerFragment stickerFragment)
|
||||
{
|
||||
super(fragmentManager);
|
||||
this.context = context.getApplicationContext();
|
||||
this.gifFragment = gifFragment;
|
||||
this.stickerFragment = stickerFragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
if (position == 0) return gifFragment;
|
||||
else return stickerFragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
if (position == 0) return context.getString(R.string.GiphyFragmentPagerAdapter_gifs);
|
||||
else return context.getString(R.string.GiphyFragmentPagerAdapter_stickers);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
package org.thoughtcrime.securesms.giph.ui;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Rect;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import android.text.Editable;
|
||||
import android.text.InputType;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.TouchDelegate;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.components.AnimatingToggle;
|
||||
|
||||
public class GiphyActivityToolbar extends Toolbar {
|
||||
|
||||
@Nullable private OnFilterChangedListener filterListener;
|
||||
@Nullable private OnLayoutChangedListener layoutListener;
|
||||
|
||||
private EditText searchText;
|
||||
private AnimatingToggle toggle;
|
||||
private ImageView action;
|
||||
private ImageView clearToggle;
|
||||
private LinearLayout toggleContainer;
|
||||
private View listLayoutToggle;
|
||||
private View gridLayoutToggle;
|
||||
private Persistence persistence;
|
||||
|
||||
public GiphyActivityToolbar(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public GiphyActivityToolbar(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, R.attr.toolbarStyle);
|
||||
}
|
||||
|
||||
public GiphyActivityToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
inflate(context, R.layout.giphy_activity_toolbar, this);
|
||||
|
||||
this.action = findViewById(R.id.action_icon);
|
||||
this.searchText = findViewById(R.id.search_view);
|
||||
this.toggle = findViewById(R.id.button_toggle);
|
||||
this.clearToggle = findViewById(R.id.search_clear);
|
||||
this.toggleContainer = findViewById(R.id.toggle_container);
|
||||
this.listLayoutToggle = findViewById(R.id.view_stream);
|
||||
this.gridLayoutToggle = findViewById(R.id.view_grid);
|
||||
|
||||
setupGridLayoutToggles();
|
||||
|
||||
this.clearToggle.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
searchText.setText("");
|
||||
clearToggle.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
});
|
||||
|
||||
this.searchText.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
if (SearchUtil.isEmpty(searchText)) clearToggle.setVisibility(View.INVISIBLE);
|
||||
else clearToggle.setVisibility(View.VISIBLE);
|
||||
|
||||
notifyListener();
|
||||
}
|
||||
});
|
||||
|
||||
this.searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||
@Override
|
||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
||||
InputMethodManager inputMethodManager = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
inputMethodManager.hideSoftInputFromWindow(searchText.getWindowToken(), 0);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
setLogo(null);
|
||||
setNavigationIcon(null);
|
||||
setContentInsetStartWithNavigation(0);
|
||||
expandTapArea(this, action);
|
||||
}
|
||||
|
||||
public void setPersistence(@NonNull Persistence persistence) {
|
||||
this.persistence = persistence;
|
||||
displayTogglingView(persistence.getGridSelected() ? listLayoutToggle : gridLayoutToggle);
|
||||
}
|
||||
|
||||
private void setupGridLayoutToggles() {
|
||||
setUpGridToggle(listLayoutToggle, gridLayoutToggle, false);
|
||||
setUpGridToggle(gridLayoutToggle, listLayoutToggle, true);
|
||||
displayTogglingView(gridLayoutToggle);
|
||||
}
|
||||
|
||||
private void setUpGridToggle(View gridToggle, View otherToggle, boolean gridLayout) {
|
||||
gridToggle.setOnClickListener(v -> {
|
||||
displayTogglingView(otherToggle);
|
||||
if (layoutListener != null) {
|
||||
layoutListener.onLayoutChanged(gridLayout);
|
||||
}
|
||||
if (persistence != null) {
|
||||
persistence.setGridSelected(gridLayout);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNavigationIcon(int resId) {
|
||||
action.setImageResource(resId);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
searchText.setText("");
|
||||
notifyListener();
|
||||
}
|
||||
|
||||
public void setOnLayoutChangedListener(@Nullable OnLayoutChangedListener layoutListener) {
|
||||
this.layoutListener = layoutListener;
|
||||
}
|
||||
|
||||
public void setOnFilterChangedListener(@Nullable OnFilterChangedListener filterListener) {
|
||||
this.filterListener = filterListener;
|
||||
}
|
||||
|
||||
private void notifyListener() {
|
||||
if (filterListener != null) filterListener.onFilterChanged(searchText.getText().toString());
|
||||
}
|
||||
|
||||
private void displayTogglingView(View view) {
|
||||
toggle.display(view);
|
||||
expandTapArea(toggleContainer, view);
|
||||
}
|
||||
|
||||
private void expandTapArea(final View container, final View child) {
|
||||
final int padding = getResources().getDimensionPixelSize(R.dimen.contact_selection_actions_tap_area);
|
||||
|
||||
container.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Rect rect = new Rect();
|
||||
child.getHitRect(rect);
|
||||
|
||||
rect.top -= padding;
|
||||
rect.left -= padding;
|
||||
rect.right += padding;
|
||||
rect.bottom += padding;
|
||||
|
||||
container.setTouchDelegate(new TouchDelegate(rect, child));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static class SearchUtil {
|
||||
public static boolean isTextInput(EditText editText) {
|
||||
return (editText.getInputType() & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_TEXT;
|
||||
}
|
||||
|
||||
public static boolean isPhoneInput(EditText editText) {
|
||||
return (editText.getInputType() & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_PHONE;
|
||||
}
|
||||
|
||||
public static boolean isEmpty(EditText editText) {
|
||||
return editText.getText().length() <= 0;
|
||||
}
|
||||
}
|
||||
|
||||
public interface OnFilterChangedListener {
|
||||
void onFilterChanged(String filter);
|
||||
}
|
||||
|
||||
public interface OnLayoutChangedListener {
|
||||
void onLayoutChanged(boolean gridLayout);
|
||||
}
|
||||
|
||||
public interface Persistence {
|
||||
boolean getGridSelected();
|
||||
void setGridSelected(boolean isGridSelected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.thoughtcrime.securesms.giph.ui;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
|
||||
class GiphyActivityToolbarTextSecurePreferencesPersistence implements GiphyActivityToolbar.Persistence {
|
||||
|
||||
static GiphyActivityToolbar.Persistence fromContext(Context context) {
|
||||
return new GiphyActivityToolbarTextSecurePreferencesPersistence(context.getApplicationContext());
|
||||
}
|
||||
|
||||
private final Context context;
|
||||
|
||||
private GiphyActivityToolbarTextSecurePreferencesPersistence(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getGridSelected() {
|
||||
return TextSecurePreferences.isGifSearchInGridLayout(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGridSelected(boolean isGridSelected) {
|
||||
TextSecurePreferences.setIsGifSearchInGridLayout(context, isGridSelected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package org.thoughtcrime.securesms.giph.ui;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ProgressBar;
|
||||
|
||||
import com.bumptech.glide.RequestBuilder;
|
||||
import com.bumptech.glide.load.DataSource;
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||
import com.bumptech.glide.load.engine.GlideException;
|
||||
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
|
||||
import com.bumptech.glide.load.resource.gif.GifDrawable;
|
||||
import com.bumptech.glide.request.RequestListener;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
import com.bumptech.glide.util.ByteBufferUtil;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.color.MaterialColor;
|
||||
import org.thoughtcrime.securesms.giph.model.GiphyImage;
|
||||
import org.thoughtcrime.securesms.giph.model.ChunkedImageUrl;
|
||||
import org.thoughtcrime.securesms.mms.GlideApp;
|
||||
import org.thoughtcrime.securesms.mms.GlideRequests;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
|
||||
class GiphyAdapter extends RecyclerView.Adapter<GiphyAdapter.GiphyViewHolder> {
|
||||
|
||||
private static final String TAG = GiphyAdapter.class.getSimpleName();
|
||||
|
||||
private final Context context;
|
||||
private final GlideRequests glideRequests;
|
||||
|
||||
private List<GiphyImage> images;
|
||||
private OnItemClickListener listener;
|
||||
|
||||
class GiphyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, RequestListener<Drawable> {
|
||||
|
||||
public AspectRatioImageView thumbnail;
|
||||
public GiphyImage image;
|
||||
public ProgressBar gifProgress;
|
||||
public volatile boolean modelReady;
|
||||
|
||||
GiphyViewHolder(View view) {
|
||||
super(view);
|
||||
thumbnail = ViewUtil.findById(view, R.id.thumbnail);
|
||||
gifProgress = ViewUtil.findById(view, R.id.gif_progress);
|
||||
thumbnail.setOnClickListener(this);
|
||||
gifProgress.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (listener != null) listener.onClick(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
|
||||
Log.w(TAG, e);
|
||||
|
||||
synchronized (this) {
|
||||
if (new ChunkedImageUrl(image.getGifUrl(), image.getGifSize()).equals(model)) {
|
||||
this.modelReady = true;
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
|
||||
synchronized (this) {
|
||||
if (new ChunkedImageUrl(image.getGifUrl(), image.getGifSize()).equals(model)) {
|
||||
this.modelReady = true;
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public byte[] getData(boolean forMms) throws ExecutionException, InterruptedException {
|
||||
synchronized (this) {
|
||||
while (!modelReady) {
|
||||
Util.wait(this, 0);
|
||||
}
|
||||
}
|
||||
|
||||
GifDrawable drawable = glideRequests.asGif()
|
||||
.load(forMms ? new ChunkedImageUrl(image.getGifMmsUrl(), image.getMmsGifSize()) :
|
||||
new ChunkedImageUrl(image.getGifUrl(), image.getGifSize()))
|
||||
.submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
|
||||
.get();
|
||||
|
||||
return ByteBufferUtil.toBytes(drawable.getBuffer());
|
||||
}
|
||||
|
||||
public synchronized void setModelReady() {
|
||||
this.modelReady = true;
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
GiphyAdapter(@NonNull Context context, @NonNull GlideRequests glideRequests, @NonNull List<GiphyImage> images) {
|
||||
this.context = context.getApplicationContext();
|
||||
this.glideRequests = glideRequests;
|
||||
this.images = images;
|
||||
}
|
||||
|
||||
public void setImages(@NonNull List<GiphyImage> images) {
|
||||
this.images = images;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void addImages(List<GiphyImage> images) {
|
||||
this.images.addAll(images);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull GiphyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View itemView = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.giphy_thumbnail, parent, false);
|
||||
|
||||
return new GiphyViewHolder(itemView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull GiphyViewHolder holder, int position) {
|
||||
GiphyImage image = images.get(position);
|
||||
|
||||
holder.modelReady = false;
|
||||
holder.image = image;
|
||||
holder.thumbnail.setAspectRatio(image.getGifAspectRatio());
|
||||
holder.gifProgress.setVisibility(View.GONE);
|
||||
|
||||
RequestBuilder<Drawable> thumbnailRequest = GlideApp.with(context)
|
||||
.load(new ChunkedImageUrl(image.getStillUrl(), image.getStillSize()))
|
||||
.diskCacheStrategy(DiskCacheStrategy.ALL);
|
||||
|
||||
if (Util.isLowMemory(context)) {
|
||||
glideRequests.load(new ChunkedImageUrl(image.getStillUrl(), image.getStillSize()))
|
||||
.placeholder(new ColorDrawable(Util.getRandomElement(MaterialColor.values()).toConversationColor(context)))
|
||||
.diskCacheStrategy(DiskCacheStrategy.ALL)
|
||||
.transition(DrawableTransitionOptions.withCrossFade())
|
||||
.listener(holder)
|
||||
.into(holder.thumbnail);
|
||||
|
||||
holder.setModelReady();
|
||||
} else {
|
||||
glideRequests.load(new ChunkedImageUrl(image.getGifUrl(), image.getGifSize()))
|
||||
.thumbnail(thumbnailRequest)
|
||||
.placeholder(new ColorDrawable(Util.getRandomElement(MaterialColor.values()).toConversationColor(context)))
|
||||
.diskCacheStrategy(DiskCacheStrategy.ALL)
|
||||
.transition(DrawableTransitionOptions.withCrossFade())
|
||||
.listener(holder)
|
||||
.into(holder.thumbnail);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewRecycled(@NonNull GiphyViewHolder holder) {
|
||||
super.onViewRecycled(holder);
|
||||
glideRequests.clear(holder.thumbnail);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return images.size();
|
||||
}
|
||||
|
||||
public void setListener(OnItemClickListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public interface OnItemClickListener {
|
||||
void onClick(GiphyViewHolder viewHolder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package org.thoughtcrime.securesms.giph.ui;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.loader.app.LoaderManager;
|
||||
import androidx.loader.content.Loader;
|
||||
import androidx.recyclerview.widget.DefaultItemAnimator;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.giph.model.GiphyImage;
|
||||
import org.thoughtcrime.securesms.giph.net.GiphyLoader;
|
||||
import org.thoughtcrime.securesms.giph.util.InfiniteScrollListener;
|
||||
import org.thoughtcrime.securesms.mms.GlideApp;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class GiphyFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<GiphyImage>>, GiphyAdapter.OnItemClickListener {
|
||||
|
||||
private static final String TAG = GiphyFragment.class.getSimpleName();
|
||||
|
||||
private GiphyAdapter giphyAdapter;
|
||||
private RecyclerView recyclerView;
|
||||
private ProgressBar loadingProgress;
|
||||
private TextView noResultsView;
|
||||
private GiphyAdapter.OnItemClickListener listener;
|
||||
|
||||
protected String searchString;
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
|
||||
ViewGroup container = ViewUtil.inflate(inflater, viewGroup, R.layout.giphy_fragment);
|
||||
this.recyclerView = ViewUtil.findById(container, R.id.giphy_list);
|
||||
this.loadingProgress = ViewUtil.findById(container, R.id.loading_progress);
|
||||
this.noResultsView = ViewUtil.findById(container, R.id.no_results);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle bundle) {
|
||||
super.onActivityCreated(bundle);
|
||||
|
||||
this.giphyAdapter = new GiphyAdapter(getActivity(), GlideApp.with(this), new LinkedList<>());
|
||||
this.giphyAdapter.setListener(this);
|
||||
|
||||
setLayoutManager(TextSecurePreferences.isGifSearchInGridLayout(getContext()));
|
||||
this.recyclerView.setItemAnimator(new DefaultItemAnimator());
|
||||
this.recyclerView.setAdapter(giphyAdapter);
|
||||
this.recyclerView.addOnScrollListener(new GiphyScrollListener());
|
||||
|
||||
getLoaderManager().initLoader(0, null, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(@NonNull Loader<List<GiphyImage>> loader, @NonNull List<GiphyImage> data) {
|
||||
this.loadingProgress.setVisibility(View.GONE);
|
||||
|
||||
if (data.isEmpty()) noResultsView.setVisibility(View.VISIBLE);
|
||||
else noResultsView.setVisibility(View.GONE);
|
||||
|
||||
this.giphyAdapter.setImages(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(@NonNull Loader<List<GiphyImage>> loader) {
|
||||
noResultsView.setVisibility(View.GONE);
|
||||
this.giphyAdapter.setImages(new LinkedList<GiphyImage>());
|
||||
}
|
||||
|
||||
public void setLayoutManager(boolean gridLayout) {
|
||||
recyclerView.setLayoutManager(getLayoutManager(gridLayout));
|
||||
}
|
||||
|
||||
private RecyclerView.LayoutManager getLayoutManager(boolean gridLayout) {
|
||||
return gridLayout ? new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
|
||||
: new LinearLayoutManager(getActivity());
|
||||
}
|
||||
|
||||
public void setClickListener(GiphyAdapter.OnItemClickListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void setSearchString(@Nullable String searchString) {
|
||||
this.searchString = searchString;
|
||||
this.noResultsView.setVisibility(View.GONE);
|
||||
this.getLoaderManager().restartLoader(0, null, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(GiphyAdapter.GiphyViewHolder viewHolder) {
|
||||
if (listener != null) listener.onClick(viewHolder);
|
||||
}
|
||||
|
||||
private class GiphyScrollListener extends InfiniteScrollListener {
|
||||
@Override
|
||||
public void onLoadMore(final int currentPage) {
|
||||
final Loader<List<GiphyImage>> loader = getLoaderManager().getLoader(0);
|
||||
if (loader == null) return;
|
||||
|
||||
new AsyncTask<Void, Void, List<GiphyImage>>() {
|
||||
@Override
|
||||
protected List<GiphyImage> doInBackground(Void... params) {
|
||||
return ((GiphyLoader)loader).loadPage(currentPage * GiphyLoader.PAGE_SIZE);
|
||||
}
|
||||
|
||||
protected void onPostExecute(List<GiphyImage> images) {
|
||||
giphyAdapter.addImages(images);
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.thoughtcrime.securesms.giph.ui;
|
||||
|
||||
|
||||
import android.os.Bundle;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.loader.content.Loader;
|
||||
|
||||
import org.thoughtcrime.securesms.giph.model.GiphyImage;
|
||||
import org.thoughtcrime.securesms.giph.net.GiphyGifLoader;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GiphyGifFragment extends GiphyFragment {
|
||||
|
||||
@Override
|
||||
public @NonNull Loader<List<GiphyImage>> onCreateLoader(int id, Bundle args) {
|
||||
return new GiphyGifLoader(getActivity(), searchString);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.thoughtcrime.securesms.giph.ui;
|
||||
|
||||
|
||||
import android.os.Bundle;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.loader.content.Loader;
|
||||
|
||||
import org.thoughtcrime.securesms.giph.model.GiphyImage;
|
||||
import org.thoughtcrime.securesms.giph.net.GiphyStickerLoader;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GiphyStickerFragment extends GiphyFragment {
|
||||
@Override
|
||||
public @NonNull Loader<List<GiphyImage>> onCreateLoader(int id, Bundle args) {
|
||||
return new GiphyStickerLoader(getActivity(), searchString);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// From https://gist.github.com/mipreamble/b6d4b3d65b0b4775a22e#file-recyclerviewpositionhelper-java
|
||||
|
||||
package org.thoughtcrime.securesms.giph.util;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
public abstract class InfiniteScrollListener extends RecyclerView.OnScrollListener {
|
||||
|
||||
public static String TAG = InfiniteScrollListener.class.getSimpleName();
|
||||
|
||||
private int previousTotal = 0; // The total number of items in the dataset after the last load
|
||||
private boolean loading = true; // True if we are still waiting for the last set of data to load.
|
||||
private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
|
||||
|
||||
int firstVisibleItem, visibleItemCount, totalItemCount;
|
||||
|
||||
private int currentPage = 1;
|
||||
|
||||
@Override
|
||||
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||||
super.onScrolled(recyclerView, dx, dy);
|
||||
|
||||
RecyclerViewPositionHelper recyclerViewPositionHelper = RecyclerViewPositionHelper.createHelper(recyclerView);
|
||||
|
||||
visibleItemCount = recyclerView.getChildCount();
|
||||
totalItemCount = recyclerViewPositionHelper.getItemCount();
|
||||
firstVisibleItem = recyclerViewPositionHelper.findFirstVisibleItemPosition();
|
||||
|
||||
if (loading) {
|
||||
if (totalItemCount > previousTotal) {
|
||||
loading = false;
|
||||
previousTotal = totalItemCount;
|
||||
}
|
||||
}
|
||||
if (!loading && (totalItemCount - visibleItemCount)
|
||||
<= (firstVisibleItem + visibleThreshold)) {
|
||||
// End has been reached
|
||||
// Do something
|
||||
currentPage++;
|
||||
|
||||
onLoadMore(currentPage);
|
||||
|
||||
loading = true;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void onLoadMore(int currentPage);
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
// From https://gist.github.com/mipreamble/b6d4b3d65b0b4775a22e#file-recyclerviewpositionhelper-java
|
||||
|
||||
package org.thoughtcrime.securesms.giph.util;
|
||||
|
||||
|
||||
import androidx.recyclerview.widget.OrientationHelper;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
|
||||
public class RecyclerViewPositionHelper {
|
||||
|
||||
final RecyclerView recyclerView;
|
||||
final RecyclerView.LayoutManager layoutManager;
|
||||
|
||||
RecyclerViewPositionHelper(RecyclerView recyclerView) {
|
||||
this.recyclerView = recyclerView;
|
||||
this.layoutManager = recyclerView.getLayoutManager();
|
||||
}
|
||||
|
||||
public static RecyclerViewPositionHelper createHelper(RecyclerView recyclerView) {
|
||||
if (recyclerView == null) {
|
||||
throw new NullPointerException("Recycler View is null");
|
||||
}
|
||||
return new RecyclerViewPositionHelper(recyclerView);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the adapter item count.
|
||||
*
|
||||
* @return The total number on items in a layout manager
|
||||
*/
|
||||
public int getItemCount() {
|
||||
return layoutManager == null ? 0 : layoutManager.getItemCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the adapter position of the first visible view. This position does not include
|
||||
* adapter changes that were dispatched after the last layout pass.
|
||||
*
|
||||
* @return The adapter position of the first visible item or {@link RecyclerView#NO_POSITION} if
|
||||
* there aren't any visible items.
|
||||
*/
|
||||
public int findFirstVisibleItemPosition() {
|
||||
final View child = findOneVisibleChild(0, layoutManager.getChildCount(), false, true);
|
||||
return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the adapter position of the first fully visible view. This position does not include
|
||||
* adapter changes that were dispatched after the last layout pass.
|
||||
*
|
||||
* @return The adapter position of the first fully visible item or
|
||||
* {@link RecyclerView#NO_POSITION} if there aren't any visible items.
|
||||
*/
|
||||
public int findFirstCompletelyVisibleItemPosition() {
|
||||
final View child = findOneVisibleChild(0, layoutManager.getChildCount(), true, false);
|
||||
return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the adapter position of the last visible view. This position does not include
|
||||
* adapter changes that were dispatched after the last layout pass.
|
||||
*
|
||||
* @return The adapter position of the last visible view or {@link RecyclerView#NO_POSITION} if
|
||||
* there aren't any visible items
|
||||
*/
|
||||
public int findLastVisibleItemPosition() {
|
||||
final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, false, true);
|
||||
return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the adapter position of the last fully visible view. This position does not include
|
||||
* adapter changes that were dispatched after the last layout pass.
|
||||
*
|
||||
* @return The adapter position of the last fully visible view or
|
||||
* {@link RecyclerView#NO_POSITION} if there aren't any visible items.
|
||||
*/
|
||||
public int findLastCompletelyVisibleItemPosition() {
|
||||
final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, true, false);
|
||||
return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
|
||||
}
|
||||
|
||||
View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
|
||||
boolean acceptPartiallyVisible) {
|
||||
OrientationHelper helper;
|
||||
if (layoutManager.canScrollVertically()) {
|
||||
helper = OrientationHelper.createVerticalHelper(layoutManager);
|
||||
} else {
|
||||
helper = OrientationHelper.createHorizontalHelper(layoutManager);
|
||||
}
|
||||
|
||||
final int start = helper.getStartAfterPadding();
|
||||
final int end = helper.getEndAfterPadding();
|
||||
final int next = toIndex > fromIndex ? 1 : -1;
|
||||
View partiallyVisible = null;
|
||||
for (int i = fromIndex; i != toIndex; i += next) {
|
||||
final View child = layoutManager.getChildAt(i);
|
||||
final int childStart = helper.getDecoratedStart(child);
|
||||
final int childEnd = helper.getDecoratedEnd(child);
|
||||
if (childStart < end && childEnd > start) {
|
||||
if (completelyVisible) {
|
||||
if (childStart >= start && childEnd <= end) {
|
||||
return child;
|
||||
} else if (acceptPartiallyVisible && partiallyVisible == null) {
|
||||
partiallyVisible = child;
|
||||
}
|
||||
} else {
|
||||
return child;
|
||||
}
|
||||
}
|
||||
}
|
||||
return partiallyVisible;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user