From c4bceb9a5e4f01433514cfcdc0818105d07de3ea Mon Sep 17 00:00:00 2001 From: Lukas Prokop Date: Mon, 19 Jan 2015 10:59:44 +0100 Subject: [PATCH 1/6] Code refactoring made by Avast Introduced new class DialogParams which holds most of dialogs parameters such as title, message, ... and styling parameters. --- gradlew | 0 .../lib/dialogs/BaseDialogBuilder.java | 354 ++++- .../lib/dialogs/BaseDialogFragment.java | 1176 ++++++++++------- .../lib/dialogs/DatePickerDialogFragment.java | 405 +++--- .../dialogs/ISimpleDialogCancelListener.java | 10 +- .../lib/dialogs/ISimpleDialogListener.java | 18 +- .../lib/dialogs/ListDialogFragment.java | 319 ++--- .../lib/dialogs/ProgressDialogFragment.java | 60 +- .../lib/dialogs/SimpleDialogFragment.java | 148 +-- 9 files changed, 1436 insertions(+), 1054 deletions(-) mode change 100644 => 100755 gradlew diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/BaseDialogBuilder.java b/library/src/main/java/eu/inmite/android/lib/dialogs/BaseDialogBuilder.java index 38f0c9c..0ae76d4 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/BaseDialogBuilder.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/BaseDialogBuilder.java @@ -1,104 +1,366 @@ package eu.inmite.android.lib.dialogs; import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; +import android.text.Html; +import android.text.SpannedString; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ListAdapter; /** * Internal base builder that holds common values for all dialog fragment builders. * - * @author Tomas Vondracek + * @author Tomas Vondracek (inmite.eu), Michal Reiter (avast.com) */ -abstract class BaseDialogBuilder> { +public abstract class BaseDialogBuilder> { - public final static String ARG_REQUEST_CODE = "request_code"; - public final static String ARG_CANCELABLE_ON_TOUCH_OUTSIDE = "cancelable_oto"; - public final static String DEFAULT_TAG = "simple_dialog"; - public final static int DEFAULT_REQUEST_CODE = -42; - - protected final Context mContext; protected final FragmentManager mFragmentManager; protected final Class mClass; + protected final DialogParams mDialogParams; private Fragment mTargetFragment; - private boolean mCancelable = true; - private boolean mCancelableOnTouchOutside = true; - - private String mTag = DEFAULT_TAG; - private int mRequestCode = DEFAULT_REQUEST_CODE; - public BaseDialogBuilder(Context context, FragmentManager fragmentManager, Class clazz) { + /** + * Public constructor. + * + * @param context Context + * @param fragmentManager {@link android.support.v4.app.FragmentManager} + * @param clazz calling class + */ + public BaseDialogBuilder(Context context, FragmentManager fragmentManager, + Class clazz) { mFragmentManager = fragmentManager; - mContext = context.getApplicationContext(); mClass = clazz; + mDialogParams = new DialogParams(context); } protected abstract T self(); protected abstract Bundle prepareArguments(); + /** + * Sets whether dialog can be cancelable. + * + * @param cancelable true/false (default is true) + * @return builder + */ public T setCancelable(boolean cancelable) { - mCancelable = cancelable; + mDialogParams.cancelable = cancelable; return self(); } + /** + * Sets whether dialog can be canceled on touch outside. + * + * @param cancelable true/false (default is true) + * @return builder + */ public T setCancelableOnTouchOutside(boolean cancelable) { - mCancelableOnTouchOutside = cancelable; + mDialogParams.cancelableOnTouchOutside = cancelable; if (cancelable) { - mCancelable = cancelable; + mDialogParams.cancelable = cancelable; } return self(); } + /** + * Sets target fragment. + * + * @param fragment target fragment + * @param requestCode code to identify dialog + * @return builder + */ public T setTargetFragment(Fragment fragment, int requestCode) { mTargetFragment = fragment; - mRequestCode = requestCode; + mDialogParams.requestCode = requestCode; return self(); } + /** + * Sets request code. + * + * @param requestCode code to identify dialog + * @return builder + */ public T setRequestCode(int requestCode) { - mRequestCode = requestCode; + mDialogParams.requestCode = requestCode; return self(); } + /** + * Sets tag for {@link android.support.v4.app.FragmentTransaction}. + * + * @param tag fragment tag + * @return builder + */ public T setTag(String tag) { - mTag = tag; + mDialogParams.tag = tag; return self(); } - private BaseDialogFragment create() { - final Bundle args = prepareArguments(); + /** + * Sets message. + * + * @param messageResourceId dialog message + * @return builder + */ + public T setMessage(int messageResourceId) { + mDialogParams.message = mDialogParams.context.getString(messageResourceId); + return self(); + } - final BaseDialogFragment fragment = (BaseDialogFragment) Fragment.instantiate(mContext, mClass.getName(), args); + /** + * Sets message. + *

+ * Allow to set resource string with HTML formatting and bind %s,%i. This is workaround for + * https://code.google.com/p/android/issues/detail?id=2923 + * + * @param resourceId dialog message with binders %s, %i + * @param formatArgs formatting params for previous string + * @return builder + */ + public T setMessage(int resourceId, Object... formatArgs) { + mDialogParams.message = Html.fromHtml(String.format(Html.toHtml(new SpannedString( + mDialogParams.context.getString(resourceId))), formatArgs)); + return self(); + } - args.putBoolean(ARG_CANCELABLE_ON_TOUCH_OUTSIDE, mCancelableOnTouchOutside); + /** + * Sets message. + * + * @param message dialog message + * @return builder + */ + public T setMessage(CharSequence message) { + mDialogParams.message = message; + return self(); + } - if (mTargetFragment != null) { - fragment.setTargetFragment(mTargetFragment, mRequestCode); - } else { - args.putInt(ARG_REQUEST_CODE, mRequestCode); - } - fragment.setCancelable(mCancelable); - return fragment; - } + /** + * Sets message. + * + * @param message dialog message + * @return builder + */ + public T setMessage(String message) { + mDialogParams.message = message; + return self(); + } + + /** + * Sets positive button text. + * + * @param textResourceId dialog label + * @return builder + */ + public T setPositiveButtonText(int textResourceId) { + mDialogParams.positiveButtonText = mDialogParams.context.getString(textResourceId); + return self(); + } + + /** + * Sets positive button text. + * + * @param text button label + * @return builder + */ + public T setPositiveButtonText(String text) { + mDialogParams.positiveButtonText = text; + return self(); + } + + /** + * Sets negative button text. + * + * @param textResourceId button label + * @return builder + */ + public T setNegativeButtonText(int textResourceId) { + mDialogParams.negativeButtonText = mDialogParams.context.getString(textResourceId); + return self(); + } + + /** + * Sets negative button text. + * + * @param text button label + * @return builder + */ + public T setNegativeButtonText(String text) { + mDialogParams.negativeButtonText = text; + return self(); + } + + /** + * Sets neutral button text. + * + * @param textResourceId button label + * @return builder + */ + public T setNeutralButtonText(int textResourceId) { + mDialogParams.neutralButtonText = mDialogParams.context.getString(textResourceId); + return self(); + } + + /** + * Sets neutral button text. + * + * @param text button label + * @return builder + */ + public T setNeutralButtonText(String text) { + mDialogParams.neutralButtonText = text; + return self(); + } + + /** + * Sets title. + * + * @param titleResourceId dialog label + * @return builder + */ + public T setTitle(int titleResourceId) { + mDialogParams.title = mDialogParams.context.getString(titleResourceId); + return self(); + } + + /** + * Sets title. + * + * @param title dialog label + * @return builder + */ + public T setTitle(String title) { + mDialogParams.title = title; + return self(); + } + + /** + * Sets title. + * + * @param title dialog label + * @return builder + */ + public T setTitle(CharSequence title) { + mDialogParams.title = title.toString(); + return self(); + } + + /** + * When there is neither positive nor negative button, default "close" button is created if it was + * enabled.
Default is true. + */ + public T hideDefaultButton(boolean hide) { + mDialogParams.showDefaultButton = !hide; + return self(); + } + + private BaseDialogFragment create() { + final Bundle args = prepareArguments(); + + final BaseDialogFragment fragment = (BaseDialogFragment)Fragment.instantiate(mDialogParams.context, mClass.getName + (), args); + + args.putBoolean(BaseDialogFragment.ARG_CANCELABLE_ON_TOUCH_OUTSIDE, mDialogParams.cancelableOnTouchOutside); + + if (mTargetFragment != null) { + fragment.setTargetFragment(mTargetFragment, mDialogParams.requestCode); + } else { + args.putInt(BaseDialogFragment.ARG_REQUEST_CODE, mDialogParams.requestCode); + } + fragment.setCancelable(mDialogParams.cancelable); + return fragment; + } public DialogFragment show() { - BaseDialogFragment fragment = create(); - fragment.show(mFragmentManager, mTag); + BaseDialogFragment fragment = create(); + fragment.show(mFragmentManager, mDialogParams.tag); + return fragment; + } + + /** + * Like show() but allows the commit to be executed after an activity's state is saved. This + * is dangerous because the commit can be lost if the activity needs to later be restored from + * its state, so this should only be used for cases where it is okay for the UI state to change + * unexpectedly on the user. + */ + public DialogFragment showAllowingStateLoss() { + BaseDialogFragment fragment = create(); + fragment.showAllowingStateLoss(mFragmentManager, mDialogParams.tag); return fragment; } - /** - * Like show() but allows the commit to be executed after an activity's state is saved. This - * is dangerous because the commit can be lost if the activity needs to later be restored from - * its state, so this should only be used for cases where it is okay for the UI state to change - * unexpectedly on the user. - */ - public DialogFragment showAllowingStateLoss() { - BaseDialogFragment fragment = create(); - fragment.showAllowingStateLoss(mFragmentManager, mTag); - return fragment; - } + /** + * Holds params for dialog similarly as in {@link AlertController.AlertParams} + */ + public static class DialogParams { + public final Context context; + + public CharSequence title = null; + public CharSequence message; + public CharSequence positiveButtonText; + public CharSequence negativeButtonText; + public CharSequence neutralButtonText; + public String[] stringItems; + public String[] multiChoiceItems; + public boolean[] multiChoiceSelectedItems; + public String tag = BaseDialogFragment.DEFAULT_TAG; + + public View.OnClickListener positiveButtonListener; + public View.OnClickListener negativeButtonListener; + public View.OnClickListener neutralButtonListener; + + public DialogFragment dialogFragment; + public ViewGroup container; + public LayoutInflater layoutInflater; + public View view; + + public boolean cancelable = true; + public boolean cancelableOnTouchOutside = true; + public boolean viewSpacingSpecified; + public boolean showDefaultButton = true; + + public int viewSpacingLeft; + public int viewSpacingTop; + public int requestCode = BaseDialogFragment.DEFAULT_REQUEST_CODE; + public int viewSpacingRight; + public int viewSpacingBottom; + public ListAdapter listAdapter; + public int listCheckedItemIdx; + public AdapterView.OnItemClickListener onItemClickListener; + public Drawable icon; + + public ColorStateList buttonTextColor; + + public int titleTextColor; + public int titleSeparatorColor; + public int messageTextColor; + + public int buttonSeparatorColor; + public int buttonBackgroundColorNormal; + public int buttonBackgroundColorPressed; + public int buttonBackgroundColorFocused; + + public int listItemSeparatorColor; + public int listItemBackgroundColorNormal; + public int listItemBackgroundColorPressed; + public int listItemBackgroundColorFocused; + + private DialogParams() { + context = null; + } + + public DialogParams(Context context) { + this.context = context; + } + + } } diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/BaseDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/BaseDialogFragment.java index 06012dc..b0d1c32 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/BaseDialogFragment.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/BaseDialogFragment.java @@ -29,6 +29,7 @@ import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -41,516 +42,803 @@ import android.widget.TextView; /** - * Base dialog fragment for all your dialogs, stylable and same design on Android 2.2+. + * Base dialog fragment for all your dialogs, styleable and same design on Android 2.2+. * * @author David Vávra (david@inmite.eu) */ public abstract class BaseDialogFragment extends DialogFragment { - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - Dialog dialog = new Dialog(getActivity(), R.style.SDL_Dialog); - // custom dialog background - final TypedArray a = getActivity().getTheme() - .obtainStyledAttributes(null, R.styleable.DialogStyle, R.attr.sdlDialogStyle, 0); - Drawable dialogBackground = a.getDrawable(R.styleable.DialogStyle_dialogBackground); - a.recycle(); - dialog.getWindow().setBackgroundDrawable(dialogBackground); - Bundle args = getArguments(); - if (args != null) { - dialog.setCanceledOnTouchOutside( - args.getBoolean(BaseDialogBuilder.ARG_CANCELABLE_ON_TOUCH_OUTSIDE)); - } - return dialog; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - Builder builder = new Builder(this, getActivity(), inflater, container); - return build(builder).create(); - } - - protected abstract Builder build(Builder initialBuilder); - - @Override - public void onDestroyView() { - // bug in the compatibility library - if (getDialog() != null && getRetainInstance()) { - getDialog().setDismissMessage(null); - } - super.onDestroyView(); - } - - public void showAllowingStateLoss(FragmentManager manager, String tag) { - FragmentTransaction ft = manager.beginTransaction(); - ft.add(this, tag); - ft.commitAllowingStateLoss(); - } - - /** - * @return the positive button if specified and the view is created, null otherwise - */ - protected Button getPositiveButton() { - if (getView() != null) { - return (Button) getView().findViewById(R.id.sdl__positive_button); - } else { - return null; - } - } - - /** - * @return the negative button if specified and the view is created, null otherwise - */ - protected Button getNegativeButton() { - if (getView() != null) { - return (Button) getView().findViewById(R.id.sdl__negative_button); - } else { - return null; - } - } - - /** - * @return the neutral button if specified and the view is created, null otherwise - */ - protected Button getNeutralButton() { - if (getView() != null) { - return (Button) getView().findViewById(R.id.sdl__neutral_button); - } else { - return null; - } - } - - /** - * Custom dialog builder - */ - protected static class Builder { - - private final DialogFragment mDialogFragment; - - private final Context mContext; - - private final ViewGroup mContainer; - - private final LayoutInflater mInflater; - - private CharSequence mTitle = null; - - private CharSequence mPositiveButtonText; - - private View.OnClickListener mPositiveButtonListener; - - private CharSequence mNegativeButtonText; - - private View.OnClickListener mNegativeButtonListener; - - private CharSequence mNeutralButtonText; - - private View.OnClickListener mNeutralButtonListener; - - private CharSequence mMessage; - - private View mView; - - private boolean mViewSpacingSpecified; - - private int mViewSpacingLeft; - - private int mViewSpacingTop; - - private int mViewSpacingRight; - - private int mViewSpacingBottom; - - private ListAdapter mListAdapter; - - private int mListCheckedItemIdx; - - private AdapterView.OnItemClickListener mOnItemClickListener; - - private Drawable mIcon; - /** - * Styling: * + * Bundle argument tags to identify dialog window. */ - private int mTitleTextColor; - - private int mTitleSeparatorColor; - - private int mMessageTextColor; - - private ColorStateList mButtonTextColor; - - private int mButtonSeparatorColor; + public final static String ARG_REQUEST_CODE = "request_code"; + public final static int DEFAULT_REQUEST_CODE = -42; - private int mButtonBackgroundColorNormal; - - private int mButtonBackgroundColorPressed; - - private int mButtonBackgroundColorFocused; + /** + * Bundle argument tags for buttons, dialog message and title. + */ + public final static String ARG_MESSAGE = "message"; + public final static String ARG_TITLE = "title"; + public final static String ARG_POSITIVE_BUTTON = "positive_button"; + public final static String ARG_NEGATIVE_BUTTON = "negative_button"; + public final static String ARG_NEUTRAL_BUTTON = "neutral_button"; - private int mListItemSeparatorColor; + /** + * Bundle argument tag for cancellation param. + */ + public final static String ARG_CANCELABLE_ON_TOUCH_OUTSIDE = "cancelable_oto"; + public final static String ARG_CANCELABLE = "cancelable"; - private int mListItemBackgroundColorNormal; + /** + * Bundle argument for list items. + */ + public final static String ARG_ITEMS = "items"; - private int mListItemBackgroundColorPressed; + /** + * Bundle arguments for multi choice list items. + */ + public final static String ARG_ITEMS_MULTICHOICE = "items_multichoice"; + public final static String ARG_ITEMS_SELECTED_MULTICHOICE = "items_selected_multichoice"; - private int mListItemBackgroundColorFocused; + /** + * Default fragment tag. + */ + public final static String DEFAULT_TAG = "simple_dialog"; + /** + * States for list items + */ private final static int[] pressedState = {android.R.attr.state_pressed}; - private final static int[] focusedState = {android.R.attr.state_focused}; - private final static int[] defaultState = {android.R.attr.state_enabled}; + /** + * Identification of caller + */ + protected int mRequestCode; - public Builder(DialogFragment dialogFragment, Context context, LayoutInflater inflater, - ViewGroup container) { - this.mDialogFragment = dialogFragment; - this.mContext = context; - this.mContainer = container; - this.mInflater = inflater; + /** + * Children should extend this to add more things to base builder. + *

+ * Typically {@code BaseDialogBuilder.setTitle} and {@code BaseDialogBuilder.setMessage} and + * {@code BaseDialogBuilder.setPositiveButton} are called here. + */ + protected abstract Builder build(Builder initialBuilder); + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Dialog dialog = new Dialog(getActivity(), R.style.SDL_Dialog); + // custom dialog background + final TypedArray a = getActivity().getTheme().obtainStyledAttributes(null, + R.styleable.DialogStyle, R.attr.sdlDialogStyle, 0); + Drawable dialogBackground = a.getDrawable(R.styleable.DialogStyle_dialogBackground); + a.recycle(); + dialog.getWindow().setBackgroundDrawable(dialogBackground); + Bundle args = getArguments(); + if (args != null) { + boolean cancelable = args.getBoolean(ARG_CANCELABLE); + dialog.setCancelable(cancelable); + dialog.setCanceledOnTouchOutside(args.getBoolean(ARG_CANCELABLE_ON_TOUCH_OUTSIDE)); + } + return dialog; } - public LayoutInflater getLayoutInflater() { - return mInflater; + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + Builder builder = new Builder(this, getActivity(), inflater, container); + return build(builder).create(); } - public Builder setTitle(int titleId) { - this.mTitle = mContext.getText(titleId); - return this; + @Override + public void onDestroyView() { + // bug in the compatibility library + if (getDialog() != null && getRetainInstance()) { + getDialog().setDismissMessage(null); + } + super.onDestroyView(); } - public Builder setTitle(CharSequence title) { - this.mTitle = title; - return this; + public void showAllowingStateLoss(FragmentManager manager, String tag) { + FragmentTransaction ft = manager.beginTransaction(); + ft.add(this, tag); + ft.commitAllowingStateLoss(); } - public Builder setPositiveButton(int textId, final View.OnClickListener listener) { - mPositiveButtonText = mContext.getText(textId); - mPositiveButtonListener = listener; - return this; + /** + * @return the positive button if specified and the view is created, null otherwise + */ + protected Button getPositiveButton() { + if (getView() != null) { + return (Button)getView().findViewById(R.id.sdl__positive_button); + } else { + return null; + } } - public Builder setPositiveButton(CharSequence text, final View.OnClickListener listener) { - mPositiveButtonText = text; - mPositiveButtonListener = listener; - return this; + /** + * @return the negative button if specified and the view is created, null otherwise + */ + protected Button getNegativeButton() { + if (getView() != null) { + return (Button)getView().findViewById(R.id.sdl__negative_button); + } else { + return null; + } } - public Builder setNegativeButton(int textId, final View.OnClickListener listener) { - mNegativeButtonText = mContext.getText(textId); - mNegativeButtonListener = listener; - return this; + /** + * @return the neutral button if specified and the view is created, null otherwise + */ + protected Button getNeutralButton() { + if (getView() != null) { + return (Button)getView().findViewById(R.id.sdl__neutral_button); + } else { + return null; + } } - public Builder setNegativeButton(CharSequence text, final View.OnClickListener listener) { - mNegativeButtonText = text; - mNegativeButtonListener = listener; - return this; + /** + * Returns the dialog message. + * + * @return message + */ + protected CharSequence getMessage() { + return getArguments().getCharSequence(ARG_MESSAGE); } - public Builder setNeutralButton(int textId, final View.OnClickListener listener) { - mNeutralButtonText = mContext.getText(textId); - mNeutralButtonListener = listener; - return this; + /** + * Returns the title. + * + * @return title + */ + protected String getTitle() { + return getArguments().getString(ARG_TITLE); } - public Builder setNeutralButton(CharSequence text, final View.OnClickListener listener) { - mNeutralButtonText = text; - mNeutralButtonListener = listener; - return this; + /** + * Returns positive button text. + * + * @return text for the button. + */ + protected String getPositiveButtonText() { + return getArguments().getString(ARG_POSITIVE_BUTTON); } - public Builder setMessage(int messageId) { - mMessage = mContext.getText(messageId); - return this; + /** + * Returns negative button text. + * + * @return text for the button. + */ + protected String getNegativeButtonText() { + return getArguments().getString(ARG_NEGATIVE_BUTTON); } - public Builder setMessage(CharSequence message) { - mMessage = message; - return this; + /** + * Returns neutral button text. + * + * @return text for the button. + */ + protected String getNeutralButtonText() { + return getArguments().getString(ARG_NEUTRAL_BUTTON); } /** - * Set list + * Gets arguments for list items. * - * @param checkedItemIdx Item check by default, -1 if no item should be checked + * @return StringArray */ - public Builder setItems(ListAdapter listAdapter, int checkedItemIdx, - final AdapterView.OnItemClickListener listener) { - mListAdapter = listAdapter; - mOnItemClickListener = listener; - mListCheckedItemIdx = checkedItemIdx; - return this; + protected String[] getItems() { + return getArguments().getStringArray(ARG_ITEMS); } - public Builder setView(View view) { - mView = view; - mViewSpacingSpecified = false; - return this; + /** + * Gets text array of multi choice list. + * + * @return array containing texts from multi choice list. + */ + protected String[] getMultiChoiceItems() { + return getArguments().getStringArray(ARG_ITEMS_MULTICHOICE); } - public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop, - int viewSpacingRight, int viewSpacingBottom) { - mView = view; - mViewSpacingSpecified = true; - mViewSpacingLeft = viewSpacingLeft; - mViewSpacingTop = viewSpacingTop; - mViewSpacingRight = viewSpacingRight; - mViewSpacingBottom = viewSpacingBottom; - return this; + /** + * Gets array with checked state of multi choice list. + * + * @return boolean array with checked states. + */ + protected boolean[] getMultiChoiceSelectedItems() { + return getArguments().getBooleanArray(ARG_ITEMS_SELECTED_MULTICHOICE); } - public Builder setIcon(int resourceId) { - mIcon = mContext.getResources().getDrawable(resourceId); - return this; - } - public Builder setIcon(Drawable drawable) { - mIcon = drawable; - return this; - } + /** + * Custom dialog builder + */ + protected static class Builder { + protected BaseDialogBuilder.DialogParams mDialogParams; + + + /** + * Public constructor. + * + * @param dialogFragment calling fragment + * @param context context + * @param inflater inflater + * @param container view group container + */ + public Builder(DialogFragment dialogFragment, Context context, LayoutInflater inflater, + ViewGroup container) { + mDialogParams = new BaseDialogBuilder.DialogParams(context); + mDialogParams.dialogFragment = dialogFragment; + mDialogParams.container = container; + mDialogParams.layoutInflater = inflater; + } - public View create() { - final Resources res = mContext.getResources(); - final int defaultTitleTextColor = res.getColor(R.color.sdl_title_text_dark); - final int defaultTitleSeparatorColor = res.getColor(R.color.sdl_title_separator_dark); - final int defaultMessageTextColor = res.getColor(R.color.sdl_message_text_dark); - final ColorStateList defaultButtonTextColor = res - .getColorStateList(R.color.sdl_button_text_dark); - final int defaultButtonSeparatorColor = res.getColor(R.color.sdl_button_separator_dark); - final int defaultButtonBackgroundColorNormal = res - .getColor(R.color.sdl_button_normal_dark); - final int defaultButtonBackgroundColorPressed = res - .getColor(R.color.sdl_button_pressed_dark); - final int defaultButtonBackgroundColorFocused = res - .getColor(R.color.sdl_button_focused_dark); - - final TypedArray a = mContext.getTheme() - .obtainStyledAttributes(null, R.styleable.DialogStyle, R.attr.sdlDialogStyle, - 0); - mTitleTextColor = a - .getColor(R.styleable.DialogStyle_titleTextColor, defaultTitleTextColor); - mTitleSeparatorColor = a.getColor(R.styleable.DialogStyle_titleSeparatorColor, - defaultTitleSeparatorColor); - mMessageTextColor = a - .getColor(R.styleable.DialogStyle_messageTextColor, defaultMessageTextColor); - mButtonTextColor = a.getColorStateList(R.styleable.DialogStyle_buttonTextColor); - if (mButtonTextColor == null) { - mButtonTextColor = defaultButtonTextColor; - } - mButtonSeparatorColor = a.getColor(R.styleable.DialogStyle_buttonSeparatorColor, - defaultButtonSeparatorColor); - mButtonBackgroundColorNormal = a - .getColor(R.styleable.DialogStyle_buttonBackgroundColorNormal, - defaultButtonBackgroundColorNormal); - mButtonBackgroundColorPressed = a - .getColor(R.styleable.DialogStyle_buttonBackgroundColorPressed, - defaultButtonBackgroundColorPressed); - mButtonBackgroundColorFocused = a - .getColor(R.styleable.DialogStyle_buttonBackgroundColorFocused, - defaultButtonBackgroundColorFocused); - if (mListAdapter != null) { - final int defaultListItemSeparatorColor = res - .getColor(R.color.sdl_list_item_separator_dark); - final int defaultListItemBackgroundColorNormal = res - .getColor(R.color.sdl_button_normal_dark); - final int defaultListItemBackgroundColorFocused = res - .getColor(R.color.sdl_button_focused_dark); - final int defaultListItemBackgroundColorPressed = res - .getColor(R.color.sdl_button_pressed_dark); - mListItemSeparatorColor = a.getColor(R.styleable.DialogStyle_listItemSeparatorColor, - defaultListItemSeparatorColor); - mListItemBackgroundColorNormal = a - .getColor(R.styleable.DialogStyle_listItemColorNormal, - defaultListItemBackgroundColorNormal); - mListItemBackgroundColorFocused = a - .getColor(R.styleable.DialogStyle_listItemColorFocused, - defaultListItemBackgroundColorFocused); - mListItemBackgroundColorPressed = a - .getColor(R.styleable.DialogStyle_listItemColorPressed, - defaultListItemBackgroundColorPressed); - } - a.recycle(); - - View v = getDialogLayoutAndInitTitle(); - - LinearLayout content = (LinearLayout) v.findViewById(R.id.sdl__content); - - if (mMessage != null) { - View viewMessage = mInflater.inflate(R.layout.dialog_part_message, content, false); - TextView tvMessage = (TextView) viewMessage.findViewById(R.id.sdl__message); - tvMessage.setTextColor(mMessageTextColor); - tvMessage.setText(mMessage); - content.addView(viewMessage); - } - - if (mView != null) { - FrameLayout customPanel = (FrameLayout) mInflater - .inflate(R.layout.dialog_part_custom, content, false); - FrameLayout custom = (FrameLayout) customPanel.findViewById(R.id.sdl__custom); - custom.addView(mView, - new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.MATCH_PARENT) - ); - if (mViewSpacingSpecified) { - custom.setPadding(mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight, - mViewSpacingBottom); - } - content.addView(customPanel); - } - - if (mListAdapter != null) { - ListView listView = (ListView) mInflater - .inflate(R.layout.dialog_part_list, content, false); - listView.setAdapter(mListAdapter); - listView.setDivider(getColoredListItemsDivider()); - listView.setDividerHeight(1); - listView.setSelector(getListItemSelector()); - listView.setOnItemClickListener(mOnItemClickListener); - if (mListCheckedItemIdx != -1) { - listView.setSelection(mListCheckedItemIdx); - } - content.addView(listView); - } - - addButtons(content); - - return v; - } + /** + * Gets LayoutInflater. + * + * @return LayoutInflater + */ + public LayoutInflater getLayoutInflater() { + return mDialogParams.layoutInflater; + } - private View getDialogLayoutAndInitTitle() { - View v = mInflater.inflate(R.layout.dialog_part_title, mContainer, false); - TextView tvTitle = (TextView) v.findViewById(R.id.sdl__title); - View viewTitleDivider = v.findViewById(R.id.sdl__titleDivider); - if (mTitle != null) { - tvTitle.setText(mTitle); - tvTitle.setTextColor(mTitleTextColor); - if (mIcon != null) { - tvTitle.setCompoundDrawablesWithIntrinsicBounds(mIcon, null, null, null); - tvTitle.setCompoundDrawablePadding( - mContext.getResources().getDimensionPixelSize(R.dimen.grid_2)); - } - viewTitleDivider.setBackgroundDrawable(new ColorDrawable(mTitleSeparatorColor)); - } else { - tvTitle.setVisibility(View.GONE); - viewTitleDivider.setVisibility(View.GONE); - } - return v; - } + /** + * Sets the dialog title. + * + * @param titleId resource id + * @return builder + */ + public Builder setTitle(int titleId) { + mDialogParams.title = mDialogParams.context.getString(titleId); + return this; + } - private void addButtons(LinearLayout llListDialog) { - if (mNegativeButtonText != null || mNeutralButtonText != null - || mPositiveButtonText != null) { - View viewButtonPanel = mInflater - .inflate(R.layout.dialog_part_button_panel, llListDialog, false); - LinearLayout llButtonPanel = (LinearLayout) viewButtonPanel - .findViewById(R.id.dialog_button_panel); - viewButtonPanel.findViewById(R.id.dialog_horizontal_separator) - .setBackgroundDrawable(new ColorDrawable(mButtonSeparatorColor)); + /** + * Sets the dialog title. + * + * @param title dialog title + * @return builder + */ + public Builder setTitle(CharSequence title) { + mDialogParams.title = title; + return this; + } - boolean addDivider = false; + /** + * Sets dialog cancelable on touch outside. + * + * @param cancelableOnTouchOutside true/false (default is true) + * @return builder + */ + public Builder setCancelableOnTouchOutside(boolean cancelableOnTouchOutside) { + mDialogParams.cancelableOnTouchOutside = cancelableOnTouchOutside; + return this; + } - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - addDivider = addPositiveButton(llButtonPanel, addDivider); - } else { - addDivider = addNegativeButton(llButtonPanel, addDivider); + /** + * Sets whether dialog can be cancelable. + * + * @param cancelable true/false (default is true) + * @return builder + */ + public Builder setCancelable(boolean cancelable) { + mDialogParams.cancelable = cancelable; + return this; } - addDivider = addNeutralButton(llButtonPanel, addDivider); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - addNegativeButton(llButtonPanel, addDivider); - } else { - addPositiveButton(llButtonPanel, addDivider); + /** + * Sets the positive button. + * + * @param textId button label + * @param listener button click listener + * @return builder + */ + public Builder setPositiveButton(int textId, final View.OnClickListener listener) { + mDialogParams.positiveButtonText = mDialogParams.context.getString(textId); + mDialogParams.positiveButtonListener = listener; + return this; } - llListDialog.addView(viewButtonPanel); - } - } + /** + * Sets the positive button. + * + * @param text button label + * @param listener button click listener + * @return builder + */ + public Builder setPositiveButton(CharSequence text, final View.OnClickListener listener) { + mDialogParams.positiveButtonText = text; + mDialogParams.positiveButtonListener = listener; + return this; + } - private boolean addNegativeButton(ViewGroup parent, boolean addDivider) { - if (mNegativeButtonText != null) { - if (addDivider) { - addDivider(parent); - } - Button btn = (Button) mInflater.inflate(R.layout.dialog_part_button, parent, false); - btn.setId(R.id.sdl__negative_button); - btn.setText(mNegativeButtonText); - btn.setTextColor(mButtonTextColor); - btn.setBackgroundDrawable(getButtonBackground()); - btn.setOnClickListener(mNegativeButtonListener); - parent.addView(btn); - return true; - } - return addDivider; - } + /** + * Sets the negative button. + * + * @param textId button label + * @param listener button click listener + * @return builder + */ + public Builder setNegativeButton(int textId, final View.OnClickListener listener) { + mDialogParams.negativeButtonText = mDialogParams.context.getString(textId); + mDialogParams.negativeButtonListener = listener; + return this; + } - private boolean addPositiveButton(ViewGroup parent, boolean addDivider) { - if (mPositiveButtonText != null) { - if (addDivider) { - addDivider(parent); - } - Button btn = (Button) mInflater.inflate(R.layout.dialog_part_button, parent, false); - btn.setId(R.id.sdl__positive_button); - btn.setText(mPositiveButtonText); - btn.setTextColor(mButtonTextColor); - btn.setBackgroundDrawable(getButtonBackground()); - btn.setOnClickListener(mPositiveButtonListener); - parent.addView(btn); - return true; - } - return addDivider; - } + /** + * Sets the negative button. + * + * @param text button label + * @param listener button click listener + * @return builder + */ + public Builder setNegativeButton(CharSequence text, final View.OnClickListener listener) { + mDialogParams.negativeButtonText = text; + mDialogParams.negativeButtonListener = listener; + return this; + } - private boolean addNeutralButton(ViewGroup parent, boolean addDivider) { - if (mNeutralButtonText != null) { - if (addDivider) { - addDivider(parent); - } - Button btn = (Button) mInflater.inflate(R.layout.dialog_part_button, parent, false); - btn.setId(R.id.sdl__neutral_button); - btn.setText(mNeutralButtonText); - btn.setTextColor(mButtonTextColor); - btn.setBackgroundDrawable(getButtonBackground()); - btn.setOnClickListener(mNeutralButtonListener); - parent.addView(btn); - return true; - } - return addDivider; - } + /** + * Sets neutral button. + * + * @param textId button label + * @param listener button click listener + * @return builder + */ + public Builder setNeutralButton(int textId, final View.OnClickListener listener) { + mDialogParams.neutralButtonText = mDialogParams.context.getString(textId); + mDialogParams.neutralButtonListener = listener; + return this; + } - private void addDivider(ViewGroup parent) { - View view = mInflater.inflate(R.layout.dialog_part_button_separator, parent, false); - view.findViewById(R.id.dialog_button_separator) - .setBackgroundDrawable(new ColorDrawable(mButtonSeparatorColor)); - parent.addView(view); - } + /** + * Sets neutral button. + * + * @param text button label + * @param listener button click listener + * @return builder + */ + public Builder setNeutralButton(CharSequence text, final View.OnClickListener listener) { + mDialogParams.neutralButtonText = text; + mDialogParams.neutralButtonListener = listener; + return this; + } - private StateListDrawable getButtonBackground() { - ColorDrawable colorDefault = new ColorDrawable(mButtonBackgroundColorNormal); - ColorDrawable colorPressed = new ColorDrawable(mButtonBackgroundColorPressed); - ColorDrawable colorFocused = new ColorDrawable(mButtonBackgroundColorFocused); - StateListDrawable background = new StateListDrawable(); - background.addState(pressedState, colorPressed); - background.addState(focusedState, colorFocused); - background.addState(defaultState, colorDefault); - return background; - } + /** + * Sets the message for dialog. + * + * @param messageId main text in dialog + * @return builder + */ + public Builder setMessage(int messageId) { + mDialogParams.message = mDialogParams.context.getString(messageId); + return this; + } - private StateListDrawable getListItemSelector() { - ColorDrawable colorDefault = new ColorDrawable(mListItemBackgroundColorNormal); - ColorDrawable colorPressed = new ColorDrawable(mListItemBackgroundColorPressed); - ColorDrawable colorFocused = new ColorDrawable(mListItemBackgroundColorFocused); - StateListDrawable background = new StateListDrawable(); - background.addState(pressedState, colorPressed); - background.addState(focusedState, colorFocused); - background.addState(defaultState, colorDefault); - return background; - } + /** + * Sets the message for dialog. + * + * @param message main text in dialog + * @return builder + */ + public Builder setMessage(CharSequence message) { + mDialogParams.message = message; + return this; + } + + /** + * Sets list. + * + * @param listAdapter list adapter + * @param checkedItemIdx Item check by default, -1 if no item should be checked + * @param listener item click listener + * @return builder + */ + public Builder setItems(ListAdapter listAdapter, int checkedItemIdx, + final AdapterView.OnItemClickListener listener) { + mDialogParams.listAdapter = listAdapter; + mDialogParams.onItemClickListener = listener; + mDialogParams.listCheckedItemIdx = checkedItemIdx; + return this; + } + + /** + * Sets the list for multi choice. + * + * @param listAdapter The list adapter. + * @return builder + */ + public Builder setMultiChoiceItems(ListAdapter listAdapter) { + mDialogParams.listAdapter = listAdapter; + return this; + } + + /** + * Sets view for dialog. + * + * @param view main view + * @return builder + */ + public Builder setView(View view) { + mDialogParams.view = view; + mDialogParams.viewSpacingSpecified = false; + return this; + } + + /** + * Sets view for dialog. + * + * @param view main view + * @param viewSpacingLeft left padding + * @param viewSpacingTop top padding + * @param viewSpacingRight right padding + * @param viewSpacingBottom bottom padding + * @return builder + */ + public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, + int viewSpacingBottom) { + mDialogParams.view = view; + mDialogParams.viewSpacingSpecified = true; + mDialogParams.viewSpacingLeft = viewSpacingLeft; + mDialogParams.viewSpacingTop = viewSpacingTop; + mDialogParams.viewSpacingRight = viewSpacingRight; + mDialogParams.viewSpacingBottom = viewSpacingBottom; + return this; + } + + /** + * Sets dialog icon. + * + * @param resourceId resource id of icon drawable + * @return builder + */ + public Builder setIcon(int resourceId) { + mDialogParams.icon = mDialogParams.context.getResources().getDrawable(resourceId); + return this; + } + + /** + * Sets dialog icon. + * + * @param drawable drawable + * @return builder + */ + public Builder setIcon(Drawable drawable) { + mDialogParams.icon = drawable; + return this; + } + + /** + * Creates dialog view. + * + * @return view + */ + public View create() { + final Resources res = mDialogParams.context.getResources(); + final int defaultTitleTextColor = res.getColor(R.color.sdl_title_text_dark); + final int defaultTitleSeparatorColor = res.getColor(R.color.sdl_title_separator_dark); + final int defaultMessageTextColor = res.getColor(R.color.sdl_message_text_dark); + final ColorStateList defaultButtonTextColor = res.getColorStateList( + R.color.sdl_button_text_dark); + final int defaultButtonSeparatorColor = res.getColor(R.color.sdl_button_separator_dark); + final int defaultButtonBackgroundColorNormal = res.getColor(R.color.sdl_button_normal_dark); + final int defaultButtonBackgroundColorPressed = res.getColor(R.color.sdl_button_pressed_dark); + final int defaultButtonBackgroundColorFocused = res.getColor(R.color.sdl_button_focused_dark); + + final TypedArray a = mDialogParams.context.getTheme().obtainStyledAttributes(null, + R.styleable.DialogStyle, R.attr.sdlDialogStyle, 0); + mDialogParams.titleTextColor = a.getColor(R.styleable.DialogStyle_titleTextColor, + defaultTitleTextColor); + mDialogParams.titleSeparatorColor = a.getColor(R.styleable.DialogStyle_titleSeparatorColor, + defaultTitleSeparatorColor); + mDialogParams.messageTextColor = a.getColor(R.styleable.DialogStyle_messageTextColor, + defaultMessageTextColor); + mDialogParams.buttonTextColor = a.getColorStateList(R.styleable.DialogStyle_buttonTextColor); + if (mDialogParams.buttonTextColor == null) { + mDialogParams.buttonTextColor = defaultButtonTextColor; + } + mDialogParams.buttonSeparatorColor = a.getColor(R.styleable.DialogStyle_buttonSeparatorColor, + defaultButtonSeparatorColor); + mDialogParams.buttonBackgroundColorNormal = a.getColor( + R.styleable.DialogStyle_buttonBackgroundColorNormal, defaultButtonBackgroundColorNormal); + mDialogParams.buttonBackgroundColorPressed = a.getColor( + R.styleable.DialogStyle_buttonBackgroundColorPressed, + defaultButtonBackgroundColorPressed); + mDialogParams.buttonBackgroundColorFocused = a.getColor( + R.styleable.DialogStyle_buttonBackgroundColorFocused, + defaultButtonBackgroundColorFocused); + + if (mDialogParams.listAdapter != null) { + final int defaultListItemSeparatorColor = res.getColor( + R.color.sdl_list_item_separator_dark); + final int defaultListItemBackgroundColorNormal = res.getColor( + R.color.sdl_button_normal_dark); + final int defaultListItemBackgroundColorFocused = res.getColor( + R.color.sdl_button_focused_dark); + final int defaultListItemBackgroundColorPressed = res.getColor( + R.color.sdl_button_pressed_dark); + mDialogParams.listItemSeparatorColor = a.getColor( + R.styleable.DialogStyle_listItemSeparatorColor, defaultListItemSeparatorColor); + mDialogParams.listItemBackgroundColorNormal = a.getColor( + R.styleable.DialogStyle_listItemColorNormal, defaultListItemBackgroundColorNormal); + mDialogParams.listItemBackgroundColorFocused = a.getColor( + R.styleable.DialogStyle_listItemColorFocused, defaultListItemBackgroundColorFocused); + mDialogParams.listItemBackgroundColorPressed = a.getColor( + R.styleable.DialogStyle_listItemColorPressed, defaultListItemBackgroundColorPressed); + } + a.recycle(); + + View view = getDialogLayoutAndInitTitle(); + + LinearLayout content = (LinearLayout)view.findViewById(R.id.sdl__content); + + if (mDialogParams.message != null) { + View viewMessage = mDialogParams.layoutInflater.inflate(R.layout.dialog_part_message, + content, false); + TextView tvMessage = (TextView)viewMessage.findViewById(R.id.sdl__message); + tvMessage.setTextColor(mDialogParams.messageTextColor); + tvMessage.setText(mDialogParams.message); + content.addView(viewMessage); + } + + if (mDialogParams.view != null) { + FrameLayout customPanel = (FrameLayout)mDialogParams.layoutInflater.inflate( + R.layout.dialog_part_custom, content, false); + FrameLayout custom = (FrameLayout)customPanel.findViewById(R.id.sdl__custom); + custom.addView(mDialogParams.view, new FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + if (mDialogParams.viewSpacingSpecified) { + custom.setPadding(mDialogParams.viewSpacingLeft, mDialogParams.viewSpacingTop, + mDialogParams.viewSpacingRight, mDialogParams.viewSpacingBottom); + } + content.addView(customPanel); + } + + if (mDialogParams.listAdapter != null) { + ListView listView = (ListView)mDialogParams.layoutInflater.inflate( + R.layout.dialog_part_list, content, false); + listView.setAdapter(mDialogParams.listAdapter); + listView.setDivider(getColoredListItemsDivider()); + listView.setDividerHeight(1); + listView.setSelector(getListItemSelector()); + listView.setOnItemClickListener(mDialogParams.onItemClickListener); + if (mDialogParams.listCheckedItemIdx != -1) { + listView.setSelection(mDialogParams.listCheckedItemIdx); + } + content.addView(listView); + } + + addButtons(content); + + return view; + } + + /** + * Gets dialog layout and inits title. + * + * @return view DialogLayout + */ + @SuppressWarnings("deprecation") + private View getDialogLayoutAndInitTitle() { + View view = mDialogParams.layoutInflater.inflate(R.layout.dialog_part_title, + mDialogParams.container, false); + TextView tvTitle = (TextView)view.findViewById(R.id.sdl__title); + View viewTitleDivider = view.findViewById(R.id.sdl__titleDivider); + + if (!TextUtils.isEmpty(mDialogParams.title)) { + tvTitle.setVisibility(View.VISIBLE); + viewTitleDivider.setVisibility(View.VISIBLE); + tvTitle.setText(mDialogParams.title); + tvTitle.setTextColor(mDialogParams.titleTextColor); + if (mDialogParams.icon != null) { + tvTitle.setCompoundDrawablesWithIntrinsicBounds(mDialogParams.icon, null, null, null); + tvTitle.setCompoundDrawablePadding( + mDialogParams.context.getResources().getDimensionPixelSize(R.dimen.grid_2)); + } + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { + viewTitleDivider.setBackgroundDrawable(new ColorDrawable( + mDialogParams.titleSeparatorColor)); + } else { + viewTitleDivider.setBackground(new ColorDrawable(mDialogParams.titleSeparatorColor)); + } + + } else { + tvTitle.setVisibility(View.GONE); + viewTitleDivider.setVisibility(View.GONE); + } + return view; + } + + /** + * Adds buttons to given layout. + * + * @param listDialog layout. + */ + @SuppressWarnings("deprecation") + private void addButtons(LinearLayout listDialog) { + if (mDialogParams.negativeButtonText != null || mDialogParams.neutralButtonText != null || + mDialogParams.positiveButtonText != null) { + View viewButtonPanel = mDialogParams.layoutInflater.inflate( + R.layout.dialog_part_button_panel, listDialog, false); + LinearLayout llButtonPanel = (LinearLayout)viewButtonPanel.findViewById( + R.id.dialog_button_panel); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { + viewButtonPanel.findViewById(R.id.dialog_horizontal_separator).setBackgroundDrawable( + new ColorDrawable(mDialogParams.buttonSeparatorColor)); + } else { + viewButtonPanel.findViewById(R.id.dialog_horizontal_separator).setBackground( + new ColorDrawable(mDialogParams.buttonSeparatorColor)); + } + + boolean addDivider = false; + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + addDivider = addPositiveButton(llButtonPanel, addDivider); + } else { + addDivider = addNegativeButton(llButtonPanel, addDivider); + } + addDivider = addNeutralButton(llButtonPanel, addDivider); + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + addNegativeButton(llButtonPanel, addDivider); + } else { + addPositiveButton(llButtonPanel, addDivider); + } + + listDialog.addView(viewButtonPanel); + } + } + + /** + * Adds negative button. + * + * @param parent parent view group + * @param addDivider true/false whether parent needs divider + * @return divider defined in parent or true if negative button was added + */ + @SuppressWarnings("deprecation") + private boolean addNegativeButton(ViewGroup parent, boolean addDivider) { + if (mDialogParams.negativeButtonText != null) { + if (addDivider) { + addDivider(parent); + } + Button btn = (Button)mDialogParams.layoutInflater.inflate(R.layout.dialog_part_button, + parent, false); + btn.setId(R.id.sdl__negative_button); + btn.setText(mDialogParams.negativeButtonText); + btn.setTextColor(mDialogParams.buttonTextColor); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { + btn.setBackgroundDrawable(getButtonBackground()); + } else { + btn.setBackground(getButtonBackground()); + } + + btn.setOnClickListener(mDialogParams.negativeButtonListener); + parent.addView(btn); + return true; + } + return addDivider; + } + + /** + * Adds positive button. + * + * @param parent parent view group + * @param addDivider true/false whether parent needs divider + * @return divider defined in parent or true if positive button was added + */ + @SuppressWarnings("deprecation") + private boolean addPositiveButton(ViewGroup parent, boolean addDivider) { + if (mDialogParams.positiveButtonText != null) { + if (addDivider) { + addDivider(parent); + } + Button btn = (Button)mDialogParams.layoutInflater.inflate(R.layout.dialog_part_button, + parent, false); + btn.setId(R.id.sdl__positive_button); + btn.setText(mDialogParams.positiveButtonText); + btn.setTextColor(mDialogParams.buttonTextColor); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { + btn.setBackgroundDrawable(getButtonBackground()); + } else { + btn.setBackground(getButtonBackground()); + } + btn.setOnClickListener(mDialogParams.positiveButtonListener); + parent.addView(btn); + return true; + } + return addDivider; + } - private ColorDrawable getColoredListItemsDivider() { - ColorDrawable colorDividerDrawable = new ColorDrawable(mListItemSeparatorColor); - return colorDividerDrawable; + /** + * Adds neutral button. + * + * @param parent parent view group + * @param addDivider true/false whether parent needs divider + * @return divider defined in parent or true if neutral button was added + */ + @SuppressWarnings("deprecation") + private boolean addNeutralButton(ViewGroup parent, boolean addDivider) { + if (mDialogParams.neutralButtonText != null) { + if (addDivider) { + addDivider(parent); + } + Button btn = (Button)mDialogParams.layoutInflater.inflate(R.layout.dialog_part_button, + parent, false); + btn.setId(R.id.sdl__neutral_button); + btn.setText(mDialogParams.neutralButtonText); + btn.setTextColor(mDialogParams.buttonTextColor); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { + btn.setBackgroundDrawable(getButtonBackground()); + } else { + btn.setBackground(getButtonBackground()); + } + btn.setOnClickListener(mDialogParams.neutralButtonListener); + parent.addView(btn); + return true; + } + return addDivider; + } + + /** + * Adds divider. + * + * @param parent parent view group + */ + @SuppressWarnings("deprecation") + private void addDivider(ViewGroup parent) { + View view = mDialogParams.layoutInflater.inflate(R.layout.dialog_part_button_separator, + parent, false); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { + view.findViewById(R.id.dialog_button_separator).setBackgroundDrawable(new ColorDrawable( + mDialogParams.buttonSeparatorColor)); + } else { + view.findViewById(R.id.dialog_button_separator).setBackground(new ColorDrawable( + mDialogParams.buttonSeparatorColor)); + } + parent.addView(view); + } + + /** + * Defines button background with all states. + * + * @return background for button. + */ + private StateListDrawable getButtonBackground() { + ColorDrawable colorDefault = new ColorDrawable(mDialogParams.buttonBackgroundColorNormal); + ColorDrawable colorPressed = new ColorDrawable(mDialogParams.buttonBackgroundColorPressed); + ColorDrawable colorFocused = new ColorDrawable(mDialogParams.buttonBackgroundColorFocused); + StateListDrawable background = new StateListDrawable(); + background.addState(pressedState, colorPressed); + background.addState(focusedState, colorFocused); + background.addState(defaultState, colorDefault); + return background; + } + + /** + * Defines list item background with all states. + * + * @return background for list. + */ + private StateListDrawable getListItemSelector() { + ColorDrawable colorDefault = new ColorDrawable(mDialogParams.listItemBackgroundColorNormal); + ColorDrawable colorPressed = new ColorDrawable(mDialogParams.listItemBackgroundColorPressed); + ColorDrawable colorFocused = new ColorDrawable(mDialogParams.listItemBackgroundColorFocused); + StateListDrawable background = new StateListDrawable(); + background.addState(pressedState, colorPressed); + background.addState(focusedState, colorFocused); + background.addState(defaultState, colorDefault); + return background; + } + + /** + * Defines list item divider. + * + * @return divider + */ + private ColorDrawable getColoredListItemsDivider() { + ColorDrawable colorDividerDrawable = new ColorDrawable(mDialogParams.listItemSeparatorColor); + return colorDividerDrawable; + } } - } } diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/DatePickerDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/DatePickerDialogFragment.java index 762afe8..dc6818d 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/DatePickerDialogFragment.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/DatePickerDialogFragment.java @@ -1,6 +1,7 @@ package eu.inmite.android.lib.dialogs; import android.content.Context; +import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; @@ -18,227 +19,185 @@ */ public class DatePickerDialogFragment extends BaseDialogFragment { - protected static final String ARG_ZONE = "zone"; - protected static final String ARG_TITLE = "title"; - protected static final String ARG_POSITIVE_BUTTON = "positive_button"; - protected static final String ARG_NEGATIVE_BUTTON = "negative_button"; - protected static final String ARG_DATE = "date"; - protected static final String ARG_24H = "24h"; - - DatePicker mDatePicker; - Calendar mCalendar; - - private int mRequestCode; - - - public static SimpleDialogBuilder createBuilder(Context context, FragmentManager fragmentManager) { - return new SimpleDialogBuilder(context, fragmentManager, DatePickerDialogFragment.class); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - final Fragment targetFragment = getTargetFragment(); - if (targetFragment != null) { - mRequestCode = getTargetRequestCode(); - } else { - Bundle args = getArguments(); - if (args != null) { - mRequestCode = args.getInt(BaseDialogBuilder.ARG_REQUEST_CODE, 0); - } - } - } - - protected IDateDialogListener getDialogListener() { - final Fragment targetFragment = getTargetFragment(); - if (targetFragment != null) { - if (targetFragment instanceof IDateDialogListener) { - return (IDateDialogListener) targetFragment; - } - } else { - if (getActivity() instanceof IDateDialogListener) { - return (IDateDialogListener) getActivity(); - } - } - return null; - } - - protected IDateDialogCancelListener getCancelListener() { - final Fragment targetFragment = getTargetFragment(); - if (targetFragment != null) { - if (targetFragment instanceof IDateDialogCancelListener) { - return (IDateDialogCancelListener) targetFragment; - } - } else { - if (getActivity() instanceof IDateDialogCancelListener) { - return (IDateDialogCancelListener) getActivity(); - } - } - return null; - } - - @Override - protected BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) { - final String title = getTitle(); - if (!TextUtils.isEmpty(title)) { - builder.setTitle(title); - } - - final String positiveButtonText = getPositiveButtonText(); - if (!TextUtils.isEmpty(positiveButtonText)) { - builder.setPositiveButton(positiveButtonText, new View.OnClickListener() { - - @Override - public void onClick(View view) { - IDateDialogListener listener = getDialogListener(); - if (listener != null) { - listener.onPositiveButtonClicked(mRequestCode, getDate()); - } - dismiss(); - } - }); - } - - final String negativeButtonText = getNegativeButtonText(); - if (!TextUtils.isEmpty(negativeButtonText)) { - builder.setNegativeButton(negativeButtonText, new View.OnClickListener() { - - @Override - public void onClick(View view) { - IDateDialogListener listener = getDialogListener(); - if (listener != null) { - listener.onNegativeButtonClicked(mRequestCode, getDate()); - } - dismiss(); - } - }); - } - mDatePicker = (DatePicker) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_part_datepicker, null); - builder.setView(mDatePicker); - - TimeZone zone = TimeZone.getTimeZone(getArguments().getString(ARG_ZONE)); - mCalendar = Calendar.getInstance(zone); - mCalendar.setTimeInMillis(getArguments().getLong(ARG_DATE, System.currentTimeMillis())); - mDatePicker.updateDate(mCalendar.get(Calendar.YEAR) - , mCalendar.get(Calendar.MONTH) - , mCalendar.get(Calendar.DAY_OF_MONTH)); - return builder; - } - - protected String getTitle() { - return getArguments().getString(ARG_TITLE); - } - - protected String getPositiveButtonText() { - return getArguments().getString(ARG_POSITIVE_BUTTON); - } - - protected String getNegativeButtonText() { - return getArguments().getString(ARG_NEGATIVE_BUTTON); - } - - public Date getDate() { - mCalendar.set(Calendar.YEAR, mDatePicker.getYear()); - mCalendar.set(Calendar.MONTH, mDatePicker.getMonth()); - mCalendar.set(Calendar.DAY_OF_MONTH, mDatePicker.getDayOfMonth()); - return mCalendar.getTime(); - } - - public static class SimpleDialogBuilder extends BaseDialogBuilder { - Date mDate = new Date(); - String mTimeZone = null; - - private String mTitle; - private String mPositiveButtonText; - private String mNegativeButtonText; - - private boolean mShowDefaultButton = true; - private boolean m24h; - - protected SimpleDialogBuilder(Context context, FragmentManager fragmentManager, Class clazz) { - super(context, fragmentManager, clazz); - } - - public SimpleDialogBuilder setTitle(int titleResourceId) { - mTitle = mContext.getString(titleResourceId); - return this; - } - - - public SimpleDialogBuilder setTitle(String title) { - mTitle = title; - return this; - } - - public SimpleDialogBuilder setPositiveButtonText(int textResourceId) { - mPositiveButtonText = mContext.getString(textResourceId); - return this; - } - - public SimpleDialogBuilder setPositiveButtonText(String text) { - mPositiveButtonText = text; - return this; - } - - public SimpleDialogBuilder setNegativeButtonText(int textResourceId) { - mNegativeButtonText = mContext.getString(textResourceId); - return this; - } - - public SimpleDialogBuilder setNegativeButtonText(String text) { - mNegativeButtonText = text; - return this; - } - - public SimpleDialogBuilder setDate(Date date) { - mDate = date; - return this; - } - - public SimpleDialogBuilder setTimeZone(String zone) { - mTimeZone = zone; - return this; - } - - public SimpleDialogBuilder set24hour(boolean state) { - m24h = state; - return this; - } - - /** - * When there is neither positive nor negative button, default "close" button is created if it was enabled.
- * Default is true. - */ - public SimpleDialogBuilder hideDefaultButton(boolean hide) { - mShowDefaultButton = !hide; - return this; - } - - - @Override - protected Bundle prepareArguments() { - if (mShowDefaultButton && mPositiveButtonText == null && mNegativeButtonText == null) { - mPositiveButtonText = mContext.getString(R.string.dialog_close); - } - - Bundle args = new Bundle(); - args.putString(SimpleDialogFragment.ARG_TITLE, mTitle); - args.putString(SimpleDialogFragment.ARG_POSITIVE_BUTTON, mPositiveButtonText); - args.putString(SimpleDialogFragment.ARG_NEGATIVE_BUTTON, mNegativeButtonText); - - args.putLong(ARG_DATE, mDate.getTime()); - args.putBoolean(ARG_24H, m24h); - if (mTimeZone != null) { - args.putString(ARG_ZONE, mTimeZone); - } else { - args.putString(ARG_ZONE, "GMT"); - } - return args; - } - - @Override - protected SimpleDialogBuilder self() { - return this; - } - } + protected static final String ARG_ZONE = "zone"; + protected static final String ARG_DATE = "date"; + protected static final String ARG_24H = "24h"; + + DatePicker mDatePicker; + Calendar mCalendar; + + private int mRequestCode; + + + public static SimpleDialogBuilder createBuilder(Context context, FragmentManager fragmentManager) { + return new SimpleDialogBuilder(context, fragmentManager, DatePickerDialogFragment.class); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + final Fragment targetFragment = getTargetFragment(); + if (targetFragment != null) { + mRequestCode = getTargetRequestCode(); + } else { + Bundle args = getArguments(); + if (args != null) { + mRequestCode = args.getInt(BaseDialogFragment.ARG_REQUEST_CODE, 0); + } + } + } + + protected IDateDialogListener getDialogListener() { + final Fragment targetFragment = getTargetFragment(); + if (targetFragment != null) { + if (targetFragment instanceof IDateDialogListener) { + return (IDateDialogListener)targetFragment; + } + } else { + if (getActivity() instanceof IDateDialogListener) { + return (IDateDialogListener)getActivity(); + } + } + return null; + } + + protected IDateDialogCancelListener getCancelListener() { + final Fragment targetFragment = getTargetFragment(); + if (targetFragment != null) { + if (targetFragment instanceof IDateDialogCancelListener) { + return (IDateDialogCancelListener)targetFragment; + } + } else { + if (getActivity() instanceof IDateDialogCancelListener) { + return (IDateDialogCancelListener)getActivity(); + } + } + return null; + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + IDateDialogCancelListener onDateDialogCanceled = getCancelListener(); + if (onDateDialogCanceled != null) { + onDateDialogCanceled.onCancelled(mRequestCode, getDate()); + } + } + + @Override + protected BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) { + final String title = getTitle(); + if (!TextUtils.isEmpty(title)) { + builder.setTitle(title); + } + + final String positiveButtonText = getPositiveButtonText(); + if (!TextUtils.isEmpty(positiveButtonText)) { + builder.setPositiveButton(positiveButtonText, new View.OnClickListener() { + + @Override + public void onClick(View view) { + IDateDialogListener listener = getDialogListener(); + if (listener != null) { + listener.onPositiveButtonClicked(mRequestCode, getDate()); + } + dismiss(); + } + }); + } + + final String negativeButtonText = getNegativeButtonText(); + if (!TextUtils.isEmpty(negativeButtonText)) { + builder.setNegativeButton(negativeButtonText, new View.OnClickListener() { + + @Override + public void onClick(View view) { + IDateDialogListener listener = getDialogListener(); + if (listener != null) { + listener.onNegativeButtonClicked(mRequestCode, getDate()); + } + dismiss(); + } + }); + } + mDatePicker = (DatePicker)LayoutInflater.from(getActivity()).inflate(R.layout.dialog_part_datepicker, null); + builder.setView(mDatePicker); + + TimeZone zone = TimeZone.getTimeZone(getArguments().getString(ARG_ZONE)); + mCalendar = Calendar.getInstance(zone); + mCalendar.setTimeInMillis(getArguments().getLong(ARG_DATE, System.currentTimeMillis())); + mDatePicker.updateDate(mCalendar.get(Calendar.YEAR) + , mCalendar.get(Calendar.MONTH) + , mCalendar.get(Calendar.DAY_OF_MONTH)); + return builder; + } + + public Date getDate() { + mCalendar.set(Calendar.YEAR, mDatePicker.getYear()); + mCalendar.set(Calendar.MONTH, mDatePicker.getMonth()); + mCalendar.set(Calendar.DAY_OF_MONTH, mDatePicker.getDayOfMonth()); + return mCalendar.getTime(); + } + + public static class SimpleDialogBuilder extends BaseDialogBuilder { + Date mDate = new Date(); + String mTimeZone = null; + + private boolean mShowDefaultButton = true; + private boolean m24h; + + protected SimpleDialogBuilder(Context context, FragmentManager fragmentManager, Class clazz) { + super(context, fragmentManager, clazz); + } + + public SimpleDialogBuilder setDate(Date date) { + mDate = date; + return this; + } + + public SimpleDialogBuilder setTimeZone(String zone) { + mTimeZone = zone; + return this; + } + + public SimpleDialogBuilder set24hour(boolean state) { + m24h = state; + return this; + } + + @Override + protected Bundle prepareArguments() { + if (mShowDefaultButton && mDialogParams.positiveButtonText == null && mDialogParams.negativeButtonText + == null) { + mDialogParams.positiveButtonText = mDialogParams.context.getString(R.string.dialog_close); + } + + Bundle args = new Bundle(); + if (!TextUtils.isEmpty(mDialogParams.title)) { + args.putString(BaseDialogFragment.ARG_TITLE, mDialogParams.title.toString()); + } + if (mDialogParams.positiveButtonText != null) { + args.putString(BaseDialogFragment.ARG_POSITIVE_BUTTON, mDialogParams.positiveButtonText + .toString()); + } + if (mDialogParams.negativeButtonText != null) { + args.putString(BaseDialogFragment.ARG_NEGATIVE_BUTTON, + mDialogParams.negativeButtonText.toString()); + } + + args.putLong(ARG_DATE, mDate.getTime()); + args.putBoolean(ARG_24H, m24h); + if (mTimeZone != null) { + args.putString(ARG_ZONE, mTimeZone); + } else { + args.putString(ARG_ZONE, "GMT"); + } + return args; + } + + @Override + protected SimpleDialogBuilder self() { + return this; + } + } } diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogCancelListener.java b/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogCancelListener.java index 896040e..b8b4d59 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogCancelListener.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogCancelListener.java @@ -17,9 +17,15 @@ package eu.inmite.android.lib.dialogs; /** - * @author Tomáš Kypta - * @since 13/05/2013 + * Implement this interface in Activity or Fragment to react to cancel event. + * + * @author Tomáš Kypta (inmite.eu) */ public interface ISimpleDialogCancelListener { + /** + * Callback to react on cancel event. + * + * @param requestCode identifies dialogue + */ public void onCancelled(int requestCode); } diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogListener.java b/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogListener.java index 8ef36a4..44b5c74 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogListener.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogListener.java @@ -17,14 +17,30 @@ package eu.inmite.android.lib.dialogs; /** - * Implement this interface in Activity or Fragment to react to positive and negative buttons. + * Implement this interface in Activity or Fragment to react to positive, negative and neutral + * buttons. * * @author David Vávra (david@inmite.eu) */ public interface ISimpleDialogListener { + /** + * Callback for click on positive button. + * + * @param requestCode enables to identify dialog + */ public void onPositiveButtonClicked(int requestCode); + /** + * Callback for click on negative button. + * + * @param requestCode enables to identify dialog + */ public void onNegativeButtonClicked(int requestCode); + /** + * Callback for click on neutral button. + * + * @param requestCode enables to identify dialog + */ public void onNeutralButtonClicked(int requestCode); } diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/ListDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/ListDialogFragment.java index 36ba208..ae5ea85 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/ListDialogFragment.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/ListDialogFragment.java @@ -17,181 +17,146 @@ */ public class ListDialogFragment extends BaseDialogFragment { - private static String ARG_ITEMS = "items"; - - public static SimpleListDialogBuilder createBuilder(Context context, - FragmentManager fragmentManager) { - return new SimpleListDialogBuilder(context, fragmentManager); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - if (getArguments() == null) { - throw new IllegalArgumentException( - "use SimpleListDialogBuilder to construct this dialog"); - } - } - - public static class SimpleListDialogBuilder extends BaseDialogBuilder { - - private String title; - - private String[] items; - - private String cancelButtonText; - - private boolean mShowDefaultButton = true; - - public SimpleListDialogBuilder(Context context, FragmentManager fragmentManager) { - super(context, fragmentManager, ListDialogFragment.class); - } - - @Override - protected SimpleListDialogBuilder self() { - return this; - } - - private Resources getResources() { - return mContext.getResources(); - } - - public SimpleListDialogBuilder setTitle(String title) { - this.title = title; - return this; - } - - public SimpleListDialogBuilder setTitle(int titleResID) { - this.title = getResources().getString(titleResID); - return this; - } - - public SimpleListDialogBuilder setItems(String[] items) { - this.items = items; - return this; - } - - public SimpleListDialogBuilder setItems(int itemsArrayResID) { - this.items = getResources().getStringArray(itemsArrayResID); - return this; - } - - public SimpleListDialogBuilder setCancelButtonText(String text) { - this.cancelButtonText = text; - return this; - } - - public SimpleListDialogBuilder setCancelButtonText(int cancelBttTextResID) { - this.cancelButtonText = getResources().getString(cancelBttTextResID); - return this; - } - - @Override - public ListDialogFragment show() { - return (ListDialogFragment) super.show(); - } - - /** - * When there is neither positive nor negative button, default "close" button is created if - * it was enabled.
- * Default is true. - */ - public SimpleListDialogBuilder hideDefaultButton(boolean hide) { - mShowDefaultButton = !hide; - return this; - } - - @Override - protected Bundle prepareArguments() { - if (mShowDefaultButton && cancelButtonText == null) { - cancelButtonText = mContext.getString(R.string.dialog_close); - } - - Bundle args = new Bundle(); - args.putString(SimpleDialogFragment.ARG_TITLE, title); - args.putString(SimpleDialogFragment.ARG_POSITIVE_BUTTON, cancelButtonText); - args.putStringArray(ARG_ITEMS, items); - - return args; - } - } - - @Override - public void onCancel(DialogInterface dialog) { - super.onCancel(dialog); - IListDialogListener onListItemSelectedListener = getDialogListener(); - if (onListItemSelectedListener != null) { - onListItemSelectedListener.onCancelled(); - } - } - - @Override - protected Builder build(Builder builder) { - final String title = getTitle(); - if (!TextUtils.isEmpty(title)) { - builder.setTitle(title); - } - - if (!TextUtils.isEmpty(getPositiveButtonText())) { - builder.setPositiveButton(getPositiveButtonText(), new View.OnClickListener() { - @Override - public void onClick(View view) { - IListDialogListener onListItemSelectedListener = getDialogListener(); - if (onListItemSelectedListener != null) { - onListItemSelectedListener.onCancelled(); - } - dismiss(); - } - }); - } - - final String[] items = getItems(); - if (items != null && items.length > 0) { - ListAdapter adapter = new ArrayAdapter(getActivity(), - R.layout.dialog_list_item, - R.id.list_item_text, - items); - - builder.setItems(adapter, 0, new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - IListDialogListener onListItemSelectedListener = getDialogListener(); - if (onListItemSelectedListener != null) { - onListItemSelectedListener - .onListItemSelected(getItems()[position], position); - dismiss(); - } - } - }); - } - - return builder; - } - - private IListDialogListener getDialogListener() { - final Fragment targetFragment = getTargetFragment(); - if (targetFragment != null) { - if (targetFragment instanceof IListDialogListener) { - return (IListDialogListener) targetFragment; - } - } else { - if (getActivity() instanceof IListDialogListener) { - return (IListDialogListener) getActivity(); - } - } - return null; - } - - private String getTitle() { - return getArguments().getString(SimpleDialogFragment.ARG_TITLE); - } - - private String[] getItems() { - return getArguments().getStringArray(ARG_ITEMS); - } - - private String getPositiveButtonText() { - return getArguments().getString(SimpleDialogFragment.ARG_POSITIVE_BUTTON); - } - + public static SimpleListDialogBuilder createBuilder(Context context, + FragmentManager fragmentManager) { + return new SimpleListDialogBuilder(context, fragmentManager); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + if (getArguments() == null) { + throw new IllegalArgumentException( + "use SimpleListDialogBuilder to construct this dialog"); + } + } + + public static class SimpleListDialogBuilder extends BaseDialogBuilder { + + private String title; + + private String[] items; + + private String cancelButtonText; + + private boolean mShowDefaultButton = true; + + public SimpleListDialogBuilder(Context context, FragmentManager fragmentManager) { + super(context, fragmentManager, ListDialogFragment.class); + } + + @Override + protected SimpleListDialogBuilder self() { + return this; + } + + private Resources getResources() { + return mDialogParams.context.getResources(); + } + + public SimpleListDialogBuilder setItems(String[] items) { + this.items = items; + return this; + } + + public SimpleListDialogBuilder setItems(int itemsArrayResID) { + this.items = getResources().getStringArray(itemsArrayResID); + return this; + } + + public SimpleListDialogBuilder setCancelButtonText(String text) { + this.cancelButtonText = text; + return this; + } + + public SimpleListDialogBuilder setCancelButtonText(int cancelBttTextResID) { + this.cancelButtonText = getResources().getString(cancelBttTextResID); + return this; + } + + @Override + public ListDialogFragment show() { + return (ListDialogFragment)super.show(); + } + + @Override + protected Bundle prepareArguments() { + if (mShowDefaultButton && cancelButtonText == null) { + cancelButtonText = mDialogParams.context.getString(R.string.dialog_close); + } + + Bundle args = new Bundle(); + args.putString(BaseDialogFragment.ARG_TITLE, title); + args.putString(BaseDialogFragment.ARG_POSITIVE_BUTTON, cancelButtonText); + args.putStringArray(BaseDialogFragment.ARG_ITEMS, items); + + return args; + } + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + IListDialogListener onListItemSelectedListener = getDialogListener(); + if (onListItemSelectedListener != null) { + onListItemSelectedListener.onCancelled(); + } + } + + @Override + protected Builder build(Builder builder) { + final String title = getTitle(); + if (!TextUtils.isEmpty(title)) { + builder.setTitle(title); + } + + if (!TextUtils.isEmpty(getPositiveButtonText())) { + builder.setPositiveButton(getPositiveButtonText(), new View.OnClickListener() { + @Override + public void onClick(View view) { + IListDialogListener onListItemSelectedListener = getDialogListener(); + if (onListItemSelectedListener != null) { + onListItemSelectedListener.onCancelled(); + } + dismiss(); + } + }); + } + + final String[] items = getItems(); + if (items != null && items.length > 0) { + ListAdapter adapter = new ArrayAdapter(getActivity(), + R.layout.dialog_list_item, + R.id.list_item_text, + items); + + builder.setItems(adapter, 0, new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + IListDialogListener onListItemSelectedListener = getDialogListener(); + if (onListItemSelectedListener != null) { + onListItemSelectedListener + .onListItemSelected(getItems()[position], position); + dismiss(); + } + } + }); + } + + return builder; + } + + private IListDialogListener getDialogListener() { + final Fragment targetFragment = getTargetFragment(); + if (targetFragment != null) { + if (targetFragment instanceof IListDialogListener) { + return (IListDialogListener)targetFragment; + } + } else { + if (getActivity() instanceof IListDialogListener) { + return (IListDialogListener)getActivity(); + } + } + return null; + } } diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/ProgressDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/ProgressDialogFragment.java index f509043..a30d593 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/ProgressDialogFragment.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/ProgressDialogFragment.java @@ -6,6 +6,7 @@ import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; @@ -23,31 +24,29 @@ */ public class ProgressDialogFragment extends BaseDialogFragment { - protected final static String ARG_MESSAGE = "message"; - protected final static String ARG_TITLE = "title"; - - protected int mRequestCode; - - public static ProgressDialogBuilder createBuilder(Context context, FragmentManager fragmentManager) { + public static ProgressDialogBuilder createBuilder(Context context, + FragmentManager fragmentManager) { return new ProgressDialogBuilder(context, fragmentManager); } @Override protected Builder build(Builder builder) { final int defaultMessageTextColor = getResources().getColor(R.color.sdl_message_text_dark); - final TypedArray a = getActivity().getTheme().obtainStyledAttributes(null, R.styleable.DialogStyle, R.attr.sdlDialogStyle, 0); - final int messageTextColor = a.getColor(R.styleable.DialogStyle_messageTextColor, defaultMessageTextColor); + final TypedArray a = getActivity().getTheme().obtainStyledAttributes(null, + R.styleable.DialogStyle, R.attr.sdlDialogStyle, 0); + final int messageTextColor = a.getColor(R.styleable.DialogStyle_messageTextColor, + defaultMessageTextColor); a.recycle(); final LayoutInflater inflater = builder.getLayoutInflater(); final View view = inflater.inflate(R.layout.dialog_part_progress, null, false); - final TextView tvMessage = (TextView) view.findViewById(R.id.sdl__message); - tvMessage.setText(getArguments().getString(ARG_MESSAGE)); + final TextView tvMessage = (TextView)view.findViewById(R.id.sdl__message); + tvMessage.setText(getArguments().getString(BaseDialogFragment.ARG_MESSAGE)); tvMessage.setTextColor(messageTextColor); builder.setView(view); - builder.setTitle(getArguments().getString(ARG_TITLE)); + builder.setTitle(getArguments().getString(BaseDialogFragment.ARG_TITLE)); return builder; } @@ -59,8 +58,8 @@ public void onActivityCreated(Bundle savedInstanceState) { throw new IllegalArgumentException("use ProgressDialogBuilder to construct this dialog"); } final Fragment targetFragment = getTargetFragment(); - mRequestCode = targetFragment != null ? - getTargetRequestCode() : getArguments().getInt(BaseDialogBuilder.ARG_REQUEST_CODE, 0); + mRequestCode = targetFragment != null ? getTargetRequestCode() : getArguments().getInt( + BaseDialogFragment.ARG_REQUEST_CODE, 0); } @Override @@ -76,11 +75,11 @@ protected ISimpleDialogCancelListener getCancelListener() { final Fragment targetFragment = getTargetFragment(); if (targetFragment != null) { if (targetFragment instanceof ISimpleDialogCancelListener) { - return (ISimpleDialogCancelListener) targetFragment; + return (ISimpleDialogCancelListener)targetFragment; } } else { if (getActivity() instanceof ISimpleDialogCancelListener) { - return (ISimpleDialogCancelListener) getActivity(); + return (ISimpleDialogCancelListener)getActivity(); } } return null; @@ -88,9 +87,6 @@ protected ISimpleDialogCancelListener getCancelListener() { public static class ProgressDialogBuilder extends BaseDialogBuilder { - private String mTitle; - private String mMessage; - protected ProgressDialogBuilder(Context context, FragmentManager fragmentManager) { super(context, fragmentManager, ProgressDialogFragment.class); } @@ -100,33 +96,13 @@ protected ProgressDialogBuilder self() { return this; } - public ProgressDialogBuilder setTitle(int titleResourceId) { - mTitle = mContext.getString(titleResourceId); - return this; - } - - - public ProgressDialogBuilder setTitle(String title) { - mTitle = title; - return this; - } - - public ProgressDialogBuilder setMessage(int messageResourceId) { - mMessage = mContext.getString(messageResourceId); - return this; - } - - public ProgressDialogBuilder setMessage(String message) { - mMessage = message; - return this; - } - @Override protected Bundle prepareArguments() { Bundle args = new Bundle(); - args.putString(SimpleDialogFragment.ARG_MESSAGE, mMessage); - args.putString(SimpleDialogFragment.ARG_TITLE, mTitle); - + args.putString(BaseDialogFragment.ARG_MESSAGE, mDialogParams.message.toString()); + if (!TextUtils.isEmpty(mDialogParams.title)) { + args.putString(BaseDialogFragment.ARG_TITLE, mDialogParams.title.toString()); + } return args; } } diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleDialogFragment.java index 1536dda..1708bc0 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleDialogFragment.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleDialogFragment.java @@ -25,6 +25,7 @@ import android.text.SpannedString; import android.text.TextUtils; import android.view.View; +import android.view.ViewGroup; /** * Dialog for displaying simple message, message with title or message with title and two buttons. Implement {@link @@ -35,14 +36,6 @@ */ public class SimpleDialogFragment extends BaseDialogFragment { - protected final static String ARG_MESSAGE = "message"; - protected final static String ARG_TITLE = "title"; - protected final static String ARG_POSITIVE_BUTTON = "positive_button"; - protected final static String ARG_NEGATIVE_BUTTON = "negative_button"; - protected final static String ARG_NEUTRAL_BUTTON = "neutral_button"; - - protected int mRequestCode; - public static SimpleDialogBuilder createBuilder(Context context, FragmentManager fragmentManager) { return new SimpleDialogBuilder(context, fragmentManager, SimpleDialogFragment.class); } @@ -56,7 +49,7 @@ public void onActivityCreated(Bundle savedInstanceState) { } else { Bundle args = getArguments(); if (args != null) { - mRequestCode = args.getInt(BaseDialogBuilder.ARG_REQUEST_CODE, 0); + mRequestCode = args.getInt(BaseDialogFragment.ARG_REQUEST_CODE, 0); } } } @@ -121,26 +114,6 @@ public void onClick(View view) { return builder; } - protected CharSequence getMessage() { - return getArguments().getCharSequence(ARG_MESSAGE); - } - - protected String getTitle() { - return getArguments().getString(ARG_TITLE); - } - - protected String getPositiveButtonText() { - return getArguments().getString(ARG_POSITIVE_BUTTON); - } - - protected String getNegativeButtonText() { - return getArguments().getString(ARG_NEGATIVE_BUTTON); - } - - protected String getNeutralButtonText() { - return getArguments().getString(ARG_NEUTRAL_BUTTON); - } - @Override public void onCancel(DialogInterface dialog) { super.onCancel(dialog); @@ -154,11 +127,11 @@ protected ISimpleDialogListener getDialogListener() { final Fragment targetFragment = getTargetFragment(); if (targetFragment != null) { if (targetFragment instanceof ISimpleDialogListener) { - return (ISimpleDialogListener) targetFragment; + return (ISimpleDialogListener)targetFragment; } } else { if (getActivity() instanceof ISimpleDialogListener) { - return (ISimpleDialogListener) getActivity(); + return (ISimpleDialogListener)getActivity(); } } return null; @@ -168,11 +141,11 @@ protected ISimpleDialogCancelListener getCancelListener() { final Fragment targetFragment = getTargetFragment(); if (targetFragment != null) { if (targetFragment instanceof ISimpleDialogCancelListener) { - return (ISimpleDialogCancelListener) targetFragment; + return (ISimpleDialogCancelListener)targetFragment; } } else { if (getActivity() instanceof ISimpleDialogCancelListener) { - return (ISimpleDialogCancelListener) getActivity(); + return (ISimpleDialogCancelListener)getActivity(); } } return null; @@ -180,15 +153,8 @@ protected ISimpleDialogCancelListener getCancelListener() { public static class SimpleDialogBuilder extends BaseDialogBuilder { - private String mTitle; - private CharSequence mMessage; - private String mPositiveButtonText; - private String mNegativeButtonText; - private String mNeutralButtonText; - - private boolean mShowDefaultButton = true; - - protected SimpleDialogBuilder(Context context, FragmentManager fragmentManager, Class clazz) { + protected SimpleDialogBuilder(Context context, FragmentManager fragmentManager, + Class clazz) { super(context, fragmentManager, clazz); } @@ -197,88 +163,32 @@ protected SimpleDialogBuilder self() { return this; } - public SimpleDialogBuilder setTitle(int titleResourceId) { - mTitle = mContext.getString(titleResourceId); - return this; - } - - - public SimpleDialogBuilder setTitle(String title) { - mTitle = title; - return this; - } - - public SimpleDialogBuilder setMessage(int messageResourceId) { - mMessage = mContext.getText(messageResourceId); - return this; - } - - /** - * Allow to set resource string with HTML formatting and bind %s,%i. - * This is workaround for https://code.google.com/p/android/issues/detail?id=2923 - */ - public SimpleDialogBuilder setMessage(int resourceId, Object... formatArgs){ - mMessage = Html.fromHtml(String.format(Html.toHtml(new SpannedString(mContext.getText(resourceId))), formatArgs)); - return this; - } - - public SimpleDialogBuilder setMessage(CharSequence message) { - mMessage = message; - return this; - } - - public SimpleDialogBuilder setPositiveButtonText(int textResourceId) { - mPositiveButtonText = mContext.getString(textResourceId); - return this; - } - - public SimpleDialogBuilder setPositiveButtonText(String text) { - mPositiveButtonText = text; - return this; - } - - public SimpleDialogBuilder setNegativeButtonText(int textResourceId) { - mNegativeButtonText = mContext.getString(textResourceId); - return this; - } - - public SimpleDialogBuilder setNegativeButtonText(String text) { - mNegativeButtonText = text; - return this; - } - - public SimpleDialogBuilder setNeutralButtonText(int textResourceId) { - mNeutralButtonText = mContext.getString(textResourceId); - return this; - } - - public SimpleDialogBuilder setNeutralButtonText(String text) { - mNeutralButtonText = text; - return this; - } - - /** - * When there is neither positive nor negative button, default "close" button is created if it was enabled.
- * Default is true. - */ - public SimpleDialogBuilder hideDefaultButton(boolean hide) { - mShowDefaultButton = !hide; - return this; - } - @Override protected Bundle prepareArguments() { - if (mShowDefaultButton && mPositiveButtonText == null && mNegativeButtonText == null) { - mPositiveButtonText = mContext.getString(R.string.dialog_close); + if (mDialogParams.showDefaultButton && mDialogParams.positiveButtonText == null && + mDialogParams.negativeButtonText == null) { + mDialogParams.positiveButtonText = mDialogParams.context.getString(R.string.dialog_close); } Bundle args = new Bundle(); - args.putCharSequence(SimpleDialogFragment.ARG_MESSAGE, mMessage); - args.putString(SimpleDialogFragment.ARG_TITLE, mTitle); - args.putString(SimpleDialogFragment.ARG_POSITIVE_BUTTON, mPositiveButtonText); - args.putString(SimpleDialogFragment.ARG_NEGATIVE_BUTTON, mNegativeButtonText); - args.putString(SimpleDialogFragment.ARG_NEUTRAL_BUTTON, mNeutralButtonText); - + args.putCharSequence(BaseDialogFragment.ARG_MESSAGE, mDialogParams.message); + if (!TextUtils.isEmpty(mDialogParams.title)) { + args.putString(BaseDialogFragment.ARG_TITLE, mDialogParams.title.toString()); + } + if (mDialogParams.positiveButtonText != null) { + args.putString(BaseDialogFragment.ARG_POSITIVE_BUTTON, mDialogParams.positiveButtonText + .toString()); + } + if (mDialogParams.negativeButtonText != null) { + args.putString(BaseDialogFragment.ARG_NEGATIVE_BUTTON, + mDialogParams.negativeButtonText.toString()); + } + if (mDialogParams.neutralButtonText != null) { + args.putString(BaseDialogFragment.ARG_NEUTRAL_BUTTON, + mDialogParams.neutralButtonText.toString()); + } + args.putBoolean(BaseDialogFragment.ARG_CANCELABLE_ON_TOUCH_OUTSIDE, + mDialogParams.cancelableOnTouchOutside); return args; } } From 26fedb439618f6fec01a19ac2438fe514e2e6993 Mon Sep 17 00:00:00 2001 From: Lukas Prokop Date: Mon, 19 Jan 2015 14:12:12 +0100 Subject: [PATCH 2/6] Add travis.yml configuration file for continuous integration --- .travis.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..15d916c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: android +android: + components: + - build-tools-20.0.0 + - android-21 + - extra-android-m2repository \ No newline at end of file From 2127dbd7c515acb87d27a4b46f1893639296b751 Mon Sep 17 00:00:00 2001 From: limojoe Date: Wed, 21 Jan 2015 13:14:08 +0100 Subject: [PATCH 3/6] Update .travis.yml + connectedAndroidTest --- .travis.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 15d916c..409ca76 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,13 @@ android: components: - build-tools-20.0.0 - android-21 - - extra-android-m2repository \ No newline at end of file + - extra-android-m2repository + +before_script: + # Create and start emulator + - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI + - emulator -avd test -no-skin -no-audio -no-window & + - adb wait-for-device + - adb shell input keyevent 82 & + +script: ./gradlew connectedAndroidTest From 896ba77f96501a9771ce4d55fdf113b7fc61dbb9 Mon Sep 17 00:00:00 2001 From: limojoe Date: Wed, 21 Jan 2015 13:17:45 +0100 Subject: [PATCH 4/6] Update .travis.yml fix params --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 409ca76..be2e532 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,9 @@ android: - build-tools-20.0.0 - android-21 - extra-android-m2repository - +env: + matrix: + - ANDROID_TARGET=android-19 ANDROID_ABI=armeabi-v7a before_script: # Create and start emulator - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI From 1d99233c16d9f6de7e8df16f23db81aad4da512c Mon Sep 17 00:00:00 2001 From: Lukas Prokop Date: Tue, 27 Jan 2015 17:27:53 +0100 Subject: [PATCH 5/6] Reformat: using new Avast code-style --- demo/src/main/AndroidManifest.xml | 38 +- .../demo/dialogs/JayneHatDialogFragment.java | 40 +- .../eu/inmite/demo/dialogs/MyActivity.java | 170 +- demo/src/main/res/layout/item_jayne_hat.xml | 8 +- demo/src/main/res/layout/item_list.xml | 9 +- demo/src/main/res/layout/main.xml | 130 +- demo/src/main/res/values-v11/theme.xml | 28 +- demo/src/main/res/values/colors.xml | 40 +- demo/src/main/res/values/strings.xml | 8 +- demo/src/main/res/values/styles.xml | 46 +- demo/src/main/res/values/theme.xml | 80 +- library/src/main/AndroidManifest.xml | 7 +- .../lib/dialogs/BaseDialogBuilder.java | 682 +++---- .../lib/dialogs/BaseDialogFragment.java | 1594 ++++++++--------- .../lib/dialogs/DatePickerDialogFragment.java | 370 ++-- .../dialogs/IDateDialogCancelListener.java | 2 +- .../lib/dialogs/IDateDialogListener.java | 4 +- .../lib/dialogs/IListDialogListener.java | 1 + .../dialogs/ISimpleDialogCancelListener.java | 12 +- .../lib/dialogs/ISimpleDialogListener.java | 36 +- .../lib/dialogs/ListDialogFragment.java | 284 +-- .../lib/dialogs/ProgressDialogFragment.java | 140 +- .../lib/dialogs/SimpleDialogFragment.java | 315 ++-- .../lib/dialogs/TimePickerDialogFragment.java | 10 +- .../res/color/sdl_primary_text_holo_dark.xml | 12 +- .../res/color/sdl_primary_text_holo_light.xml | 12 +- .../main/res/layout/dialog_part_button.xml | 2 +- .../res/layout/dialog_part_button_panel.xml | 16 +- .../layout/dialog_part_button_separator.xml | 4 +- .../main/res/layout/dialog_part_custom.xml | 10 +- .../res/layout/dialog_part_datepicker.xml | 4 +- .../src/main/res/layout/dialog_part_list.xml | 4 +- .../main/res/layout/dialog_part_message.xml | 12 +- .../main/res/layout/dialog_part_progress.xml | 17 +- .../res/layout/dialog_part_timepicker.xml | 4 +- .../src/main/res/layout/dialog_part_title.xml | 21 +- .../src/main/res/values-v11/sdl__theme.xml | 16 +- library/src/main/res/values/sdl__attrs.xml | 36 +- library/src/main/res/values/sdl__colors.xml | 40 +- library/src/main/res/values/sdl__dimens.xml | 62 +- library/src/main/res/values/sdl__strings.xml | 2 +- library/src/main/res/values/sdl__theme.xml | 294 ++- 42 files changed, 2305 insertions(+), 2317 deletions(-) diff --git a/demo/src/main/AndroidManifest.xml b/demo/src/main/AndroidManifest.xml index f8b6c4d..2dd0a19 100644 --- a/demo/src/main/AndroidManifest.xml +++ b/demo/src/main/AndroidManifest.xml @@ -1,23 +1,23 @@ + package="eu.inmite.demo.dialogs" + android:versionCode="1" + android:versionName="1.0"> - - - - - - - - - + + + + + + + + + diff --git a/demo/src/main/java/eu/inmite/demo/dialogs/JayneHatDialogFragment.java b/demo/src/main/java/eu/inmite/demo/dialogs/JayneHatDialogFragment.java index f6140a5..9886082 100644 --- a/demo/src/main/java/eu/inmite/demo/dialogs/JayneHatDialogFragment.java +++ b/demo/src/main/java/eu/inmite/demo/dialogs/JayneHatDialogFragment.java @@ -30,26 +30,26 @@ */ public class JayneHatDialogFragment extends SimpleDialogFragment { - public static String TAG = "jayne"; + public static String TAG = "jayne"; - public static void show(FragmentActivity activity) { - new JayneHatDialogFragment().show(activity.getSupportFragmentManager(), TAG); - } + public static void show(FragmentActivity activity) { + new JayneHatDialogFragment().show(activity.getSupportFragmentManager(), TAG); + } - @Override - public BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) { - builder.setTitle("Jayne's hat"); - builder.setView(LayoutInflater.from(getActivity()).inflate(R.layout.item_jayne_hat, null)); - builder.setPositiveButton("I want one", new View.OnClickListener() { - @Override - public void onClick(View v) { - ISimpleDialogListener listener = getDialogListener(); - if (listener != null) { - listener.onPositiveButtonClicked(0); - } - dismiss(); - } - }); - return builder; - } + @Override + public BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) { + builder.setTitle("Jayne's hat"); + builder.setView(LayoutInflater.from(getActivity()).inflate(R.layout.item_jayne_hat, null)); + builder.setPositiveButton("I want one", new View.OnClickListener() { + @Override + public void onClick(View v) { + ISimpleDialogListener listener = getDialogListener(); + if (listener != null) { + listener.onPositiveButtonClicked(0); + } + dismiss(); + } + }); + return builder; + } } diff --git a/demo/src/main/java/eu/inmite/demo/dialogs/MyActivity.java b/demo/src/main/java/eu/inmite/demo/dialogs/MyActivity.java index a9531e6..9945365 100644 --- a/demo/src/main/java/eu/inmite/demo/dialogs/MyActivity.java +++ b/demo/src/main/java/eu/inmite/demo/dialogs/MyActivity.java @@ -16,30 +16,22 @@ package eu.inmite.demo.dialogs; +import java.text.DateFormat; +import java.util.Date; + import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.View; import android.widget.Toast; -import java.text.DateFormat; -import java.util.Date; - -import eu.inmite.android.lib.dialogs.DatePickerDialogFragment; -import eu.inmite.android.lib.dialogs.IDateDialogListener; -import eu.inmite.android.lib.dialogs.IListDialogListener; -import eu.inmite.android.lib.dialogs.ISimpleDialogCancelListener; -import eu.inmite.android.lib.dialogs.ISimpleDialogListener; -import eu.inmite.android.lib.dialogs.ListDialogFragment; -import eu.inmite.android.lib.dialogs.ProgressDialogFragment; -import eu.inmite.android.lib.dialogs.SimpleDialogFragment; -import eu.inmite.android.lib.dialogs.TimePickerDialogFragment; +import eu.inmite.android.lib.dialogs.*; public class MyActivity extends FragmentActivity implements - ISimpleDialogListener, - IDateDialogListener, - ISimpleDialogCancelListener, - IListDialogListener { + ISimpleDialogListener, + IDateDialogListener, + ISimpleDialogCancelListener, + IListDialogListener { public static final int THEME_DEFAULT_DARK = 0; @@ -64,48 +56,48 @@ public void onCreate(Bundle savedInstanceState) { @Override public void onClick(View v) { SimpleDialogFragment.createBuilder(c, getSupportFragmentManager()) - .setMessage(R.string.message_1).show(); + .setMessage(R.string.message_1).show(); } }); findViewById(R.id.message_title_dialog).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SimpleDialogFragment.createBuilder(c, getSupportFragmentManager()) - .setTitle(R.string.title).setMessage(R.string.message_2).show(); + .setTitle(R.string.title).setMessage(R.string.message_2).show(); } }); findViewById(R.id.message_title_buttons_dialog) - .setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - SimpleDialogFragment.createBuilder(c, getSupportFragmentManager()) - .setTitle(R.string.title) - .setMessage(R.string.message_3) - .setPositiveButtonText(R.string.positive_button) - .setNegativeButtonText(R.string.negative_button).setRequestCode(42) - .setTag("custom-tag") - .show(); - } - }); + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + SimpleDialogFragment.createBuilder(c, getSupportFragmentManager()) + .setTitle(R.string.title) + .setMessage(R.string.message_3) + .setPositiveButtonText(R.string.positive_button) + .setNegativeButtonText(R.string.negative_button).setRequestCode(42) + .setTag("custom-tag") + .show(); + } + }); findViewById(R.id.progress_dialog).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ProgressDialogFragment.createBuilder(c, getSupportFragmentManager()) - .setMessage(R.string.message_4) - .setRequestCode(REQUEST_PROGRESS) - .setTitle(R.string.app_name) - .show(); + .setMessage(R.string.message_4) + .setRequestCode(REQUEST_PROGRESS) + .setTitle(R.string.app_name) + .show(); } }); findViewById(R.id.list_dialog).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ListDialogFragment - .createBuilder(c, getSupportFragmentManager()) - .setTitle("Your favorite character:") - .setItems(new String[]{"Jayne", "Malcolm", "Kaylee", - "Wash", "Zoe", "River"}) - .show(); + .createBuilder(c, getSupportFragmentManager()) + .setTitle("Your favorite character:") + .setItems(new String[]{"Jayne", "Malcolm", "Kaylee", + "Wash", "Zoe", "River"}) + .show(); } }); @@ -139,32 +131,32 @@ public void onClick(View v) { setCurrentTheme(THEME_CUSTOM_LIGHT); } }); - findViewById(R.id.time_picker).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - TimePickerDialogFragment - .createBuilder(MyActivity.this, getSupportFragmentManager()) - .setDate(new Date()) - .set24hour(true) - .setPositiveButtonText(android.R.string.ok) - .setNegativeButtonText(android.R.string.cancel) - .setRequestCode(13) - .show(); - } - }); - findViewById(R.id.date_picker).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - DatePickerDialogFragment - .createBuilder(MyActivity.this, getSupportFragmentManager()) - .setDate(new Date()) - .set24hour(true) - .setPositiveButtonText(android.R.string.ok) - .setNegativeButtonText(android.R.string.cancel) - .setRequestCode(12) - .show(); - } - }); + findViewById(R.id.time_picker).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + TimePickerDialogFragment + .createBuilder(MyActivity.this, getSupportFragmentManager()) + .setDate(new Date()) + .set24hour(true) + .setPositiveButtonText(android.R.string.ok) + .setNegativeButtonText(android.R.string.cancel) + .setRequestCode(13) + .show(); + } + }); + findViewById(R.id.date_picker).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + DatePickerDialogFragment + .createBuilder(MyActivity.this, getSupportFragmentManager()) + .setDate(new Date()) + .set24hour(true) + .setPositiveButtonText(android.R.string.ok) + .setNegativeButtonText(android.R.string.cancel) + .setRequestCode(12) + .show(); + } + }); } // IListDialogListener @@ -216,30 +208,30 @@ public void onNeutralButtonClicked(int requestCode) { // IDateDialogListener @Override - public void onNegativeButtonClicked(int resultCode, Date date) { - String text=""; - if (resultCode==12) { - text="Date "; - } else if (resultCode==13) { - text="Time "; - } - - DateFormat dateFormat= DateFormat.getDateInstance(DateFormat.DEFAULT); - Toast.makeText(this,text+"Cancelled "+ dateFormat.format(date),Toast.LENGTH_SHORT).show(); - } - - @Override - public void onPositiveButtonClicked(int resultCode, Date date) { - String text=""; - if (resultCode==12) { - text="Date "; - } else if (resultCode==13) { - text="Time "; - } - - DateFormat dateFormat= DateFormat.getDateTimeInstance(); - Toast.makeText(this,text+ "Success! "+ dateFormat.format(date),Toast.LENGTH_SHORT).show(); - } + public void onNegativeButtonClicked(int resultCode, Date date) { + String text = ""; + if (resultCode == 12) { + text = "Date "; + } else if (resultCode == 13) { + text = "Time "; + } + + DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT); + Toast.makeText(this, text + "Cancelled " + dateFormat.format(date), Toast.LENGTH_SHORT).show(); + } + + @Override + public void onPositiveButtonClicked(int resultCode, Date date) { + String text = ""; + if (resultCode == 12) { + text = "Date "; + } else if (resultCode == 13) { + text = "Time "; + } + + DateFormat dateFormat = DateFormat.getDateTimeInstance(); + Toast.makeText(this, text + "Success! " + dateFormat.format(date), Toast.LENGTH_SHORT).show(); + } private void setCurrentTheme(int theme) { Intent i = new Intent(c, MyActivity.class); diff --git a/demo/src/main/res/layout/item_jayne_hat.xml b/demo/src/main/res/layout/item_jayne_hat.xml index e707e29..a24f340 100644 --- a/demo/src/main/res/layout/item_jayne_hat.xml +++ b/demo/src/main/res/layout/item_jayne_hat.xml @@ -1,6 +1,6 @@ \ No newline at end of file + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:src="@drawable/jayne_hat" + android:padding="@dimen/grid_2" /> \ No newline at end of file diff --git a/demo/src/main/res/layout/item_list.xml b/demo/src/main/res/layout/item_list.xml index 83ded4a..937f72b 100644 --- a/demo/src/main/res/layout/item_list.xml +++ b/demo/src/main/res/layout/item_list.xml @@ -1,7 +1,8 @@ - + style="@style/ListItem"> + + \ No newline at end of file diff --git a/demo/src/main/res/layout/main.xml b/demo/src/main/res/layout/main.xml index e81d0d5..8cc7efe 100644 --- a/demo/src/main/res/layout/main.xml +++ b/demo/src/main/res/layout/main.xml @@ -1,81 +1,81 @@ + android:layout_width="match_parent" + android:layout_height="match_parent"> - + - + -