Tidying Up: Renaming Classes
Tidying Up: Renaming Classes
We could now quite happily carry on and embed our mapping application into the PhoneGap application without making further changes. Given that we are going to be building applications that we want to deploy to the Android marketplace, it’s probably worth looking at what is needed to remove additional references to “Sample” from the PhoneGap sample project.
CHAPTER 9: Native Bridging with PhoneGap
There aren’t too many places we have to do this, but it’s worth pointing out that this is a little more complicated than what we did in our previous steps. So, if you would prefer to tackle this once you are actually building a production application instead, you are quite welcome to do so.
First, we need to change the class com.phonegap.Sample.Sample to something that makes sense for our application. Depending on the tools you are using, this process can
be as simple as pressing a Refactor button and letting the IDE work it out. However, as we are building web apps and our primary tool is a text editor, we will need to make the change in a few more places. So let’s begin:
1. Update the com/phonegap/Sample directory structure to match something more in line with what we are building. Something like com/prowebapps/maptest is a good choice.
2. Next, rename the sample.java file MapTest.java, as this better represents the application we are building.
3. Finally, you will need to update the contents of the Java file, and change the com.phonegap.sample and sample references to com.prowebapps.maptest and MapTest, respectively.
The following is a sample of what the MapTest.java file might look like after you have completed the necessary changes:
package com.prowebapps.maptest;
import android.app.Activity; import android.os.Bundle; import com.phonegap.*;
public class MapTest extends DroidGap {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.loadUrl("file:///android_asset/www/index.html");
Once the Java source file has been modified, we are halfway there. Now we just need to update the AndroidManifest.xml file in the project root directory to reflect the changes we have made. Once again, this involves modifying the com.phonegap.Sample reference to com.prowebapps.maptest and the Sample reference to MapTest.
The following is an example of what the AndroidManifest.xml file would look like after the changes. Once again, the modified sections are marked in bold.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.prowebapps.maptest" android:versionName="1.1" android:versionCode="1"> <supports-screens
CHAPTER 9: Native Bridging with PhoneGap
android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:resizeable="true" android:anyDensity="true" />
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".MapTest"
android:label="@string/app_name" android:configChanges="orientation|keyboardHidden"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="2" />
</manifest> That should do it. To make sure you haven’t made any typos, rebuild the application and
attempt to run it in the emulator. If you made an error somewhere along the way, then a screen like Figure 9–12 will be displayed.
CHAPTER 9: Native Bridging with PhoneGap
Figure 9–12. Typos can occur while renaming classes in the sample project, resulting in application crashes. If you do see an error while trying to load your application, then double-check that the
activity name in the AndroidManifest.xml file matches the name you gave to the main application class, bearing in mind case-sensitivity.
This brings us to the point where things are structured correctly for building our test mapping application. When deploying a production application, there are still a few additional things to cover (such as updating the application icon to something other than the default), but we will cover those in Chapter 11.
For now, let’s continue to focus on getting some existing HTML code into our PhoneGap wrapper.