From: Nathaniel McCallum Date: Wed, 4 Dec 2013 20:51:53 +0000 (-0500) Subject: Migrate About dialog to use a DialogFragment X-Git-Url: http://pileus.org/git/?p=~andy%2Ffreeotp;a=commitdiff_plain;h=bde1ee094d83819ebec2b2946ee14ae005c411a7 Migrate About dialog to use a DialogFragment --- diff --git a/res/values/strings.xml b/res/values/strings.xml index 4c4ae83..23d354f 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -21,11 +21,13 @@ Algorithm Digits + About %1$s Version %1$s (%2$d) © 2013 - Red Hat, Inc., et al. FreeOTP is licensed under %1$s. For more information, see our %s. We welcome your feedback:<br/>• %1$s<br/>• %2$s + <a href="http://freeotp.fedorahosted.org">website</a> <a href="http://fedorahosted.org/freeotp/newticket">Report a Problem</a> <a href="http://lists.fedorahosted.org/mailman/listinfo/freeotp-devel">Ask for Help</a> diff --git a/src/org/fedorahosted/freeotp/AboutDialog.java b/src/org/fedorahosted/freeotp/AboutDialog.java deleted file mode 100644 index c215099..0000000 --- a/src/org/fedorahosted/freeotp/AboutDialog.java +++ /dev/null @@ -1,78 +0,0 @@ -package org.fedorahosted.freeotp; - -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.res.Resources; -import android.text.Html; -import android.text.method.LinkMovementMethod; -import android.view.View; -import android.widget.TextView; - -public class AboutDialog extends AlertDialog { - private void init(Context ctx) { - Resources res = ctx.getResources(); - View v = getLayoutInflater().inflate(R.layout.about, null, false); - TextView tv; - - try { - PackageManager pm = ctx.getPackageManager(); - PackageInfo info = pm.getPackageInfo(ctx.getPackageName(), 0); - String version = res.getString(R.string.about_version, - info.versionName, - info.versionCode); - tv = (TextView) v.findViewById(R.id.about_version); - tv.setText(version); - } catch (PackageManager.NameNotFoundException e) { - e.printStackTrace(); - } - - String apache2 = res.getString(R.string.link_apache2); - String license = res.getString(R.string.about_license, apache2); - tv = (TextView) v.findViewById(R.id.about_license); - tv.setMovementMethod(LinkMovementMethod.getInstance()); - tv.setText(Html.fromHtml(license)); - - String lwebsite = res.getString(R.string.link_website); - String swebsite = res.getString(R.string.about_website, lwebsite); - tv = (TextView) v.findViewById(R.id.about_website); - tv.setMovementMethod(LinkMovementMethod.getInstance()); - tv.setText(Html.fromHtml(swebsite)); - - String problem = res.getString(R.string.link_report_a_problem); - String help = res.getString(R.string.link_ask_for_help); - String feedback = res.getString(R.string.about_feedback, problem, help); - tv = (TextView) v.findViewById(R.id.about_feedback); - tv.setMovementMethod(LinkMovementMethod.getInstance()); - tv.setText(Html.fromHtml(feedback)); - - String title = ctx.getResources().getString(R.string.about); - setTitle(title + " " + ctx.getResources().getString(R.string.app_name)); - setView(v); - - String ok = res.getString(android.R.string.ok); - setButton(BUTTON_POSITIVE, ok, new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - } - }); - } - - public AboutDialog(Context context, boolean cancelable, - OnCancelListener cancelListener) { - super(context, cancelable, cancelListener); - init(context); - } - - public AboutDialog(Context context, int theme) { - super(context, theme); - init(context); - } - - public AboutDialog(Context context) { - super(context); - init(context); - } -} diff --git a/src/org/fedorahosted/freeotp/AboutDialogFragment.java b/src/org/fedorahosted/freeotp/AboutDialogFragment.java new file mode 100644 index 0000000..3385ad0 --- /dev/null +++ b/src/org/fedorahosted/freeotp/AboutDialogFragment.java @@ -0,0 +1,89 @@ +/* + * FreeOTP + * + * Authors: Nathaniel McCallum + * + * Copyright (C) 2013 Nathaniel McCallum, Red Hat + * + * 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 org.fedorahosted.freeotp; + +import android.content.DialogInterface; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.content.res.Resources; +import android.text.Html; +import android.text.method.LinkMovementMethod; +import android.view.View; +import android.widget.TextView; + +public class AboutDialogFragment extends BaseAlertDialogFragment { + public static final String FRAGMENT_TAG = "fragment_about"; + + public AboutDialogFragment() { + super(R.string.about, R.layout.about, 0, android.R.string.ok, 0); + } + + @Override + protected void onViewInflated(View view) { + Resources res = getActivity().getResources(); + TextView tv; + + try { + PackageManager pm = getActivity().getPackageManager(); + PackageInfo info = pm.getPackageInfo(getActivity().getPackageName(), 0); + String version = res.getString(R.string.about_version, + info.versionName, + info.versionCode); + tv = (TextView) view.findViewById(R.id.about_version); + tv.setText(version); + } catch (PackageManager.NameNotFoundException e) { + e.printStackTrace(); + } + + String apache2 = res.getString(R.string.link_apache2); + String license = res.getString(R.string.about_license, apache2); + tv = (TextView) view.findViewById(R.id.about_license); + tv.setMovementMethod(LinkMovementMethod.getInstance()); + tv.setText(Html.fromHtml(license)); + + String lwebsite = res.getString(R.string.link_website); + String swebsite = res.getString(R.string.about_website, lwebsite); + tv = (TextView) view.findViewById(R.id.about_website); + tv.setMovementMethod(LinkMovementMethod.getInstance()); + tv.setText(Html.fromHtml(swebsite)); + + String problem = res.getString(R.string.link_report_a_problem); + String help = res.getString(R.string.link_ask_for_help); + String feedback = res.getString(R.string.about_feedback, problem, help); + tv = (TextView) view.findViewById(R.id.about_feedback); + tv.setMovementMethod(LinkMovementMethod.getInstance()); + tv.setText(Html.fromHtml(feedback)); + } + + @Override + public void onStart() { + super.onStart(); + + Resources res = getActivity().getResources(); + String title = res.getString(R.string.about_title, + res.getString(R.string.app_name)); + getDialog().setTitle(title); + } + + @Override + public void onClick(DialogInterface arg0, int arg1) { + } +} diff --git a/src/org/fedorahosted/freeotp/BaseAlertDialogFragment.java b/src/org/fedorahosted/freeotp/BaseAlertDialogFragment.java new file mode 100644 index 0000000..3526f0a --- /dev/null +++ b/src/org/fedorahosted/freeotp/BaseAlertDialogFragment.java @@ -0,0 +1,65 @@ +/* + * FreeOTP + * + * Authors: Nathaniel McCallum + * + * Copyright (C) 2013 Nathaniel McCallum, Red Hat + * + * 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 org.fedorahosted.freeotp; + +import android.app.AlertDialog; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.view.View; + +public abstract class BaseAlertDialogFragment extends DialogFragment implements DialogInterface.OnClickListener { + private final int mTitle; + private final int mLayout; + private final int mNegative; + private final int mNeutral; + private final int mPositive; + + public BaseAlertDialogFragment(int title, int layout, int negative, int neutral, int positive) { + mTitle = title; + mLayout = layout; + mNegative = negative; + mNeutral = neutral; + mPositive = positive; + } + + @Override + public android.app.Dialog onCreateDialog(android.os.Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(mTitle); + + if (mNegative != 0) + builder.setNegativeButton(mNegative, this); + + if (mNeutral != 0) + builder.setNeutralButton(mNeutral, this); + + if (mPositive != 0) + builder.setPositiveButton(mPositive, this); + + View view = getActivity().getLayoutInflater().inflate(mLayout, null, false); + onViewInflated(view); + builder.setView(view); + + return builder.create(); + }; + + protected abstract void onViewInflated(View view); +} diff --git a/src/org/fedorahosted/freeotp/MainActivity.java b/src/org/fedorahosted/freeotp/MainActivity.java index 5a2f5a7..6ffe093 100644 --- a/src/org/fedorahosted/freeotp/MainActivity.java +++ b/src/org/fedorahosted/freeotp/MainActivity.java @@ -176,7 +176,8 @@ public class MainActivity extends Activity { menu.findItem(R.id.action_about).setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { - new AboutDialog(MainActivity.this).show(); + new AboutDialogFragment().show(getFragmentManager(), + AboutDialogFragment.FRAGMENT_TAG); return true; } });