Android Open Source - openmidaas-android-app 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.
 * //from  w  ww .  ja  v  a2 s  .  c  om
 * 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.json.JSONObject;
import org.openmidaas.app.R;
import org.openmidaas.library.model.GenericAttribute;
import org.openmidaas.library.model.GenericAttributeFactory;
import org.openmidaas.library.model.InvalidAttributeNameException;
import org.openmidaas.library.model.InvalidAttributeValueException;
import org.openmidaas.library.model.core.AbstractAttribute;
import org.openmidaas.library.model.core.MIDaaSException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Base64;

public final class Utils {
  
  /**
   * Formats the value typically from a date-picker widget and returns it as a easy to read string
   * @param dayOfMonth
   * @param monthOfYear
   * @param year
   * @return easy to read date as string
   */
  public static String getFormattedDate(int dayOfMonth, int monthOfYear, int year) {
    StringBuilder builder = new StringBuilder();
    return (builder.append(dayOfMonth).append("-").append(monthOfYear+1).append("-").append(year).append(" ").toString());
  }
  
  /**
   * Returns a user-friendly label for the attribute. 
   * If a mapping exists, it returns the mapped label, otherwise it checks to see if the 
   * attribute has a label, if not, the attribute's name is returned. 
   * @param attribute
   * @return
   */
  public static String getAttributeDisplayLabel(AbstractAttribute<?> attribute) {
    String label = "";
    if(attribute != null) {
      CategoryMap map = CategoryMap.get(attribute.getName());
      if(attribute.getLabel() != null && !attribute.getLabel().isEmpty()) {
        label = attribute.getLabel();
      }else if(map!=null){
        label = map.getAttributeLabel();
      }else{
        label = attribute.getName();
      }
    } 
    return label;
  }
  
  /**
   * Creates a generic attribute and tries to save it
   * @param activity
   * @param name
   * @param value
   * @param label
   */
  public static void createGenericAttribute(Activity activity, String name, String value, String label) {
    try {
      GenericAttribute attribute = GenericAttributeFactory.createAttribute(name);
      if(label != null) {
        attribute.setLabel(label);
      }
      setValueAndPersist(activity, attribute, value);
    } catch (InvalidAttributeNameException e) {
      DialogUtils.showNeutralButtonDialog(activity, activity.getString(R.string.defaultErrorDialogTitle), "Invalid name" );
    } 
  }
  
  /**
   * Sets the attribute with the provided value and tries to save it
   * @param activity
   * @param attribute
   * @param value
   */
  public static void modifyGenericAttribute(Activity activity, GenericAttribute attribute, String value) {
    setValueAndPersist(activity, attribute, value);
  }
  public static String getAttributeDetailsLabel(AbstractAttribute<?> attribute){
    String message = "Name: " + attribute.getName() + "\n" +
         "Value: " + attribute.toString() + "\n"; 
    String[] jwsParams = null;
    JSONObject object = null;
    String audience = "";
    String issuer = "";
    String subject = "";
    if(attribute.getSignedToken() != null) {
      jwsParams = attribute.getSignedToken().split("\\."); 
      try {
        object = new JSONObject(new String(Base64.decode(jwsParams[1], Base64.NO_WRAP), "UTF-8"));
        if(object != null) {
          if (object.getString("aud")!=null)
            audience = object.getString("aud");
          message += "Audience: " + audience + "\n";
          
          if (object.getString("iss")!=null)
            issuer = object.getString("iss");
          message += "Issuer: " + issuer + "\n";
          
          if (object.getString("sub")!=null)
            subject = object.getString("sub");
          message += "Subject: " + subject + "\n";
          
          message += "Signature: " + jwsParams[2];
        }
      } catch(Exception e) {
      }
    }
    return message;
  }
  
  private static void setValueAndPersist(Activity activity, GenericAttribute attribute, String value) {
    try {
      attribute.setValue(value.toString());
      attribute.save();
      activity.sendBroadcast(new Intent().setAction(Intents.ATTRIBUTE_LIST_CHANGE_EVENT));
    } catch (InvalidAttributeValueException e) {
      DialogUtils.showNeutralButtonDialog(activity, activity.getString(R.string.defaultErrorDialogTitle), "Invalid value ");
    } catch (MIDaaSException e) {
      DialogUtils.showNeutralButtonDialog(activity, activity.getString(R.string.defaultErrorDialogTitle), e.getError().getErrorMessage());
    }
  }
  
  /**
   * To get the version number from Manifest
   */
  public static String getVersionNumber(Context context ){
    
    String versionName;
        try {
      versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
      return versionName;
    } catch (Exception e) {
      Logger.error(Utils.class , e);
      Logger.error(Utils.class, "Version number unknown");
    }
        return "Unknown";
  }
  
  
}




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