Wednesday, January 29, 2014

Getting Generic Types in Java

Due to some crazy inter-process communication, and serialization I needed to do I ended up with an interesting problem.  Now I will admit I was probably over complicating things, or doing something I didn't need to do, but I had a lot of fun writing this and learning about the internal workings of the JVM.

The problem, I needed to be able to check if a specific method of Class that implements my interface, if it's signature matches objects that I receive elsewhere.  Now I didn't want to just execute this method and throw an exception since it would occur at a later time.  I needed to check at the moment I get the parameters so I can give the user (probably more likely the programmer using my library) a reliable and easy to understand error message if it doesn't match.

After looking on stack overflow most posts that were looking to do the same thing the responses where you can't.  Now I'm not generally one to let things go and if someone says you can't I try to prove them wrong.  Well anyways here is some sample code that demonstraits how you can get this.  There are some catches and some special cases I actually handled in my code, but this should get the point across. It's by no means elegain and I would recommend you try to find better ways, but if you really need to do this, well here you go.


Thursday, January 23, 2014

Run Netflix on Linux round two

A post or two ago I suggested using a VM to run netflix.  Although this isn't the most ideal option it does happen to work.  Well since then I have found a more ideal option.  Some very nice people have created packages for different linux systems that put together wine, silverlight and some other packages to create an easy to install netflix application for your system.


Tuesday, January 7, 2014

Download an HTML Table With JavaScript

I needed to download a list of users for the same website I talked about in my last blog post.  For whatever reason Joomla doesn't have an export function.  We are using a very old version, I don't know much about Joomla, and I didn't have access to the database.  So, I did the only thing I knew how, loop through the table grabbing the values with JavaScript.

Here is how I did it.  I post it here in hopes that is helpful to someone else, or myself when I forget how I did it in a few days (hello future self).


Sunday, January 5, 2014

Adding Water Marks to Images: Bash Script

I got tasked with building a website for the Boy Scout Troop I am affiliated with.  With that came lots and lots of photos people wanted to view.  About 6.6GB in total.  These images also needed to be water marked.  We are using Square Space because of it's ease of use, but there is no way to have it automatically watermark the images.  So I stitched some scripts together and came up with the following.

It's not the prettiest thing I have ever written but it get's the job done.  There are a lot more options that could be added in; oh well maybe another day.

Saturday, January 4, 2014

Changing My Diet

I recently completely changed my diet. I went from eating a pound of bacon for breakfast to eating bananas and apples. From eating McDonalds for lunch to a salad, and from eating hot wings for dinner to eating, well another salad. People have asked why and rather than go into the gory details with everyone I thought could give them a little overview and tell them they can come here to check out the whole story.

I was having some strange issues, some related to food others I had no idea where related to food. One issue was my stomach hurting really bad, especially at about 6am. Another was certain foods would cause me to be running for the bathroom about a half hour after eating them. I couldn’t put my finger on what foods were causing this issue, and I thought it was more that I was eating fast food and such and though everyone had these issues.

My mother had some issues, although she never told me exactly what they were, I know one was a breathing issues. After lots and lots of tests from doctors she never received any definitive answer on what the issue was. She was pretty much expected to be taking steroids for the rest of her life. Not something to look forward too. She eventually come across something called the LEAP test/diet. She took a blood test and found out what foods she was sensitive to, cut these foods out and amazingly her breathing problems went away.

Because of my symptoms my mother suggested that I get this test as well. The test itself is very simple. You go get your blood drawn and in a week or so you receive the results by mail.

After receiving these results I went on an elimination diet, basically for two weeks I ate only things that were very low on my sensitivity results, and then after the two weeks I slowly started adding other foods back in. This is where everything changed.

The first thing I noticed while on this diet was that I was always hungry, my stomach would feel like it was going to burst, but I wanted to eat more, it was very odd. Secondly, after a day or so, and lasting for about a week and a half, I was depressed. Now I like to think of myself as an intelligent person, and I know I didn’t have a reason to be depressed, and I knew that both of these symptoms seem to be coming after I made a pretty big change in diet, so I started thinking, and my conclusion was, this was the first time in my life I had not had sugar, or a bunch of random chemicals in my diet. My diet at this point consisted mostly of potatoes, apples, and beef.

