Friday, May 6, 2016

GPS Location Programaticaly in Android by tuning ON GPS

There are various examples on internet which shows how to get GPS location of a device in Android using code. Its fairly simple to get location when GPS is turned on but its little bit tricky when GPS is turned off. Majority of examples found on internet exploit a bug in Android to turn on GPS. This hack used to work pre Kitkat (4.4) but it stopped working now. 
Following is the code to exploit this hack -


private void turnGPSOn(){
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if(!provider.contains("gps")){ //if gps is disabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3")); 
        sendBroadcast(poke);
    }
}

Then, there is few examples which just open the Android Settings page and then user has to toggle GPS settings, following is the code to launch settings page -


Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);

Couple of months back, google introduced a standard way of asking user to turn on GPS by showing following dialog to user -



GPS is automatically turned on if user says 'Yes'.  No need to open Settings page and then ask user to toggle GPS setting. Majority of standard apps use this method now. Ola, Uber and other gps location based cab booking apps use this method too.

In this blog post I will explain how to use this method and the I will explain how to use Geocoder class to get complete address from location longitude and latitude. Please note that -

  • To use GPS, internet is not required i.e. longitude and latitude can be obtained even when device is not connected to internet.
  • To get address from longitude and latitude, internet is required.

Following is step by step process of getting Android device location programatically by turning on GPS -


Step 1 - Create a Custom LocationListner which helps in listening to locations changes.


  • This can be done by implementing LocationListener  class. 
  • Then Geocoder class can be used to get address info from location. Geocoder class can provide following important properties encapsulated in Address object -
String mAdminArea;
String mSubAdminArea;
String mLocality;
String mSubLocality;
String mThoroughfare;
String mSubThoroughfare;
String mPremises;
String mPostalCode;
String mCountryCode;
String mCountryName;
double mLatitude;
double mLongitude;
  • Following is a sample implementation of LocationListener  class -


private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {
            String cityName="";
            Geocoder gcd = new Geocoder(getBaseContext(),Locale.getDefault());
            List<Address> addresses;
            try {
                addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);               
                cityName=addresses.get(0).getLocality();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            textView.setText("Lat: " +loc.getLatitude() +"\n"+ " Lng: " + loc.getLongitude()
                    +"\nMy Currrent City is: "+cityName);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStatusChanged(String provider,
                                    int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    }


Step 2 - Check whether GPS is turned ON


  • If GPS is turned ON, then add LocationListener created in Step 1.
  • If GPS is not turned ON, then show a dialog to user.



public void requestToTurnOnGps()
    {
        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                .addApi(LocationServices.API)
                .build();
        googleApiClient.connect();

        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(30 * 1000);
        locationRequest.setFastestInterval(5 * 1000);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
        builder.setAlwaysShow(true); //DO NOT forget this, this is the key ingredient

        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                final LocationSettingsStates state = result.getLocationSettingsStates();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        //this means gps is already turned ON
                        startListeningLocationUpdates();
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        //this means gps is turned off and you need to show the dialog
                        try {
                            status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        textView.setText("Looks like GPS is not available in your device.");
                        break;
                }
            }
        });
    }

private void startListeningLocationUpdates() {
        try {
            textView.setText("Searching for location...\n It might take some time. Please wait ...");
            locationListener = new MyLocationListener();
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
        catch (SecurityException e)
        {
            textView.setText("No permission to use GPS.");
        }
    }


Following line will launch a dialog to the user with Yes and No options. Result of the dialog i.e. user choose 'Yes' option or 'No' option will be given as onActivityResult with status code passed as second parameter (REQUEST_CHECK_SETTINGS).

status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);


Step 3 - Handle result of GPS turn ON\OFF dialog shown to user


  • If user said Yes, then GPS must be enabled now. React accordingly.
  • If user said No, then GPS should still be disabled. Handle this case.
  • Following is sample implementation -

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        //user said YES and GPS is enabled
                        startListeningLocationUpdates();
                        break;
                    case Activity.RESULT_CANCELED:
                        //user said NO and GPS is still disabled
                        textView.setText("GPS is still disabled.");
                        break;
                }
                break;
        }
    }


Step 4 - Stop listening to location updates once you have location.

