Using Sherlock Actionbar to change tabs with a List Navigation in the Actionbar in Android, or switch between Activities with a List in the Actionbar.

Long title, I know.

So you have a TabHost and a TabWidget. You also are implementing Actionbars to give your app a more mobile-feel and interaction to it, so actual tabs aren’t that neat. However, tabs offer switching between Activities in a easy way, that works for you, or whatever your reasoning is.

So, you have the Actionbar up top, and you want to implement it so that the user doesn’t switch via tabs, but rather via a list. If you’re here, you probably know what I’m talking about. If not, then it’s a dropdown list that on item selected, it displays a new activity in the area below the Actionbar. I think I just repeated the same thing three or four times.

Anywys, I had a TabHost with a TabWidget and what not, all working fine. Looked okay, just it didn’t flow with the gameplay view. So I set out to change this menu to use an Actionbar and list.

Problem being, SherlockActivity extended Activity, and not ActivityGroup, which as some may know, is the known way to have the whole tab shebang working. So the problem arises that, if you change your Menu that did extend ActivityGroup to the SherlockActivity, you have TabHost.setup(LocalActivityManager). This problem is easily solved by a Google that you use:

		th = (TabHost) findViewById(R.id.thMain);
		LocalActivityManager mLocalActivityManager = new LocalActivityManager(
				this, false);
		mLocalActivityManager.dispatchCreate(savedInstanceState);
		th.setup(mLocalActivityManager);

In the onCreate with savedInstanceState being the Bundle that you pass it.

So no big deal so far. This works perfectly fine really. Then you add in the needed List requirements from Sherlock:

		Context context = getSupportActionBar().getThemedContext();
		ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
				context, R.array.mainmenu, R.layout.sherlock_spinner_item);
		list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		getSupportActionBar().setListNavigationCallbacks(list, this);

R.array.mainmenu is in my XML file in res/values/array.xml. You can Google this as well.

Finally, in the method you implement from the interface: ActionBar.OnNavigationListener, onNavigationItemSelected(int itemPosition, long itemId), you can do a simple:

		if (itemPosition < th.getTabWidget().getTabCount()) {
			th.setCurrentTab(itemPosition);
			return true;
		}
		return false;

So really, pretty easy. Nothing too big, it’s useful as it allows you to switch between Activities with a List, also though, without using Fragments (Which I assume is the “correct” way to do this), or to have custom Views and what not that you change between.

Also, if you want that dirt Tab bar to disappear you want to add to the XML of the TabWidget:

            android:visibility="gone" 

Android: Grabbing pixel xy coordinates when image has been scaled or translated via Matrix.

