Android Open Source - sdl_tester_android Base Alert Dialog






From Project

Back to project page sdl_tester_android.

License

The source code is released under:

Copyright (c) 2014, Ford Motor Company All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

If you think the Android project sdl_tester_android 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.livio.sdl.dialogs;
//  w  w w  .j a  v  a  2 s.c o m
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

/**
 * An abstract base class for custom alert dialogs.  The class is basically
 * just a wrapper using Android's AlertDialog.Builder class to create
 * relatively simple custom dialogs.
 *
 * @author Mike Burke
 *
 */
public abstract class BaseAlertDialog {
  
  /**
   * Allows another class to listen for the result of the subclass dialog.  Results
   * are returned as an object.
   *
   * @author Mike Burke
   *
   */
  public interface Listener{
    /**
     * Should be called when data from the dialog is ready to return.
     * 
     * @param resultData The data results for the dialog
     */
    void onResult(Object resultData);
  }

  /**
   * Called after views have been inflated, allowing the subclass an opportunity
   * to find their custom views using view.findViewById(id).
   * 
   * @param parent The parent view which was inflated from input resource id.
   */
  protected abstract void findViews(View parent);
  
  // since this is an abstract class, we'll set all variables to protected scope in case subclasses want to use them directly
  protected Context context;
  protected AlertDialog dialog;
  protected Listener listener;
  protected String title;
  protected View view;
  protected boolean cancelable = true;
  
  /**
   * Creates a BaseDialog object with context, title and resource id.
   * 
   * @param context The context of the dialog
   * @param title The title of the dialog
   * @param resource The resource id used for inflating the main view of the dialog
   */
  public BaseAlertDialog(Context context, String title, int resource) {
    this.context = context;
    this.title = title;
    
    // inflate the view and allow the subclass to locate its views
    inflateView(resource);
    findViews(view);
  }
  
  /**
   * Creates the dialog object using the input data.
   */
  protected void createDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(title)
           .setView(view)
           .setCancelable(cancelable);
    dialog = builder.create();
  }
  
  /**
   * Creates the main dialog view from the input resource id.
   * 
   * @param resourceId The id of the XML view to inflate
   */
  protected void inflateView(int resourceId){
    //grab the system inflater to build views from XML for us
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(resourceId, null);
  }
  
  //public methods
  /**
   * Creates the dialog object if necessary and shows it.
   */
  public void show(){
    if(dialog == null){
      createDialog();
    }
    
    if(!dialog.isShowing()){
      dialog.show();
    }
  }
  
  /**
   * If the dialog exists and is currently showing, dismisses it.
   */
  public void dismiss(){
    if(dialog != null && dialog.isShowing()){
      dialog.dismiss();
    }
  }
  
  /**
   * Returns whether the dialog is currently showing or not.
   * 
   * @return True if the dialog is showing, false otherwise
   */
  public boolean isShowing(){
    if(dialog == null){
      return false;
    }
    
    return dialog.isShowing();
  }
  
  /**
   * Sets a listener for any subclasses of BaseDialog.
   * 
   * @param l The listener to set
   */
  public void setListener(Listener l){
    listener = l;
  }
  
  /**
   * Sets whether or not the dialog is allowed to be cancelled.
   * 
   * @param cancelable True if this dialog is cancelable, false if not
   */
  public void setCancelable(boolean cancelable){
    this.cancelable = cancelable;
    if(dialog != null){
      dialog.setCancelable(cancelable);
    }
  }
  
  /**
   * Notifies an existing dialog listener that results are ready.
   * 
   * @param resultData Object containing dialog results
   */
  public void notifyListener(Object resultData){
    if(listener != null){
      listener.onResult(resultData);
    }
  }
  
}




Java Source Code List

