Android Open Source - Resonos-Android-Framework Alert Fragment






From Project

Back to project page Resonos-Android-Framework.

License

The source code is released under:

Apache License

If you think the Android project Resonos-Android-Framework 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.resonos.apps.library;
/*from www  . j  a  v  a 2 s.  co  m*/
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.WazaBe.HoloEverywhere.HoloAlertDialogBuilder;
import com.resonos.app.library.R;

/**
 * Class that manages an alert dialog fragment.
 * Use the {@link AlertBuilder} class to create one.
 */
public class AlertFragment extends DialogFragment implements android.view.View.OnClickListener {
  
  // constants
  private static final String ARG_TITLE = "title", ARG_MSG = "msg",
      ARG_YES = "yesBtn", ARG_NO = "noBtn", ARG_MAYBE = "maybeBtn",
      ARG_CUSTOM_VIEW = "customView", ARG_ID = "id", TAG = "alertDlg";
  
  public enum Result {YES, NO, MAYBE, CANCEL};
  
  // vars
  private boolean mNoDismissCall = false;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String title = getArguments().getString(ARG_TITLE);
        String msg = getArguments().getString(ARG_MSG);
        boolean customView = getArguments().getBoolean(ARG_CUSTOM_VIEW);
        int id = getArguments().getInt(ARG_ID);

    AlertFragmentBuilder afb = new AlertFragmentBuilder(getActivity());
    if (title != null)
      afb.setTitle(title);
    if (msg != null)
      afb.setTitle(msg);
    if (customView)
      afb.setView(((FragmentBaseActivity)getActivity()).onDialogCreateCustomView(id));
    return afb.create();
    }
  
  @Override
  public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    if (mNoDismissCall)
      return;
    FragmentBaseActivity a = (FragmentBaseActivity)getActivity();
        int id = getArguments().getInt(ARG_ID);
    a.onDialogResult(id, Result.CANCEL);
  }
  
  @Override
  public void onClick(View v) {
    FragmentBaseActivity a = (FragmentBaseActivity)getActivity();
        int id = getArguments().getInt(ARG_ID);
    if (v.getId() == R.id.bPos) {
      a.onDialogResult(id, Result.YES);
      mNoDismissCall = true;
      dismiss();
    } else if (v.getId() == R.id.bNeg) {
      a.onDialogResult(id, Result.NO);
      mNoDismissCall = true;
      dismiss();
    } else if (v.getId() == R.id.bNeu) {
      a.onDialogResult(id, Result.MAYBE);
      mNoDismissCall = true;
      dismiss();
    }
  }
  
  /**
   * Actually does the dialog building
   */
  private class AlertFragmentBuilder extends HoloAlertDialogBuilder {
    
    private View mCustomView;
    private View mCustomView2;

    public AlertFragmentBuilder(Context context) {
      super(context);
    }

    @Override
    public AlertFragmentBuilder setView(View v) {
      if (v.getId() == R.id.contentPanel) // this is the message from HoloAlertDialogBuilder
        mCustomView = v;
      else
        mCustomView2 = v;
      return this;
    }
    
    @Override
    public AlertDialog create() {
          String yes = getArguments().getString(ARG_YES);
          String no = getArguments().getString(ARG_NO);
          String maybe = getArguments().getString(ARG_MAYBE);
          
      if (yes != null || no != null
          || maybe != null || mCustomView != null || mCustomView2 != null) {
        View buttonView = ((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.alert_dialog_holo_buttons, null);
        Button bPos = (Button)buttonView.findViewById(R.id.bPos);
        Button bNeg = (Button)buttonView.findViewById(R.id.bNeg);
        Button bNeu = (Button)buttonView.findViewById(R.id.bNeu);
        LinearLayout viewHolder = (LinearLayout)buttonView.findViewById(R.id.viewHolder);
        
        bPos.setVisibility(yes != null ? View.VISIBLE : View.GONE);
        if (yes != null) bPos.setText(yes);
        bPos.setOnClickListener(AlertFragment.this);
        
        bNeg.setVisibility(no != null ? View.VISIBLE : View.GONE);
        if (no != null) bNeg.setText(no);
        bNeg.setOnClickListener(AlertFragment.this);
        
        bNeu.setVisibility(maybe != null ? View.VISIBLE : View.GONE);
        if (maybe != null) bNeu.setText(maybe);
        bNeu.setOnClickListener(AlertFragment.this);
  
        if (mCustomView != null)
          viewHolder.addView(mCustomView);
        if (mCustomView2 != null)
          viewHolder.addView(mCustomView2);
        
        super.setView(buttonView);
      }
      return super.create();
    }
  }

  /**
   * Public interface for creating a dialog that interacts
   *  properly with the fragment ecosystem.
   */
  public static class AlertBuilder {
    private Bundle args;
    private Context cx;
  
    public AlertBuilder(Context context) {
      cx = context;
      args = new Bundle();
    }
  
    public AlertBuilder setTitle(int textId) {
      args.putString(ARG_TITLE, cx.getText(textId).toString());
      return this;
    }
  
    public AlertBuilder setTitle(CharSequence text) {
      args.putString(ARG_TITLE, text.toString());
      return this;
    }
  
    public AlertBuilder setMessage(int textId) {
      args.putString(ARG_MSG, cx.getText(textId).toString());
      return this;
    }
  
    public AlertBuilder setMessage(CharSequence text) {
      args.putString(ARG_MSG, text.toString());
      return this;
    }
  
    public AlertBuilder setPositiveButton(int textId) {
      args.putString(ARG_YES, cx.getText(textId).toString());
      return this;
    }
  
    public AlertBuilder setPositiveButton(CharSequence text) {
      args.putString(ARG_YES, text.toString());
      return this;
    }
  
    public AlertBuilder setNegativeButton(int textId) {
      args.putString(ARG_NO, cx.getText(textId).toString());
      return this;
    }
  
    public AlertBuilder setNegativeButton(CharSequence text) {
      args.putString(ARG_NO, text.toString());
      return this;
    }
  
    public AlertBuilder setNeutralButton(int textId) {
      args.putString(ARG_MAYBE, cx.getText(textId).toString());
      return this;
    }
  
    public AlertBuilder setNeutralButton(CharSequence text) {
      args.putString(ARG_MAYBE, text.toString());
      return this;
    }
    
    public AlertBuilder setHasView(boolean hasView) {
      args.putBoolean(ARG_CUSTOM_VIEW, hasView);
      return this;
    }
    
    public AlertFragment show(FragmentBaseActivity a, int id) {
      AlertFragment f = new AlertFragment();
      args.putInt(ARG_ID, id);
      f.setArguments(args);
      f.show(a.getSupportFragmentManager(), TAG);
      return f;
    }
  }
}




