05 July 2012

Cloning a remote branch in git

My current project at work uses git, and I have always been a CVS/SVN baby so I'm still trying to find my way around it. Today I wanted to clone a remote branch to my local computer. This remote branch also has submodules, so I want to get those too.

This assumes that you use Git Bash. First, navigate to the folder in you local computer where you want git to clone the remote branch. Once there, we can start cloning the repo. The following steps do the dirty work:
$ git init
$ git fetch <git url> <branch name>:refs/remotes/origin/<branch name>
$ git checkout -b <branch name> origin/<branch name>

This retrieves the contents of the remote branch and copies it to our local computer in a local branch (confused yet?). To update our copy of the submodules, the following commands should work:
$ git submodule init
$ git submodule update

22 May 2012

Adding Preferences On-The-Fly

(Make appropriate whooshing sounds)

Today, I will show you how to add preferences to your SharedPreferences (confused yet?) programatically. In an app that I am doing, I wanted to present a ListPreference to the user from the SharedPreferences page. However, the values contained in this list is not known at compile time. (Think Facebook groups that differ per user, or Twitter lists, or things like such, and I want to set a default option.)

Anyway, here's what I did to make this work. I added a preference in my PreferenceScreen, a sort of placeholder, if you will. Here is my ListPreference:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

 <PreferenceCategory android:title="@string/pref_settings_header">
  <ListPreference android:key="@string/pref_key_default_group" android:title="@string/pref_default_group"/>
 </PreferenceCategory>

</PreferenceScreen>
And then in the class that handles the SharedPreferences, I get the values that I want displayed and plug them in to the placeholder preference. I will not bother to explain here, just heavily commenting the code:
public class UserPreferences extends PreferenceActivity {

 private static final String LOG_TAG = "UserPreferences";
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // Load the preferences from an XML resource
  addPreferencesFromResource(R.xml.my_apps_prefs);
  
  // Manage the "Default Group" preference
  addDefaultGroupPrefs();  
 }
 
 
 private void addDefaultGroupPrefs() {
  // Get our "placeholder" preference. I prefer to use strings instead of hard-coding the preference key.
  // The value passed into findPreference is the value in the android:key parameter in the preference XML file.
  ListPreference groupsListPrefs = (ListPreference) findPreference(getString(R.string.pref_key_default_group));
  
  // Get your groups, this may come from a utility class or wherever
  // In this example, I have a custom object that has a "name" and an "id"
  List<CustomGroup> myGroups = getMyGroups();
  if (myGroups == null) {
   // Disable the preference if we don't have a list
   groupsListPrefs.setEnabled(false);
  } else {
   // Enable the preference if we have a list
   groupsListPrefs.setEnabled(true);

   // We split the list into names and IDs
   // We show the names in the list, and the values as the entries
   List<String> groupNames = new ArrayList<String>();
   List<String> groupIds = new ArrayList<String>();
   for (CustomGroup group : myGroups){
    groupIds.add(group.getId());
    groupNames.add(group.getName());
    
    // I guess you can add directly to a String[]
    // just make sure the order is preserved (name1=id1, name2=id2, etc)
   }
   
   // These should be user-readable strings
   groupsListPrefs.setEntries(groupNames.toArray(new String[groupNames.size()]));
   
   // These are the actual values to be saved in the XML file
   groupsListPrefs.setEntryValues(groupIds.toArray(new String[groupIds.size()]));
  }
 }
 
 private List<customgroup> getMyGroups(){
  // do work here, query server or db or whatever
  // return the list of groups
 }
}

And we're done. :)

17 May 2012

Inspecting your Shared Preferences

Did you know that you can look at your SharedPreferences file?

If during development you want to inspect what your SharedPreferences now contain, you can pull a copy of the XML file from the emulator.

In Eclipse, you can do that by following these steps:

  1. Open the DDMS perspective (Window > Open Perspective > Other > DDMS).
  2. Click on the File Explorer tab and navigate to your app's data folder (that's under data/data/<your package name>/shared_prefs/).
  3. Choose the file you want to download.
  4. Click on the Pull file from device button.

26 April 2012

13 April 2012

Adding a float value to your resources

Earlier today, I was trying to figure out how to add a float value to constants file. What I used to do was add a string value in my strings.xml, retrieve the string value, and convert it to float.
float floatFromFile = Float.valueOf(getResources().getString(R.string.my_float));
I was trying out something new but it wasn't working, so I decided to look for a more accepted solution. Google led me to this StackOverflow question. I was on the right track after all! I think the accepted answer is incomplete, or not clear enough for my purposes. I ended up having this entry in my dimensions.xml file:
<item name="face_feature_marker_size" type="vals" format="float">2.0</item>
And then in my code, I retrieve the value as:
TypedValue tempVal = new TypedValue();
getResources().getValue(R.vals.face_feature_marker_size, tempVal, true);
float markerSize = testVal.getFloat();
I ended up having more lines of code with no idea if this is more optimized. Let me know what you think!