At about two weeks I started to feel a lot better. The symptoms both from changing my diet, and the symptoms that caused me to go on this diet had seem to have all gone away. Other things I had never thought were food related stopped too. Feeling extremely tired at 1pm every day, had gone away!. For the last 6 years I would get a cough in the fall and it would last most of the winter, gone! Weird eye twitch I thought was from staring at computers for about 80% of my day, gone! Back and knees being sore (not a good sign at my age, but I assumed it was from all the skateboarding and snowboard I did), gone! I was feeling amazing, but with the realization of what caused the depression made me do a lot more research.

Now, I am not going to post links or even go on about MSG, or sugar or anything else, but the research I did scared me. Where our food comes from, scared me, what is being done to it and so on, all scares me. From there I started to create rules about what I was going to eat. The first and easiest one was no white powders. No more sugar, salt, that sort of thing. But as I continued doing my research and looking at different ways of eating, I started to realize I need more fruit and veggies in my diet, I also wanted less chemicals. This lead me down the path of vegan, since milk, eggs, and meat have lots and lots of chemicals poured into them (on a side note, not eating meat is significantly more environmentally friendly too). But, as I looked at the vegan lifestyle I found that well, oreos are vegan! I needed something better something that made sense.

I finally found raw vegan. Now I don’t fully embrace it, I shoot for about 80% raw (I need my coffee). This works for me, and I feel great! Anytime I stray from the path, there are generally noticeable consequences that make me immediately go back. Some other crazy things I have noticed since I have made these changes are my hair is softer and fluffier, my finger nails grow crazy fast, I don’t get pit stains. Yeah, that last one is weird. I have been rock climbing and where I use to be super sweaty and gross, I’m not. It’s just gone! Another thing I have found, is I no longer make food an event. I don’t look forward to going out to eat. I don’t get cravings for a food. I no longer live to eat. I eat to live, which actually makes me realize a lot of strange behaviors I use to have.

In conclusion, I have decided to adopt a mostly raw vegan diet, consuming mostly non cooked foods. The foods I do consume that are not raw are still vegan. This choice was made after doing lots of research about our food, where it comes from and what’s in it. I have noticed some beneficial changes in my life, the most notable one being I don’t feel tired and groggy all the time. I feel great!

Sunday, December 22, 2013

How to Run Netflix in Linux

I have played with different options for getting Netflix running on Linux, and the best and easiest option I found was to install Windows in Virtual box.  Yes, it's not an elegant solution but it works and it works well. Now if you are using Linux you probably don't have a spare Windows key lying around so to get past that hurdle we will use an IE Testing VM.

Go to http://www.modern.ie/en-us/virtualization-tools#downloads and download a version of windows you would like.  I am using Windows 8 with IE11.

wget -i https://az412801.vo.msecnd.net/vhd/VMBuild_20131127/VirtualBox/IE11_Win7/Linux/IE11.Win7.ForLinuxVirtualBox.txt
chmod +x part1.sfx
./part1.sfx

You will now have an ova file.  This file is used to import the VM into virtual box.

Now you will need to install virtual box.  This will depend on your distro.  I am on fedora so it would be sudo yum install VirtualBox.  There might be a little more to it than that depending on your distro/setup.

