Do you have a Parcel to send?

I have been working on the Android platform recently. I am truly enjoying the time I am spending building an app on the platform. One useful concept is the Parcel interface.

Java already has a Serializable interface to marshal and unmarshal objects, so why use Parcelable? Good Question, Java’s serializable will try to marshall everything in your object using a lot of reflection, which can be slower. Parcelable on the other hand makes you, the programmer, marshal and unmarshal your object manually. This method does not use reflection making it theoretically faster.

So lets see an example of implementing Parcelable.

class Foo implements Parcelable {
    int testInt;
    boolean testBool;
    String testStr;
    
    public int describeContent() {
        return 0;
    }
    
    public void writeToParcel(Parcel pc, int flags) {
        pc.writeInt(testInt);
        // There is no writeBoolean this is an alternative
        pc.writeByte((byte)(testBool ? 1 : 0));
        pc.writeString(testStr);
    }
    
    public static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>() {
        public Foo createFromParcel(Parcel pc) {
            return new Foo(pc);
        }
        
        public Foo[] newArray(int size) {
            return new Foo[size];
        }
    };
    
    public Foo(Parcel pc) {
        testInt = pc.readInt();
        testBool = (pc.readByte() == 1);
        testStr = pc.readString();
    }
}

 

Some important things to note.

  • The order of how you write the variables into the parcel must also be the order you read them out of the parcel. This is the manual marshaling and unmarshaling we talked about before.
  • Parcelable.Creator implementation is required.

Hope this helps explain Parcelable.

Day 1
Meal 3: Lunch
2 apples
1 orange
1 tomato
1 red bell pepper
1 lime
2 stalks of celery
2 cups of parsley

Wow the worst tasting drink so far. It also doesn’t help that I really don’t like the taste of tomatoes, the bell pepper didn’t help any either.

Day 1
Meal 3: Lunch
2 apples
1 orange
1 tomato
1 red bell pepper
1 lime
2 stalks of celery
2 cups of parsley

Wow the worst tasting drink so far. It also doesn’t help that I really don’t like the taste of tomatoes, the bell pepper didn’t help any either.

Day 1
Meal 2: Mid Morning Juice
1 Cucumber
4 Celery stalks
8 Kale Leaves
1/2 Lemon
2 Apples
This produced a lot of juice, 2 cups worth. The taste, unfortunately, was not as good as the meal 1. I am starting the feel affects on my body already. I am not sure if it is a feeling attributed to hunger, but I do feel a slight tingly sensation through my whole body.

Day 1

Meal 2: Mid Morning Juice

1 Cucumber

4 Celery stalks

8 Kale Leaves

1/2 Lemon

2 Apples

This produced a lot of juice, 2 cups worth. The taste, unfortunately, was not as good as the meal 1. I am starting the feel affects on my body already. I am not sure if it is a feeling attributed to hunger, but I do feel a slight tingly sensation through my whole body.

It is 10:00 in the morning and I this is my breakfast.

2 Apples

3 Carrots

and an inch of ginger

Taste:

I personally don’t like carrots, so I was quite skeptical I would like this juice. Of course then I proceeded to drink it and it tastes pretty darn good. I think the ginger made it very refreshing along with the sweetness of the apples.

First drink done ! :) Will see how I feel in two hours.

Juicing: Prelude

It has been a while since I have written on here, and more I’m compelled to write again.

So much has changed since the last post, new job, new life, new surroundings. It’s been awesome so far. So keeping with this theme of new, this weekend I am going to try out a juice reboot of my body.

Mainly I really want to see if this juicing fad actually works. Will it actually make me feel more alert, have more energy, feel better in general? I will be blogging my experience through this three day juice reboot.

Getting your own number - WP7

So right now I am trying to figure out a way to uniquely identify one phone from another, I am thinking of using the phone number as the identifier of the account. This way if the user switches phones, possibly to a different platform, the system still knows the person’s friends. This removes the head ache of adding all your friends again after switching platforms. Of course this hypothesis is counting on the fact that people aren’t switching phone numbers willy nilly.

