Android Open Source - Speedometer Measurement Task






From Project

Back to project page Speedometer.

License

The source code is released under:

Apache License

If you think the Android project Speedometer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* Copyright 2012 Google Inc.
 */*  w  ww.ja  v a2s  . c  om*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.wireless.speed.speedometer;


import com.google.wireless.speed.speedometer.measurements.DnsLookupTask;
import com.google.wireless.speed.speedometer.measurements.HttpTask;
import com.google.wireless.speed.speedometer.measurements.PingTask;
import com.google.wireless.speed.speedometer.measurements.TracerouteTask;
import com.google.wireless.speed.speedometer.measurements.UDPBurstTask;


import android.content.Context;
import android.content.Intent;

import java.io.InvalidClassException;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.Callable;

/**
 * Represents a scheduled measurement task. Subclasses implement functionality
 * for performing the actual measurement. Comparable interface allow comparison
 * inside the priority queue.
 * 
 * @author mdw@google.com (Matt Welsh)
 * @author wenjiezeng@google.com (Wenjie Zeng)
 */
@SuppressWarnings("rawtypes")
public abstract class MeasurementTask implements Callable<MeasurementResult>, Comparable {
  // the priority queue we use puts the smallest element in the head of the queue
  public static final int USER_PRIORITY = Integer.MIN_VALUE;
  public static final int INVALID_PRIORITY = Integer.MAX_VALUE;
  public static final int INFINITE_COUNT = -1;
  
  protected MeasurementDesc measurementDesc;
  protected Context parent;
  /* When updating the 'progress' field, ensure that it's within the range between 0 and
   * Config.MAX_PROGRESS_BAR_VALUE, inclusive. Values outside this range have special meanings and
   * can trigger unexpected results.
   */
  protected int progress;
  private static HashMap<String, Class> measurementTypes;
  // Maps between the type of task and its readable name
  private static HashMap<String, String> measurementDescToType;
  
  // TODO(Wenjie): Static initializer for type -> Measurement map
  // Add new measurement types here to enable them 
  static {    
    measurementTypes = new HashMap<String, Class>();
    measurementDescToType = new HashMap<String, String>();
    measurementTypes.put(PingTask.TYPE, PingTask.class);
    measurementDescToType.put(PingTask.DESCRIPTOR, PingTask.TYPE);
    measurementTypes.put(HttpTask.TYPE, HttpTask.class);
    measurementDescToType.put(HttpTask.DESCRIPTOR, HttpTask.TYPE);
    measurementTypes.put(TracerouteTask.TYPE, TracerouteTask.class);
    measurementDescToType.put(TracerouteTask.DESCRIPTOR, TracerouteTask.TYPE);
    measurementTypes.put(DnsLookupTask.TYPE, DnsLookupTask.class);
    measurementDescToType.put(DnsLookupTask.DESCRIPTOR, DnsLookupTask.TYPE);
    measurementTypes.put(UDPBurstTask.TYPE, UDPBurstTask.class);
    measurementDescToType.put(UDPBurstTask.DESCRIPTOR, UDPBurstTask.TYPE);    
  }
  
  /** Gets the currently available measurement types*/
  public static Set<String> getMeasurementNames() {
    return measurementDescToType.keySet();
  }
  
  /** Get the type of a measurement based on its name. Type is for JSON interface only
   * where as measurement name is a readable string for the UI */
  public static String getTypeForMeasurementName(String name) {
    return measurementDescToType.get(name);
  }
  
  public static Class getTaskClassForMeasurement(String type) {
    return measurementTypes.get(type);
  }
  
  /* This is put here for consistency that all MeasurementTask should
   * have a getDescClassForMeasurement() method. However, the MeasurementDesc is abstract 
   * and cannot be instantiated */
  public static Class getDescClass() throws InvalidClassException {
    throw new InvalidClassException("getDescClass() should only be invoked on "
        + "subclasses of MeasurementTask.");
  }
  
  /**
   * @param measurementDesc
   * @param parent
   */
  protected MeasurementTask(MeasurementDesc measurementDesc, Context parent) {
    super();
    this.measurementDesc = measurementDesc;
    this.parent = parent;
    this.progress = 0;
  }
  
