[Galaxy S 4 i9505 Android Development] hawkerpaul:
I have written this shell script for those of you who are having problems with your GS4's screen not responding as expected during calls, such as the screen not turning off when you put your device to your ear, and not turning on when you take your device away from your ear etc.

Before running this script, the first thing to try would be to clean the proximity sensor itself. Dismantling the device and cleaning it is by far the best method. See this video.

However, an easier method is to blow compressed air down into the sensor (which is located under the silver earpiece at the top of the phone) and also clean it with a vacuum cleaner.

If you are still experiencing erratic behaviour even after doing this, then this adb shell utility may be of some help to you.

NOTE: You need a rooted device for this utility to work!!

Downoad Here

Download Here

Installation Instructions

1. Download ProxCal.sh from the above link

2. If downloaded via PC, copy ProxCal.sh onto your device.

Job done! :)

Script Execution

1. Open up an ADB Shell on your PC, or any sort of Terminal Emulator console session on your handset

2. If you are not at the root (#) prompt, enter su

3. Change directory to where you downloaded/copied ProxCal.sh eg. cd /sdcard/scripts

4. sh ProxCal.sh

You will then be presented with a straight forward menu of options:

ProxCal v1.00

-------------

Re-calibrate your handset's proximity sensor

Use this utility if you are having problems with your handset's

screen reacting normally during calls, even after cleaning

the sensor with compressed air etc.

Handset: GS4 (GT-I9505G)

Sensor: MAX88920

1. Show current values

2. Test run

3. Recalibrate proximity sensor

X. Exit

Enter choice:​

1. Show current values

Shows the current values of proximity sensor related files on your device. The state value is the important one here. With nothing in front of the sensor, this should be as close to 0 as possible. A high value signifies a problem with the sensor, and that it requires cleaning and/or re-calibrating. Try performing this function as many times as you wish, placing your hand at varying distances from the sensor each time. See how the sensor reacts by examining the state value.

Current Proximity Sensor Values:

prox_cal : 0,60,45

state : 4

efs_prox_cal: *​

2. Test run

Performs a test run. No files are altered during this process. The function only displays what it would have done on an actual recalibration run.

Re-calibrating GS4 proximity sensor.

(Test Mode)

Please wait.......

Recalculating efs offset...

Current offset: 0

Current state: 3

New offset: 3

Test Run Log

------------

echo 0 > /sys/devices/virtual/sensors/proximity_sensor/prox_cal

echo 1 > /sys/devices/virtual/sensors/proximity_sensor/prox_cal

mkdir -p /sdcard/tmp/ProxCal/BKP

cp /efs/prox_cal /sdcard/tmp/ProxCal/BKP/efs_prox_cal.2014-09-04--07-55

echo $\x3 > /efs/prox_cal

chown system:system /efs/prox_cal

chmod 644 /efs/prox_cal

Re-calibration test complete.​

3. Recalibrate proximity sensor

This option actually recalibrates the sensor by updating the necessary files.

The proximity sensor will now be recalibrated.

Please ensure there is nothing in front of the sensor.

Proceed? (Y/N) y

y

Re-calibrating GS4 proximity sensor.

Please wait.......

Recalculating efs offset...

Current offset: 0

Current state: 3

/efs/prox_cal backup created:

/sdcard/tmp/ProxCal/BKP/efs_prox_cal.2014-09-04--08-01

New offset: 3

Re-calibration complete.​

Backups

A backup of /efs/prox_cal is performed during an actual run (menu option 3)

Backups of this file are made into /sdcard/tmp/ProxCal/BKP

A backup is made on a date/time basis. Eg. efs_prox_cal.2014-09-04--07-55

Should you need to restore a backed up file, then type the following at and adb shell # prompt:

cd /sdcard/tmp/ProxCal/BKP

cp efs_prox_cal.[date-stamp] /efs/prox_cal

X. Exit

Exits the shell script and returns you back to a # prompt.

If the sensor was calibrated you will get the following message:

Your proximity sensor has been re-calibrated.

Please reboot your GS4 then re-run

this utility to check the new values.

Pay particular attention to the 'state' value.

When nothing is close to the sensor, this should

be a very low value (the closer to 0 the better)

** Please reboot your GS4 ***

proxcal.sh completed successfully.​

Automatic Script Execution

The script can be called to automatically calibrate the sensor without any further user interaction.

This is done by including the auto parameter.

ie:

sh ProxCal.sh auto

This is useful if you want to automatically recalibrate the sensor on each boot using init.d scripts.

Credits

This script merely automates the tasks that I found on the internet which recalibrates the sensor. Examples:

XDA thread: Proximity Sensor Bug Fixed, .Screen turns off during call.ISSUE FIXED by [MENTION=4454975]Ammar Saood[/MENTION]

Android Legend post

CM thread on Reddit

CM Forum thread

Recalibration app on the Play Store, suggested in above thread

Full credit goes to these guys. :)