But I just cannot find the API to find one’s own phone number. Anyone know how?

The search continues!

Update:
Any this is unavailable information :( I guess I could use the unique ID

Windows Phone App Project: Beacon

Hey guys,
Just started a new project, an app for the Windows Phone platform.
Basically what I want to create is an app, with your permission, will broadcast your geolocation to your friends as well as getting your friends geolocations as well.

Not exactly sure how this will turn out as this is my first time creating a Window Phone App. Wish me luck.

If you interested in following the app development I am uploading all the code to github: Beacon

FizzBuzz but exciting

So I came across this implementation of FizzBuzz a while back, and I thought it was amazingly creative. It shows a deeper understanding of C++ pointers than just using regular old if statements. This one is definitely going into my interview questions bank.

In case you don’t know the FizzBuzz question, here is a variant:

Write a function FizzBuzz that takes a integer. In the range from 1 to the passed in integer output “Fizz” is the number if divisible by 3 evenly and “Buzz” if divisible by 5 evenly, otherwise output the number.

void fizzBuzz(int input)
{
  char* result = "Fizz\0Buzz\0FizzBuzz\0%i\0";
  for (int i = 1; i <= input; ++i)
  {
    int start = i%3?i%5?19:5:i%5?0:10;
    printf(result+start, i);
    cout << endl;
  }
}

Wow, isn’t that beautiful?
Let us take a look at what this is doing.
The line that is of interest is: int start = i%3?i%5?19:5:i%5?0:10; This is essentially where all the magic happens.

We can rewrite that big ternary expression into => (i%3)?(i%5?19:5):(i%5?0:10); This gives us a better picture of what is going on. The first conditional statement i%3 tests if the current number is divisible by 3 evenly. If it does we go to i%5?0:10. Thats the “false” option if you guys weren’t paying attention. I’ll let you think about why that is. This sub ternary expression tests if the current number is divisible by 5 evenly. If it does then start = 0 and if it doesn’t then start = 10. The other cases are taken care of in the other ternary expression.

This start variable controls where we start printing in the result string. As we can see all the available printable strings are in there delimited by \0 or NULL characters. And of course %i represent a specifier for a number number.

When it comes time to execute the printf statement, the first arugment tells printf to start at the specified location and print until you see a NULL character. The second parameter will only be used if there is a specifier like %i.

One other thing worth noting here is that whenever you are adding a scalar to a pointer, most compilers today, I don’t know of any that don’t but there may be some, will automatically transform that into adding sizeof(<type pointer is pointing to)*scalar bytes to the pointer. If you think about this, it makes a lot of sense, just think of an array of types.

Hope you thought this was as interesting as I found it to be.

Happy Coding!

Ever Thought the For Loop was a bad Idea?

I just read a great article about for loops.

Definitely worth the read: http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-with-for-loop.html

PHP: Redirect Hell

Wow, I just spent a couple hours trying to figure out why my $_SESSION variables were not being stored after I redirected to a different page using the header();

Sad face to the max.
SO here is the fix guys:
put an exit(); or die(); right after the header redirect.

The Reason:
It was strange, after search many hours online many people suggested that putting exit(); or die(); after the redirect fixes this problem, but no one really explained why. The explaination that led me to understand why it wasn’t working was that the script continues to execute even after the header() redirect.

In my case, I unset the session variable later on, on that page for a different error coming from a different redirect. (uhm I hope that was clear enough)

So remember to put that exit(); or die(); after that redirect and it seems you should be fine.

Side Note:
I thought that saving the $_SESSION variable might have been a client -server timing issue, because I thought once the header() function was called the server would send the header to the client immediately, but this is not the case. I made 2 test scripts, test1.php and test2.php.
test2.php started session and then is held up in a for loop for str_repeat(“99999”, 5); times. When the redirect happened, (which is after test2.php finished executing) test1.php had the correct $_SESSION variable saved. So it was just because I performed an action on the variable I wanted later on in the script that was redirecting.

Hope this helps you guys :)
Happy Coding!