Finally import the ova file.  Open up virtual box and go to File -> Import, click next like you would if you were on windows unless you really need to update the vm options.  Once complete start up the VM.  Once started you can open up IE and navigate to Netflix and give it a try (You can install Chrome, but I'm not sure if that is against the License).


If you want to go full screen or run seemless mode you will need to install guest additions.  This can be done by clicking Devices -> Insert Guest Additions CD Image...


Fedora, The One True OS

I have officially switched all my main computers over to Fedora Linux.  A friend of mine at work uses Fedora, and recently I had some issue with Mint, at the time my favorite distro, so I deiced to try Fedora out.  I also switched over from GNOME, to KDE and that has been a great expierence also.  The look and feel are great, and all of the options are really easy to setup compared to some of my recent experiences with GNOME 3 / Unity.  I got all of my hot keys setup the way I like and I feel very productive.  Anything I could think of I was able to find a program, setting, option or theme for.

On top of that, always having the latest and greats programs is really nice.  No more trying to figure out how to get the latest version of something and having to do hacky things to make it work.  I recently updated from Fedora 19 to 20 on my laptop and had no issues.  I went to the wiki, ran the commands it said, after a few minutes I was back in business.  Straight forward and easy, couldn't ask for more in an OS.  Having Steam run on linux is pretty great too.  I've added to my rules of purchasing games, does it run on linux.  I don't play many games but all the ones I have purchased from the Humble Bundle have worked so far.

TLDR; switched to Fedora, and it's awesome. I would recommend you try it out.

Friday, December 20, 2013

I'm Back

I haven't posted in a while, but I have decided to come back.  I was trying to create my own blog using Symfony CMF using the BlogBundle, but it isn't quite baked yet. I am working on updating my website from node.js using express to use Symfony CMF.  So far it's been an interesting experience.  I had to learn PHP, then Symfony, and finally the CMF.  Anyways, hope to be posting more soon!

-Andrew

Wednesday, March 7, 2012

Where Text Messages Are Stored in Android

I have been playing around with apps that can send text messages and was wondering why ones sent from my app did not show up in the text message application. This also got me wondering where are the messages stored and so on.

Well I did a quick Google search and couldn't find anything of real use. So I decided to check my phone.

Using adb I got a command line to my phone.
(Note you must have a rooted phone to do what's next)

I did a quick find to see where messages would be kept.
#find . -name "*sms*" -print

This displayed a good amount of results but the most promising was
/data/app/com.android.providers.telephone/databases/mmssms.db

Knowing that you can open up .db files with sqlite, I used the command
sqlite3 /data/app/com.android.providers.telephone/databases/mmssms.db

After using a few command like .tables and .schema as well as a few queries I was able to conclude that this is where all the text messages are stored from the default message app on android phones.

Now, I know there are a few other apps out there that you can install from the marketplace that will leverage this database, specifically Handcent. I would assume that the way they do it is leveraging the code provided by Google here and tying into this database.

Hopefully this will be helpful to anyone else who was curious about this, or thinking about making there own SMS application.

Tuesday, March 6, 2012

Creating Custom Buttons in Android on the Fly

I needed to be able to set the border color of a button or the "stroke" as it's referred to in android on in the code and not the xml, because it was based on a color received from a web service. Everywhere I looked people were easily creating custom buttons with xml, but these values, at least with what I played with could not be changed on the fly.

Now another big issue is that the xml for shapes does not seem to correspond 1 to 1 with their code counter parts. I can easily create a rectangle with one color as the boarder and another color as the fill in xml not so easy in the code.
Here is the solution I came up with for my custom button in code.
First I created a custom shape class and override the onDraw method.


private class CustomRect extends ShapeDrawable
{
    Paint fillpaint, strokepaint;
    private static final int STROKE_WIDTH = 10;

    public customRect(Shape s)
    {
        super(s);
        fillpaint = this.getPaint();
        strokepaint = new Paint(fillpaint);
        strokepaint.setStyle(Style.STROKE);
        strokepaint.setStrokeWidth(STROKE_WIDTH);
        fillpaint.setAlpha(128);
    }

    @Override
    protected void onDraw(Shape shape, Canvas canvas, Paint fillpaint)
    {
        shape.draw(canvas, fillpaint);
       shape.draw(canvas, strokepaint);
    }

    public void setStrokeColor(int c)
    {
       strokepaint.setColor(c);
    }

    public void setFillColor(int c)
    {
       fillpaint.setColor(c);
       fillpaint.setAlpha(128);
    }
}


This class is geared very specifically to how I wanted the rectangle to be but should be a good starting point to anyone else trying to do the same.

Next create the shapes you want to use for your button.


Final CustRect cust_rect_enabled = new CustRect(new RectShape());
cust_rect_enabled.setStrokeColor(myColor1);
cust_rect_enabled.setFillColor(myColor2);



Final CustRect cust_rect_pressed = new CustRect(new RectShape());
cust_rect_pressed.setStrokeColor(myColor2);
cust_rect_pressed.setFillColor(myColor1);


Now you want to add these Drawables to a StateListDrawable, in order to handle what the button looks like when it is pressed.


StateListDrawable state_list_drawable = new StateListDrawable();
state_list_drawable.addState(new int[] {android.R.attr.state_pressed}, cust_rect_pressed);
state_list_drawable.addState(StateSet.WILD_CARD, cust_rect_enabled);


This will tell the button what to use depending on what state it is in. For everything but the pressed event it will be the cust_rect_enabled, when pressed it will be cust_rect_pressed. This works for what I need it to, but I would image other projects would need other states to be handled as well. You can find the other states you may want to set in android.R.attr, the ones you will want to use will generally start with state_. I did play around with a few of these and was having some issues with getting them to do what I wanted, it is very different than the states in the xml setting them to true and false.

Lastly you want to add a default regular android button to the layout file you are using, and set the height and width to what you want it as. Then in the code you get your handle to it the usual way of calling findViewById, and set the background Drawable.


Button myCustButton = (Button) findViewById(R.id.buttonQuizNextQuestion);
myCustButton.setBackgroundDrawable(state_list_drawable);


And that is all there is to it!

Thursday, June 30, 2011

Creating Thumbnails C#

I was creating a website for a friend, and the photo gallery I decided to use needed thumbnails of all the pictures she had given me.  Now rather than do this by hand, or try and use the uploader I made on the site that can only handle three pictures at a time, I decided to make a program to do this for me.
First I create a folder in the path that was specified by the user, the string folder.  Then iterate on each file that is in the directory.
Directory.CreateDirectory(Path.Combine(folder, "thumbs"));
foreach (string pic in Directory.GetFiles(folder))
{
switch (Path.GetExtension(pic.ToLower()))
{
              case ".jpg": break;
case ".bmp": break;
case ".gif": break;
case ".exif": break;
case ".png": break;
case ".tiff": break;
default: // not a recognized format by
//System.Drawing.Bitmap so skip and go to the next
continue;
}
//more code to follow

I do some funky things to calculate the width and height that gets the images sized down to the way I want them.  I don’t think it’s really important and if you’re trying to implement this you should feel free to calculate the ratio any way you like.   Next, I create an empty bitmap to the height and width I want.  Then use the graphics class to load in the bitmap.  The settings SmoothingMode,  InterpolationMode, and PixelOffsetMode are all ones I found to keep the thumbnail looking good.  The last call with the graphics class, DrawImage is loading the image you want to resize onto the bitmap previously set up, shrinking it to fit, give you a nice thumbnail in memory.  The last thing to do is write the image to disc.  You can see in the code, I place the thumbnail in the thumbs directory previously created and use the name of the original file with thumb- prepended to the name.

using (Bitmap newImage = new Bitmap(newWidth, newHeight))
{
using (Graphics gr = Graphics.FromImage(newImage))
       {
              gr.SmoothingMode = SmoothingMode.AntiAlias;
              gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
              gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
              gr.DrawImage(new Bitmap(pic), new Rectangle(0, 0, newWidth, newHeight));
}

       string imgName = Path.GetFileName(pic); //get the images name
       //write the image to thumbs directory with the thumb-imageName
       newImage.Save(Path.Combine(folder, "thumbs\\thumb-" + imgName));
}
} //end foreach

That’s really all there is to it folks.  If you for some reason need to create a bunch of thumbnails and don’t want to implement it yourself, here is the executable I created, along with the source.

Friday, May 20, 2011

Why I've Been So Busy Pt2: Compilers

Took my a while to get time to write this up.  I've been busy moving stuff and getting back to work now that schools out.  Anyways, I took a compilers course last semester, it was a lot of work but it was fun and I learned a lot.  I mainly took it because of this blog post http://steve-yegge.blogspot.com/2007/06/rich-programmer-food.html that was in Hacker Monthly.  After reading it I was all about taking compilers.  Then a month went by and it was time to sign up for classes.  I almost didn't take compilers, in fact I had to read the article again to convince myself to.  I am glad I did.  I have a better understanding how they work, and what errors mean just to name a few things.

Anyways, in the class we made our own interpreter for a language KLOGO, specified by the professor.  It is a "logo" like language, in that it interprets your commands and moves a turtle around to draw a picture.  We used flex and bison for our parser/scanner respectively and the graphics professor at my school programed the graphics part we were to interface with.  After the scanning and parsing an AST was produced, that was type checked and finally executed the code.  It sounds pretty simple when I say it now, but it was a lot of work.  Below is an image of my interpreter interpreting a program I made.  It demonstrates most features of the interpreter.  Note: the output in the terminal was to demonstrate to the professor that it actually was working, and not just an image I drew.


Monday, May 9, 2011

Why I've Been So Busy Pt1: Ray Tracing

I've been super busy over the last semester.  The main reason would be my graphics class.  The main project is building a ray tracer.  Here are pictures from start to finish.

Circle Intersections Working

Triangle Intersection Working

Multiple Triangle Intersections

First 3D Object: Lambertian Shading

Multiple Sphere Intersection

1000 Spheres


Blinn-Phong Shading Working

Mirrors Working


Playing With Mirrored Spheres

First part of the project done (many hours later), next step bounding volume hierarchy, distributed rays, instancing and loading meshes. 

BVH working, 1 Million Spheres taking about 12 minutes, 10 depth reflections picture size 1000x1000

Anti-Aliasing


Image for Picture Contest

Spock



Soft Shadows and Instancing


Full Screen Photo Gallery - I made some of these to be wallpapers so feel free to use them as such.  Otherwise if you use them anywhere else just give me some credit.



Thursday, September 23, 2010

Instructions on How to Use a Wiimote in Ubuntu


Requirements: You need a wiimote, a Bluetooth dongle, Ubuntu 9.10 or higher (will work on lower version just not these exact instructions).
Recommended: Source of infrared light, candles are cheap and easy to use or you can use and infrared LED like I do.

Step One: Add the WiiCan PPA
                                sudo apt-add-repository ppa:wiicanteam/ppa && sudo apt-get update

Step Two: Install WiiCan
                                sudo apt-get install wiican

Step Three: Navigate to WiiCan and try it out.
                                Applications -> Accessories -> WiiCan,

You should see a wiimote icon next to the volume control, generally on the right side of the upper panel.  Click this and select mouse. Hold down the wiimote buttons 1 and 2, until the LEDs start to flash.  It should connect and you are in business.  Now try disconnecting and reconnecting and closing WiiCan and doing it over again.  If it still works after that you are set if not procede to the next step.  (WiiCan only works the first time I connect after I install then freezes up and gives me errors every time after).
Step 4: So, if WiiCan does not work like it should, use sudo wminout, in the terminal.

Optional: If you want it to connect faster get your wiimote’s MAC address and place it behind the wminout command.
                                Type 
                                lswm
                                Then hold down the 1 and 2 buttons on the wiimote
                                sudo wminout XX:XX:XX:XX:XX:XX <- MAC address returned from previous command.

Optional Configuration:  There are many files that can be used with your wiimote found in the /etc/cwiid/wminput folder.  By default for both WiiCan and wminout the default file is used (listed as mouse in WiiCan).  To change anything in WiiCan or add another configuration file just simply right click the icon and go to properties, then add or edit a file (check out the files in /etc/cwiid/wminput and just copy and paste one you like).  For wminout, you can simply put –c followed by the file name you wish to use.  Example; wminput –c mouse.  Any custom file you want to make just add to the /etc/cwiid/wminput or edit one of the already existing.

For more information about the commands you can use type man wminput in the terminal.