From fa1f79f66715c2a9603c43c9a00ac751084b7ff9 Mon Sep 17 00:00:00 2001 From: barondem Date: Sun, 27 Jul 2014 18:18:52 +0200 Subject: [PATCH 1/3] Added methods to use custom dialog theme and divider color and tweaked license text parsing --- .../psdev/licensesdialog/LicensesDialog.java | 149 +++++++++++++++--- .../LicensesDialogFragment.java | 73 ++++++++- .../licensesdialog/licenses/License.java | 2 +- 3 files changed, 201 insertions(+), 23 deletions(-) diff --git a/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java b/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java index a57ddd1..0a5b054 100644 --- a/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java +++ b/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java @@ -23,6 +23,8 @@ import android.content.Context; import android.content.DialogInterface; import android.content.res.Resources; +import android.view.ContextThemeWrapper; +import android.view.View; import android.webkit.WebView; import de.psdev.licensesdialog.licenses.ApacheSoftwareLicense20; import de.psdev.licensesdialog.model.Notice; @@ -40,9 +42,12 @@ public class LicensesDialog { // private DialogInterface.OnDismissListener mOnDismissListener; + private int mThemeResourceId; + private int mDividerColor; public LicensesDialog(final Context context, final int titleResourceId, final int rawNoticesResourceId, final int closeResourceId, - final boolean showFullLicenseText, final boolean includeOwnLicense) { + final boolean showFullLicenseText, final boolean includeOwnLicense, + final int themeResourceId, final int dividerColor) { mContext = context; // Load defaults final String style = context.getString(R.string.notices_default_style); @@ -64,6 +69,60 @@ public LicensesDialog(final Context context, final int titleResourceId, final in throw new IllegalStateException(e); } mCloseText = context.getString(closeResourceId); + mThemeResourceId = themeResourceId; + mDividerColor = dividerColor; + } + + public LicensesDialog(final Context context, final int titleResourceId, final int rawNoticesResourceId, final int closeResourceId, + final boolean showFullLicenseText, final boolean includeOwnLicense, + final int themeResourceId) { + mContext = context; + // Load defaults + final String style = context.getString(R.string.notices_default_style); + mTitleText = context.getString(titleResourceId); + try { + final Resources resources = context.getResources(); + if ("raw".equals(resources.getResourceTypeName(rawNoticesResourceId))) { + final Notices notices = NoticesXmlParser.parse(resources.openRawResource(rawNoticesResourceId)); + if (includeOwnLicense) { + final List noticeList = notices.getNotices(); + noticeList.add(LICENSES_DIALOG_NOTICE); + } + mLicensesText = NoticesHtmlBuilder.create(mContext).setShowFullLicenseText(showFullLicenseText).setNotices(notices).setStyle(style) + .build(); + } else { + throw new IllegalStateException("not a raw resource"); + } + } catch (final Exception e) { + throw new IllegalStateException(e); + } + mCloseText = context.getString(closeResourceId); + mThemeResourceId = themeResourceId; + } + + public LicensesDialog(final Context context, final int titleResourceId, final int rawNoticesResourceId, final int closeResourceId, + final boolean showFullLicenseText, final boolean includeOwnLicense) { + mContext = context; + // Load defaults + final String style = context.getString(R.string.notices_default_style); + mTitleText = context.getString(titleResourceId); + try { + final Resources resources = context.getResources(); + if ("raw".equals(resources.getResourceTypeName(rawNoticesResourceId))) { + final Notices notices = NoticesXmlParser.parse(resources.openRawResource(rawNoticesResourceId)); + if (includeOwnLicense) { + final List noticeList = notices.getNotices(); + noticeList.add(LICENSES_DIALOG_NOTICE); + } + mLicensesText = NoticesHtmlBuilder.create(mContext).setShowFullLicenseText(showFullLicenseText).setNotices(notices).setStyle(style) + .build(); + } else { + throw new IllegalStateException("not a raw resource"); + } + } catch (final Exception e) { + throw new IllegalStateException(e); + } + mCloseText = context.getString(closeResourceId); } public LicensesDialog(final Context context, final int rawNoticesResourceId, final boolean showFullLicenseText, final boolean includeOwnLicense) { @@ -95,6 +154,25 @@ public LicensesDialog(final Context context, final String titleText, final Strin mCloseText = closeText; } + public LicensesDialog(final Context context, final String titleText, final String licensesText, final String closeText, + final int themeResourceId) { + mContext = context; + mTitleText = titleText; + mLicensesText = licensesText; + mCloseText = closeText; + mThemeResourceId = themeResourceId; + } + + public LicensesDialog(final Context context, final String titleText, final String licensesText, final String closeText, + final int themeResourceId, final int dividerColor) { + mContext = context; + mTitleText = titleText; + mLicensesText = licensesText; + mCloseText = closeText; + mThemeResourceId = themeResourceId; + mDividerColor = dividerColor; + } + public LicensesDialog setOnDismissListener(final DialogInterface.OnDismissListener onDismissListener) { mOnDismissListener = onDismissListener; return this; @@ -104,30 +182,59 @@ public Dialog create() { //Get resources final WebView webView = new WebView(mContext); webView.loadDataWithBaseURL(null, mLicensesText, "text/html", "utf-8", null); - final AlertDialog.Builder builder = new AlertDialog.Builder(mContext) - .setTitle(mTitleText) - .setView(webView) - .setPositiveButton(mCloseText, new Dialog.OnClickListener() { - public void onClick(final DialogInterface dialogInterface, final int i) { - dialogInterface.dismiss(); - } - }); - final AlertDialog dialog = builder.create(); - dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { - @Override - public void onDismiss(final DialogInterface dialog) { - if (mOnDismissListener != null) { - mOnDismissListener.onDismiss(dialog); - } - } - }); - return dialog; - } + final AlertDialog.Builder builder; + if (mThemeResourceId != 0) { + builder = new AlertDialog.Builder(new ContextThemeWrapper(mContext, mThemeResourceId)); + } else { + builder = new AlertDialog.Builder(mContext); + } + builder.setTitle(mTitleText) + .setView(webView) + .setPositiveButton(mCloseText, new Dialog.OnClickListener() { + public void onClick(final DialogInterface dialogInterface, final int i) { + dialogInterface.dismiss(); + } + }); + final AlertDialog dialog = builder.create(); + dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(final DialogInterface dialog) { + if (mOnDismissListener != null) { + mOnDismissListener.onDismiss(dialog); + } + } + }); + return dialog; + } public void show() { - create().show(); + if (mDividerColor != 0) { + Dialog dialog = create(); + dialog.show(); + // Set title divider color + int titleDividerId = mContext.getResources().getIdentifier("titleDivider", "id", "android"); + View titleDivider = dialog.findViewById(titleDividerId); + if (titleDivider != null) { + titleDivider.setBackgroundColor(mDividerColor); + } + } } + public Dialog createAndShow() { + if (mDividerColor != 0) { + Dialog dialog = create(); + dialog.show(); + // Set title divider color + int titleDividerId = mContext.getResources().getIdentifier("titleDivider", "id", "android"); + View titleDivider = dialog.findViewById(titleDividerId); + if (titleDivider != null) { + titleDivider.setBackgroundColor(mDividerColor); + } + return dialog; + } + return create(); + } + // diff --git a/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java b/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java index 3ac4f65..6db2505 100644 --- a/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java +++ b/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java @@ -19,6 +19,7 @@ import android.app.Dialog; import android.content.DialogInterface; import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.support.v4.app.DialogFragment; import de.psdev.licensesdialog.model.Notices; @@ -29,14 +30,20 @@ public class LicensesDialogFragment extends DialogFragment { private static final String ARGUMENT_NOTICES_XML_ID = "ARGUMENT_NOTICES_XML_ID"; private static final String ARGUMENT_INCLUDE_OWN_LICENSE = "ARGUMENT_INCLUDE_OWN_LICENSE"; private static final String ARGUMENT_FULL_LICENSE_TEXT = "ARGUMENT_FULL_LICENSE_TEXT"; + private static final String ARGUMENT_THEME_XML_ID = "ARGUMENT_THEME_XML_ID"; + private static final String ARGUMENT_DIVIDER_COLOR = "ARGUMENT_DIVIDER_COLOR"; private static final String STATE_TITLE_TEXT = "title_text"; private static final String STATE_LICENSES_TEXT = "licenses_text"; private static final String STATE_CLOSE_TEXT = "close_text"; + private static final String STATE_THEME_XML_ID = "theme_xml_id"; + private static final String STATE_DIVIDER_COLOR = "divider_color"; // private String mTitleText; private String mCloseButtonText; private String mLicensesText; + private int mThemeResourceId; + private int mDividerColor; private DialogInterface.OnDismissListener mOnDismissListener; @@ -55,6 +62,33 @@ public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, return licensesDialogFragment; } + public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, + final boolean showFullLicenseText, final boolean includeOwnLicense, + final int themeResourceId) { + final LicensesDialogFragment licensesDialogFragment = new LicensesDialogFragment(); + final Bundle args = new Bundle(); + args.putInt(ARGUMENT_NOTICES_XML_ID, rawNoticesResourceId); + args.putBoolean(ARGUMENT_FULL_LICENSE_TEXT, showFullLicenseText); + args.putBoolean(ARGUMENT_INCLUDE_OWN_LICENSE, includeOwnLicense); + args.putInt(ARGUMENT_THEME_XML_ID, themeResourceId); + licensesDialogFragment.setArguments(args); + return licensesDialogFragment; + } + + public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, + final boolean showFullLicenseText, final boolean includeOwnLicense, + final int themeResourceId, final int dividerColor) { + final LicensesDialogFragment licensesDialogFragment = new LicensesDialogFragment(); + final Bundle args = new Bundle(); + args.putInt(ARGUMENT_NOTICES_XML_ID, rawNoticesResourceId); + args.putBoolean(ARGUMENT_FULL_LICENSE_TEXT, showFullLicenseText); + args.putBoolean(ARGUMENT_INCLUDE_OWN_LICENSE, includeOwnLicense); + args.putInt(ARGUMENT_THEME_XML_ID, themeResourceId); + args.putInt(ARGUMENT_DIVIDER_COLOR, dividerColor); + licensesDialogFragment.setArguments(args); + return licensesDialogFragment; + } + public static LicensesDialogFragment newInstance(final Notices notices, final boolean showFullLicenseText, final boolean includeOwnLicense) { final LicensesDialogFragment licensesDialogFragment = new LicensesDialogFragment(); final Bundle args = new Bundle(); @@ -77,6 +111,12 @@ public void onCreate(final Bundle savedInstanceState) { mTitleText = savedInstanceState.getString(STATE_TITLE_TEXT); mLicensesText = savedInstanceState.getString(STATE_LICENSES_TEXT); mCloseButtonText = savedInstanceState.getString(STATE_CLOSE_TEXT); + if (savedInstanceState.containsKey(STATE_THEME_XML_ID)) { + mThemeResourceId = savedInstanceState.getInt(STATE_THEME_XML_ID); + } + if (savedInstanceState.containsKey(STATE_DIVIDER_COLOR)) { + mDividerColor = savedInstanceState.getInt(STATE_DIVIDER_COLOR); + } } else { mTitleText = resources.getString(R.string.notices_title); mCloseButtonText = resources.getString(R.string.notices_close); @@ -95,6 +135,20 @@ public void onCreate(final Bundle savedInstanceState) { notices.getNotices().add(LicensesDialog.LICENSES_DIALOG_NOTICE); } final boolean showFullLicenseText = arguments.getBoolean(ARGUMENT_FULL_LICENSE_TEXT, false); + if (arguments.containsKey(ARGUMENT_THEME_XML_ID)) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + mThemeResourceId = arguments.getInt(ARGUMENT_THEME_XML_ID, android.R.style.Theme_DeviceDefault_Light_Dialog); + } else { + mThemeResourceId = arguments.getInt(ARGUMENT_THEME_XML_ID); + } + } + if (arguments.containsKey(ARGUMENT_DIVIDER_COLOR)) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + mDividerColor = arguments.getInt(ARGUMENT_DIVIDER_COLOR, android.R.color.holo_blue_light); + } else { + mDividerColor = arguments.getInt(ARGUMENT_DIVIDER_COLOR); + } + } mLicensesText = NoticesHtmlBuilder.create(getActivity()).setNotices(notices).setShowFullLicenseText(showFullLicenseText).build(); } else { throw new IllegalStateException("Missing arguments"); @@ -111,11 +165,28 @@ public void onSaveInstanceState(final Bundle outState) { outState.putString(STATE_TITLE_TEXT, mTitleText); outState.putString(STATE_LICENSES_TEXT, mLicensesText); outState.putString(STATE_CLOSE_TEXT, mCloseButtonText); + if (mThemeResourceId != 0) { + outState.putInt(STATE_THEME_XML_ID, mThemeResourceId); + } + if (mDividerColor != 0) { + outState.putInt(STATE_DIVIDER_COLOR, mDividerColor); + } } @Override public Dialog onCreateDialog(final Bundle savedInstanceState) { - return new LicensesDialog(getActivity(), mTitleText, mLicensesText, mCloseButtonText).create(); + if (mThemeResourceId != 0) { + if (mDividerColor != 0) { + return new LicensesDialog(getActivity(), mTitleText, mLicensesText, + mCloseButtonText, mThemeResourceId, mDividerColor).createAndShow(); + } else { + return new LicensesDialog(getActivity(), mTitleText, mLicensesText, + mCloseButtonText, mThemeResourceId).create(); + } + } else { + return new LicensesDialog(getActivity(), mTitleText, mLicensesText, + mCloseButtonText).create(); + } } @Override diff --git a/library/src/main/java/de/psdev/licensesdialog/licenses/License.java b/library/src/main/java/de/psdev/licensesdialog/licenses/License.java index 15726a9..f24b728 100644 --- a/library/src/main/java/de/psdev/licensesdialog/licenses/License.java +++ b/library/src/main/java/de/psdev/licensesdialog/licenses/License.java @@ -66,7 +66,7 @@ private String toString(final BufferedReader reader) throws IOException { final StringBuilder builder = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { - builder.append(line); + builder.append(line).append('\n'); } return builder.toString(); } From b00aec9531e863488a89a06d6c5cabb79d86e373 Mon Sep 17 00:00:00 2001 From: barondem Date: Thu, 14 Aug 2014 17:41:02 +0200 Subject: [PATCH 2/3] Fixed bug in show() and createAndShow() methods and optimized code --- .../psdev/licensesdialog/LicensesDialog.java | 79 ++++--------------- .../LicensesDialogFragment.java | 21 +---- 2 files changed, 19 insertions(+), 81 deletions(-) diff --git a/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java b/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java index 0a5b054..be178a2 100644 --- a/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java +++ b/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java @@ -76,57 +76,18 @@ public LicensesDialog(final Context context, final int titleResourceId, final in public LicensesDialog(final Context context, final int titleResourceId, final int rawNoticesResourceId, final int closeResourceId, final boolean showFullLicenseText, final boolean includeOwnLicense, final int themeResourceId) { - mContext = context; - // Load defaults - final String style = context.getString(R.string.notices_default_style); - mTitleText = context.getString(titleResourceId); - try { - final Resources resources = context.getResources(); - if ("raw".equals(resources.getResourceTypeName(rawNoticesResourceId))) { - final Notices notices = NoticesXmlParser.parse(resources.openRawResource(rawNoticesResourceId)); - if (includeOwnLicense) { - final List noticeList = notices.getNotices(); - noticeList.add(LICENSES_DIALOG_NOTICE); - } - mLicensesText = NoticesHtmlBuilder.create(mContext).setShowFullLicenseText(showFullLicenseText).setNotices(notices).setStyle(style) - .build(); - } else { - throw new IllegalStateException("not a raw resource"); - } - } catch (final Exception e) { - throw new IllegalStateException(e); - } - mCloseText = context.getString(closeResourceId); - mThemeResourceId = themeResourceId; + this(context, titleResourceId, rawNoticesResourceId, closeResourceId, showFullLicenseText, + includeOwnLicense, themeResourceId, 0); } public LicensesDialog(final Context context, final int titleResourceId, final int rawNoticesResourceId, final int closeResourceId, final boolean showFullLicenseText, final boolean includeOwnLicense) { - mContext = context; - // Load defaults - final String style = context.getString(R.string.notices_default_style); - mTitleText = context.getString(titleResourceId); - try { - final Resources resources = context.getResources(); - if ("raw".equals(resources.getResourceTypeName(rawNoticesResourceId))) { - final Notices notices = NoticesXmlParser.parse(resources.openRawResource(rawNoticesResourceId)); - if (includeOwnLicense) { - final List noticeList = notices.getNotices(); - noticeList.add(LICENSES_DIALOG_NOTICE); - } - mLicensesText = NoticesHtmlBuilder.create(mContext).setShowFullLicenseText(showFullLicenseText).setNotices(notices).setStyle(style) - .build(); - } else { - throw new IllegalStateException("not a raw resource"); - } - } catch (final Exception e) { - throw new IllegalStateException(e); - } - mCloseText = context.getString(closeResourceId); + this(context, titleResourceId, rawNoticesResourceId, closeResourceId, showFullLicenseText, + includeOwnLicense, 0, 0); } public LicensesDialog(final Context context, final int rawNoticesResourceId, final boolean showFullLicenseText, final boolean includeOwnLicense) { - this(context, R.string.notices_title, rawNoticesResourceId, R.string.notices_close, showFullLicenseText, includeOwnLicense); + this(context, R.string.notices_title, rawNoticesResourceId, R.string.notices_close, showFullLicenseText, includeOwnLicense, 0, 0); } public LicensesDialog(final Context context, final Notices notices, final boolean showFullLicenseText, final boolean includeOwnLicense) { @@ -154,7 +115,7 @@ public LicensesDialog(final Context context, final String titleText, final Strin mCloseText = closeText; } - public LicensesDialog(final Context context, final String titleText, final String licensesText, final String closeText, + public LicensesDialog(final Context context, final String titleText, final String licensesText, final String closeText, final int themeResourceId) { mContext = context; mTitleText = titleText; @@ -173,11 +134,6 @@ public LicensesDialog(final Context context, final String titleText, final Strin mDividerColor = dividerColor; } - public LicensesDialog setOnDismissListener(final DialogInterface.OnDismissListener onDismissListener) { - mOnDismissListener = onDismissListener; - return this; - } - public Dialog create() { //Get resources final WebView webView = new WebView(mContext); @@ -208,9 +164,13 @@ public void onDismiss(final DialogInterface dialog) { } public void show() { + Dialog dialog = create(); + show(dialog); + } + + public void show(Dialog dialog) { + dialog.show(); if (mDividerColor != 0) { - Dialog dialog = create(); - dialog.show(); // Set title divider color int titleDividerId = mContext.getResources().getIdentifier("titleDivider", "id", "android"); View titleDivider = dialog.findViewById(titleDividerId); @@ -221,18 +181,9 @@ public void show() { } public Dialog createAndShow() { - if (mDividerColor != 0) { - Dialog dialog = create(); - dialog.show(); - // Set title divider color - int titleDividerId = mContext.getResources().getIdentifier("titleDivider", "id", "android"); - View titleDivider = dialog.findViewById(titleDividerId); - if (titleDivider != null) { - titleDivider.setBackgroundColor(mDividerColor); - } - return dialog; - } - return create(); + Dialog dialog = create(); + show(dialog); + return dialog; } // diff --git a/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java b/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java index 6db2505..efb24c6 100644 --- a/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java +++ b/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java @@ -48,31 +48,18 @@ public class LicensesDialogFragment extends DialogFragment { private DialogInterface.OnDismissListener mOnDismissListener; public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, final boolean includeOwnLicense) { - return newInstance(rawNoticesResourceId, false, includeOwnLicense); + return newInstance(rawNoticesResourceId, false, includeOwnLicense, 0, 0); } public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, final boolean showFullLicenseText, final boolean includeOwnLicense) { - final LicensesDialogFragment licensesDialogFragment = new LicensesDialogFragment(); - final Bundle args = new Bundle(); - args.putInt(ARGUMENT_NOTICES_XML_ID, rawNoticesResourceId); - args.putBoolean(ARGUMENT_FULL_LICENSE_TEXT, showFullLicenseText); - args.putBoolean(ARGUMENT_INCLUDE_OWN_LICENSE, includeOwnLicense); - licensesDialogFragment.setArguments(args); - return licensesDialogFragment; + return newInstance(rawNoticesResourceId, showFullLicenseText, includeOwnLicense, 0, 0); } - public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, + public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, final boolean showFullLicenseText, final boolean includeOwnLicense, final int themeResourceId) { - final LicensesDialogFragment licensesDialogFragment = new LicensesDialogFragment(); - final Bundle args = new Bundle(); - args.putInt(ARGUMENT_NOTICES_XML_ID, rawNoticesResourceId); - args.putBoolean(ARGUMENT_FULL_LICENSE_TEXT, showFullLicenseText); - args.putBoolean(ARGUMENT_INCLUDE_OWN_LICENSE, includeOwnLicense); - args.putInt(ARGUMENT_THEME_XML_ID, themeResourceId); - licensesDialogFragment.setArguments(args); - return licensesDialogFragment; + return newInstance(rawNoticesResourceId, showFullLicenseText, includeOwnLicense, themeResourceId, 0); } public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, From ff5c9c26b35216d6b2a55e1b509ff16ecdfbea23 Mon Sep 17 00:00:00 2001 From: barondem Date: Thu, 14 Aug 2014 17:48:22 +0200 Subject: [PATCH 3/3] Readded wrongly removed code --- .../main/java/de/psdev/licensesdialog/LicensesDialog.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java b/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java index be178a2..48e8534 100644 --- a/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java +++ b/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java @@ -134,6 +134,11 @@ public LicensesDialog(final Context context, final String titleText, final Strin mDividerColor = dividerColor; } + public LicensesDialog setOnDismissListener(final DialogInterface.OnDismissListener onDismissListener) { + mOnDismissListener = onDismissListener; + return this; + } + public Dialog create() { //Get resources final WebView webView = new WebView(mContext);