10 April 2012

Quick Tip: git cloning

A user-friendly way of cloning a git repo is through the eGit plug-in in Eclipse. But sometimes, especially on Windows machines, Eclipse has trouble cleaning up after itself after completing a clone operation. The best workaround for this is to clone the repo from git bash and then import the repo in Eclipse.
default@ZDOMINGUEZ-T420 ~
$ git clone git@github.com:<your git repo> <local folder to check out to>
When git finishes cloning your repo, import it to Eclipse.
Browse to the folder you checked out to, click OK, and the newly-cloned repo should now appear on the Git Repositories view.

14 February 2012

Selenium and File Downloads

Lately, one of my tasks has been to automate regression tests on one of our apps. Since this is a web app, we are using Selenium. Here, I enumerate the steps to configure Firefox for file downloading using Selenium and JUnit by foregoing the downloads dialog box.

Setting up the profile
We will create a new profile to be used for testing. A profile is simply a set of configuration that Firefox will use when you start it. You can use your existing profile as well, but I opted to create a new one so that the Firefox instance for testing will be as pristine as possible (i.e. no unnecessary plugins, no bookmarks, etc). A note: I am working on a Windows machine.
  1. Bring up the command prompt and start the profile chooser by typing in firefox.exe -ProfileManager -no-remote
  2. Click on Create Profile.
  3. Enter the profile name you wish to use. I suggest you include the name of your project instead of just naming it "Test".
  4. Click on Choose Folder and navigate to the folder where you wish to store the profile files. Make sure you can access that folder easily; write the path down because you will need this in JUnit.
  5. Click on Finish.
  6. You will be brought back to the profile chooser dialog. Click on the Don't ask at start-up checkbox.
Note: To make Firefox use your original configurations when you are browsing, bring up the profile chooser dialog, choose the profile named default, and start Firefox.

Configuring Firefox
Now we will configure Firefox to behave like how we want it to.
  1. From the profile chooser dialog, highlight your test profile and click the Start Firefox button.
  2. (Optional if Firefox is not your default browser) Uncheck "Always perform this check when starting Firefox".
  3. Click on Tools > Options. We will go through the steps for each tab in the Options dialog box.
  4. General tab
    • Under Startup, choose Show a blank page from the When Firefox starts dropdown.
    • Under Downloads, uncheck the Show the Downloads window when downloading a file checkbox.
    • Still under Downloads, click on Browse for the Save files to option. Navigate to the folder where you want Firefox to put downloaded files. Take note of this folder as well because we will use this in JUnit.
  5. Tabs tab
    • Uncheck Open new windows in a new tab instead and uncheck all warnings.
  6. Content tab
    • Uncheck Block pop-up windows.
  7. (Optional) Privacy tab
    • Under History, choose Never remember history from the Firefox will: dropdown.
  8. Advanced tab
    • Under the General sub-tab, uncheck Use autoscrolling and uncheck Always check to see if Firefox is the default browser on startup.
    • If you will be using a proxy server, choose the Network sub-tab and input your proxy settings there.
