Android Open Source - Speedometer Measurement Schedule Console Activity






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.
 */* ww w  . ja va  2s .  co m*/
 * 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 android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.AbstractCollection;
import java.util.Date;
import java.util.HashMap;

/**
 * Activity that shows the current measurement schedule of the scheduler
 * 
 * @author wenjiezeng@google.com (Steve Zeng)
 *
 */
public class MeasurementScheduleConsoleActivity extends Activity {
  public static final String TAB_TAG = "MEASUREMENT_SCHEDULE";
  
  private MeasurementScheduler scheduler;
  private SpeedometerApp parent;
  private ListView consoleView;
  private TextView lastCheckinTimeText;
  private ArrayAdapter<String> consoleContent;
  // Maps the toString() of a measurementTask to its key
  private HashMap<String, String> taskMap;
  private int longClickedItemPosition = -1;
  private BroadcastReceiver receiver;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.measurement_schedule);
    
    taskMap = new HashMap<String, String>();
    parent = (SpeedometerApp) this.getParent();
    consoleContent = new ArrayAdapter<String>(this, R.layout.list_item);
    this.consoleView = (ListView) this.findViewById(R.id.measurementScheduleConsole);
    this.consoleView.setAdapter(consoleContent);
    lastCheckinTimeText = (TextView)this.findViewById(R.id.lastCheckinTime);
    Button checkinButton = (Button) this.findViewById(R.id.checkinButton);
    checkinButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        doCheckin();
      }
    });

    registerForContextMenu(consoleView);
    consoleView.setOnItemLongClickListener(new OnItemLongClickListener() {
      /**
       * Records which item in the list is selected
       */
      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        longClickedItemPosition = position;
        return false;
      }
    });
    
    // Register activity specific BroadcastReceiver here    
    IntentFilter filter = new IntentFilter();
    filter.addAction(UpdateIntent.SCHEDULER_CONNECTED_ACTION);
    filter.addAction(UpdateIntent.SYSTEM_STATUS_UPDATE_ACTION);
    this.receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
        Logger.d("MeasurementConsole got intent");
        /* The content of the console is maintained by the scheduler. We simply hook up the 
         * view with the content here. */
        updateConsole();
      }
    };
    registerReceiver(receiver, filter);
  }
  
  /**
   * Handles context menu creation for the ListView in the console
   */
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v,
                                  ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.scheduler_console_context_menu, menu);
  }
  
  @Override
  protected void onResume() {
    super.onResume();
    updateConsole();
  }
  
  @Override
  protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
  }
  
  /**
   * Handles the deletion of the measurement tasks when the user clicks the context menu
   */
  @Override
  public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
      case R.id.ctxMenuDeleteTask:
        scheduler = parent.getScheduler();
        if (scheduler != null) {
          String selectedTaskString = consoleContent.getItem(longClickedItemPosition);
          String taskKey = taskMap.get(selectedTaskString);
          if (taskKey != null) {
            scheduler.removeTaskByKey(taskKey);
          }
        }
        updateConsole();
        return true;
      default:
    }
    return false;
  }
  
  private void updateLastCheckinTime() {
    Logger.i("updateLastCheckinTime() called");
    scheduler = parent.getScheduler();
    if (scheduler != null) {
      Date lastCheckin = scheduler.getLastCheckinTime();
      if (lastCheckin != null) {
        lastCheckinTimeText.setText("Last checkin " + lastCheckin);
      } else {
        lastCheckinTimeText.setText("No checkins yet");
      }
    }
  }
  
  private void updateConsole() {
    Logger.i("updateConsole() called");
    scheduler = parent.getScheduler();
    if (scheduler != null) {
      AbstractCollection<MeasurementTask> tasks = scheduler.getTaskQueue();
      consoleContent.clear();
      taskMap.clear();
      for (MeasurementTask task : tasks) {
        String taskStr = task.toString();
        consoleContent.add(taskStr);
        taskMap.put(taskStr, task.getDescription().key);
      }
    }
    updateLastCheckinTime();
  }
  
  private void doCheckin() {
    Logger.i("doCheckin() called");
    scheduler = parent.getScheduler();
    if (scheduler != null) {
      lastCheckinTimeText.setText("Checking in...");
      scheduler.handleCheckin(true);
    }
  }
  
}




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