Day Code Snippet – adding NAVi

A small code snippet which makes Integration across the android apps easy.

Try calling a Implicit Intent with the below intent info, you can see yourself that your app is now integrated with the Navigation.

Uri IntentUri = Uri.parse(“google.navigation:q=” + latitude + “,” + longitude);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, IntentUri);
startActivity(mapIntent);

Happy coding 🙂

 

Advertisement

Project Lombok

Hi Guys, many in Java space would have come across the term “Lombok”. For those who are not aware about it, Its a Boiler plate code killer in your model/POJO class. Its been there for a while. It has more to offer. Refer here for more info

reason for me to talk about it is the availability of Lombok as a Android studio plugin. Just install and you are good to go.

screen-shot-2017-02-17-at-1-23-11-pm

Its makes code look clean and simple to understand. Happy coding 🙂

Smart Lock on Android

HI guys, happened to see on Linkedin App, Smart lock dialog; Just wondered what is that?

I found to be a interesting feature particularly for app which want to provide a seamless integration of sign-in for the user who have already saved their credentials in Google chrome. its pretty straight forward to understand. Say in your linkedin app, just Login and chrome might prompt you to save password. On click of save password, chrome stores your credentials. Then you launch your Linked-in app, the credentials info from Chrome is shared to your mobile App and you got to signin automatically. seems magic; that is what smart lock does. Refer for more info

you would be wondering , sharing credentials…is it safe? Even I have no idea whether is safe proof. But I wondered that credentials object just gives password as String. Need to investigate.

screen-shot-2017-02-22-at-4-35-20-pm

To start with , you need to utilize Credentials API . All we need is to save the credentials and then read the credentials. pretty much sharing of credentials from Mobile to Web or vice versa.

Save:

Store user credentials with Auth.CredentialsApi.save():

Auth.CredentialsApi.save(mCredentialsClient, credential).setResultCallback(
  new ResultCallback() {
      @Override
      public void onResult(Status status) {
          if (status.isSuccess()) {
              // Credentials were saved
          } else {
              if (status.hasResolution()) {
                  // Try to resolve the save request. This will prompt the user if
                  // the credential is new.
                  try {
                      status.startResolutionForResult(this, RC_SAVE);
                  } catch (IntentSender.SendIntentException e) {
                      // Could not resolve the request
                  }
              }
          }
      }
  });

 

Read:

Retrieve stored credentials with Auth.CredentialsApi.request():

Auth.CredentialsApi.request(mCredentialsClient, mCredentialRequest).setResultCallback(
  new ResultCallback() {
      @Override
      public void onResult(CredentialRequestResult credentialRequestResult) {
          if (credentialRequestResult.getStatus().isSuccess()) {
              // Handle successful credential requests
          } else {
              // Handle unsuccessful and incomplete credential requests
          }
      }
  });

 

Seeing Netflix, Linked in and other major apps are moving towards it. Probably thats the future. Happy coding 🙂

Say Hi to “Parceler”

In my previous blog, i have talked about why parcelable is efficient and fast compared to serializable and also said about Android Parcelable plugin, does the auto generate code for your class. Recently I have come across a library which does the parcelable and avoid the boiler plate code such as

writeToParcel, readFromParcel and Parcelable.Creator

One of the issue with these code, you need to write and read in same order. Though not a big deal, but still if we can do better than this, then why not?. Parceler does the Job. No need to write the boiler plate codes and check for the order of read and write.

Refer the Github Link for more info. Parceler

Ok, how its works. Just annote with @Parcel, its does the serialization for you without have to override all the above mentioned code.

Configuration:

compile 'org.parceler:parceler-api:1.1.6'
annotationProcessor 'org.parceler:parceler:1.1.6'

If its throws a error saying “unable to find the generated parcelable class”, it might be due annotationProcessor not enabled for your project. In that case, refer the below.

compile 'org.parceler:parceler-api:1.1.6'
apt 'org.parceler:parceler:1.1.6'

Next step, Just define Parcel Annotation for your class

@org.parceler.Parcel
public class MyParcelClass {

        public String suburb;
        public String state;
        public String postcode;
        public String country;
   }

If suppose you have a Inner class , just do the same as you did to the parent class. Just annotate with @Parcel

@org.parceler.Parcel
public class MyParcelClass {

        public String suburb;
        public String state;
        public String postcode;
        public String country;

        @Parcel
        public static class MyName{
          public String firstname;
          public String LastName;
        }
        @Parcel
        public static class MyID{
          public String DL;
        }

   }

“Be careful not to use private fields when using the default field serialization strategy as it will incur a performance penalty due to reflection.”

To avoid the performance penalty , just define the annotation as

@org.parceler.Parcel(org.parceler.Parcel.Serialization.BEAN)

Till now all Good, But how about Custom serialization? Yes, there is a way to do it.

All you need is to define annotation to the field which you want to do custom serialization.

@ParcelPropertyConverter(NameConverter.class)
public MyName name;

@org.parceler.Parcel
public class NameConverter implements ParcelConverter<MyParcelClass.MyName> {
@Override
public void toParcel(MyParcelClass.MyName input, Parcel parcel) {
if (input == null) {
parcel.writeInt(-1);
} else {
parcel.writeParcelable(Parcels.wrap(input.firstName), 0);
parcel.writeParcelable(Parcels.wrap(input.LastName), 0);
}
}

@Override
public LocationPoint.Address fromParcel(Parcel parcel) {

MyClassName.MyName name = new MyClassName.MyName();
name.firstName = Parcels.unwrap(parcel.readParcelable(MyFirstName.MyName.class.getClassLoader()));
name.LastName = Parcels.unwrap(parcel.readParcelable(MyFirstName.MyName.class.getClassLoader()));

return name;
}
}

There you are; now you don’t need to worry on how to read and write to a  parcel class.

Voilla! Happy Coding :)