Android Open Source - hpush About Dialog Fragment






From Project

Back to project page hpush.

License

The source code is released under:

MIT License

If you think the Android project hpush listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hpush.app.fragments;
//  w  w  w  .  java  2 s.c  om
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.hpush.R;
import com.hpush.bus.EULAConfirmedEvent;
import com.hpush.bus.EULARejectEvent;
import com.hpush.utils.Prefs;

import de.greenrobot.event.EventBus;


/**
 * Dialog popup a "About", include legal text, open source licenses etc.
 * <p/>
 * A lot inspired by App <a href="https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples
 * /apps/iosched/util/HelpUtils.java">Google I/O 2014</a>
 *
 * @author Xinyue Zhao
 */
public final class AboutDialogFragment extends DialogFragment {
  /**
   * Error-handling.
   */
  private static final String VERSION_UNAVAILABLE = "N/A";

  /**
   * Initialize an {@link  AboutDialogFragment}.
   *
   * @param context
   *     A {@link android.content.Context} object.
   *
   * @return An instance of {@link AboutDialogFragment}.
   */
  public static DialogFragment newInstance(Context context) {
    return (DialogFragment) Fragment.instantiate(context, AboutDialogFragment.class.getName());
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setCancelable(false);
  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Get app version
    PackageManager pm = getActivity().getPackageManager();
    String packageName = getActivity().getPackageName();
    String versionName;
    try {
      PackageInfo info = pm.getPackageInfo(packageName, 0);
      versionName = info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
      versionName = VERSION_UNAVAILABLE;
    }

    // About.
    SpannableStringBuilder aboutBody = new SpannableStringBuilder();
    aboutBody.append(Html.fromHtml(getString(R.string.about_body, getString(R.string.application_name), versionName)));

    // Licenses.
    SpannableString licensesLink = new SpannableString(getString(R.string.about_licenses));
    licensesLink.setSpan(new ClickableSpan() {
      @Override
      public void onClick(View view) {
        showOpenSourceLicenses(getActivity());
      }
    }, 0, licensesLink.length(), 0);
    aboutBody.append("\n\n");
    aboutBody.append(licensesLink);

    // End User License Agreement.
    SpannableString eulaLink = new SpannableString(getString(R.string.about_eula));
    eulaLink.setSpan(new ClickableSpan() {
      @Override
      public void onClick(View view) {
        showEula(getActivity());
      }
    }, 0, eulaLink.length(), 0);
    aboutBody.append("\n\n");
    aboutBody.append(eulaLink);



    // Show "About" dialog.
    LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
    TextView aboutBodyView = (TextView) layoutInflater.inflate(R.layout.fragment_dialog_about, null);
    aboutBodyView.setText(aboutBody);
    aboutBodyView.setMovementMethod(new LinkMovementMethod());

    return new AlertDialog.Builder(getActivity()).setTitle(R.string.lbl_about).setView(aboutBodyView)
        .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
          }
        }).create();
  }

  /**
   * To open showing open source licenses.
   *
   * @param activity
   *     Host {@link android.support.v4.app.FragmentActivity}.
   */
  private static void showOpenSourceLicenses(FragmentActivity activity) {
    FragmentManager fm = activity.getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("dialog_licenses");
    if (prev != null) {
      ft.remove(prev);
    }
    ft.addToBackStack(null);
    new OpenSourceLicensesDialog().show(ft, "dialog_licenses");
  }

  /**
   * Dialog to open showing open source licenses.
   *
   * @author Xinyue Zhao
   */
  public static class OpenSourceLicensesDialog extends DialogFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setCancelable(false);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
      WebView webView = new WebView(getActivity());
      webView.loadUrl("file:///android_asset/licenses.html");

      return new AlertDialog.Builder(getActivity()).setTitle(R.string.about_licenses).setView(webView)
          .setPositiveButton(R.string.btn_confirm, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
              dialog.dismiss();
            }
          }).create();
    }
  }

  /**
   * Dialog to open showing the "End User License Agreement".
   *
   * @author Xinyue Zhao
   */
  public static void showEula(FragmentActivity activity) {
    FragmentManager fm = activity.getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("dialog_eula");
    if (prev != null) {
      ft.remove(prev);
    }
    ft.addToBackStack(null);

    new EulaDialog().show(ft, "dialog_eula");
  }

  /**
   * Dialog to open showing the "End User License Agreement".
   *
   * @author Xinyue Zhao
   */
  public static class EulaDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
      int padding = getResources().getDimensionPixelSize(R.dimen.padding_eula);
      TextView eulaTextView = new TextView(getActivity());
      eulaTextView.setText(Html.fromHtml(getString(R.string.about_eula_legal_text)));
      eulaTextView.setMovementMethod(LinkMovementMethod.getInstance());
      eulaTextView.setPadding(padding, padding, padding, padding);
      return new AlertDialog.Builder(getActivity()).setTitle(R.string.about_eula).setView(eulaTextView)
          .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
              dialog.dismiss();
            }
          }).create();
    }
  }


  /**
   * Dialog to open showing the "End User License Agreement".
   * <p/>
   * When user does not agree, user can not use the application.
   *
   * @author Xinyue Zhao
   */
  public static class EulaConfirmationDialog extends DialogFragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setCancelable(false);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
      int padding = getResources().getDimensionPixelSize(R.dimen.padding_eula);
      TextView eulaTextView = new TextView(getActivity());
      eulaTextView.setText(Html.fromHtml(getString(R.string.about_eula_legal_text)));
      eulaTextView.setBackgroundColor(getResources().getColor(R.color.common_white));
      eulaTextView.setTextColor(getResources().getColor(R.color.common_black));
      eulaTextView.setMovementMethod(LinkMovementMethod.getInstance());
      eulaTextView.setPadding(padding, padding, padding, padding);
      return new AlertDialog.Builder(getActivity()).setTitle(R.string.about_eula).setView(eulaTextView)
          .setPositiveButton(R.string.btn_agree, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
              Prefs.getInstance(getActivity().getApplication()).setEULAOnceConfirmed(true);
              dismiss();
              EventBus.getDefault().post(new EULAConfirmedEvent());
            }
          }).setNegativeButton(R.string.btn_not_agree, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
              Prefs.getInstance(getActivity().getApplication()).setEULAOnceConfirmed(false);
              dismiss();
              EventBus.getDefault().post(new EULARejectEvent());
            }
          }).create();
    }
  }

}




