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-09-29 20:14:22 +00:00
|
|
|
import android.app.Activity;
|
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;
|
2015-07-30 22:02:20 +00:00
|
|
|
import android.support.annotation.IdRes;
|
2015-09-15 22:28:27 +00:00
|
|
|
import android.support.annotation.LayoutRes;
|
2015-07-28 22:38:38 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2015-11-03 01:40:41 +00:00
|
|
|
import android.support.v4.view.ViewCompat;
|
2015-09-18 00:53:09 +00:00
|
|
|
import android.support.v4.view.animation.FastOutSlowInInterpolator;
|
2015-07-28 22:38:38 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextUtils.TruncateAt;
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
public static CharSequence ellipsize(@Nullable CharSequence text, @NonNull TextView view) {
|
|
|
|
if (TextUtils.isEmpty(text) || view.getWidth() == 0 || view.getEllipsize() != TruncateAt.END) {
|
|
|
|
return text;
|
|
|
|
} else {
|
|
|
|
return TextUtils.ellipsize(text,
|
|
|
|
view.getPaint(),
|
|
|
|
view.getWidth() - view.getPaddingRight() - view.getPaddingLeft(),
|
|
|
|
TruncateAt.END);
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void fadeOut(final @NonNull View view, final int duration) {
|
|
|
|
animateOut(view, getAlphaAnimation(1f, 0f, duration));
|
|
|
|
}
|
|
|
|
|
2015-09-10 04:05:21 +00:00
|
|
|
public static void animateOut(final @NonNull View view, final @NonNull Animation animation) {
|
|
|
|
if (view.getVisibility() == View.GONE) return;
|
|
|
|
|
|
|
|
view.clearAnimation();
|
2015-09-18 00:53:09 +00:00
|
|
|
animation.reset();
|
|
|
|
animation.setStartTime(0);
|
2015-09-10 04:05:21 +00:00
|
|
|
animation.setAnimationListener(new Animation.AnimationListener() {
|
|
|
|
@Override public void onAnimationStart(Animation animation) {}
|
|
|
|
@Override public void onAnimationRepeat(Animation animation) {}
|
|
|
|
@Override public void onAnimationEnd(Animation animation) {
|
|
|
|
view.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
view.startAnimation(animation);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-30 06:37:01 +00:00
|
|
|
}
|