diff --git a/app/src/main/java/fr/free/nrw/commons/ui/widget/CompatTextView.java b/app/src/main/java/fr/free/nrw/commons/ui/widget/CompatTextView.java index 2508877ed..a04f7f8c8 100644 --- a/app/src/main/java/fr/free/nrw/commons/ui/widget/CompatTextView.java +++ b/app/src/main/java/fr/free/nrw/commons/ui/widget/CompatTextView.java @@ -1,73 +1,96 @@ -package fr.free.nrw.commons.ui.widget; - -/** - * Created by mikel on 07/08/2017. - */ - -import android.content.Context; -import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; -import android.support.annotation.Nullable; -import android.support.v4.view.ViewCompat; -import android.support.v7.widget.AppCompatDrawableManager; -import android.support.v7.widget.AppCompatTextView; -import android.util.AttributeSet; - -import fr.free.nrw.commons.R; -import fr.free.nrw.commons.utils.UiUtils; - -public class CompatTextView extends AppCompatTextView { - public CompatTextView(Context context) { - super(context); - init(null); - } - - public CompatTextView(Context context, AttributeSet attrs) { - super(context, attrs); - init(attrs); - } - - public CompatTextView(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - init(attrs); - } - - private void init(@Nullable AttributeSet attrs) { - if (attrs != null) { - Context context = getContext(); - TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompatTextView); - - // Obtain DrawableManager used to pull Drawables safely, and check if we're in RTL - AppCompatDrawableManager dm = AppCompatDrawableManager.get(); - boolean rtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL; - - // Grab the compat drawable padding from the XML - float drawablePadding = a.getDimension(R.styleable.CompatTextView_drawablePadding, 0); - - // Grab the compat drawable resources from the XML - int startDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableStart, 0); - int topDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableTop, 0); - int endDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableEnd, 0); - int bottomDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableBottom, 0); - - // Load the used drawables, fall back to whatever was set in an "android:" - Drawable[] currentDrawables = getCompoundDrawables(); - Drawable left = startDrawableRes != 0 - ? dm.getDrawable(context, startDrawableRes) : currentDrawables[0]; - Drawable right = endDrawableRes != 0 - ? dm.getDrawable(context, endDrawableRes) : currentDrawables[1]; - Drawable top = topDrawableRes != 0 - ? dm.getDrawable(context, topDrawableRes) : currentDrawables[2]; - Drawable bottom = bottomDrawableRes != 0 - ? dm.getDrawable(context, bottomDrawableRes) : currentDrawables[3]; - - // Account for RTL and apply the compound Drawables - Drawable start = rtl ? right : left; - Drawable end = rtl ? left : right; - setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom); - setCompoundDrawablePadding((int) UiUtils.convertDpToPixel(drawablePadding, getContext())); - - a.recycle(); - } - } -} +package fr.free.nrw.commons.ui.widget; + +/** + * Created by mikel on 07/08/2017. + */ + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.support.annotation.Nullable; +import android.support.v4.view.ViewCompat; +import android.support.v7.widget.AppCompatDrawableManager; +import android.support.v7.widget.AppCompatTextView; +import android.util.AttributeSet; + +import fr.free.nrw.commons.R; +import fr.free.nrw.commons.utils.UiUtils; + +/** + * a text view compatible with older versions of the platform + */ +public class CompatTextView extends AppCompatTextView { + + /** + * Constructs a new instance of CompatTextView + * @param context the view context + */ + public CompatTextView(Context context) { + super(context); + init(null); + } + + /** + * Constructs a new instance of CompatTextView + * @param context the view context + * @param attrs the set of attributes for the view + */ + public CompatTextView(Context context, AttributeSet attrs) { + super(context, attrs); + init(attrs); + } + + /** + * Constructs a new instance of CompatTextView + * @param context + * @param attrs + * @param defStyleAttr + */ + public CompatTextView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(attrs); + } + + /** + * initializes the view + * @param attrs the attribute set of the view, which can be null + */ + private void init(@Nullable AttributeSet attrs) { + if (attrs != null) { + Context context = getContext(); + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompatTextView); + + // Obtain DrawableManager used to pull Drawables safely, and check if we're in RTL + AppCompatDrawableManager dm = AppCompatDrawableManager.get(); + boolean rtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL; + + // Grab the compat drawable padding from the XML + float drawablePadding = a.getDimension(R.styleable.CompatTextView_drawablePadding, 0); + + // Grab the compat drawable resources from the XML + int startDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableStart, 0); + int topDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableTop, 0); + int endDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableEnd, 0); + int bottomDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableBottom, 0); + + // Load the used drawables, fall back to whatever was set in an "android:" + Drawable[] currentDrawables = getCompoundDrawables(); + Drawable left = startDrawableRes != 0 + ? dm.getDrawable(context, startDrawableRes) : currentDrawables[0]; + Drawable right = endDrawableRes != 0 + ? dm.getDrawable(context, endDrawableRes) : currentDrawables[1]; + Drawable top = topDrawableRes != 0 + ? dm.getDrawable(context, topDrawableRes) : currentDrawables[2]; + Drawable bottom = bottomDrawableRes != 0 + ? dm.getDrawable(context, bottomDrawableRes) : currentDrawables[3]; + + // Account for RTL and apply the compound Drawables + Drawable start = rtl ? right : left; + Drawable end = rtl ? left : right; + setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom); + setCompoundDrawablePadding((int) UiUtils.convertDpToPixel(drawablePadding, getContext())); + + a.recycle(); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/ui/widget/HtmlTextView.java b/app/src/main/java/fr/free/nrw/commons/ui/widget/HtmlTextView.java index e7f5eaeeb..4e73776c1 100644 --- a/app/src/main/java/fr/free/nrw/commons/ui/widget/HtmlTextView.java +++ b/app/src/main/java/fr/free/nrw/commons/ui/widget/HtmlTextView.java @@ -1,42 +1,51 @@ -package fr.free.nrw.commons.ui.widget; - -import android.content.Context; -import android.os.Build; -import android.support.v7.widget.AppCompatTextView; -import android.text.Html; -import android.text.Spanned; -import android.text.method.LinkMovementMethod; -import android.util.AttributeSet; - -/** - * An {@link AppCompatTextView} which formats the text to HTML displayable text and makes any - * links clickable. - */ -public class HtmlTextView extends AppCompatTextView { - - public HtmlTextView(Context context, AttributeSet attrs) { - super(context, attrs); - - setMovementMethod(LinkMovementMethod.getInstance()); - setText(fromHtml(getText().toString())); - } - - public void setHtmlText(String newText) { - setText(fromHtml(newText)); - } - - /** - * Fix Html.fromHtml is deprecated problem - * - * @param source provided Html string - * @return returned Spanned of appropriate method according to version check - */ - private static Spanned fromHtml(String source) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY); - } else { - //noinspection deprecation - return Html.fromHtml(source); - } - } -} +package fr.free.nrw.commons.ui.widget; + +import android.content.Context; +import android.os.Build; +import android.support.v7.widget.AppCompatTextView; +import android.text.Html; +import android.text.Spanned; +import android.text.method.LinkMovementMethod; +import android.util.AttributeSet; + +/** + * An {@link AppCompatTextView} which formats the text to HTML displayable text and makes any + * links clickable. + */ +public class HtmlTextView extends AppCompatTextView { + + /** + * Constructs a new instance of HtmlTextView + * @param context the context of the view + * @param attrs the set of attributes for the view + */ + public HtmlTextView(Context context, AttributeSet attrs) { + super(context, attrs); + + setMovementMethod(LinkMovementMethod.getInstance()); + setText(fromHtml(getText().toString())); + } + + /** + * Sets the text to be displayed + * @param newText the text to be displayed + */ + public void setHtmlText(String newText) { + setText(fromHtml(newText)); + } + + /** + * Fix Html.fromHtml is deprecated problem + * + * @param source provided Html string + * @return returned Spanned of appropriate method according to version check + */ + private static Spanned fromHtml(String source) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY); + } else { + //noinspection deprecation + return Html.fromHtml(source); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/ui/widget/OverlayDialog.java b/app/src/main/java/fr/free/nrw/commons/ui/widget/OverlayDialog.java index 6b2913d6d..4dea43ffb 100644 --- a/app/src/main/java/fr/free/nrw/commons/ui/widget/OverlayDialog.java +++ b/app/src/main/java/fr/free/nrw/commons/ui/widget/OverlayDialog.java @@ -1,46 +1,61 @@ -package fr.free.nrw.commons.ui.widget; - -import android.app.Dialog; -import android.graphics.Color; -import android.graphics.drawable.ColorDrawable; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v4.app.DialogFragment; -import android.view.Gravity; -import android.view.View; -import android.view.Window; -import android.view.WindowManager; - -public abstract class OverlayDialog extends DialogFragment { - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light); - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - setDialogLayoutToFullScreen(); - super.onViewCreated(view, savedInstanceState); - } - - private void setDialogLayoutToFullScreen() { - Window window = getDialog().getWindow(); - WindowManager.LayoutParams wlp = window.getAttributes(); - window.requestFeature(Window.FEATURE_NO_TITLE); - wlp.gravity = Gravity.BOTTOM; - wlp.width = WindowManager.LayoutParams.MATCH_PARENT; - wlp.height = WindowManager.LayoutParams.MATCH_PARENT; - window.setAttributes(wlp); - } - - @NonNull - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - Dialog dialog = super.onCreateDialog(savedInstanceState); - Window window = dialog.getWindow(); - window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); - return dialog; - } +package fr.free.nrw.commons.ui.widget; + +import android.app.Dialog; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.DialogFragment; +import android.view.Gravity; +import android.view.View; +import android.view.Window; +import android.view.WindowManager; + +/** + * a formatted dialog fragment + */ +public abstract class OverlayDialog extends DialogFragment { + + /** + * creates a DialogFragment with the correct style and theme + * @param savedInstanceState + */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light); + } + + /** + * allows subclasses to initialize themselves if needed + * @param view the view being used + * @param savedInstanceState bundle re-constructed from a previous saved state + */ + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + setDialogLayoutToFullScreen(); + super.onViewCreated(view, savedInstanceState); + } + + /** + * sets the dialog layout to fullscreen + */ + private void setDialogLayoutToFullScreen() { + Window window = getDialog().getWindow(); + WindowManager.LayoutParams wlp = window.getAttributes(); + window.requestFeature(Window.FEATURE_NO_TITLE); + wlp.gravity = Gravity.BOTTOM; + wlp.width = WindowManager.LayoutParams.MATCH_PARENT; + wlp.height = WindowManager.LayoutParams.MATCH_PARENT; + window.setAttributes(wlp); + } + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Dialog dialog = super.onCreateDialog(savedInstanceState); + Window window = dialog.getWindow(); + window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + return dialog; + } } \ No newline at end of file