Android Open Source - openmidaas-android-app Dialog Utils






From Project

Back to project page openmidaas-android-app.

License

The source code is released under:

Apache License

If you think the Android project openmidaas-android-app 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

/*******************************************************************************
 * Copyright 2013 SecureKey Technologies Inc.
 * /*w  ww .j a  va  2  s. co  m*/
 * 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.openmidaas.app.common;

import org.openmidaas.app.R;
import org.openmidaas.app.activities.ui.list.AbstractAttributeListElement;
import org.openmidaas.library.common.Constants.ATTRIBUTE_STATE;
import org.openmidaas.library.model.GenericAttribute;
import org.openmidaas.library.model.InvalidAttributeValueException;
import org.openmidaas.library.model.core.AbstractAttribute;
import org.openmidaas.library.model.core.CompleteVerificationCallback;
import org.openmidaas.library.model.core.MIDaaSException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * 
 * Helper class to display UI dialogs. 
 *
 */
public final class DialogUtils {
  
  
  /**
   * Displays a "OK" dialog box.
   * @param activity the activity displaying the notification
   * @param title the dialog box title
   * @param message the message
   */
  public static void showNeutralButtonDialog(final Activity activity, final String title, final String message) {
    activity.runOnUiThread(new Runnable() {

      @Override
      public void run() {
        new AlertDialog.Builder(activity)
          .setTitle(title)
          .setMessage(message)
          .setNeutralButton(activity.getString(R.string.okButtonText),  new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface arg0, int arg1) {
            
          }
          })
           .show();
      }
    });
  }
  
  /**
   * Displays a list of essential attributes that are missing from an authorization. 
   * @param activity
   * @param message
   * @param proceedButtonListener
   */
  public static void showEssentialAttributeMissingDialog(final Activity activity, final String message, final DialogInterface.OnClickListener proceedButtonListener) {
    activity.runOnUiThread(new Runnable() {

      @Override
      public void run() {
        
        AlertDialog.Builder alert = new AlertDialog.Builder(activity);
        alert.setTitle("Missing information");
        alert.setMessage(message);
          alert.setPositiveButton("Proceed",  proceedButtonListener);
          alert.setNegativeButton(activity.getString(R.string.backButtonText),  new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface arg0, int arg1) {}
          });
          alert.show();
      }
    });
  }
  
  /**
   * Displays a name-value collection dialog for general attributes
   * @param activity
   */
  public static void showNameValueCollectionDialog(final Activity activity) {
     final AlertDialog.Builder alert = new AlertDialog.Builder(activity);  
     LinearLayout lila1= new LinearLayout(activity);
       lila1.setOrientation(1); //1 is for vertical orientation
       final EditText etName = new EditText(activity); 
       etName.setHint(activity.getString(R.string.generalAttributeNamePrompt));
       final EditText etValue = new EditText(activity);
       etValue.setHint(activity.getString(R.string.generalAttributeValuePrompt));
       lila1.addView(etName);
       lila1.addView(etValue);
       alert.setView(lila1);
       alert.setTitle(activity.getString(R.string.generalAttributeDialogCollectionTitle));
       alert.setNeutralButton(activity.getString(R.string.saveButtonText), new OnClickListener() {

      @Override
      public void onClick(DialogInterface arg0, int arg1) {
        final String name = etName.getText().toString();
        final String value = etValue.getText().toString();
        Utils.createGenericAttribute(activity, name, value, null);
      }
         
       });
       final AlertDialog alertDialog = alert.create();
       etName.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean hasFocus) {
          if (hasFocus) {
            alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
              }
        }
        
      });
      alertDialog.show();
  }
  
  /**
   * Displays the birthday dialog. The onDateSet function is being called twice for some reason. So a flag is set to ensure that
   * the attribute is created and saved only once. 
   * @param activity
   * @param attribute
   */
  public static void showBirthdayDatePickerDialog(final Activity activity, final AbstractAttribute<?> attribute) {
    DatePickerDialog.OnDateSetListener dateLisenter = new DatePickerDialog.OnDateSetListener() {
      boolean hasAlreadyFired = false;
      @Override
      public void onDateSet(DatePicker view, int year, int monthOfYear,
          int dayOfMonth) {
        
        if(hasAlreadyFired) {
          return;
        } else {
          if(attribute != null) {
            Utils.modifyGenericAttribute(activity, (GenericAttribute)attribute,  Utils.getFormattedDate(dayOfMonth, monthOfYear, year));
          } else {
            Utils.createGenericAttribute(activity, Constants.AttributeNames.BIRTHDAY, Utils.getFormattedDate(dayOfMonth, monthOfYear, year), null);
          }
          hasAlreadyFired = true;
        }
      }
    };
    DatePickerDialog datePicker = new DatePickerDialog(activity, dateLisenter, 01, 01, 2000);
    datePicker.setTitle(activity.getString(R.string.birthdayDialogTitle));
    datePicker.show();  
    
  }
  
  /**
   * Displays a dialog containing a list of radio items as defined by the itemsToDisplay parameter
   * @param activity
   * @param message
   * @param itemsToDisplay
   * @param listener
   */
  public static void showRadioButtonDialog(final Activity activity, final String message, final String[] itemsToDisplay, final DialogInterface.OnClickListener listener) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
      builder.setTitle(message);
      builder.setSingleChoiceItems(itemsToDisplay, -1, listener);
      builder.create().show();  
  }
  
  /**
   * Displays a toast notification. 
   * @param activity the activity displaying the notification 
   * @param message the message to be displayed
   */
  public static void showToast(final Activity activity, final String message) {
    activity.runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Toast.makeText(activity, message, Toast.LENGTH_LONG).show();
      }
    });
  }
  
  /**
   * Displays the attribute details
   * @param activity
   * @param attribute
   */
  public static void showAttributeDetails(Activity activity, AbstractAttributeListElement listElement) {
    AbstractAttribute<?> attribute = listElement.getAttribute();
    showDeleteAttributeDialog(activity,listElement, Utils.getAttributeDetailsLabel(attribute));
  }
  
  /**
   * Displays a dialog to modify an attribute
   * @param activity
   * @param attribute
   */
  public static void showGenericAttributeModificationDialog(final Activity activity, final AbstractAttribute<?> attribute) {
    AlertDialog.Builder alert = new AlertDialog.Builder(activity);
    alert.setTitle(Utils.getAttributeDisplayLabel(attribute));
    alert.setMessage(activity.getString(R.string.modifyAttributeText));

    // Set an EditText view to get user input 
    final EditText input = new EditText(activity);
    input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
    alert.setView(input);
    
    alert.setPositiveButton(activity.getString(R.string.saveButtonText), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      Editable value = input.getText();
      Utils.modifyGenericAttribute(activity, (GenericAttribute)attribute, value.toString());
      }
    });

    alert.setNegativeButton(activity.getString(R.string.cancelButtonText), new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {}
    });

    final AlertDialog alertDialog = alert.create();
    input.setOnFocusChangeListener(new OnFocusChangeListener() {

      @Override
      public void onFocusChange(View arg0, boolean hasFocus) {
        if (hasFocus) {
          alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
      }
      
    });
    alertDialog.show();
  }
  
  /**
   * Displays a dialog that enables the user to enter the verification code they obtained. 
   * @param activity
   * @param attribute
   */
  public static void showCodeCollectionDialog(final Activity activity, final AbstractAttribute<?> attribute) {
    AlertDialog.Builder alert = new AlertDialog.Builder(activity);

    alert.setTitle("Verify " + attribute.getName());
    alert.setMessage(activity.getString(R.string.enterPinText));

    // Set an EditText view to get user input 
    final EditText input = new EditText(activity);
    alert.setView(input);

    alert.setPositiveButton(activity.getString(R.string.okButtonText), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      Editable value = input.getText();
      final ProgressDialog progressDialog = new ProgressDialog(activity);
      progressDialog.setMessage("Verifying...");
      progressDialog.show();
      attribute.completeVerification(value.toString(), new CompleteVerificationCallback() {

        @Override
        public void onSuccess() {
          try {
            attribute.save();
          } catch (MIDaaSException e) {
            DialogUtils.showNeutralButtonDialog(activity, activity.getString(R.string.defaultErrorDialogTitle), e.getError().getErrorMessage());
          } catch (InvalidAttributeValueException e) {
            DialogUtils.showNeutralButtonDialog(activity, activity.getString(R.string.defaultErrorDialogTitle), e.getMessage());
          }
          activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
              if(progressDialog.isShowing()) {
                progressDialog.dismiss();
              }
              activity.sendBroadcast(new Intent().setAction(Intents.ATTRIBUTE_LIST_CHANGE_EVENT));
            }
          });
          
        }

        @Override
        public void onError(MIDaaSException exception) {
          if(progressDialog.isShowing()) {
            progressDialog.dismiss();
          }
          DialogUtils.showNeutralButtonDialog(activity, activity.getString(R.string.defaultErrorDialogTitle), exception.getError().getErrorMessage());
        }
        
      });
      }
    });

    alert.setNegativeButton(activity.getString(R.string.cancelButtonText), new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
          
      }
    });
    final AlertDialog alertDialog = alert.create();
    input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
    input.setOnFocusChangeListener(new OnFocusChangeListener() {

      @Override
      public void onFocusChange(View arg0, boolean hasFocus) {
        if (hasFocus) {
          alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
      }
      
    });
    alertDialog.show();
  }
  
  /**
   * Dialog that asks the user whether they would like to delete an attribute 
   * @param activity
   * @param listElement
   * @param message
   */
  public static void showDeleteAttributeDialog(final Activity activity,  final AbstractAttributeListElement listElement, final String message) {
    final AbstractAttribute<?> attribute = listElement.getAttribute();
    activity.runOnUiThread(new Runnable() {

      @Override
      public void run() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        
          builder.setTitle(activity.getString(R.string.deleteButtonText));
          builder.setMessage(message);
          if( (attribute.getState() != ATTRIBUTE_STATE.VERIFIED) && (attribute.getState() != ATTRIBUTE_STATE.NOT_VERIFIABLE) ){
        
            builder.setPositiveButton("Re-verify", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
              if(attribute.getState() == ATTRIBUTE_STATE.VERIFIED){
                Toast.makeText(activity, activity.getString(R.string.verifiedAttributeText), Toast.LENGTH_LONG).show();
              }else if(attribute.getState() == ATTRIBUTE_STATE.NOT_VERIFIABLE){
                Toast.makeText(activity, activity.getString(R.string.unverifiableAttributeText), Toast.LENGTH_LONG).show();
              }else if(attribute.getState() == ATTRIBUTE_STATE.UNKNOWN){
                Toast.makeText(activity, activity.getString(R.string.unknownAttributeStateText), Toast.LENGTH_LONG).show();
              }else if(attribute.getState() == ATTRIBUTE_STATE.ERROR_IN_SAVE){
                Toast.makeText(activity, activity.getString(R.string.errorInAttributeSaveText), Toast.LENGTH_LONG).show();
              }else if(attribute.getState() == ATTRIBUTE_STATE.PENDING_VERIFICATION || attribute.getState() == ATTRIBUTE_STATE.NOT_VERIFIED) {
                  AttributeRegistrationHelper.verifyAttribute(activity, "Starting "+attribute.getName() +" verification...", "Verification code has been sent to "+listElement.getRenderedAttributeValue(), attribute);
              }
            } 
          });
          }
          builder.setNegativeButton(activity.getString(R.string.deleteButtonText),  new DialogInterface.OnClickListener() {
            
          @Override
          public void onClick(DialogInterface arg0, int arg1) {
            try {
              attribute.delete();
            
              activity.sendBroadcast(new Intent().setAction(Intents.ATTRIBUTE_LIST_CHANGE_EVENT));
            } catch (MIDaaSException e) {
              
            }
          }
          });  
          AlertDialog dialog = builder.create();
          dialog.show();
      }
      
    });
  }
  
  /**
   * Dialog that collects an attribute value when a list element is pressed 
   * @param activity
   * @param attributeName
   * @param label
   */
  public static void showAttributeValueCollectionDialog( final Activity activity, final String attributeName, String label) {
    AlertDialog.Builder alert = new AlertDialog.Builder(activity);

    alert.setTitle("Adding " + label);
    alert.setMessage("Enter your " + label);

    // Set an EditText view to get user input 
    final EditText input = new EditText(activity);
    input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
    alert.setView(input);

    alert.setPositiveButton(activity.getString(R.string.okButtonText), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      Editable value = input.getText();
      Utils.createGenericAttribute(activity, attributeName, value.toString(), null);
      activity.startActivity(activity.getIntent());
      activity.finish();
      activity.overridePendingTransition(0, 0);
      }
    });

    alert.setNegativeButton(activity.getString(R.string.cancelButtonText), new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
          
      }
    });

    final AlertDialog alertDialog = alert.create();
    input.setOnFocusChangeListener(new OnFocusChangeListener() {

      @Override
      public void onFocusChange(View arg0, boolean hasFocus) {
        if (hasFocus) {
          alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
      }
      
    });
    alertDialog.show();
  }
}




