Code example for Lock

Methods: tryLock, unlock

0
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true; 
    } 
     
    public void updateUI(){ 
    	if(lock.tryLock()){
    		float pitchDiff = initPitch-mPitch;
    		float rollDiff = initRoll-mRoll;
    		boolean changed = false;
    		 
    		// Tilt Left To Select Letter 
    		if(rollDiff >= 15){
    			changed = true;
    			currCharPos = (currCharPos == 0)?stringSize-1:currCharPos-1;
    		} 
    		// Tilt Right To Change Type 
    		else if(rollDiff <= -15){
    			changed = true;
    			currType = (currType == 2)?0:currType+1;
        		stringSize = dictionary[currType][currGroup].length();
    			currCharPos = 0;
    		} 
    		// Tilt Up To Change Group 
    		if(pitchDiff >= 15){
    			changed = true;
    			currGroup = (currGroup == 0)?4:currGroup-1;
        		stringSize = dictionary[currType][currGroup].length();
    			currCharPos = 0;
    		} 
    		// Tilt Down To Input 
    		else if(pitchDiff <= -10){
    			String prevText = mTypeField.getText().toString();
    			mTypeField.setText(String.format("%s%s", prevText,dictionary[currType][currGroup].subSequence(currCharPos, currCharPos+1)));
    			mMediaPlayer.start();
    		} 
    		 
    		if(changed){
    			setText(); 
    			mMediaPlayer.start();
    		} 
    		lock.unlock();
    	} 
    } 
     
    public void setText(){ 
		medTopText.setText(dictionary[currType][(currGroup == 0)?4:currGroup-1]);