Java Source Code List

com.resonos.apps.library.Action.java
com.resonos.apps.library.AlertFragment.java
com.resonos.apps.library.App.java
com.resonos.apps.library.BaseFragment.java
com.resonos.apps.library.FragmentBaseActivity.java
com.resonos.apps.library.file.AltAndroidFileHandle.java
com.resonos.apps.library.file.AltAndroidFiles.java
com.resonos.apps.library.file.AltFileHandle.java
com.resonos.apps.library.file.FileCache.java
com.resonos.apps.library.media.AudioVisualizer.java
com.resonos.apps.library.media.BitmapMemoryCache.java
com.resonos.apps.library.media.HueColorFilter.java
com.resonos.apps.library.media.ImageLoader.java
com.resonos.apps.library.media.MediaScannerNotifier.java
com.resonos.apps.library.model.Coord.java
com.resonos.apps.library.model.ImmutableCoord.java
com.resonos.apps.library.tabviewpager.CustomViewPager.java
com.resonos.apps.library.tabviewpager.PageIndicator.java
com.resonos.apps.library.tabviewpager.TabPageIndicator.java
com.resonos.apps.library.tabviewpager.TabViewPagerAdapter.java
com.resonos.apps.library.tabviewpager.TabViewPagerFragment.java
com.resonos.apps.library.tabviewpager.TitleProvider.java
com.resonos.apps.library.util.AppUtils.java
com.resonos.apps.library.util.ErrorReporter.java
com.resonos.apps.library.util.LifecycleTaskQueue.java
com.resonos.apps.library.util.M.java
com.resonos.apps.library.util.NetworkClient.java
com.resonos.apps.library.util.NetworkRequest.java
com.resonos.apps.library.util.ParameterList.java
com.resonos.apps.library.util.SensorReader.java
com.resonos.apps.library.util.TouchViewWorker.java
com.resonos.apps.library.util.ViewServer.java
com.resonos.apps.library.widget.DashboardLayout.java
com.resonos.apps.library.widget.FormBuilder.java
com.resonos.apps.library.widget.FormElement.java
com.resonos.apps.library.widget.ListFormBuilder.java
com.resonos.apps.library.widget.PopupWindows3D.java
com.resonos.apps.library.widget.QuickAction3D.java
com.resonos.apps.library.widget.RangeSeekBar.java
com.resonos.apps.library.widget.SeekBar.java
com.resonos.apps.library.widget.ToolBarButton.java
com.resonos.apps.library.widget.ToolBar.java