Java Source Code List

org.openmidaas.app.App.java
org.openmidaas.app.Settings.java
org.openmidaas.app.activities.AboutUsActivity.java
org.openmidaas.app.activities.AbstractActivity.java
org.openmidaas.app.activities.AbstractAttributeRegistrationActivity.java
org.openmidaas.app.activities.AddressActivity.java
org.openmidaas.app.activities.AttributeListFragment.java
org.openmidaas.app.activities.AuthorizationActivity.java
org.openmidaas.app.activities.CreditCardActivity.java
org.openmidaas.app.activities.EmailRegistrationActivity.java
org.openmidaas.app.activities.EnterURLDialogFragment.java
org.openmidaas.app.activities.MainTabActivity.java
org.openmidaas.app.activities.ManageConsentActivity.java
org.openmidaas.app.activities.PhoneRegistrationActivity.java
org.openmidaas.app.activities.ProfileFragment.java
org.openmidaas.app.activities.PushNotificationActivity.java
org.openmidaas.app.activities.ScanFragment.java
org.openmidaas.app.activities.SplashActivity.java
org.openmidaas.app.activities.ui.ConsentedDetailsDialogListAdapter.java
org.openmidaas.app.activities.ui.ConsentedDetailsDialog.java
org.openmidaas.app.activities.ui.list.AbstractAttributeListElement.java
org.openmidaas.app.activities.ui.list.AbstractListCategory.java
org.openmidaas.app.activities.ui.list.AddressCategory.java
org.openmidaas.app.activities.ui.list.AddressListElement.java
org.openmidaas.app.activities.ui.list.AttributeExpandableListAdapter.java
org.openmidaas.app.activities.ui.list.AuthorizationListAdapter.java
org.openmidaas.app.activities.ui.list.BirthdayListElement.java
org.openmidaas.app.activities.ui.list.ConsentListAdapter.java
org.openmidaas.app.activities.ui.list.CreditCardCategory.java
org.openmidaas.app.activities.ui.list.CreditCardListElement.java
org.openmidaas.app.activities.ui.list.EmailAttributeListElement.java
org.openmidaas.app.activities.ui.list.EmailCategory.java
org.openmidaas.app.activities.ui.list.GenderListElement.java
org.openmidaas.app.activities.ui.list.GeneralCategory.java
org.openmidaas.app.activities.ui.list.GenericAttributeListElement.java
org.openmidaas.app.activities.ui.list.OnListElementLongTouch.java
org.openmidaas.app.activities.ui.list.OnListElementTouch.java
org.openmidaas.app.activities.ui.list.PersonalListCategory.java
org.openmidaas.app.activities.ui.list.PhoneAttributeListElement.java
org.openmidaas.app.activities.ui.list.PhoneCategory.java
org.openmidaas.app.activities.ui.spinner.AttributeSpinnerAdapter.java
org.openmidaas.app.common.AttributeRegistrationHelper.java
org.openmidaas.app.common.CategoryManager.java
org.openmidaas.app.common.CategoryMap.java
org.openmidaas.app.common.Constants.java
org.openmidaas.app.common.DialogUtils.java
org.openmidaas.app.common.Intents.java
org.openmidaas.app.common.Logger.java
org.openmidaas.app.common.Utils.java
org.openmidaas.app.services.GCMIntentService.java
org.openmidaas.app.services.GCMReceiver.java
org.openmidaas.app.session.AttributeFetchException.java
org.openmidaas.app.session.AttributeRequestObjectException.java
org.openmidaas.app.session.ConsentManager.java
org.openmidaas.app.session.EssentialAttributeMissingException.java
org.openmidaas.app.session.PostbackReturnStrategy.java
org.openmidaas.app.session.ReturnStrategyFactory.java
org.openmidaas.app.session.ReturnStrategy.java
org.openmidaas.app.session.SessionCreationException.java
org.openmidaas.app.session.SessionManager.java
org.openmidaas.app.session.Session.java
org.openmidaas.app.session.attributeset.AbstractAttributeSet.java
org.openmidaas.app.session.attributeset.AddressAttributeSet.java
org.openmidaas.app.session.attributeset.AttributeSetFactory.java
org.openmidaas.app.session.attributeset.CreditCardAttributeSet.java
org.openmidaas.app.session.attributeset.EmailAttributeSet.java
org.openmidaas.app.session.attributeset.GenericAttributeSet.java
org.openmidaas.app.session.attributeset.PhoneAttributeSet.java