Month: November 2011

  • How to centering google map on Balloon center in Android

    How to centering google map on Balloon center in Android

    If you click (tap) the balloon on Android Google Maps in your application, and want to centering map on center of balloon. You need to look this post.

    Below you can download the source code of sample Android project.

    The secret in Utils class. Below you can see the source of this class

    package com.hrupin.lazymarkers;
    
    import android.content.Context;
    import android.util.DisplayMetrics;
    
    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapView;
    
    public class Utils {
        private static final int BALLOON_OFFSET_IN_DP = 150;
        private static final double EQUATRE_LENGTH_IN_KM = 40075.7;
        private static final double EARTH_RADIUS_IN_KM = 6356.8;
        private static DisplayMetrics metrics = null;
        private static float density;
    
        public static GeoPoint getBalloonCenterCoords(GeoPoint markerGeoPoint, MapView mapView) {
            int zoom = mapView.getZoomLevel();
            int balloonOffset = getOffsetInDegreesE6(zoom, dpToPxConverter(mapView.getContext(), BALLOON_OFFSET_IN_DP));
            int balloonCenterLatitude = markerGeoPoint.getLatitudeE6() + balloonOffset;
            return new GeoPoint(balloonCenterLatitude, markerGeoPoint.getLongitudeE6());
        }
    
        private static int getOffsetInDegreesE6(int zoom, int offsetInPx) {
            // 256 - equator length on map if zoom level = 0
            double equatreLengthOnMapInPx = (double) 256 * Math.pow(2, zoom);
            double earthRadiusOnMapInPx = (equatreLengthOnMapInPx * EARTH_RADIUS_IN_KM) / EQUATRE_LENGTH_IN_KM;
            double tangensValue = (double) offsetInPx / earthRadiusOnMapInPx;
            double offsetInDegrees = Math.toDegrees(Math.atan(tangensValue));
            return (int) (offsetInDegrees * 1E6);
        }
    
        private static int dpToPxConverter(Context context, int dp) {
            if (metrics == null) {
                metrics = context.getResources().getDisplayMetrics();
                density = metrics.density;
            }
            return (int) (density * dp + 0.5f);
        }
    }

    Please tell me if you know other solution.

    Hope it help you!

    Here you can download the source code of sample Android project.

    Download it from github
  • How to Clear User Data in your Android Application programmatically

    How to Clear User Data in your Android Application programmatically

    How to clear user data of your application programmatically? Here I share with you my solution how to do this.

    You also can clear your user data by clicking “Clear Data” button in Settings–>Applications–>Manage Aplications–> YOUR APPLICATION

    To do it programmatically you need to remove user data from application dir.

    Below you can download source code of sample Android project.

    Pay attention in MyApplication class:

    package com.hrupin.cleaner;
    
    import java.io.File;
    
    import android.app.Application;
    import android.util.Log;
    
    public class MyApplication extends Application {
    	private static MyApplication instance;
    
    	@Override
    	public void onCreate() {
    		super.onCreate();
    		instance = this;
    	}
    	
    	public static MyApplication getInstance(){
    		return instance;
    	}
    	
    	public void clearApplicationData() {
    		File cache = getCacheDir();
    		File appDir = new File(cache.getParent());
    		if(appDir.exists()){
    			String[] children = appDir.list();
    			for(String s : children){
    				if(!s.equals("lib")){
    					deleteDir(new File(appDir, s));
    					Log.i("TAG", "File /data/data/APP_PACKAGE/" + s +" DELETED");
    				}
    			}
    		}
    	}
    	
    	public static boolean deleteDir(File dir) {
    	    if (dir != null && dir.isDirectory()) {
    	        String[] children = dir.list();
    	        for (int i = 0; i < children.length; i++) {
    	            boolean success = deleteDir(new File(dir, children[i]));
    	            if (!success) {
    	                return false;
    	            }
    	        }
    	    }
    
    	    return dir.delete();
    	}
    }
    Download it from github
  • How to include Facebook Android SDK in project as maven module

    How to include Facebook Android SDK in project as maven module

    This post will be usefull if you have Android maven multimodule project and want to include Facebook Android SDK  in your project as module.

    How do this?

    As you see here https://github.com/facebook/facebook-android-sdk the Facebook SDK used not as maven module.

    Below you can download source code of Android project

    To do this you need:

    1. Download the Facebook Android SDK from github.

    2. Add Facebook Android SDK lib project in your workspace near other modules of your maven project. Look at screenshot below:

    3. Ensure that your facebook dir contauns next files and dirs. Look at screenshot below:

    4. Add to parent project’s pom.xml new module. The block may look like this:

    <modules>
        <module>facebook</module>
        <module>application</module>
    </modules>

    5. Add in to facebook dir next pom.xml file. Don’t forget setup group_id, artifact_id and version of your parent project. I noticed in code where you need to do this:

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelversion>4.0.0</modelversion>
    </project>
    
    <parent> 
        <groupid>YOUR_PARENT_PROJECT_GROUP_ID</groupid>
        <artifactid>YOUR_PARENT_PROJECT_ARTIFACT_ID</artifactid>
        <version>YOUR_PARENT_PROJECT_VERSION</version> 
    </parent>
    
    <groupid>com.facebook.android</groupid>
    <artifactid>facebook-android-sdk</artifactid>
    <version>0.0.1</version> 
    <packaging>jar</packaging> 
    <name>Facebook Android SDK</name>
    
    <dependencies>
        <dependency>
            <groupid>com.google.android</groupid>
            <artifactid>android</artifactid>
        </dependency>
    </dependencies>
    <build> 
        <plugins> 
            <plugin> 
                <groupid>com.jayway.maven.plugins.android.generation2</groupid>
                <artifactid>maven-android-plugin</artifactid>
                <configuration>
                    <androidmanifestfile>${project.basedir}/AndroidManifest.xml
                    </androidmanifestfile>
                    <assetsdirectory>${project.basedir}/assets</assetsdirectory>
                    <resourcedirectory>${project.basedir}/res</resourcedirectory>
                    <nativelibrariesdirectory>${project.basedir}/src/main/native
                    </nativelibrariesdirectory>
                    <sdk> 
                        <platform>8</platform> 
                    </sdk>
                    <deleteconflictingfiles>true</deleteconflictingfiles>
                    <undeploybeforedeploy>true</undeploybeforedeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin> 
            <plugin> 
                <artifactid>maven-compiler-plugin</artifactid>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    6. Change path to java source files from src/com/facebook/android to src/main/java/com/facebook/android

    7. Delete all projects from your Eclipse’s package explorer.

    8. Import project as Maven project. If you do it right you can see next:

    When you import this project the Eclipse will create target dir in your facebook project with APK file.

    8. Add Facebook Android SDK dependencies in your application. To do this open pom.xml of your app and add next code in <dependencies>.

    <dependency>
        <groupid>com.facebook.android</groupid>
        <artifactid>facebook-android-sdk</artifactid>
        <version>0.0.1</version>
    </dependency>

    And whole pom.xml of your app need to be looks like this:

    <?xml version="1.0" encoding="UTF-8"?> 
    <project xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelversion>4.0.0</modelversion>
    </project>
    
    <parent> 
        <groupid>YOUR_PARENT_PROJECT_GROUP_ID</groupid>
        <artifactid>YOUR_PARENT_PROJECT_ARTIFACT_ID</artifactid>
        <version>YOUR_PARENT_PROJECT_VERSION</version> 
    </parent>
    
    <groupid>com.hrupin</groupid>
    <artifactid>facebook-android-maven-sample</artifactid>
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>apk</packaging> 
    <name>facebook-android-maven-sample - Application</name>
    
    <dependencies>
        <dependency>
            <groupid>com.google.android</groupid>
            <artifactid>android</artifactid>
        </dependency>
        <dependency>
            <groupid>com.facebook.android</groupid>
            <artifactid>facebook-android-sdk</artifactid>
            <version>0.0.1</version>
        </dependency>
    </dependencies>
    <build> 
        <plugins> 
            <plugin> 
            <groupid>com.jayway.maven.plugins.android.generation2</groupid>
                <artifactid>maven-android-plugin</artifactid>
                <configuration>
                    <androidmanifestfile>${project.basedir}/AndroidManifest.xml</androidmanifestfile>
                    <assetsdirectory>${project.basedir}/assets</assetsdirectory>
                    <resourcedirectory>${project.basedir}/res</resourcedirectory>
                    <nativelibrariesdirectory>${project.basedir}/src/main/native</nativelibrariesdirectory>
                    <sdk> 
                        <platform>8</platform>
                    </sdk>
                    <deleteconflictingfiles>true</deleteconflictingfiles>
                    <undeploybeforedeploy>true</undeploybeforedeploy>
                </configuration>
                <extensions>true</extensions> 
            </plugin> 
            <plugin> 
                <artifactid>maven-compiler-plugin</artifactid>
                <configuration>
                    <source />1.6
                    <target>1.6</target>
                </configuration> 
            </plugin> 
        </plugins> 
    </build>

    That’s all. Try to build your project with maven:

    cd your_parent_project_dir
    mvn clean install

    This sample is not detailed. If you have some troubles you can leave me comment and i will try to ask you.

    Hope, it help you!
    Here you can download sample project with Facebook Android SDK as maven module

    Download it from github