Sunday, December 23, 2012

Simple Mono Droid Entry point for new features.


One of the things I've needed, in order to create a rapid development environment, is an entry point that is extensible.    Using this setup, I was able to easily throw in new features at a running pace.


public static class Selector
    {
        public static void Selected (string component,Activity act,int Id)
        {
            switch (component) 
            {
                case "Participant List":
                    var intent = new Intent();
                    intent.SetClass(act, typeof (ParticipantListActivity));
                    intent.PutExtra("Type","");
                    intent.PutExtra("Id","");
                    act.StartActivity(intent);
                break;
                case "Child Detail":
                    var childintent = new Intent();
                    childintent.SetClass(act, typeof (ChildDetailsActivity));
                    childintent.PutExtra("currentChildId",Id);
                    act.StartActivity(childintent);
                break;
                case "Parent List":
                    var parentintent = new Intent();
                    parentintent.SetClass(act, typeof (ParentListActivity));
                    act.StartActivity(parentintent);
                break;
                case "Scanner":
                var scannerintent = new Intent();
                scannerintent.SetClass(act,typeof(ScannerActivity));
                act.StartActivity(scannerintent);
                break;
                case "School List":
                    var schoolintent = new Intent();
                    schoolintent.SetClass(act,typeof(SchoolListActivity));
                    act.StartActivity (schoolintent);
                break;
                case "Kiosk":
                var kioskintent = new Intent();
                kioskintent.SetClass(act,typeof(LocationListActivity));
                act.StartActivity(kioskintent);
                break;
                default:
                break;


            }
        }
    }


public class Main : ListActivity
    {
        ProgressDialog progress;

        string[] Entries = { "Participant List","Parent List","Scanner","Kiosk"};

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.TextViewItem);

            this.ListAdapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleListItem1,Entries);

        }

        protected override void OnStart ()
        {
            base.OnStart ();
        }

        protected override void OnResume ()
        {
            base.OnResume();
        }

        protected override void OnListItemClick (ListView l, View v, int position, long id)
        {

            string place = ((ArrayAdapter<string>)this.ListAdapter).GetItem(position);
        
            Selector.Selected(place,this,0);
        }
    }

Rest Sharp MonoDroid, Post in Body.

   I needed to add an item in the body of a post via RestSharp in Mono Droid.  Luckily, the way I've built things, doesn't really expose any endpoints via code, for this reference.  So here is the code.. if you want to know how to post an object in the body of a Rest Sharp request.

public static Credentials Login(Credentials credentials)
        {
            var client = new RestClient();
            var request = new RestRequest();
            request.Method = Method.POST;
            var json = JsonConvert.SerializeObject(credentials);
            request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
            request.Resource = ServiceBase+string.Format(Data.Service.Login);

            RestResponse<Credentials> response = (RestResponse<Credentials>)client.Execute<Credentials>(request);
            Credentials item = new Credentials();
            JsonConvert.PopulateObject(response.Content,item);
            return item;
        }

Pretty simple, yes? -smiles-

On Boot Receiver in MonoDroid.



Sometimes your samples, for the technology you are using don't have a great example for what you need to accomplish.

Here is a working example for a class that uses a BroadcastReceiver to receive the On Boot Completed Event, and then start an Activity.

Get into your solution.  Right click on your Project, select Options. Go to Mono for Android Applications :



This is your class. Feel free to tweak the Intent to your own Activity class that you wish to start.

    [BroadcastReceiver] 
    [IntentFilter(new string[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.HighPriority)]
    public class BootBroadcastReceiver : BroadcastReceiver {

        public override void OnReceive(Context context, Intent intent) {
            Intent startServiceIntent = new Intent();

            startServiceIntent.SetClass(context, typeof(Login));
            startServiceIntent.AddFlags(ActivityFlags.NewTask);
            context.StartActivity(startServiceIntent);
        }
    }

I thought I needed to modify the AndroidManifest.xml.  Boy, was that wrong.  Just use the Attributes.  They will make your broadcast receiver automatically respond to the event.

A good link to check out. Blue Stacks. Android on PC and Mac.

Friday, December 21, 2012

Some cross platform projects for Mono Develop.

I have to be careful with this blog, as it is labeled Android Development, however, I have made an application that works for both Android and IOS.  Now, keep in mind, you won't get all the features in Xamarin from this project in comparison to their Mono Droid / Mono Touch sdk's, but it does provide enough to make real applications.  MVC Style.

The Monocross Project

What I have found is, the best way to implement this sweet layer, is to make sure that your web services are truly restful.  If you are in .net land, and I assume you are, this means RIA Web Services, or the new .net 4.5 API, which is supported in Azure.. with the October 2012 Azure SDK.

In order to utilize this tool, I'll have to do some extensive reworking on my web service side.

I've already planned on doing it, but it will be delayed a little bit, due to side projects currently ongoing.

I've also looked at Cross platform Mobile Game Development environment.  Now, the real winner in this arena is Unity. Hands down.  They have the gear, and the right environment for full game development, but that comes at an expensive price tag.  A lot of people like doing homebrew in the XNA platform.  Currently, Mono Game.. supports 2D games.  It's working up for 3D games, but for my purposes.. Mono Game.. being free and all, works well enough to get my feet wet.  So I've been playing around there.

The Mono Game Project.

Thursday, December 20, 2012

Useful Mono Droid Snippets.


Nowdays, everybody loves JSON.  Truth is.. I feel it is much easier to use in some regards for Web, and Mobile projects than XML, but there is something to be said for both data formats.

This is a snippet for retrieving any web response in JSON, via Rest Sharp in a Mono Droid, or Mono Touch environment, that you can then parse with your JSON Deserializer (Whatever flavor you prefer, hopefully one that supports AsParallel()).

This isn't the code I am using anymore.  As the first security layer is in place.  Below I have a post semi-related to the 2nd security layer, which is semi in-progress.. involving X509 certificates.

I also have made a model for a web service that will provide all User Application management functions for all mobile applications that I'll be developing.  I prefer the cloud.  It's cheap, I don't need anyone other than myself.  (Even though I'm a pretty decent sys admin).

        private static RestClient CutDownBloat()
        {
            var client = new RestClient();
            client.AddHandler("application/json; charset=utf-8",new RestSharp.Deserializers.JsonDeserializer());
            return client;
        }
        private static RestResponse<Object> CutDownCode(string url)
        {
            var request = new RestRequest();
            request.Method = Method.GET;
            request.Resource = url;
            var client = CutDownBloat();
            RestResponse<Object> response = (RestResponse<Object>)client.Execute<Object>(request);
            return response;
        }


In order to make a true secure system for a mobile application, you need a unique ID for the device attempting to access your services.

Just getting a unique device ID is a key component.


This is a snippet that will return a Unique Device ID for Android devices.


public static class DeviceData
    {
        public static string getDeviceID(Context context)
        {
            TelephonyManager tm = (TelephonyManager) context.GetSystemService(Context.TelephonyService);
            return tm.DeviceId;
        }
    }

Sudden Change for the better.

Truth is, I've changed things up.  I dropped the native Java code.

I shifted to Xamarin.  The software for both MonoTouch and Mono Droid were about $800,
but I code faster.  Many things are re-usable.

I've added two new team members to the Developer Team.  I'm hoping for at least one more.

Here are some pictures from the new C# (Xamarin) Mono Develop application.

I also gave up on maintaining two separate code bases.  The mobile app, is now also the kiosk.

The staff will use tablets, or if I can sell the Board of Directors on it, they will use Mobile Phones.

A lot has changed, but I guess I'm just as dedicated.