If you want location only once then you can stop listening to location updates once you have location. Call following code whenever you want to stop listening to location updates -

 private void stopListeningLocationUpdates() {
        try {
            if (locationListener != null)
                locationManager.removeUpdates(locationListener);
        }
        catch (SecurityException e)
        {
            //do nothing
        }
    }


You can download working Android project from this link.  

This project asks request to enable GPS is not already enabled. Then it reads GPS co-ordinates. Once it has co-ordinates, it tries to get current city. Then it stop listening to location updates.
Following are the screenshots of this sample project -





Please feel free to comment below in case you have any doubts.


Wednesday, February 3, 2016

Calling Google Translation API in Java for free

The official Google translate API is available with a fee but there is a way using which you can call the API with free of cost. The trick is to make  a direct call to  secret translate.googleapis.com API that is internally used by the Google Translate extension for Chrome. Luckily  translate.googleapis.com API doesn't require any authentication. 

Following is the java code to call this API and get your word translation from any Google supported language to other language -


package default;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONArray;

public class Translator {

 public static void main(String[] args) throws Exception 
 {

  Translator http = new Translator();
  String word = http.callUrlAndParseResult("en", "hi", "hello");
  
  System.out.println(word);
 }
 
 private String callUrlAndParseResult(String langFrom, String langTo,
                                             String word) throws Exception 
 {

  String url = "https://translate.googleapis.com/translate_a/single?"+
    "client=gtx&"+
    "sl=" + langFrom + 
    "&tl=" + langTo + 
    "&dt=t&q=" + URLEncoder.encode(word, "UTF-8");    
  
  URL obj = new URL(url);
  HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
  con.setRequestProperty("User-Agent", "Mozilla/5.0");
 
  BufferedReader in = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
  String inputLine;
  StringBuffer response = new StringBuffer();
 
  while ((inputLine = in.readLine()) != null) {
   response.append(inputLine);
  }
  in.close();
 
  return parseResult(response.toString());
 }
 
 private String parseResult(String inputJson) throws Exception
 {
  /*
   * inputJson for word 'hello' translated to language Hindi from English-
   * [[["नमस्ते","hello",,,1]],,"en"]
   * We have to get 'नमस्ते ' from this json.
   */
  
  JSONArray jsonArray = new JSONArray(inputJson);
  JSONArray jsonArray2 = (JSONArray) jsonArray.get(0);
  JSONArray jsonArray3 = (JSONArray) jsonArray2.get(0);
  
  return jsonArray3.get(0).toString();
 }
}

You can download org.json jar from here.

If you are seeing garbage looking characters in output then you will have to set character encoding as "UTF-8" in your Java Project. This happens when output language is of unicode type. You can set character encoding from Project Properties -> Resource -> Text file encoding. Following is the screenshot to help you in this -



Please feel free to comment below if you have any doubts.

Thursday, January 28, 2016

Passing Parameters to FragmentPagerItem (SmartTabLayout) in Android

Following is how you can pass parameters to FragmentPageItem in SmartTablayout -

public class HomeActivity extends AppCompatActivity {

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_demo);

 …

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

 …
    FragmentPagerItems pages = new FragmentPagerItems(this);

    fragmentPagerItems.add(FragmentPagerItem.of("Tutorial", 
                 ContentFragment.class, ContentFragment.arguments("param1")));

    fragmentPagerItems.add(FragmentPagerItem.of("Programs", 
                 ContentFragment.class, ContentFragment.arguments("param2")));
 
    FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(

        getSupportFragmentManager(), pages);

    viewPager.setAdapter(adapter);
 …
  }

}


Here is the relevant code for ContentFragment -

public class ContentFragment extends Fragment {


  //parameter to be passed
  private static final String KEY_PARAM = "key_param";



  public static ContentFragment newInstance(String param) {

    ContentFragment f = new ContentFragment();

    f.setArguments(arguments(param));

    return f;

  }



  public static Bundle arguments(String param) {

    return new Bundler()

            .putString(KEY_PARAM, param)

            .get();

  }
...

  @Override

  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);


    //use KEY_PARAM here
   String paramValue = getArguments().getString(KEY_PARAM);
  } }