So you have a view, that has an image.  You move around this image, scaling/translate the matrix to give the effect (I was using a view someone else created: https://github.com/MikeOrtiz/TouchImageView, gives a Map View that you create an image of, in a rough way of explaining it).

His code uses a matrix to alter the image to allow you to move around the screen.  This changes your coordinates, as you probably know because you’re looking at this.

So, the fix is easy, and just takes a bit of thinking, but it was still a pain to figure it out, and I couldn’t find any sources online about it, so I’ll put what I figured out below:

 matrix.getValues(m);
 float transX = m[Matrix.MTRANS_X] * -1;
 float transY = m[Matrix.MTRANS_Y] * -1;
 float scaleX = m[Matrix.MSCALE_X];
 float scaleY = m[Matrix.MSCALE_Y];
 lastTouchX = (int) ((event.getX() + transX) / scaleX);
 lastTouchY = (int) ((event.getY() + transY) / scaleY);
 lastTouchX = Math.abs(lastTouchX);
 lastTouchY = Math.abs(lastTouchY);

So, in general, matrix.getValues(m) stores some values you can access from the matrix you’ve been using. In the View I’m using, it has the matrix as a class variable that can be accessed when needed, along with a float matrix, so it worked fine.

Next four lines are just me grabbing some of the values from the matrix, you can look at the android doc here. They’re the constants up top. I multiplied the transX and transY by -1 as it returned a negative value, which I used a positive coordinate system.

Next, we add the translate to the actual MotionEvent’s x/y value. This gives us our total movement with touch, then divide by scale amount. This moves our position in regards to how much we’ve zoomed in and out.

The abs is just a backup, I don’t believe it’s needed, I was testing other things, but just in case, I’ll keep it there.

Putting Android on HP Touchpad

My brother was lucky and bought one of the HP Touchpads when they went real low in price. Recently though, he couldn’t play movies on it, as the codecs didn’t work correctly or the Touchpad didn’t support it (I didn’t see it, just guessing). He asked me to put on Android for him, so this is basically what I did.
1. Downloading all the stuff
2. Some small commands
3. What happened/Extras!

Downloads

I received a “Unable to find device” on Novacom after putting in the command (Shown in step two), so I had to download this.

I also had the SDK downloaded from HP’s Palm site, so you may want to download this first, then download the previous program to duplicate exactly what I did.  Here is the download.

This is the actual CM9 download.

This is the actual Google Apps.

This is ACME.

This is MOBoot.

This is CWM.

Small Commands

So the “Technical” portion.  Basically, on your machine, go to the furthest Novacom folder, mine was here:

C:\Program Files (x86)\HP webOS\SDK\bin\novacomd\x86>

In this folder, put the download above called ACME.  It won’t be executable by you, but it will know what to do when you enter the command in the cmd (Command prompt, I assume on Linux and Mac you’d use Terminal).  Before this command however, you need to do some work on the Touchpad.  Connect the device as you normally would to the computer.  When connected, on the device, it’ll prompt to enable USB mode.  Say yes.  On the computer, go to the devices folder (Via My Computer) and open up the external device.  Make a directory called “cminstall”.  In here, you want to put the CWM and MOBoot download .zip’s in to this folder.

Now, shut down the device.  But when you go to start it up (Be sure it’s connected to the computer still!), you want to hold the volume up button, (The button closest to the powerbutton).  Once this happens, you want to have your command prompt/terminal still in the previously mentioned path, and input this command:

novacom boot mem:// < ACMEInstaller3

This is the “Magic” portion.  Upon completion, unplug.  If the USB logo stays on, you’ll want to hold all three, the volume up button, the home button (The middle guy below the screen), and then the power button.  Hold until it goes away.  Then go on as normal.

Now, if you start up, you’ll notice that you have WebOS working still.  But you also have a small boot menu.

Now to install the CM9.  You downloaded a .zip file, of which, is named a bit oddly.  Rename the CM9 folder to update-cm.zip.  (I believe it complained about the naming however, so this may change depending on the versions of things, but it tells you on the HP device what to rename it to).  You want to do the same for the Gapps too, rename it to gapps-gb.zip.

Now, run that command above again in the same path, and then it’ll do its thing.  Now you’re done!

Extras

I said their were extras.  I was surprised to see that you can still boot in to WebOS.  So you didn’t lose this functionality, which I think my brother will enjoy.  To do this, you want to hold the power button, click Reboot, then select bootloader, then Ok.  It’ll reboot, and prompt you for selection.  To use this menu, use the volume buttons to scroll, then you use the home button to select an option.

Go-Lang

So I’ve started to poke around Google App Engine and Go.  Honestly, I like it.

1. Thoughts

2. Why

3. Little Tutorial

Go

Go, or commonly search, “golang” is a language made by Google.  Dah dah dah, I’m not a guy who knows much about the language, but I’ve read about it, and don’t want to give false information about it, so read it’s Wikipedia page if you’d like.  I decided I wanted to learn Go several months ago.  Problem was, no reason to learn it.  Not that I don’t enjoy learning new things on my own, I just can’t think of project ideas to actually put it to use.  But I found out about Go several months ago, followed it a bit for about a year, then decided I should learn it.

Go, the language, I do enjoy writing in.  I did some of the go-tour (It got boring after number 17 a couple months ago and couldn’t bring myself past fixing Pi in the tour this time around), but decided that I’d just jump in and go for it.  I usually learn this way, unless it’s something that’s massive and complex, in which case I try to find break down tutorials.  But this was actually successful, I’m getting the hang of the syntax and differences from other languages surprisingly quickly.  Again, I’m not doing anything dramatic with Go, but compared to how I feel learning other things, this went very smooth.

Why Go/GAE?

So I’m working on my Android app and I need some multiplayer work done (Nothing major, honestly just submit some info, get some info, pretty basic).  I looked around for some platforms I can get this on (Mostly to test and get it working locally).  I though of Google App Engine and Amazon’s EC2.  Amazon, your interface sucks.  I’m sorry, but honestly, it’s a massive wall of text with what seems (To me, a guy who just wants to make a small app) like a lot of worthless data that I won’t want or need. Google App Engine however, looks appealing, secondly, it’s more structured and organized, and I feel I can actually do something with it.  That’s honestly why I chose what I chose, nothing to do with specs (I’m making a small app that I don’t expect more than a thousand people playing, I don’t need any fancy stuff, just something that works).  Further, GAE supports Go.  I’m slightly certain you could get it to work, maybe with some hassle, but for what I’m planning, I’m sure it’s more than able.  If not, I could just use good ol’ PHP.

Tutorial, how to use POST/GET, and what about the datastore?

Below, I’m omitting package, imports, and the init, simply because the basic tutorial on GAE goes over this pretty well.

type Griddler struct {
	Id         string
	Author     string
	Name       string
	Rank       string
	Difficulty string
	Width      string
	Height     string
	Solution   string
}

func createHandler(w http.ResponseWriter, r *http.Request) {
	//Creating a Griddler to add to datastore.
	c := appengine.NewContext(r)
	parsed, err := url.Parse(r.URL.String())

	if err != nil {
		fmt.Fprintln(w, "Bad stuff happened in the create handler\t")
	}
	q := parsed.Query()       //Parse the URL.
	griddler := new(Griddler) //Create and set all the data in your griddler.
	griddler.Id = q.Get("id")
	griddler.Author = q.Get("author")
	griddler.Name = q.Get("name")
	griddler.Rank = q.Get("rank")
	griddler.Difficulty = q.Get("diff")
	griddler.Width = q.Get("width")
	griddler.Height = q.Get("height")
	griddler.Solution = q.Get("solution")
	k := datastore.NewKey(c, "Griddler", griddler.Id, 0, nil) //Make a key based on the Id of the Griddler.
	if _, err := datastore.Put(c, k, griddler); err != nil {
		fmt.Fprint(w, "Error during adding item in data store.  Sorry mate.\t", err)
	} else {
		fmt.Fprintf(w, "Added: %s with key %s", griddler, k) //If no error putting in, send back a success message.
	}

}

So, Honestly this code isn’t too interesting. You parse the url from the request, then you just use the query to set some data. Next you get a key for this Griddler (Please note, all Id’s will be a different hash, so first I’ll want to check if this hash exists).

Beyond this, you Put the griddler in the datastore, and you output the result. Prior, I wrote my own URL parser, which I mean, great and all because I control what the app does, but I’d much rather not doing this.

Coding Style

1.  Am I weird?

2.  Spacing

3.  Examples

4.  Final Thoughts

Weird

I always wonder if I’m just an awkward programmer. I totally understand that everyone has their own style and “mark” with programming, however, I’ve never really met someone who likes as much spacing as I do.  Every default/built-in formatter must be replaced or altered so that it works how I like it.  It’s not that their format is bad or not correct, you could really put everything on one line.  But it’s just not like a book.  Like a book, everything is structured, and such is code.  I’ve always loved books that offer chapters.  Eragon, Harry Potter, Game of Thrones, etc.  All three of these titles use chapters often.  I love it.  I can’t stand reading books that are more or less, compact with tons of information, hence why many academic books, I can’t stand.  I think my coding style relates to this.

Spacing

By spacing, I mean new lines, tons and tons of new lines.  My methods will knowingly be on this one tab mark.  I will not have else’s hanging near a curly brace, and their will be new lines between different types of packages, and different purposed variables.  And please, please please please, do not have a whole if statement, condition and execution, on one line.  But at the same time, I like long lines.  No 80 character limit, we aren’t in the 90’s anymore.  We have monitors that support three times that many characters now, so please don’t have your method arguments containing multiple lines.  It’s not comfortable to skip down like this, even with the spacing.  To bring back the book idea, one thought, per a line.  If I’m running a condition, that’s a line.  Then I pre-prompt the execution (Curly brace), on another line, then I give the execution, and to end that off, my closing (Another curly brace).

As My Math Professor Says, “I’ll start off with some examples” **Spoken with an English accent**

The simple statement:

 while(true) { System.out.println("Infinite"); } 

No!  Should be something like:

while(true)
{
     System.out.println("Infinite");
}

I understand this adds a lot of lines for something not needed.  But read it.  What is easier to comprehend?  If I was to add a comment explaining it, I just add it.  I don’t worry about to reference in the comment, I just put it above or on the side (Depending on size).

Another least favorite of mine:

 if(i==0) i+=10; 

Really?  I mean, I understand usually with a line like this, it’s hard to not understand what’s going on.  But it’s just awkward to me.  I’m sure you can guess what I’d rather see:

if(i==0)
{
    i+=10;
}

Finally

Enough of the ramble.  I’ll just leave off with, I like tabs. I don’t like spaces.  Not all editors handle erasing whitespace similarly.  Where-as, I delete a tab, guess what? It deletes that whole block, not that block, including a line or two above it  But alas, I can adapt.  I can also use Astyle (Really love this program) to switch between what a class/company wants, to what I want.  Painlessly simple too.

Distributed File System (CEPH), SSHFS, and More

What all has been happening in the past two to three weeks?

1. Change of plan

2. Learning new things

3. Other projects

Plan Changer

So, previously the main goal was to make a service that would track what all happens on your device, data traffic wise. However, a change has been made, temporarily. While this is still the main goal, I’ll be switching gears towards something else (Look at number two). First though, what’s taken me so long. This extends to point three, but also something to address here. Obviously, I’ll be using other machines to work on everything. Because of this, and it being an university ran system, everything isn’t set up how I normally operate, and I’ve been customizing things to fit how I work. So the past week or two has been me getting used to the changes, not only on the system, but also what I’ll be doing in the future. Rather than jump in to the project, I’m trying to plan out how to go about it and have everything worked out before hand, as this (In my opinion), will make the end shorter.

New Things

The new project is distributed file system, which I’m very interested in, and hopefully can sit-in on our distributed system class here at Wright State. The lab I’m working in contains ~30 machines, each machine with 350 gigs of space, not being used, in its own partition. My professor, Dr. Mateti, has requested that I set up these machines to all collaborate together. He’s recommended the program CEPH, which from what I’ve read thus far, seems to be nice, and simple enough to set up. More about that later.

Also, something I found extremely interesting as I’ve wanted to do this, connect a file system to my local machine that handles files on external servers. I knew it was possible, but never looked in to it, nor have I actually done it. Dr. Mateti has this set up from his home and to multiple machines in the building, and we went over SSHFS. Obviously, SSH is secure shell, and FS is file system. This was the most painless configuration I’ve done for a while.

To set up SSHFS, you first want to make a directory on your local system. Then you run a command similiar to such:

sshfs -o idmap=user remoteUserName@remoteIPOrServer:/your/remote/directory $HOME/YourLocalDirectory

You want to change anything bolded. Once done, go in to the directory, type mount, then enter information requested, and you’re set. Honestly, one of the easiest and most useful things I’ve done for a bit of time.

My Projects

I’m currently working on two personal projects. One of which is a simple app that I do in spare time in my room, probably two or three hours a week. Another is working on Fennec, Firefox’s mobile client. I’ll write more about each of these, but a summary for now.

Fennec, I’m doing general bug fixes. This project is very helpful to me for several reasons. First, I’m learning how to effectively use a large code base. While not wasting anyone’s time but my own, I can go through the files, do what I can to make sense of them, and Log.d every couple of lines and see what’s happening. Secondly, I’m seeing how people who program for a living, program. This is surprisingly not common in academics, even though I believe it should be enforced (This, and revision control, whether it be git or Mercurial, etc.). It’s extremely important, in my opinion, to be able to read and understand others code, be able to implement it effectively, and come up with ways to do this. Lastly, I’m working on something that sees the light of day, using technologies that exist and are used everywhere, collaborating (I’m on their IRC 24/7) with others, helping out a public and open-source project, etc. etc. Their really is no “lastly”.

Pic2Griddler is an app that I’m developing. Yet Another Puzzle App, in other words. This is more for my personal use, as I’ve been in to Griddlers for several months now, but I’m not a fan of paying for an app that I’d get a couple weeks out of, then be done. So my solution, make an app that gives me unlimited amount of time to work on. I’ll be posting more in-depth about this later, you can read the README on the GitHub page on the side bar.

Up and Running

Rundown:

1. Easier then I thought.

2. Personal Updates

Easy

As of now, this was not difficult at all.  Booting up and finished boot.  To be honest, this wasn’t difficult. I’m not saying there were not problems, because as always, there were.  But it went smooth.  As in…  I did it in about two hours Monday.

So some tips for future/others.

Most important command:

make mrproper

Now, this lil’ bit of command cleans up your mistakes.  You will make mistakes.  Luckily, this command also takes about 10 seconds to run.  So no big deal.  This saved me countless of times and lots of time.  I am pretty thorough with my work, so I like to make sure everything is at least some-what back to the start/normalized.  I don’t like having a chance that what I did won’t be reproducible because one file was where it wasn’t normally because of a previous mistake.  So I highly recommend and love this snippet of a command.

Beyond that, I found it most difficult of what commands to run.  Going off memory, I believe it was along the lines of:

ARCH=arm SUBARCH=arm CROSS_COMPILE=arm-eabi make goldfish_defconfig

This allows you to set the variables temporarily, makes the config file, then you can run a normal “make”.
That’s really all you need to do after you download the goldfish kernel and you’re golden ;).
Personal Changes