Java Source Code List

com.hpush.app.App.java
com.hpush.app.activities.BasicActivity.java
com.hpush.app.activities.DailiesActivity.java
com.hpush.app.activities.MainActivity.java
com.hpush.app.activities.SettingActivity.java
com.hpush.app.activities.WebViewActivity.java
com.hpush.app.adapters.DailiesListAdapter.java
com.hpush.app.adapters.MainViewPagerAdapter.java
com.hpush.app.adapters.MessagesListAdapter.java
com.hpush.app.fragments.AboutDialogFragment.java
com.hpush.app.fragments.AdFragment.java
com.hpush.app.fragments.AppListImpFragment.java
com.hpush.app.fragments.BookmarksListFragment.java
com.hpush.app.fragments.DailiesLstFragment.java
com.hpush.app.fragments.GPlusFragment.java
com.hpush.app.fragments.MessagesListFragment.java
com.hpush.bus.BookmarkAllEvent.java
com.hpush.bus.BookmarkMessageEvent.java
com.hpush.bus.BookmarkedEvent.java
com.hpush.bus.ClickMessageCommentsEvent.java
com.hpush.bus.ClickMessageEvent.java
com.hpush.bus.ClickMessageLinkEvent.java
com.hpush.bus.DeleteAccountEvent.java
com.hpush.bus.EULAConfirmedEvent.java
com.hpush.bus.EULARejectEvent.java
com.hpush.bus.EditSettingsEvent.java
com.hpush.bus.GCMRegistedEvent.java
com.hpush.bus.InsertAccountEvent.java
com.hpush.bus.LoadAllEvent.java
com.hpush.bus.LoginedGPlusEvent.java
com.hpush.bus.LogoutGPlusEvent.java
com.hpush.bus.RemoveAllEvent.java
com.hpush.bus.SelectMessageEvent.java
com.hpush.bus.ShareMessageEvent.java
com.hpush.bus.ShowActionBar.java
com.hpush.bus.SortAllEvent.java
com.hpush.bus.UpdateCurrentTotalMessagesEvent.java
com.hpush.data.DailyListItem.java
com.hpush.data.Daily.java
com.hpush.data.FunctionType.java
com.hpush.data.MessageListItem.java
com.hpush.data.Message.java
com.hpush.data.Status.java
com.hpush.data.SyncList.java
com.hpush.db.BookmarksTbl.java
com.hpush.db.DB.java
com.hpush.db.DailyTbl.java
com.hpush.db.DatabaseHelper.java
com.hpush.db.MessagesTbl.java
com.hpush.gcm.ChangeSettingsTask.java
com.hpush.gcm.GcmBroadcastReceiver.java
com.hpush.gcm.GcmIntentService.java
com.hpush.gcm.RegGCMTask.java
com.hpush.gcm.SyncTask.java
com.hpush.gcm.UnregGCMTask.java
com.hpush.utils.Prefs.java
com.hpush.utils.Utils.java
com.hpush.views.FontTextView.java
com.hpush.views.OnViewAnimatedClickedListener2.java
com.hpush.views.OnViewAnimatedClickedListener3.java
com.hpush.views.OnViewAnimatedClickedListener.java
com.hpush.views.SortActionViewProvider.java
com.hpush.views.WebViewEx.java