com.livio.sdl.IdGenerator.java
com.livio.sdl.IpAddress.java
com.livio.sdl.SdlConstants.java
com.livio.sdl.SdlImageItem.java
com.livio.sdl.SdlLogMessage.java
com.livio.sdl.SdlRequestFactory.java
com.livio.sdl.SdlResponseFactory.java
com.livio.sdl.SdlResponseTracker.java
com.livio.sdl.SdlService.java
com.livio.sdl.adapters.SdlImageAdapter.java
com.livio.sdl.adapters.SdlMessageAdapter.java
com.livio.sdl.dialogs.BaseAlertDialog.java
com.livio.sdl.dialogs.BaseImageListDialog.java
com.livio.sdl.dialogs.BaseMultipleListViewDialog.java
com.livio.sdl.dialogs.BaseOkCancelDialog.java
com.livio.sdl.dialogs.BaseSingleListViewDialog.java
com.livio.sdl.dialogs.ImageListDialog.java
com.livio.sdl.dialogs.IndeterminateProgressDialog.java
com.livio.sdl.dialogs.JsonFlipperDialog.java
com.livio.sdl.dialogs.ListViewDialog.java
com.livio.sdl.dialogs.MultipleListViewDialog.java
com.livio.sdl.dialogs.SingleJsonDialog.java
com.livio.sdl.dialogs.TextViewAlertDialog.java
com.livio.sdl.dialogs.TextViewOkCancelDialog.java
com.livio.sdl.enums.EnumClickListener.java
com.livio.sdl.enums.EnumComparator.java
com.livio.sdl.enums.SdlButton.java
com.livio.sdl.enums.SdlCommand.java
com.livio.sdl.enums.SdlImageType.java
com.livio.sdl.enums.SdlInteractionMode.java
com.livio.sdl.enums.SdlLanguage.java
com.livio.sdl.enums.SdlSpeechCapability.java
com.livio.sdl.enums.SdlSystemAction.java
com.livio.sdl.enums.SdlTextAlignment.java
com.livio.sdl.enums.SdlTransportType.java
com.livio.sdl.enums.SdlUpdateMode.java
com.livio.sdl.enums.SdlVehicleData.java
com.livio.sdl.menu.CommandButton.java
com.livio.sdl.menu.MenuItem.java
com.livio.sdl.menu.MenuManager.java
com.livio.sdl.menu.SubmenuButton.java
com.livio.sdl.utils.AndroidUtils.java
com.livio.sdl.utils.ApplicationPreferences.java
com.livio.sdl.utils.Counter.java
com.livio.sdl.utils.DownCounter.java
com.livio.sdl.utils.MathUtils.java
com.livio.sdl.utils.SdlUtils.java
com.livio.sdl.utils.StringUtils.java
com.livio.sdl.utils.Timeout.java
com.livio.sdl.utils.UpCounter.java
com.livio.sdl.utils.WifiUtils.java
com.livio.sdl.viewhelpers.MinMaxInputFilter.java
com.livio.sdl.viewhelpers.SeekBarCalculator.java
com.livio.sdltester.HelpActivity.java
com.livio.sdltester.LivioSdlTesterPreferences.java
com.livio.sdltester.MainActivity.java
com.livio.sdltester.SdlTesterImageResource.java
com.livio.sdltester.dialogs.AddCommandDialog.java
com.livio.sdltester.dialogs.AddSubMenuDialog.java
com.livio.sdltester.dialogs.ButtonSubscriptionDialog.java
com.livio.sdltester.dialogs.ButtonUnsubscriptionDialog.java
com.livio.sdltester.dialogs.ChangeRegistrationDialog.java
com.livio.sdltester.dialogs.ChoiceItemDialog.java
com.livio.sdltester.dialogs.CreateInteractionChoiceSetDialog.java
com.livio.sdltester.dialogs.DeleteCommandDialog.java
com.livio.sdltester.dialogs.DeleteFileDialog.java
com.livio.sdltester.dialogs.DeleteInteractionDialog.java
com.livio.sdltester.dialogs.DeleteSubmenuDialog.java
com.livio.sdltester.dialogs.GetDtcsDialog.java
com.livio.sdltester.dialogs.PerformInteractionDialog.java
com.livio.sdltester.dialogs.PutFileDialog.java
com.livio.sdltester.dialogs.ReadDidsDialog.java
com.livio.sdltester.dialogs.ScrollableMessageDialog.java
com.livio.sdltester.dialogs.SdlAlertDialog.java
com.livio.sdltester.dialogs.SdlConnectionDialog.java
com.livio.sdltester.dialogs.SetAppIconDialog.java
com.livio.sdltester.dialogs.SetMediaClockTimerDialog.java
com.livio.sdltester.dialogs.ShowDialog.java
com.livio.sdltester.dialogs.SliderDialog.java
com.livio.sdltester.dialogs.SoftButtonItemDialog.java
com.livio.sdltester.dialogs.SoftButtonListDialog.java
com.livio.sdltester.dialogs.SpeakDialog.java