So, my own development machine has changed a lot.  Previously, I had a rule about not working on my own machine, but downloading the Android source takes about 12 hours, building takes more hours, and the kernel takes about as long, so I’d be lucky if someone didn’t turn off the computer in a local lab while this went on.  So that rule, for the time being, is scrapped.  But I’ve also noticed, everyone uses Linux.  I have nothing against Linux, I think it’s great, and to be honest (After using it for a weekend), I’ll be happy about it (Besides the no Spotify support, that irks me, and no, Wine does not count as I get pop up overlays and those are annoying too).  But I don’t mean everyone uses Linux as in, everyone should use it, or that if you don’t use it, it’s bad.  I just mean, every documentation is built off the idea that the end user is using Linux.  Further, if you have a problem?  Guess what, their is a solution.  I remember back in middle school when I first installed Linux, finding solutions was…  awkward.  Every solution wasn’t much of a solution as it was an idea towards a possible solution, which combined with me being about thirteen (Is that middle school?), heck if I could put two and two together in a terminal, I didn’t even know about man pages!

So, no more tangent, I now am running Linux.  Ubuntu 12 (I don’t like it, but again, many projects are geared towards Ubuntu, also lots of support) is now my primary OS, but I still have Windows 7 for now, in case I need to recover something.  I’ll give a list of my favorite command line and other utilities on Ubuntu, but I already have several that keep me sane.

Also, I’m an Emacs user.  I do use Eclipse for personal projects, but for most of my programming, I’ll be trying to use Emacs.  How bad it has gotten?  I’ve done the cut/paste command in a web browser…  Ctrl – W is close tab for most browsers, so that got old fast.  This was a goal of mine sense school started though, and as of now, I love Emacs, and I know I don’t even know half of it.

Back to programming though!  I have built and got Jelly Bean working on this new machine all in one swoop.  No problems.  Same goes with the Goldfish kernel, also, no problem.  Further, I’m trying to pick up Open Source.  I’ll give another update of that when I actually complete something though.

I’ll be meeting with Dr. Mateti to discuss what to do next tomorrow.  Until then… Yeah!