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.