Early Resource Binding

Context

Implementation,UI

Affects

Energy Efficiency

Problem

Gottschalk, Josefiok, Jelschen and Winter discussed in “Removing Energy Code Smells with Reengineering Services”
that if physical, energy-consuming resources of an Android device are requested too early more energy is consumed.
More precisely, “too early” means when they are requested in methods being executed before the user is interacting with the app.

For instance, in the following example the GPS component of an Android device is requested in the 'onCreate()' method already.
Thus, the GPS physical component consumes energy while the user isn't interacting with any map since nothing is visible yet.
This example is taken from the above mentioned publication:

public class GpsPrint extends Activity implements OnClickListener, Listener, LocationListener {

    private LocationManager lm;

    public void onCreate(Bundle savedInstanceState) {
        lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        if(lm.getAllProviders().contains(LocationManager.GPS_PROVIDER)){
            if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                lm.addGpsStatusListener(this);
                // here the physical component is requested and consumes energy
                // but the user cannot yet interact with the app in this state
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
                statusView.setText(”GPS service started”);
            } else {
                statusView.setText(”Please enable GPS”);
                saveLocationButton.setEnabled(false);
            }
        }
    }

    public void onPause() {
        lm.removeUpdates(this);
    }
}

Refactorings

Move Resource Request to Visible Method

Resolves

Energy Efficiency

Affects

Responsiveness

Solution

Move the physical resource requesting statement to the onResume().
This method corresponds to the visible state of an Activity.
Thus, the physical resource is consuming energy but only when the app is visible.
Hence, less energy is consumed because of less time.

public class GpsPrint extends Activity implements OnClickListener, Listener, LocationListener {

    private LocationManager lm;

    public void onCreate(Bundle savedInstanceState) {
        lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        if(lm.getAllProviders().contains(LocationManager.GPS_PROVIDER)){
            if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                lm.addGpsStatusListener(this);
                // here the request was removed
                statusView.setText(”GPS service started”);
            } else {
                statusView.setText(”Please enable GPS”);
                saveLocationButton.setEnabled(false);
            }
        }
    }
    public void onPause() {
        lm.removeUpdates(this);
    }
    public void onResume(){
        // request was moved to the 'visible' onResume() method
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
    }
}

Links

Links

Related