Ch07 Listings


keytool -genkey -v -keystore <FULL PATH OF release.keystore FILE FROM STEP 1>
-alias androidbook -storepass paxxword -keypass paxxword -keyalg RSA 
-validity 14000

jarsigner -keystore <PATH TO YOUR release.keystore FILE> -storepass paxxword 
-keypass paxxword  <PATH TO YOUR APK FILE> androidbook

<manifest>
    <application>
         ...
    </application>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_CALENDAR" />
</manifest>

package com.cust.perm;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;


public class PrivActivity extends Activity
{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout view = new LinearLayout(this);

        view.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        view.setOrientation(LinearLayout.HORIZONTAL);

        TextView nameLbl = new TextView(this);

        nameLbl.setText("Hello from PrivActivity");
        view.addView(nameLbl);

        setContentView(view);

    }
}

<permission
android:protectionLevel="normal"
android:name="syh.permission.STARTMYACTIVITY "
android:label="Start My Activity"
android:description="@string/startMyActivityDesc"></permission>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.cust.perm"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CustPermMainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <activity android:name="PrivActivity" 
android:permission="syh.permission.STARTMYACTIVITY">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<permission
android:protectionLevel="normal"
android:label="Start My Activity"
android:description="@string/startMyActivityDesc" 
android:name="syh.permission.STARTMYACTIVITY"></permission>

    <uses-sdk android:minSdkVersion="2" />
</manifest>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:id="@+id/btn"
    android:text="Launch PrivActivity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

package com.client.cust.perm;
// This file is ClientCustPermMainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ClientCustPermMainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

    
                Intent intent = new Intent();
    
                intent.setClassName("com.cust.perm","com.cust.perm.PrivActivity");
                startActivity(intent);
            }});

    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.client.cust.perm"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ClientCustPermMainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

    <uses-permission android:name="syh.permission.STARTMYACTIVITY"></uses-permission>
    <uses-sdk android:minSdkVersion="2" />
</manifest>

keytool -list -alias androiddebugkey -keystore 
"FULL PATH OF YOUR debug.keystore FILE" -storepass android -keypass android

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidbook"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".MapViewDemoActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-sdk android:minSdkVersion="3" />
</manifest>

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/mapview.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="horizontal" android:layout_width="fill_parent"
		android:layout_height="wrap_content">

		<Button android:id="@+id/zoomin" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="+"
			android:onClick="myClickHandler" android:padding="12px" />

		<Button android:id="@+id/zoomout" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="-"
			android:onClick="myClickHandler" android:padding="12px" />

		<Button android:id="@+id/sat" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="Satellite"
			android:onClick="myClickHandler" android:padding="8px" />

		<Button android:id="@+id/street" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="Street"
			android:onClick="myClickHandler" android:padding="8px" />

		<Button android:id="@+id/traffic" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="Traffic"
			android:onClick="myClickHandler" android:padding="8px" />

		<Button android:id="@+id/normal" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="Normal"
			android:onClick="myClickHandler" android:padding="8px" />

	</LinearLayout>

	<com.google.android.maps.MapView
		android:id="@+id/mapview" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:clickable="true"
		android:apiKey="YOUR MAP API KEY GOES HERE" />

</LinearLayout>

// This file is MapViewDemoActivity.java
import android.os.Bundle;
import android.view.View;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MapViewDemoActivity extends MapActivity
{
    private MapView mapView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);

        mapView = (MapView)findViewById(R.id.mapview);
    }

    public void myClickHandler(View target) {
        switch(target.getId()) {
        case R.id.zoomin:
        	mapView.getController().zoomIn();
        	break;
        case R.id.zoomout:
        	mapView.getController().zoomOut();
        	break;
        case R.id.sat:
        	mapView.setSatellite(true);
        	break;
        case R.id.street:
        	mapView.setStreetView(true);
        	break;
        case R.id.traffic:
        	mapView.setTraffic(true);
        	break;
        case R.id.normal:
        	mapView.setSatellite(false);
        	mapView.setStreetView(false);
        	mapView.setTraffic(false);
        	break;
        }
    }

    @Override
    protected boolean isLocationDisplayed() {
        return false;
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

public class MapViewDemoActivity extends MapActivity
{
    private MapView mapView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.mapview);

        mapView = (MapView)findViewById(R.id.mapview);

        mapView.setBuiltInZoomControls(true);
    }

    @Override
    protected boolean isLocationDisplayed() {
        return false;
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/mapview.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">

     <com.google.android.maps.MapView android:id="@+id/mapview"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:clickable=�?�true�?�
             android:apiKey="YOUR MAP API KEY GOES HERE"
             />
</RelativeLayout>

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MappingOverlayActivity extends MapActivity {
    private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.mapview);

        mapView = (MapView) findViewById(R.id.mapview);

        mapView.setBuiltInZoomControls(true);

        mapView.setClickable(true);

        Drawable marker=getResources().getDrawable(R.drawable.mapmarker); 
        marker.setBounds(0, 0, marker.getIntrinsicWidth(), 
                                      marker.getIntrinsicHeight());
        
        InterestingLocations funPlaces = new InterestingLocations(marker);
        mapView.getOverlays().add(funPlaces);
        
        GeoPoint pt = funPlaces.getCenter();    // get the first-ranked point
        mapView.getController().setCenter(pt);
        mapView.getController().setZoom(15);     // cheating. We could iterate
                                                                           // and figure out a proper zoom.    
    }

    @Override
    protected boolean isLocationDisplayed() {
        return false;
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    class InterestingLocations extends ItemizedOverlay {
        private List<OverlayItem> locations = new ArrayList<OverlayItem>();
        private Drawable marker;

        public InterestingLocations(Drawable marker)
        {
            super(marker);
            this.marker=marker;
            // create locations of interest
            GeoPoint disneyMagicKingdom = new 
GeoPoint((int)(28.418971*1000000),(int)(-81.581436*1000000));
            GeoPoint disneySevenLagoon = new 
GeoPoint((int)(28.410067*1000000),(int)(-81.583699*1000000));

            locations.add(new OverlayItem(disneyMagicKingdom , 
"Magic Kingdom", "Magic Kingdom"));
            locations.add(new OverlayItem(disneySevenLagoon , 
"Seven Lagoon", "Seven Lagoon"));

            populate();
        }

        @Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow) {
            super.draw(canvas, mapView, shadow);

            boundCenterBottom(marker);
        }

        @Override
        protected OverlayItem createItem(int i) {
            return locations.get(i);
        }

        @Override
        public int size() {
            return locations.size();
        }

    }
}

