Bia Securities

Archive for July, 2011

Howto: Install non-authorized apk files on AT&T android phones

by on Jul.20, 2011, under Mobile Development

I don’t know why this has to be so difficult, but these are the steps I took to install my development apk file to a test droid phone:

  1. Make sure I have the Android SDK installed on my PC (Required for Droid Explorer)
  2. Download and install HTC Sync – If the htc site search is broken, Google to the rescue
  3. Connect the phone to your PC via USB and select HTC Sync as the connection type
  4. Download and install Droid Explorer from http://de.codeplex.com/
  5. Right click the apk file and select install
Hope this helps someone…
1 Comment more...

Access the wordpress browser check api from asp.net

by on Jul.08, 2011, under Uncategorized

New versions of WordPress have a pretty handy widget included with the admin dashboard.  The widget checks the version of the browser that the user is running (via the user agent) and alerts them if it is insecure (ie6) or has an upgrade (Firefox 3.x).  I wanted to include a similar widget with Acturent and if possible, take advantage of their service api.

After a short time tinkering, I have a nice c# wrapper to their api.  The code below uses a class called BiaCache that is basically a wrapper around System.Web.Cache or an in-memory dictionary, based on if System.Web.Cache exists.  A slight modification will be needed based on your usage.  I cache the results for a week, based on the user agent.

To deserialize the string that comes back from the WordPress service, I found the Sharp Serialization Library works well.

Basic usage for my helper is:

var browserInfo = new HappyBrowsingHelper().GetBrowserInfo(Request.UserAgent);

The results from the web service get stored in a model object called BrowserInfo:

public class BrowserInfo
{
  public string Name { get; set; }
  public string Version { get; set; }
  public string Url { get; set; }
  public string ImageUrl { get; set; }
  public string CurrentVersion { get; set; }
  public bool HasUpgrade { get; set; }
  public bool IsInsecure { get; set; }
}

The helper method to make the service call is as follows:

public BrowserInfo GetBrowserInfo(string userAgent)
{
  if (string.IsNullOrWhiteSpace(userAgent))
    return new BrowserInfo();

  var key = "__GetBrowserInfo_" + userAgent;
  var item = BiaCache.Get<BrowserInfo>(key);
  if (item == null)
  {
    string serializedResponse = null;
    try
    {
      var postData = "useragent=" + userAgent;

      var request = WebRequest.Create("http://api.wordpress.org/core/browse-happy/1.0/");
      request.Method = "POST";
      request.ContentType = "application/x-www-form-urlencoded";
      request.ContentLength = postData.Length;
      using (var writeStream = request.GetRequestStream())
      {
        var encoding = new UTF8Encoding();
        var bytes = encoding.GetBytes(postData);
        writeStream.Write(bytes, 0, bytes.Length);
      }

      using (var response = request.GetResponse())
      {
        using (var responseStream = response.GetResponseStream())
        {
          using (var reader = new StreamReader(responseStream, Encoding.UTF8))
          {
            serializedResponse = reader.ReadToEnd();
          }
        }
      }

      var serializer = new PhpSerializer();
      var result = (Hashtable) serializer.Deserialize(serializedResponse);
      item = new BrowserInfo
          {
              Name = ToStringOrNull(result["name"]),
              Version = ToStringOrNull(result["version"]),
              Url = ToStringOrNull(result["update_url"]),
              ImageUrl = ToStringOrNull(result["img_src_ssl"]),
              CurrentVersion = ToStringOrNull(result["current_version"]),
              HasUpgrade = (bool) result["upgrade"],
              IsInsecure = (bool) result["insecure"]
          };

      BiaCache.Add(key, item, (int) TimeSpan.FromDays(7).TotalMinutes, System.Web.Caching.CacheItemPriority.AboveNormal);
    }
    catch (Exception ex)
    {
      Log.Fatal("Error getting browser info from wordpress :(  nUser agent: " + userAgent + "nResult: " + (serializedResponse ?? string.Empty) + "nn" + ex, ex);
      item = new BrowserInfo();
    }
  }
  return item;
}

private static string ToStringOrNull(object o)
{
  return o == null ? null : o.ToString();
}

Thats basically about it…  Happy browsing!  You can download a demo project: HappyBrowsingDemo.

Leave a Comment more...

FreeTextExpression for NHibernate 3.2

by on Jul.04, 2011, under NHibernate

I upgraded to the latest NHibernate 3.2 and found my FreeText AbstractCriterion wasn’t working as it previously did.  This updated version works for me:

[Serializable]
public class FreeTextExpression : AbstractCriterion
{
  private readonly string _propertyName;
  private readonly object _value;
  private readonly IProjection _projection;

  /// <summary>
  /// Initializes a new instance of the <see cref="FreeTextExpression"/> class.
  /// </summary>
  /// <param name="projection">The projection.</param>
  /// <param name="value">The value.</param>
  public FreeTextExpression(IProjection projection, object value)
  {
    _projection = projection;
    _value = value;
  }

  /// <summary>
  /// Initialize a new instance of the <see cref="FreeTextExpression" />
  /// class for a named Property and its value.
  /// </summary>
  /// <param name="propertyName">The name of the Property in the class.</param>
  /// <param name="value">The value for the Property.</param>
  public FreeTextExpression(string propertyName, object value)
  {
    _propertyName = propertyName;
    _value = value;
  }

  public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
  {
    var sqlBuilder = new SqlStringBuilder();
    var columnNames = CriterionUtil.GetColumnNames(_propertyName, _projection, criteriaQuery, criteria, enabledFilters);

    if (columnNames.Length != 1)
    {
      throw new HibernateException("Contains may only be used with single-column properties");
    }

    sqlBuilder.Add("freetext(")
      .Add(columnNames[0])
      .Add(",");

    sqlBuilder.Add(criteriaQuery.NewQueryParameter(GetParameterTypedValue(criteria, criteriaQuery)).Single());
    sqlBuilder.Add(")");

    return sqlBuilder.ToSqlString();
  }

  public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
  {
    var typedValues = new List<TypedValue>();

    if (_projection != null)
    {
      typedValues.AddRange(_projection.GetTypedValues(criteria, criteriaQuery));
    }
    typedValues.Add(GetParameterTypedValue(criteria, criteriaQuery)); 

    return typedValues.ToArray();
  }

  public TypedValue GetParameterTypedValue(ICriteria criteria, ICriteriaQuery criteriaQuery)
  {
    var matchValue = _value.ToString();
    if (_projection != null)
    {
      return CriterionUtil.GetTypedValues(criteriaQuery, criteria, _projection, null, matchValue).Single();
    }
    return new TypedValue(NHibernateUtil.String, _value.ToString(), EntityMode.Poco);
  }

  public override IProjection[] GetProjections()
  {
    if (_projection != null)
    {
      return new [] { _projection };
    }
    return null;
  }

  public override string ToString()
  {
    return " freetext(" + (_projection ?? (object)_propertyName) + "," + _value + ")";
  }
}
2 Comments more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!