Adding MIME Types
Now we need to tell Firefox how to handle downloading each specific type of file.
  1. Navigate to the folder where you saved your custom profile and open the mimeTypes.rdf file in a text editor.
  2. Add the following lines towards the end of the file, right above the closing </RDF:RDF> tag.
  3. <RDF:Seq RDF:about="urn:mimetypes:root">
     <RDF:li RDF:resource="urn:mimetype:text/plain"/>
    </RDF:Seq>
       
    <RDF:Description RDF:about="urn:mimetype:handler:text/plain" NC:alwaysAsk="false" NC:saveToDisk="true">
            <NC:externalApplication RDF:resource="urn:mimetype:externalApplication:text/plain"/>
    </RDF:Description>
    
    <RDF:Description RDF:about="urn:mimetype:text/plain" NC:value="text/plain" NC:editable="true" NC:fileExtensions="txt" NC:description="Text Document">
     <NC:handlerProp RDF:resource="urn:mimetype:handler:text/plain"/>
    </RDF:Description>

    What we did there is we told Firefox to directly download files with MIME type text/plain. If you need to test downloading other file types like .doc or .pdf, you would need to add their MIME types to this file too.
  4. There are two ways to add MIME types to the mimeTypes.rdf file.
    • Via the Firefox Applications GUI
      • From Firefox, choose Tools > Options > Applications
      • If you have previously downloaded a file of the required MIME type, look for it in the list. In the dropdown menu on the right, under Action choose Save File.
      • If the MIME type you want is not in the list, you would need to go to a website that allows you to download a sample file. If the file downloads automatically without showing the download pop-up, you would not have to do anything. If the pop-up shows up, activate the Save File radio button and check the Do this automatically for files like this from now on checkbox and click Okay.
    • Manually editing the file
    •  Note: This is generally not the advised way to edit the file due to its complexity. Care is required!
      • Open the mimeTypes.rdf file.
      • Look for the RDF:Seq node and add your desired MIME type.
      • <RDF:Seq RDF:about="urn:mimetypes:root">
           <RDF:li RDF:resource="urn:mimetype:text/plain"/>
           <RDF:li RDF:resource="urn:mimetype:application/pdf"/>
        </RDF:Seq>
      • Add the RDF: Description nodes for that MIME type.
      • <RDF:Description RDF:about="urn:mimetype:application/pdf" NC:fileExtensions="pdf" NC:description="Adobe Acrobat Document" NC:value="application/pdf" NC:editable="true">
         <NC:handlerProp RDF:resource="urn:mimetype:handler:application/pdf"/>
        </RDF:Description>
        
        <RDF:Description RDF:about="urn:mimetype:handler:application/pdf" NC:saveToDisk="true" NC:alwaysAsk="false" />
To use this profile in our Selenium test:
FirefoxProfile profile = new FirefoxProfile(new File("path/to/your/profile"));
  
// We can also set the download folder via code
File testFile = new File("path/to/download/folder");
String downloadFolder = testFile.getAbsolutePath();
profile.setPreference("browser.download.dir", downloadFolder);
  
WebDriver driver = new FirefoxDriver(profile);
And you're done. :)

20 December 2011

MongoDB and Authentication

By default, MongoDB allows access to the database without authentication. Adding a user with a username/password is easy, but authenticating might be a bit tricky since the official documentation does not say the command directly.
First, we add an admin account. Navigate to the MongoDB directory on your machine then start the database.
$ ./mongo
> use admin
> db.addUser(adminuser, adminpassword)
Switch to the database of your choice and add users to it.
> use foo
> db.addUser(myuser, userpassword)
This adds a user myuser that has read and write access to the database. If we want a user with read-only access, set the third parameter for addUser().
> db.addUser(guest, guestpassword, true)
You can check for users with access to a particular database like thus:
> db.system.users.find().pretty()
{
        "_id" : ObjectId("4ee9863d954eb7168e07089d"),
        "user" : "zarah",
        "readOnly" : false,
        "pwd" : "70581bfb1e32e2286df11fe119addc7a"
}
{
        "_id" : ObjectId("4ee98658954eb7168e07089e"),
        "user" : "guest",
        "readOnly" : true,
        "pwd" : "88558f1ece63fa0b528012b9840bd9de"
}

Now stop the MongoDB server and restart it with authentication enabled.
$ ./mongod --auth
> mongo foo -u myuser -p userpassword
where foo is the database that myuser has access to.
You can now read and write into database foo. Notice however that querying for databases would result to an error:
> show dbs
Mon Dec 19 17:21:20 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 }

Exit MongoDB and login again, this time using the read-only account. If we try inserting a document, an error should appear:
> db.foo.insert({"title","MongoDB Authentication Test"})
unauthorized
The read-only account can query for collections and use find() and its variations. It can't, however, query for databases.

06 December 2011

Hello, it's me again.


To my two readers out there, hello! It's been a while since I posted here. I was transferred to another (non-Android) project and lost all my Internet privileges, hence the silence. I still can't believe almost every other site is blocked by the office firewall! Makes software development ten times harder. Ugh.

Anyway, as of the last three months, I have been working on backend development (J2EE). I don't know a lot about it, and the past months have been a great journey of learning. It is quite a shock being exposed all in one go to so many technologies and tools, I'm lucky my head didn't explode.

Happy as I am to learn new things, I am quite sad to leave Android development behind. I'm still hoping though that I would be given the chance to go back to it, and pick up where I left off. Hopefully the Android train won't be too far off by then.

In the meantime, I will be posting stuff that I've learned while working on J2EE, and hopefully somebody can learn from my mistakes. :)

07 September 2011

Where's my R.java?

This afternoon, I tried importing an existing project into Eclipse. Doing a Project > Clean usually clears up the R.java not found errors, but this time it didn't work. I tried re-importing the project, copy-and-pasting it into a new workspace, restarting Eclipse, but the error is still there.

Just a quick note to self: If there is an error in the layout files, resolve that first then Clean.

I find it weird that no more specific error message is presented. Oh well.