import java.io.IOException;
import java.util.List;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class GeocodingDemoActivity extends MapActivity
{
    Geocoder geocoder = null;
    MapView mapView = null;

    @Override
    protected boolean isLocationDisplayed() {
        return false;
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.geocode);
        mapView = (MapView)findViewById(R.id.geoMap);
        mapView.setBuiltInZoomControls(true);

        // lat/long of Jacksonville, FL
        int lat = (int)(30.334954*1000000);
        int lng = (int)(-81.5625*1000000);
        GeoPoint pt = new GeoPoint(lat,lng);
        mapView.getController().setZoom(10);
        mapView.getController().setCenter(pt);

        Button geoBtn =(Button)findViewById(R.id.geocodeBtn);

        geocoder = new Geocoder(this);

        geoBtn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            try {
                EditText loc = (EditText)findViewById(R.id.location);
                String locationName = loc.getText().toString();

                List<Address> addressList = 
geocoder.getFromLocationName(locationName, 5);
                if(addressList!=null && addressList.size()>0)
                {
                    int lat = (int)(addressList.get(0).getLatitude()*1000000);
                    int lng = (int)(addressList.get(0).getLongitude()*1000000);

                    GeoPoint pt = new GeoPoint(lat,lng);
                    mapView.getController().setZoom(15);
                    mapView.getController().setCenter(pt);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }});

    }
}

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/geocode.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout android:layout_width="fill_parent" 
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" android:orientation="vertical" >

            <EditText android:layout_width="fill_parent" android:id="@+id/location"
            android:layout_height="wrap_content" android:text="White House"/>

            <Button android:id="@+id/geocodeBtn" 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content" android:text="Find Location"/>
        </LinearLayout>

        <com.google.android.maps.MapView
        android:id="@+id/geoMap" android:clickable="true"
                 android:layout_width="fill_parent"
                 android:layout_height="320px"
                 android:apiKey="YOUR MAP API KEY GOES HERE"
                 />

</RelativeLayout>

import java.io.IOException;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class GeocodingDemoActivity extends MapActivity
{
    Geocoder geocoder = null;
    MapView mapView = null;
    ProgressDialog progDialog=null;
    List<Address> addressList=null;

    @Override
    protected boolean isLocationDisplayed() {
        return false;
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.geocode);
        mapView = (MapView)findViewById(R.id.geoMap);
        mapView.setBuiltInZoomControls(true);

        // lat/long of Jacksonville, FL
        int lat = (int)(30.334954*1000000);
        int lng = (int)(-81.5625*1000000);
        GeoPoint pt = new GeoPoint(lat,lng);
        mapView.getController().setZoom(10);
        mapView.getController().setCenter(pt);

        Button geoBtn =(Button)findViewById(R.id.geocodeBtn);

        geocoder = new Geocoder(this);

        geoBtn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View view) {
                EditText loc = (EditText)findViewById(R.id.location);
                String locationName = loc.getText().toString();

                progDialog = 
ProgressDialog.show(GeocodingDemoActivity.this, 
"Processing...", "Finding Location...", true, false);

                findLocation(locationName);
            }});

    }

    private void findLocation(final String locationName)
    {
        Thread thrd = new Thread()
        {
            public void run()
            {
                try {
                    // do backgrond work
                    addressList = geocoder.getFromLocationName(locationName, 5);
                    //send message to handler to process results
                    uiCallback.sendEmptyMessage(0);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        thrd.start();
    }
    // ui thread callback handler
    private Handler uiCallback = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            progDialog.dismiss();

            if(addressList!=null && addressList.size()>0)
            {
                int lat = (int)(addressList.get(0).getLatitude()*1000000);
                int lng = (int)(addressList.get(0).getLongitude()*1000000);
                GeoPoint pt = new GeoPoint(lat,lng);
                mapView.getController().setZoom(15);
                mapView.getController().setCenter(pt);
            }
            else
            {
                Dialog foundNothingDlg = new 
AlertDialog.Builder(GeocodingDemoActivity.this)
                  .setIcon(0)
                  .setTitle("Failed to Find Location")
                  .setPositiveButton("Ok", null)
                  .setMessage("Location Not Found...")
                .create();
                foundNothingDlg.show();
            }
        }
    };
}

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;