  /* Compare priority as the first order. Then compare start time.*/
  @Override
  public int compareTo(Object t) {
    MeasurementTask another = (MeasurementTask) t;
    Long myPrority = this.measurementDesc.priority;
    Long anotherPriority = another.measurementDesc.priority;
    int priorityComparison = myPrority.compareTo(anotherPriority); 
    if (priorityComparison == 0 && 
        this.measurementDesc.startTime != null && another.measurementDesc.startTime != null) {
      return this.measurementDesc.startTime.compareTo(another.measurementDesc.startTime);
    } else {
      return priorityComparison;
    }
  }  
  
  public long timeFromExecution() {
    return this.measurementDesc.startTime.getTime() - System.currentTimeMillis();
  }
  
  public boolean isPassedDeadline() {
    if (this.measurementDesc.endTime == null) { 
      return false;
    } else {
      long endTime = this.measurementDesc.endTime.getTime();
      return endTime <= System.currentTimeMillis();
    }
  }
  
  public String getMeasurementType() {
    return this.measurementDesc.type;
  }
  
  public MeasurementDesc getDescription() {
    return this.measurementDesc;
  }
  
  /**
   * Returns a brief human-readable descriptor of the task.
   */
  public abstract String getDescriptor();
  
  @Override
  public abstract MeasurementResult call() throws MeasurementError; 
  
  /** Return the string indicating the measurement type. */
  public abstract String getType();
  
  /* Place holder in case user wants to view the progress of active measurements*/
  public int getProgress() {
    return this.progress;
  }
    
  @Override
  public String toString() {
    String result = "[Measurement " + getDescriptor() + " scheduled to run at " + 
        getDescription().startTime + "]";
    
    return this.measurementDesc.toString();
  }
  
  @Override
  public abstract MeasurementTask clone();
  
  /**
   * Stop the measurement, even when it is running. There is no side effect 
   * if the measurement has not started or is already finished.
   */
  public abstract void stop();
  
  public void broadcastProgressForUser(int progress) {
    if (measurementDesc.priority == MeasurementTask.USER_PRIORITY) {
      Intent intent = new Intent();
      intent.setAction(UpdateIntent.MEASUREMENT_PROGRESS_UPDATE_ACTION);
      intent.putExtra(UpdateIntent.PROGRESS_PAYLOAD, progress);
      intent.putExtra(UpdateIntent.TASK_PRIORITY_PAYLOAD, MeasurementTask.USER_PRIORITY);
      parent.sendBroadcast(intent);
    }
  }
}




Java Source Code List

com.google.wireless.speed.speedometer.AboutActivity.java
com.google.wireless.speed.speedometer.AccountSelector.java
com.google.wireless.speed.speedometer.BatteryCapPowerManager.java
com.google.wireless.speed.speedometer.Checkin.java
com.google.wireless.speed.speedometer.Config.java
com.google.wireless.speed.speedometer.DeviceInfo.java
com.google.wireless.speed.speedometer.DeviceProperty.java
com.google.wireless.speed.speedometer.Logger.java
com.google.wireless.speed.speedometer.MeasurementCreationActivity.java
com.google.wireless.speed.speedometer.MeasurementDesc.java
com.google.wireless.speed.speedometer.MeasurementError.java
com.google.wireless.speed.speedometer.MeasurementResult.java
com.google.wireless.speed.speedometer.MeasurementScheduleConsoleActivity.java
com.google.wireless.speed.speedometer.MeasurementScheduler.java
com.google.wireless.speed.speedometer.MeasurementSkippedException.java
com.google.wireless.speed.speedometer.MeasurementTask.java
com.google.wireless.speed.speedometer.ResultsConsoleActivity.java
com.google.wireless.speed.speedometer.SpeedometerApp.java
com.google.wireless.speed.speedometer.SpeedometerPreferenceActivity.java
com.google.wireless.speed.speedometer.SplashScreenActivity.java
com.google.wireless.speed.speedometer.SystemConsoleActivity.java
com.google.wireless.speed.speedometer.UpdateIntent.java
com.google.wireless.speed.speedometer.WatchdogBootReceiver.java
com.google.wireless.speed.speedometer.measurements.DnsLookupTask.java
com.google.wireless.speed.speedometer.measurements.HttpTask.java
com.google.wireless.speed.speedometer.measurements.PingTask.java
com.google.wireless.speed.speedometer.measurements.TracerouteTask.java
com.google.wireless.speed.speedometer.measurements.UDPBurstTask.java
com.google.wireless.speed.speedometer.util.MeasurementJsonConvertor.java
com.google.wireless.speed.speedometer.util.PhoneUtils.java
com.google.wireless.speed.speedometer.util.Util.java