Wednesday, December 14, 2016

Best Inspirational Story I have ever read..

Story of Life..

There was once a man who got lost in the desert. The water in his flask ran out two days ago, and he was on his last legs. He knew that if he didn't get some water soon, he would surely perish. The man saw a shack ahead of him. He thought it might be a mirage or hallucination, but having no other option, he moved toward it. As he got closer he realized it was quite real, so he dragged his weary body to the door with the last of his strength.
The shack was not occupied and seemed like it had been abandoned for quite some time. The man gained entrance, hoping against hope that he might find water inside.His heart skipped a beat when he saw what was in the shack: a water pump..It had a pipe going down through the floor, perhaps tapping a source of water deep under-ground.
He began working the pump, but no water came out. He kept at it and still nothing happened. Finally he gave up from exhaustion and frustration. He threw up his hands in despair. It looked as if he was going to die after all.Then the man noticed a bottle in one corner of the shack. It was filled with water and corked up to prevent evaporation.
He uncorked the bottle and was about to gulp down the sweet life-giving water when he noticed a piece of paper attached to it. Handwriting on the paper read: "Use this water to start the pump. Don't forget to fill the bottle when you're done."
He had a dilemma. He could follow the instruction and pour the water into the pump, or he could ignore it and just drink the water.What to do? If he let the water go into the pump, what assurance did he have that it would work? What if the pump malfunctioned? What if the pipe had a leak? What if the underground reservoir had long dried up?
But then... maybe the instruction was correct. Should he risk it? If it turned out to be false, he would be throwing away the last water he would ever see.Hands trembling, he poured the water into the pump. Then he closed his eyes, said a prayer, and started working the pump.He heard a gurgling sound, and then water came gushing out, more than he could possibly use. He luxuriated in the cool and refreshing stream. He was going to live!
After drinking his fill and feeling much better, he looked around the shack. He found a pencil and a map of the region. The map showed that he was still far away from civilization, but at least now he knew where he was and which direction to go.
He filled his flask for the journey ahead. He also filled the bottle and put the cork back in. Before leaving the shack, he added his own writing below the instruction: "Believe me, it works!"
This story is all about life. It teaches us that we must give before we can receive abundantly. More importantly, it also teaches that faith plays an important role in giving. The man did not know if his action would be rewarded, but he proceeded regardless. Without knowing what to expect, he made a leap of faith. Water in this story represents the good things in life. Something that brings a smile to your face. It can be intangible knowledge or it can represent money, love, family, friendship, happiness, respect, or any number of other things you value. Whatever it is that you would like to get out of life, that's water.
The water pump represents the workings of the karmic mechanism. Give it some water to work with, and it will return far more than you put in.

Tuesday, December 6, 2016

Application level shared preference

A single class of shared preference which can store any type of data and objects at application level.

Example Code :

Shared Prefrence Class.

public class AppSharedPrefs
{

    private static AppSharedPrefs sharedPrefs = null;
    private SharedPreferences sf;


    public AppSharedPrefs(Context context)
    {
        sf = context.getSharedPreferences("key_shared_preference", Context.MODE_PRIVATE);
    }

    public static AppSharedPrefs getInstance(Context context)
    {
        if(sharedPrefs == null)
        {
            sharedPrefs = new AppSharedPrefs(context);
        }

        return sharedPrefs;
    }


    public Object get(String key)
    {
        Map<String, ?> map = sf.getAll();
        return map.get((String)key);
    }


    public void put(String key, Object value)
    {
        SharedPreferences.Editor edit = sf.edit();

        if(value == null)
        {
            edit.putString(key, null);
        }
        else if(value instanceof Boolean)
        {
            edit.putBoolean(key, (boolean)value);
        }
        else if(value instanceof Float)
        {
            edit.putFloat(key, (float)value);
        }
        else if(value instanceof Integer)
        {
            edit.putInt(key, (int)value);
        }
        else if(value instanceof Long)
        {
            edit.putLong(key, (long) value);
        }
        else if(value instanceof String)
        {
            edit.putString(key, (String)value);
        }
       else if(value instanceof  Model)
        {
            edit.putString(key, (Model)value);
        }
        else if(value instanceof Set)
        {
            edit.putStringSet(key, (Set)value);
        }

        edit.commit();
    }

    public void clearAll()
    {
        sf.edit().clear().commit();
    }

    public void clear(String key)
    {
        sf.edit().remove(key).commit();
    }
}


store various type of data in shared preference from any class as shown below.

public class RegisterActivity extends AppCompatActivity {

    private AppSharedPrefs sharedPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        //initialise shared prefs manager
        sharedPrefs = AppSharedPrefs.getInstance(RegisterActivity.this);

        sharedPrefs.put("KeyName", "Zafar Imam");//stroing string value
        sharedPrefs.put("KeyAge", 24); //storing integer value
        sharedPrefs.put("IsRegister", true); //storing boolean value

        ModelClass model = new ModelClass();
        model.setFirstName("Zafar");
        model.setLastName("Imam");
        model.setRollNumber(512);

        //here you can store object of model class inside prefrence to transfer from one class to other
        sharedPrefs.put("Key_Serializable_Object", model);

    }
}

Model class of getter and setter: 

public class ModelClass implements Serializable {

    private String firstName;
    private String lastName;
    private int rollNumber;

    public int getRollNumber() {
        return rollNumber;
    }

    public void setRollNumber(int rollNumber) {
        this.rollNumber = rollNumber;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

Get the data from shared preference in other class as :


public class ProfileActivity extends AppCompatActivity {
    ModelClass modelClass;
    private AppSharedPrefs sharedPrefs;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);

        //initialise shared prefs manager
        sharedPrefs = AppSharedPrefs.getInstance(ProfileActivity.this);

        Boolean isRegister = (Boolean) sharedPrefs.get("IsRegister");//getting stored data

        String name = (String) sharedPrefs.get("KeyName");//getting stored data

        int age = (Integer) sharedPrefs.get("KeyAge");//getting stored data

         modelClass = (ModelClass) sharedPrefs.get("Key_Serializable_Object");//getting object 

        if (isRegister){

            String firstName = modelClass.getFirstName();
            int rollNumber = modelClass.getRollNumber();
        }

    }
}

Note: for transferring Object from one class to other class, respected class(like ModelClass) must have implemented Serializable or Parcelable interface.