public class LocationManagerDemoActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        LocationManager locMgr = 
(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Location loc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

List<String> providerList = locMgr.getAllProviders();

    }
}

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class LocationUpdateDemoActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        LocationManager locMgr = (LocationManager) 
                     getSystemService(Context.LOCATION_SERVICE);

        LocationListener locListener = new LocationListener()
        {

            public void  onLocationChanged(Location location)
                {
                        if (location != null)
                        {
                                Toast.makeText(getBaseContext(),
                                    "New location latitude [" + 
                                          location.getLatitude() +
                                    "] longitude [" + location.getLongitude()+"]",
                                    Toast.LENGTH_SHORT).show();
                        }
                }

                public void  onProviderDisabled(String provider)
                {
                }

                public void  onProviderEnabled(String provider)
                {
                }

                public void  onStatusChanged(String provider, 
                                   int status, Bundle extras)
                {
                }

        };

        locMgr.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            0,		// minTime in ms
            0,		// minDistance in meters
            locListener);
    }
}

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

import android.os.Bundle;

public class MyLocationDemoActivity extends MapActivity {
    
    MapView mapView = null;
    MapController mapController = null;
    MyLocationOverlay whereAmI = null;
    
    @Override
    protected boolean isLocationDisplayed() {
        return whereAmI.isMyLocationEnabled();
    }
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView)findViewById(R.id.geoMap);
        mapView.setBuiltInZoomControls(true);
        
        mapController = mapView.getController();
        mapController.setZoom(15);

        whereAmI = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(whereAmI);
        mapView.postInvalidate();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        whereAmI.enableMyLocation();
        whereAmI.runOnFirstFix(new Runnable() {
            public void run() {
                mapController.setCenter(whereAmI.getMyLocation());
            }
        });
    }

    @Override
    public void onPause()
    {
        super.onPause();
        whereAmI.disableMyLocation();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <com.google.android.maps.MapView
        android:id="@+id/geoMap" android:clickable="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="YOUR MAP API KEY GOES HERE"
        />

</RelativeLayout>

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MyLocationDemoActivity extends MapActivity {

    MapView mapView = null;
    MyLocationOverlay whereAmI = null;
    LocationManager locMgr = null;
    LocationListener locListener = null;
    
    @Override
    protected boolean isLocationDisplayed() {
        return whereAmI.isMyLocationEnabled();
    }
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView)findViewById(R.id.geoMap);
        mapView.setBuiltInZoomControls(true);
        mapView.getController().setZoom(15);
        
        whereAmI = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(whereAmI);
        mapView.postInvalidate();

        locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        locListener = new LocationListener()
        {
            public void  onLocationChanged(Location location)
            {
            	showLocation(location);
            }

            public void  onProviderDisabled(String provider)
            {
            }

            public void  onProviderEnabled(String provider)
            {
            }

            public void  onStatusChanged(String provider, 
                int status, Bundle extras)
            {
            }
        };
    }

    @Override
    public void onResume()
    {
        super.onResume();
        Location lastLoc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        showLocation(lastLoc);
        locMgr.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            0,			// minTime in ms
            0,			// minDistance in meters
            locListener);
        whereAmI.enableMyLocation();
        whereAmI.runOnFirstFix(new Runnable() {
            public void run() {
                mapView.getController().setCenter(whereAmI.getMyLocation());
            }
        });
    }

    @Override
    public void onPause()
    {
        super.onPause();
        locMgr.removeUpdates(locListener);
        whereAmI.disableMyLocation();
    }
    
    private void showLocation(Location location) {
        if (location != null)
        {
        	double lat = location.getLatitude();
        	double lng = location.getLongitude();
            GeoPoint myLocation = new GeoPoint(
                (int)(lat*1000000),
                (int)(lng*1000000));
            Toast.makeText(getBaseContext(),
                "New location latitude [" + 
                lat + "] longitude [" + lng +"]",
                Toast.LENGTH_SHORT).show();
            mapView.getController().animateTo(myLocation);
        }
    }
}