From 93333b8dab9f5a78f93c908028f0c55e886622e4 Mon Sep 17 00:00:00 2001 From: Philip Schiffer Date: Mon, 8 Jun 2015 10:37:47 +0200 Subject: [PATCH] Support Appcompat and alert dialogs in the support library - resolves #42 --- library/AndroidManifest.xml | 6 +- library/pom.xml | 8 +- library/project.properties | 12 - .../psdev/licensesdialog/LicensesDialog.java | 63 +++++- .../LicensesDialogFragment.java | 205 +++++++++++++----- .../SingleLicenseDialogFragment.java | 131 ----------- pom.xml | 28 ++- sample/AndroidManifest.xml | 28 ++- sample/pom.xml | 12 +- sample/project.properties | 12 - sample/res/layout/activity_main.xml | 23 ++ .../sample/AppCompatSampleActivity.java | 142 ++++++++++++ .../licensesdialog/sample/MainActivity.java | 31 +++ .../licensesdialog/sample/SampleActivity.java | 81 +++++-- 14 files changed, 525 insertions(+), 257 deletions(-) delete mode 100644 library/project.properties delete mode 100644 library/src/main/java/de/psdev/licensesdialog/SingleLicenseDialogFragment.java delete mode 100644 sample/project.properties create mode 100644 sample/res/layout/activity_main.xml create mode 100644 sample/src/main/java/de/psdev/licensesdialog/sample/AppCompatSampleActivity.java create mode 100644 sample/src/main/java/de/psdev/licensesdialog/sample/MainActivity.java diff --git a/library/AndroidManifest.xml b/library/AndroidManifest.xml index d20b8e0..26fd06e 100644 --- a/library/AndroidManifest.xml +++ b/library/AndroidManifest.xml @@ -16,12 +16,12 @@ --> + android:targetSdkVersion="22" /> \ No newline at end of file diff --git a/library/pom.xml b/library/pom.xml index ac54498..4d22368 100644 --- a/library/pom.xml +++ b/library/pom.xml @@ -4,7 +4,7 @@ de.psdev.licensesdialog parent - 1.7.1-SNAPSHOT + 1.8.0-SNAPSHOT licensesdialog @@ -36,6 +36,12 @@ support-v4 aar + + com.android.support + appcompat-v7 + aar + true + diff --git a/library/project.properties b/library/project.properties deleted file mode 100644 index 616f300..0000000 --- a/library/project.properties +++ /dev/null @@ -1,12 +0,0 @@ -# This file is automatically generated by Android Tools. -# Do not modify this file -- YOUR CHANGES WILL BE ERASED! -# -# This file must be checked in Version Control Systems. -# -# To customize properties used by the Ant build system use, -# "ant.properties", and override values to adapt the script to your -# project structure. - -android.library=true -# Project target. -target=android-16 diff --git a/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java b/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java index 7cdc266..c1aa179 100644 --- a/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java +++ b/library/src/main/java/de/psdev/licensesdialog/LicensesDialog.java @@ -43,9 +43,12 @@ public class LicensesDialog { private final int mThemeResourceId; private final int mDividerColor; - // private DialogInterface.OnDismissListener mOnDismissListener; + // ========================================================================================================================== + // Constructor + // ========================================================================================================================== + private LicensesDialog(final Context context, final String licensesText, final String titleText, final String closeText, final int themeResourceId, final int dividerColor) { mContext = context; @@ -56,6 +59,10 @@ private LicensesDialog(final Context context, final String licensesText, final S mDividerColor = dividerColor; } + // ========================================================================================================================== + // Public API + // ========================================================================================================================== + public LicensesDialog setOnDismissListener(final DialogInterface.OnDismissListener onDismissListener) { mOnDismissListener = onDismissListener; return this; @@ -103,13 +110,63 @@ public void onShow(final DialogInterface dialogInterface) { return dialog; } + public Dialog createAppCompat() { + //Get resources + final WebView webView = new WebView(mContext); + webView.loadDataWithBaseURL(null, mLicensesText, "text/html", "utf-8", null); + final android.support.v7.app.AlertDialog.Builder builder; + if (mThemeResourceId != 0) { + builder = new android.support.v7.app.AlertDialog.Builder(new ContextThemeWrapper(mContext, mThemeResourceId)); + } else { + builder = new android.support.v7.app.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 android.support.v7.app.AlertDialog dialog = builder.create(); + dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(final DialogInterface dialog) { + if (mOnDismissListener != null) { + mOnDismissListener.onDismiss(dialog); + } + } + }); + dialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(final DialogInterface dialogInterface) { + if (mDividerColor != 0) { + // Set title divider color + final int titleDividerId = mContext.getResources().getIdentifier("titleDivider", "id", "android"); + final View titleDivider = dialog.findViewById(titleDividerId); + if (titleDivider != null) { + titleDivider.setBackgroundColor(mDividerColor); + } + } + } + }); + return dialog; + } + public Dialog show() { final Dialog dialog = create(); dialog.show(); return dialog; } - // + public Dialog showAppCompat() { + final Dialog dialog = createAppCompat(); + dialog.show(); + return dialog; + } + + // ========================================================================================================================== + // Private API + // ========================================================================================================================== private static Notices getNotices(final Context context, final int rawNoticesResourceId) { try { @@ -144,7 +201,9 @@ private static Notices getSingleNoticeNotices(final Notice notice) { return notices; } + // ========================================================================================================================== // Inner classes + // ========================================================================================================================== public static final class Builder { diff --git a/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java b/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java index f538db4..6a22144 100644 --- a/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java +++ b/library/src/main/java/de/psdev/licensesdialog/LicensesDialogFragment.java @@ -22,7 +22,13 @@ import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.support.annotation.ColorInt; +import android.support.annotation.ColorRes; +import android.support.annotation.NonNull; +import android.support.annotation.RawRes; +import android.support.annotation.StyleRes; import android.support.v4.app.DialogFragment; +import de.psdev.licensesdialog.model.Notice; import de.psdev.licensesdialog.model.Notices; public class LicensesDialogFragment extends DialogFragment { @@ -33,6 +39,7 @@ public class LicensesDialogFragment extends DialogFragment { 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 ARGUMENT_USE_APPCOMPAT = "ARGUMENT_USE_APPCOMPAT"; 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"; @@ -48,76 +55,57 @@ public class LicensesDialogFragment extends DialogFragment { private DialogInterface.OnDismissListener mOnDismissListener; - public static LicensesDialogFragment newInstance(final int rawNoticesResourceId) { - return newInstance(rawNoticesResourceId, false); - } - - public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, final boolean showFullLicenseText) { - return newInstance(rawNoticesResourceId, showFullLicenseText, false); - } - - public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, final boolean showFullLicenseText, final boolean includeOwnLicense) { - return newInstance(rawNoticesResourceId, showFullLicenseText, includeOwnLicense, 0); - } - - public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, final boolean showFullLicenseText, final boolean includeOwnLicense, - final int themeResourceId) { - return newInstance(rawNoticesResourceId, showFullLicenseText, includeOwnLicense, themeResourceId, 0); - } - - public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, final boolean showFullLicenseText, final boolean includeOwnLicense, - final int themeResourceId, final int dividerColor) { - return newInstance(null, rawNoticesResourceId, showFullLicenseText, includeOwnLicense, themeResourceId, dividerColor); - } - - public static LicensesDialogFragment newInstance(final int rawNoticesResourceId, final boolean showFullLicenseText, final boolean includeOwnLicense, - final int themeResourceId, final int dividerColorId, final Context context) { - return newInstance(null, rawNoticesResourceId, showFullLicenseText, includeOwnLicense, themeResourceId, getColor(dividerColorId, context)); - } - - public static LicensesDialogFragment newInstance(final Notices notices, final boolean showFullLicenseText, final boolean includeOwnLicense) { - return newInstance(notices, showFullLicenseText, includeOwnLicense, 0); - } - - public static LicensesDialogFragment newInstance(final Notices notices, final boolean showFullLicenseText, final boolean includeOwnLicense, - final int themeResourceId) { - return newInstance(notices, showFullLicenseText, includeOwnLicense, themeResourceId, 0); - } - - public static LicensesDialogFragment newInstance(final Notices notices, final boolean showFullLicenseText, final boolean includeOwnLicense, - final int themeResourceId, final int dividerColor) { - return newInstance(notices, -1, showFullLicenseText, includeOwnLicense, themeResourceId, dividerColor); - } + // ========================================================================================================================== + // Factory + // ========================================================================================================================== - public static LicensesDialogFragment newInstance(final Notices notices, final boolean showFullLicenseText, final boolean includeOwnLicense, - final int themeResourceId, final int dividerColorId, final Context context) { - return newInstance(notices, -1, showFullLicenseText, includeOwnLicense, themeResourceId, getColor(dividerColorId, context)); + private static LicensesDialogFragment newInstance(final Notices notices, + final boolean showFullLicenseText, + final boolean includeOwnLicense, + final int themeResourceId, + final int dividerColor, + final boolean useAppCompat) { + final LicensesDialogFragment licensesDialogFragment = new LicensesDialogFragment(); + final Bundle args = new Bundle(); + args.putParcelable(ARGUMENT_NOTICES, notices); + 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); + args.putBoolean(ARGUMENT_USE_APPCOMPAT, useAppCompat); + licensesDialogFragment.setArguments(args); + return licensesDialogFragment; } - private static LicensesDialogFragment newInstance(final Notices notices, final int rawNoticesResourceId, final boolean showFullLicenseText, - final boolean includeOwnLicense, final int themeResourceId, final int dividerColor) { + private static LicensesDialogFragment newInstance(final int rawNoticesResourceId, + final boolean showFullLicenseText, + final boolean includeOwnLicense, + final int themeResourceId, + final int dividerColor, + final boolean useAppCompat) { final LicensesDialogFragment licensesDialogFragment = new LicensesDialogFragment(); final Bundle args = new Bundle(); - if (notices != null) { - args.putParcelable(ARGUMENT_NOTICES, notices); - } else { - args.putInt(ARGUMENT_NOTICES_XML_ID, rawNoticesResourceId); - } + 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); + args.putBoolean(ARGUMENT_USE_APPCOMPAT, useAppCompat); licensesDialogFragment.setArguments(args); return licensesDialogFragment; } - private static int getColor(final int dividerColorId, final Context context) { - return context.getResources().getColor(dividerColorId); - } + // ========================================================================================================================== + // Constructor + // ========================================================================================================================== public LicensesDialogFragment() { } + // ========================================================================================================================== + // Android Lifecycle + // ========================================================================================================================== + @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -191,11 +179,16 @@ public void onSaveInstanceState(final Bundle outState) { @Override public Dialog onCreateDialog(final Bundle savedInstanceState) { - return new LicensesDialog.Builder(getActivity()) + final LicensesDialog.Builder builder = new LicensesDialog.Builder(getActivity()) .setNotices(mLicensesText) .setTitle(mTitleText).setCloseText(mCloseButtonText) - .setThemeResourceId(mThemeResourceId).setDividerColor(mDividerColor) - .build().create(); + .setThemeResourceId(mThemeResourceId).setDividerColor(mDividerColor); + final LicensesDialog licensesDialog = builder.build(); + if (getArguments().getBoolean(ARGUMENT_USE_APPCOMPAT, false)) { + return licensesDialog.createAppCompat(); + } else { + return licensesDialog.create(); + } } @Override @@ -206,7 +199,9 @@ public void onDismiss(final DialogInterface dialog) { } } - // + // ========================================================================================================================== + // Public API + // ========================================================================================================================== public DialogInterface.OnDismissListener getOnDismissListener() { return mOnDismissListener; @@ -216,7 +211,9 @@ public void setOnDismissListener(final DialogInterface.OnDismissListener onDismi mOnDismissListener = onDismissListener; } - // + // ========================================================================================================================== + // Private API + // ========================================================================================================================== private int getNoticesXmlResourceId() { int resourceId = R.raw.notices; @@ -230,4 +227,96 @@ private int getNoticesXmlResourceId() { return resourceId; } + + // ========================================================================================================================== + // Inner classes + // ========================================================================================================================== + + public static class Builder { + + private final Context mContext; + private Notices mNotices; + private Integer mRawNoticesResourceId; + private boolean mShowFullLicenseText; + private boolean mIncludeOwnLicense; + private int mThemeResourceId; + private int mDividerColor; + private boolean mUseAppCompat; + + // ========================================================================================================================== + // Constructor + // ========================================================================================================================== + + public Builder(@NonNull final Context context) { + mContext = context; + // Set default values + mShowFullLicenseText = false; + mIncludeOwnLicense = true; + mThemeResourceId = 0; + mDividerColor = 0; + mUseAppCompat = false; + } + + // ========================================================================================================================== + // Public API + // ========================================================================================================================== + + public Builder setNotice(final Notice notice) { + mNotices = new Notices(); + mNotices.addNotice(notice); + return this; + } + + public Builder setNotices(final Notices notices) { + mNotices = notices; + return this; + } + + public Builder setNotices(@RawRes final int rawNoticesResourceId) throws Exception { + mRawNoticesResourceId = rawNoticesResourceId; + return this; + } + + public Builder setShowFullLicenseText(final boolean showFullLicenseText) { + mShowFullLicenseText = showFullLicenseText; + return this; + } + + public Builder setIncludeOwnLicense(final boolean includeOwnLicense) { + mIncludeOwnLicense = includeOwnLicense; + return this; + } + + public Builder setThemeResourceId(@StyleRes final int themeResourceId) { + mThemeResourceId = themeResourceId; + return this; + } + + public Builder setDividerColorRes(@ColorRes final int dividerColor) { + mDividerColor = mContext.getResources().getColor(dividerColor); + return this; + } + + public Builder setDividerColor(@ColorInt final int dividerColor) { + mDividerColor = dividerColor; + return this; + } + + public Builder setUseAppCompat(final boolean useAppCompat) { + mUseAppCompat = useAppCompat; + return this; + } + + public LicensesDialogFragment build() { + if (mNotices != null) { + return newInstance(mNotices, mShowFullLicenseText, mIncludeOwnLicense, mThemeResourceId, mDividerColor, mUseAppCompat); + } else if (mRawNoticesResourceId != null) { + return newInstance(mRawNoticesResourceId, mShowFullLicenseText, mIncludeOwnLicense, mThemeResourceId, mDividerColor, mUseAppCompat); + } else { + throw new IllegalStateException("Required parameter not set. You need to call setNotices."); + } + } + + } + } diff --git a/library/src/main/java/de/psdev/licensesdialog/SingleLicenseDialogFragment.java b/library/src/main/java/de/psdev/licensesdialog/SingleLicenseDialogFragment.java deleted file mode 100644 index f5ccae3..0000000 --- a/library/src/main/java/de/psdev/licensesdialog/SingleLicenseDialogFragment.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright 2013 Philip Schiffer - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package de.psdev.licensesdialog; - -import android.app.Dialog; -import android.content.DialogInterface; -import android.content.res.Resources; -import android.os.Bundle; -import android.support.v4.app.DialogFragment; -import de.psdev.licensesdialog.model.Notice; - -public class SingleLicenseDialogFragment extends DialogFragment { - - private static final String ARGUMENT_NOTICE = "ARGUMENT_NOTICE"; - private static final String ARGUMENT_FULL_LICENSE_TEXT = "ARGUMENT_FULL_LICENSE_TEXT"; - private static final String STATE_LICENSE_TEXT = "license_text"; - private static final String STATE_TITLE_TEXT = "title_text"; - private static final String STATE_CLOSE_TEXT = "close_text"; - - // - private String mTitleText; - private String mCloseButtonText; - private String mLicenseText; - - // - private boolean mShowFullLicenseText; - private DialogInterface.OnDismissListener mOnDismissListener; - - public static SingleLicenseDialogFragment newInstance(final Notice notice) { - return newInstance(notice, false); - } - - public static SingleLicenseDialogFragment newInstance(final Notice notice, final boolean showFullLicenseText) { - final SingleLicenseDialogFragment fragment = new SingleLicenseDialogFragment(); - final Bundle args = new Bundle(); - args.putParcelable(ARGUMENT_NOTICE, notice); - args.putBoolean(ARGUMENT_FULL_LICENSE_TEXT, showFullLicenseText); - fragment.setArguments(args); - return fragment; - } - - public SingleLicenseDialogFragment() { - } - - @Override - public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - final Resources resources = getResources(); - - if (savedInstanceState != null) { - mTitleText = savedInstanceState.getString(STATE_TITLE_TEXT); - mLicenseText = savedInstanceState.getString(STATE_LICENSE_TEXT); - mCloseButtonText = savedInstanceState.getString(STATE_CLOSE_TEXT); - } else { - mTitleText = resources.getString(R.string.notices_title); - mCloseButtonText = resources.getString(R.string.notices_close); - - try { - final Notice notice = getNotice(); - final boolean showFullLicenseText = getArguments().getBoolean(ARGUMENT_FULL_LICENSE_TEXT, false); - mLicenseText = NoticesHtmlBuilder.create(getActivity()).setNotice(notice).setShowFullLicenseText(showFullLicenseText).build(); - } catch (final Exception e) { - throw new IllegalStateException(e); - } - } - } - - @Override - public void onSaveInstanceState(final Bundle outState) { - super.onSaveInstanceState(outState); - outState.putString(STATE_TITLE_TEXT, mTitleText); - outState.putString(STATE_LICENSE_TEXT, mLicenseText); - outState.putString(STATE_CLOSE_TEXT, mCloseButtonText); - } - - @Override - public Dialog onCreateDialog(final Bundle savedInstanceState) { - return new LicensesDialog.Builder(getActivity()).setNotices(mLicenseText).setTitle(mTitleText).setCloseText(mCloseButtonText).build().create(); - } - - @Override - public void onDismiss(final DialogInterface dialog) { - super.onDismiss(dialog); - if (mOnDismissListener != null) { - mOnDismissListener.onDismiss(dialog); - } - } - - // - - public DialogInterface.OnDismissListener getOnDismissListener() { - return mOnDismissListener; - } - - public void setOnDismissListener(final DialogInterface.OnDismissListener onDismissListener) { - mOnDismissListener = onDismissListener; - } - - public boolean isShowFullLicenseText() { - return mShowFullLicenseText; - } - - public void setShowFullLicenseText(final boolean showFullLicenseText) { - mShowFullLicenseText = showFullLicenseText; - } - - // - - private Notice getNotice() { - final Bundle arguments = getArguments(); - if (arguments != null && arguments.containsKey(ARGUMENT_NOTICE)) { - return (Notice) arguments.getParcelable(ARGUMENT_NOTICE); - } - - throw new IllegalStateException("no notice provided"); - } -} diff --git a/pom.xml b/pom.xml index 1eea559..6760de6 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ LicensesDialog is an open source library to display licenses used by third-party libraries in an Android app. http://psdev.de/LicensesDialog 2013 - 1.7.1-SNAPSHOT + 1.8.0-SNAPSHOT library @@ -65,22 +65,22 @@ UTF-8 UTF-8 1.6 - 21 + 22 - 4.11 - 2.4 - 5.0.1_r2 - 21.0.3 + 4.12 + 3.0-rc3 + 5.1.1_r2 + 22.2.0 3.0.0 - 3.2 - 4.1.1 - 2.5.1 - 2.10.1 + 3.3 + 4.2.1 + 2.5.2 + 2.10.3 2.4 - 0.7.2.201409121644 + 0.7.5.201505241946 1.4 @@ -112,6 +112,12 @@ ${android-support.version} aar + + com.android.support + appcompat-v7 + ${android-support.version} + aar + diff --git a/sample/AndroidManifest.xml b/sample/AndroidManifest.xml index 0189e88..af32fdb 100644 --- a/sample/AndroidManifest.xml +++ b/sample/AndroidManifest.xml @@ -1,5 +1,4 @@ - - - + + android:targetSdkVersion="22" /> - + android:label="LicensesDialog Sample"> + + + + + + \ No newline at end of file diff --git a/sample/pom.xml b/sample/pom.xml index 50ec2bb..4bba98c 100644 --- a/sample/pom.xml +++ b/sample/pom.xml @@ -1,10 +1,11 @@ - + 4.0.0 de.psdev.licensesdialog parent - 1.7.1-SNAPSHOT + 1.8.0-SNAPSHOT sample @@ -27,6 +28,13 @@ provided + + + com.android.support + appcompat-v7 + aar + + de.psdev.licensesdialog diff --git a/sample/project.properties b/sample/project.properties deleted file mode 100644 index 874eafd..0000000 --- a/sample/project.properties +++ /dev/null @@ -1,12 +0,0 @@ -# This file is automatically generated by Android Tools. -# Do not modify this file -- YOUR CHANGES WILL BE ERASED! -# -# This file must be checked in Version Control Systems. -# -# To customize properties used by the Ant build system use, -# "ant.properties", and override values to adapt the script to your -# project structure. - -# Project target. -target=android-16 -android.library.reference.1=../library diff --git a/sample/res/layout/activity_main.xml b/sample/res/layout/activity_main.xml new file mode 100644 index 0000000..d568bcb --- /dev/null +++ b/sample/res/layout/activity_main.xml @@ -0,0 +1,23 @@ + + + +