]> Pileus Git - ~andy/freeotp/commitdiff
Migrate About dialog to use a DialogFragment
authorNathaniel McCallum <npmccallum@redhat.com>
Wed, 4 Dec 2013 20:51:53 +0000 (15:51 -0500)
committerNathaniel McCallum <npmccallum@redhat.com>
Wed, 4 Dec 2013 20:51:53 +0000 (15:51 -0500)
res/values/strings.xml
src/org/fedorahosted/freeotp/AboutDialog.java [deleted file]
src/org/fedorahosted/freeotp/AboutDialogFragment.java [new file with mode: 0644]
src/org/fedorahosted/freeotp/BaseAlertDialogFragment.java [new file with mode: 0644]
src/org/fedorahosted/freeotp/MainActivity.java

index 4c4ae83bb33055c7087b52c9bf6df36546cf7a8e..23d354f16b06fe88daad240fe51a10e7e3f0682d 100644 (file)
     <string name="algorithm">Algorithm</string>
     <string name="digits">Digits</string>
 
+    <string name="about_title">About %1$s</string>
     <string name="about_version">Version %1$s (%2$d)</string>
     <string name="about_copyright">© 2013 - Red Hat, Inc., et al.</string>
     <string name="about_license">FreeOTP is licensed under %1$s.</string>
     <string name="about_website">For more information, see our %s.</string>
     <string name="about_feedback">We welcome your feedback:&lt;br/&gt;• %1$s&lt;br/&gt;• %2$s</string>
+
     <string name="link_website">&lt;a href=&quot;http://freeotp.fedorahosted.org&quot;&gt;website&lt;/a&gt;</string>
     <string name="link_report_a_problem">&lt;a href=&quot;http://fedorahosted.org/freeotp/newticket&quot;&gt;Report a Problem&lt;/a&gt;</string>
     <string name="link_ask_for_help">&lt;a href=&quot;http://lists.fedorahosted.org/mailman/listinfo/freeotp-devel&quot;&gt;Ask for Help&lt;/a&gt;</string>
diff --git a/src/org/fedorahosted/freeotp/AboutDialog.java b/src/org/fedorahosted/freeotp/AboutDialog.java
deleted file mode 100644 (file)
index c215099..0000000
+++ /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 (file)
index 0000000..3385ad0
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * FreeOTP
+ *
+ * Authors: Nathaniel McCallum <npmccallum@redhat.com>
+ *
+ * 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 (file)
index 0000000..3526f0a
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * FreeOTP
+ *
+ * Authors: Nathaniel McCallum <npmccallum@redhat.com>
+ *
+ * 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);
+}
index 5a2f5a7289e6d95c3a45b476d179a177054f698f..6ffe093c583a44160e0cf653d2d4b3560a9efb9d 100644 (file)
@@ -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;
                        }
                });