Monday, 25 July 2011

Maps Debug & Release versions

We have an app with a debug and a release keys for the google maps API. Inorder to load the correct maps key we must dynamically add the map object to the layout like this:


  try {
   if (Utils.isDebugBuild(getApplicationContext())) {
    map = new MapView(this, getString(R.string.maps_debug_key));
   } else {
    map = new MapView(this, getString(R.string.maps_release_key));
   }
   AbsListView.LayoutParams params = new AbsListView.LayoutParams(
    AbsListView.LayoutParams.FILL_PARENT,
    AbsListView.LayoutParams.FILL_PARENT);
   map.setEnabled(true);
   map.setClickable(true);

   FrameLayout topLevelView = (FrameLayout)findViewById(R.id.home_container);
   topLevelView.addView(map, 0, params );
  } catch (PackageManager.NameNotFoundException e) {
   Log.e("busmapper", "Couldn't find package name", e);
   CustomToast.makeToastAndShow(this, "Serious Error - no package name");
  }

... And our Utils class:

public static boolean isDebugBuild(Context c) throws  PackageManager.NameNotFoundException {
  PackageManager pm = c.getPackageManager();
  PackageInfo pi = pm.getPackageInfo(c.getPackageName(), 0);
  return ((pi.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
}

No comments:

Post a comment