2015-01-30 06:37:01 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2015 Open Whisper Systems
|
|
|
|
*
|
|
|
|
* 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.thoughtcrime.securesms.util;
|
|
|
|
|
2015-11-26 18:04:27 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2015-09-29 20:14:22 +00:00
|
|
|
import android.app.Activity;
|
2015-11-26 18:04:27 +00:00
|
|
|
import android.content.Context;
|
2015-01-30 06:37:01 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2015-09-18 00:53:09 +00:00
|
|
|
import android.os.Build.VERSION;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
2020-08-19 00:06:26 +00:00
|
|
|
import androidx.annotation.IdRes;
|
|
|
|
import androidx.annotation.LayoutRes;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.core.view.ViewCompat;
|
|
|
|
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;
|
2015-11-26 18:04:27 +00:00
|
|
|
import android.view.Gravity;
|
2015-09-15 22:28:27 +00:00
|
|
|
import android.view.LayoutInflater;
|
2015-01-30 06:37:01 +00:00
|
|
|
import android.view.View;
|
2015-07-28 00:00:44 +00:00
|
|
|
import android.view.ViewGroup;
|
2015-07-30 22:02:20 +00:00
|
|
|
import android.view.ViewStub;
|
2015-09-18 00:53:09 +00:00
|
|
|
import android.view.animation.AlphaAnimation;
|
2015-09-10 04:05:21 +00:00
|
|
|
import android.view.animation.Animation;
|
2015-11-03 01:40:41 +00:00
|
|
|
import android.widget.LinearLayout.LayoutParams;
|
2015-07-28 22:38:38 +00:00
|
|
|
import android.widget.TextView;
|
2015-01-30 06:37:01 +00:00
|
|
|
|
2015-11-12 18:47:42 +00:00
|
|
|
import org.thoughtcrime.securesms.util.concurrent.ListenableFuture;
|
|
|
|
import org.thoughtcrime.securesms.util.concurrent.SettableFuture;
|
2017-01-19 02:46:40 +00:00
|
|
|
import org.thoughtcrime.securesms.util.views.Stub;
|
2015-11-12 18:47:42 +00:00
|
|
|
|
2015-01-30 06:37:01 +00:00
|
|
|
public class ViewUtil {
|
2015-09-18 00:53:09 +00:00
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public static void setBackground(final @NonNull View v, final @Nullable Drawable drawable) {
|
|
|
|
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
|
|
|
|
v.setBackground(drawable);
|
|
|
|
} else {
|
|
|
|
v.setBackgroundDrawable(drawable);
|
|
|
|
}
|
2015-01-30 06:37:01 +00:00
|
|
|
}
|
2015-07-28 00:00:44 +00:00
|
|
|
|
2015-11-03 01:40:41 +00:00
|
|
|
public static void setY(final @NonNull View v, final int y) {
|
|
|
|
if (VERSION.SDK_INT >= 11) {
|
|
|
|
ViewCompat.setY(v, y);
|
|
|
|
} else {
|
|
|
|
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)v.getLayoutParams();
|
|
|
|
params.topMargin = y;
|
|
|
|
v.setLayoutParams(params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static float getY(final @NonNull View v) {
|
|
|
|
if (VERSION.SDK_INT >= 11) {
|
|
|
|
return ViewCompat.getY(v);
|
|
|
|
} else {
|
|
|
|
return ((ViewGroup.MarginLayoutParams)v.getLayoutParams()).topMargin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setX(final @NonNull View v, final int x) {
|
|
|
|
if (VERSION.SDK_INT >= 11) {
|
|
|
|
ViewCompat.setX(v, x);
|
|
|
|
} else {
|
|
|
|
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)v.getLayoutParams();
|
|
|
|
params.leftMargin = x;
|
|
|
|
v.setLayoutParams(params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static float getX(final @NonNull View v) {
|
|
|
|
if (VERSION.SDK_INT >= 11) {
|
|
|
|
return ViewCompat.getX(v);
|
|
|
|
} else {
|
|
|
|
return ((LayoutParams)v.getLayoutParams()).leftMargin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 00:00:44 +00:00
|
|
|
public static void swapChildInPlace(ViewGroup parent, View toRemove, View toAdd, int defaultIndex) {
|
|
|
|
int childIndex = parent.indexOfChild(toRemove);
|
|
|
|
if (childIndex > -1) parent.removeView(toRemove);
|
|
|
|
parent.addView(toAdd, childIndex > -1 ? childIndex : defaultIndex);
|
|
|
|
}
|
2015-07-28 22:38:38 +00:00
|
|
|
|
2015-07-30 22:02:20 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <T extends View> T inflateStub(@NonNull View parent, @IdRes int stubId) {
|
|
|
|
return (T)((ViewStub)parent.findViewById(stubId)).inflate();
|
|
|
|
}
|
2015-09-08 01:08:44 +00:00
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <T extends View> T findById(@NonNull View parent, @IdRes int resId) {
|
|
|
|
return (T) parent.findViewById(resId);
|
|
|
|
}
|
2015-09-10 04:05:21 +00:00
|
|
|
|
2015-09-29 20:14:22 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <T extends View> T findById(@NonNull Activity parent, @IdRes int resId) {
|
|
|
|
return (T) parent.findViewById(resId);
|
|
|
|
}
|
|
|
|
|
2017-01-19 02:46:40 +00:00
|
|
|
public static <T extends View> Stub<T> findStubById(@NonNull Activity parent, @IdRes int resId) {
|
|
|
|
return new Stub<T>((ViewStub)parent.findViewById(resId));
|
|
|
|
}
|
|
|
|
|
2015-09-18 00:53:09 +00:00
|
|
|
private static Animation getAlphaAnimation(float from, float to, int duration) {
|
|
|
|
final Animation anim = new AlphaAnimation(from, to);
|
|
|
|
anim.setInterpolator(new FastOutSlowInInterpolator());
|
|
|
|
anim.setDuration(duration);
|
|
|
|
return anim;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void fadeIn(final @NonNull View view, final int duration) {
|
|
|
|
animateIn(view, getAlphaAnimation(0f, 1f, duration));
|
|
|
|
}
|
|
|
|
|
2015-11-17 02:38:36 +00:00
|
|
|
public static ListenableFuture<Boolean> fadeOut(final @NonNull View view, final int duration) {
|
2015-11-18 22:52:26 +00:00
|
|
|
return fadeOut(view, duration, View.GONE);
|
2015-09-18 00:53:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 22:52:26 +00:00
|
|
|
public static ListenableFuture<Boolean> fadeOut(@NonNull View view, int duration, int visibility) {
|
|
|
|
return animateOut(view, getAlphaAnimation(1f, 0f, duration), visibility);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ListenableFuture<Boolean> animateOut(final @NonNull View view, final @NonNull Animation animation, final int visibility) {
|
2015-11-12 18:47:42 +00:00
|
|
|
final SettableFuture future = new SettableFuture();
|
2015-11-18 22:52:26 +00:00
|
|
|
if (view.getVisibility() == visibility) {
|
2015-11-12 18:47:42 +00:00
|
|
|
future.set(true);
|
|
|
|
} else {
|
|
|
|
view.clearAnimation();
|
|
|
|
animation.reset();
|
|
|
|
animation.setStartTime(0);
|
|
|
|
animation.setAnimationListener(new Animation.AnimationListener() {
|
|
|
|
@Override
|
|
|
|
public void onAnimationStart(Animation animation) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationRepeat(Animation animation) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationEnd(Animation animation) {
|
2015-11-18 22:52:26 +00:00
|
|
|
view.setVisibility(visibility);
|
2015-11-12 18:47:42 +00:00
|
|
|
future.set(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
view.startAnimation(animation);
|
|
|
|
}
|
|
|
|
return future;
|
2015-09-10 04:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void animateIn(final @NonNull View view, final @NonNull Animation animation) {
|
|
|
|
if (view.getVisibility() == View.VISIBLE) return;
|
2015-09-18 00:53:09 +00:00
|
|
|
|
2015-09-10 04:05:21 +00:00
|
|
|
view.clearAnimation();
|
2015-09-18 00:53:09 +00:00
|
|
|
animation.reset();
|
|
|
|
animation.setStartTime(0);
|
2015-09-10 04:05:21 +00:00
|
|
|
view.setVisibility(View.VISIBLE);
|
|
|
|
view.startAnimation(animation);
|
|
|
|
}
|
2015-09-15 22:28:27 +00:00
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <T extends View> T inflate(@NonNull LayoutInflater inflater,
|
|
|
|
@NonNull ViewGroup parent,
|
|
|
|
@LayoutRes int layoutResId)
|
|
|
|
{
|
|
|
|
return (T)(inflater.inflate(layoutResId, parent, false));
|
|
|
|
}
|
2015-11-26 18:04:27 +00:00
|
|
|
|
|
|
|
@SuppressLint("RtlHardcoded")
|
|
|
|
public static void setTextViewGravityStart(final @NonNull TextView textView, @NonNull Context context) {
|
|
|
|
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
|
|
|
|
if (DynamicLanguage.getLayoutDirection(context) == View.LAYOUT_DIRECTION_RTL) {
|
|
|
|
textView.setGravity(Gravity.RIGHT);
|
|
|
|
} else {
|
|
|
|
textView.setGravity(Gravity.LEFT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void mirrorIfRtl(View view, Context context) {
|
|
|
|
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1 &&
|
|
|
|
DynamicLanguage.getLayoutDirection(context) == View.LAYOUT_DIRECTION_RTL) {
|
|
|
|
view.setScaleX(-1.0f);
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 19:05:53 +00:00
|
|
|
|
|
|
|
public static int dpToPx(Context context, int dp) {
|
|
|
|
return (int)((dp * context.getResources().getDisplayMetrics().density) + 0.5);
|
|
|
|
}
|
2018-06-26 17:27:44 +00:00
|
|
|
|
|
|
|
public static void updateLayoutParams(@NonNull View view, int width, int height) {
|
|
|
|
view.getLayoutParams().width = width;
|
|
|
|
view.getLayoutParams().height = height;
|
|
|
|
view.requestLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getLeftMargin(@NonNull View view) {
|
|
|
|
if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_LTR) {
|
|
|
|
return ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).leftMargin;
|
|
|
|
}
|
|
|
|
return ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).rightMargin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getRightMargin(@NonNull View view) {
|
|
|
|
if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_LTR) {
|
|
|
|
return ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).rightMargin;
|
|
|
|
}
|
|
|
|
return ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).leftMargin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setLeftMargin(@NonNull View view, int margin) {
|
|
|
|
if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_LTR) {
|
|
|
|
((ViewGroup.MarginLayoutParams) view.getLayoutParams()).leftMargin = margin;
|
|
|
|
} else {
|
|
|
|
((ViewGroup.MarginLayoutParams) view.getLayoutParams()).rightMargin = margin;
|
|
|
|
}
|
2018-07-18 20:53:50 +00:00
|
|
|
view.forceLayout();
|
2018-06-26 17:27:44 +00:00
|
|
|
view.requestLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setTopMargin(@NonNull View view, int margin) {
|
|
|
|
((ViewGroup.MarginLayoutParams) view.getLayoutParams()).topMargin = margin;
|
|
|
|
view.requestLayout();
|
|
|
|
}
|
|
|
|
|
2018-07-23 12:08:45 +00:00
|
|
|
public static void setPaddingTop(@NonNull View view, int padding) {
|
|
|
|
view.setPadding(view.getPaddingLeft(), padding, view.getPaddingRight(), view.getPaddingBottom());
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:27:44 +00:00
|
|
|
public static void setPaddingBottom(@NonNull View view, int padding) {
|
|
|
|
view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), padding);
|
|
|
|
}
|
2019-04-17 14:21:30 +00:00
|
|
|
|
|
|
|
public static boolean isPointInsideView(@NonNull View view, float x, float y) {
|
|
|
|
int[] location = new int[2];
|
|
|
|
|
|
|
|
view.getLocationOnScreen(location);
|
|
|
|
|
|
|
|
int viewX = location[0];
|
|
|
|
int viewY = location[1];
|
|
|
|
|
|
|
|
return x > viewX && x < viewX + view.getWidth() &&
|
|
|
|
y > viewY && y < viewY + view.getHeight();
|
|
|
|
}
|
2015-01-30 06:37:01 +00:00
|
|
|
}
|