/* Copyright 2012 Google Inc.
*
* 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.myjson.reflect.TypeToken;
import com.google.wireless.speed.speedometer.BatteryCapPowerManager.PowerAwareTask;
import com.google.wireless.speed.speedometer.util.MeasurementJsonConvertor;
import com.google.wireless.speed.speedometer.util.PhoneUtils;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.ArrayAdapter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.PriorityBlockingQueue;
/**
* The single scheduler thread that monitors the task queue, runs tasks at their specified
* times, and finally retrieves and reports results once they finish.
*
* All method invocations on the singleton object are thread-safe.
*
* @author wenjiezeng@google.com (Steve Zeng)
*/
public class MeasurementScheduler extends Service {
// This arbitrary id is private to Speedometer
private static final int NOTIFICATION_ID = 1234;
private ExecutorService measurementExecutor;
private BroadcastReceiver broadcastReceiver;
private Boolean pauseRequested = true;
private boolean stopRequested = false;
private boolean isSchedulerStarted = false;
private Checkin checkin;
private long checkinIntervalSec;
private long checkinRetryIntervalSec;
private int checkinRetryCnt;
private CheckinTask checkinTask;
private Calendar lastCheckinTime;
private PendingIntent checkinIntentSender;
/**
* Intent for checkin retries. Reusing checkinIntentSender for retries will cancel any
* previously configured periodic checkin schedule. Thus we need a separate intent sender */
private PendingIntent checkinRetryIntentSender;
private PendingIntent measurementIntentSender;
private AlarmManager alarmManager;
private BatteryCapPowerManager powerManager;
/* Both taskQueue and pendingTasks are thread safe and operations on them are atomic.
* To guarantee reliable value propagation between threads, use volatile keyword.
*/
private volatile PriorityBlockingQueue<MeasurementTask> taskQueue;
private volatile
ConcurrentHashMap<MeasurementTask, Future<MeasurementResult>> pendingTasks;
// Binder given to clients
private final IBinder binder = new SchedulerBinder();
private MeasurementTask currentTask;
private NotificationManager notificationManager;
private int completedMeasurementCnt = 0;
private int failedMeasurementCnt = 0;
private ArrayList<String> userResults;
private ArrayList<String> systemResults;
private ArrayList<String> systemConsole;
private PhoneUtils phoneUtils;
/**
* The Binder class that returns an instance of running scheduler
*/
public class SchedulerBinder extends Binder {
public MeasurementScheduler getService() {
return MeasurementScheduler.this;
}
}
/* Returns a IBinder that contains the instance of the MeasurementScheduler object
* @see android.app.Service#onBind(android.content.Intent)
*/
@Override
public IBinder onBind(Intent intent) {
Logger.d("Service onBind called");
return this.binder;
}
// Service objects are by nature singletons enforced by Android
@Override
public void onCreate() {
Logger.d("Service onCreate called");
PhoneUtils.setGlobalContext(this.getApplicationContext());
phoneUtils = PhoneUtils.getPhoneUtils();
phoneUtils.registerSignalStrengthListener();
this.checkin = new Checkin(this);
this.checkinRetryIntervalSec = Config.MIN_CHECKIN_RETRY_INTERVAL_SEC;
this.checkinRetryCnt = 0;
this.checkinTask = new CheckinTask();
this.pauseRequested = true;
this.stopRequested = false;
this.measurementExecutor = Executors.newSingleThreadExecutor();
this.taskQueue =
new PriorityBlockingQueue<MeasurementTask>(Config.MAX_TASK_QUEUE_SIZE,
new TaskComparator());
this.pendingTasks =
new ConcurrentHashMap<MeasurementTask, Future<MeasurementResult>>();
this.notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
this.alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
this.powerManager = new BatteryCapPowerManager(Config.DEFAULT_BATTERY_THRESH_PRECENT, this);
restoreState();
// Register activity specific BroadcastReceiver here
IntentFilter filter = new IntentFilter();
filter.addAction(UpdateIntent.PREFERENCE_ACTION);
filter.addAction(UpdateIntent.MSG_ACTION);
filter.addAction(UpdateIntent.CHECKIN_ACTION);
filter.addAction(UpdateIntent.CHECKIN_RETRY_ACTION);
filter.addAction(UpdateIntent.MEASUREMENT_ACTION);
filter.addAction(UpdateIntent.MEASUREMENT_PROGRESS_UPDATE_ACTION);
broadcastReceiver = new BroadcastReceiver() {
// Handles various broadcast intents.
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(UpdateIntent.PREFERENCE_ACTION)) {
updateFromPreference();
} else if (intent.getAction().equals(UpdateIntent.CHECKIN_ACTION) ||
intent.getAction().equals(UpdateIntent.CHECKIN_RETRY_ACTION)) {
Logger.d("Checkin intent received");
handleCheckin(false);
} else if (intent.getAction().equals(UpdateIntent.MEASUREMENT_ACTION)) {
Logger.d("MeasurementIntent intent received");
handleMeasurement();
} else if (intent.getAction().equals(UpdateIntent.MEASUREMENT_PROGRESS_UPDATE_ACTION)) {
Logger.d("MeasurementIntent update intent received");
if (intent.getIntExtra(UpdateIntent.PROGRESS_PAYLOAD, Config.INVALID_PROGRESS) ==
Config.MEASUREMENT_END_PROGRESS) {
if (intent.getStringExtra(UpdateIntent.ERROR_STRING_PAYLOAD) != null) {
failedMeasurementCnt++;
} else {
completedMeasurementCnt++;
}
updateResultsConsole(intent);
}
} else if (intent.getAction().equals(UpdateIntent.MSG_ACTION)) {
String msg = intent.getExtras().getString(UpdateIntent.STRING_PAYLOAD);
Date now = Calendar.getInstance().getTime();
insertStringToConsole(systemConsole, now + "\n\n" + msg);
}
}
};
this.registerReceiver(broadcastReceiver, filter);
// TODO(mdw):
没有合适的资源?快使用搜索试试~ 我知道了~
github上的网络测速demo
共200个文件
class:82个
png:54个
java:33个
需积分: 10 18 下载量 29 浏览量
2015-09-22
10:52:01
上传
评论
收藏 1.06MB ZIP 举报
温馨提示
github上的网络测速demo,下载看看是不是你想要的
资源推荐
资源详情
资源评论
收起资源包目录
github上的网络测速demo (200个子文件)
resources.ap_ 125KB
Speedometer Google.apk 256KB
MeasurementScheduler.class 29KB
PhoneUtils.class 20KB
Checkin.class 14KB
PingTask.class 14KB
SpeedometerApp.class 12KB
TracerouteTask.class 11KB
UDPBurstTask.class 10KB
HttpTask.class 10KB
MeasurementResult.class 9KB
MeasurementCreationActivity.class 8KB
MeasurementScheduleConsoleActivity.class 7KB
AccountSelector.class 7KB
MeasurementCreationActivity$ButtonOnClickListener.class 7KB
MeasurementTask.class 6KB
BatteryCapPowerManager$PowerAwareTask.class 6KB
Util.class 5KB
MeasurementJsonConvertor.class 5KB
ResultsConsoleActivity.class 5KB
AccountSelector$GetCookieTask.class 5KB
DnsLookupTask.class 5KB
MeasurementScheduler$UserMeasurementTask.class 4KB
MeasurementScheduler$CheckinTask.class 4KB
TracerouteTask$TracerouteDesc.class 4KB
MeasurementScheduler$1.class 3KB
MeasurementJsonConvertor$DateTypeConverter.class 3KB
UDPBurstTask$UDPBurstDesc.class 3KB
SystemConsoleActivity.class 3KB
HttpTask$HttpDesc.class 3KB
R$string.class 2KB
MeasurementCreationActivity$MeasurementTypeOnItemSelectedListener.class 2KB
PingTask$PingDesc.class 2KB
SpeedometerPreferenceActivity.class 2KB
SpeedometerPreferenceActivity$1.class 2KB
UpdateIntent.class 2KB
Config.class 2KB
MeasurementDesc.class 2KB
SpeedometerApp$1.class 2KB
Checkin$MySSLSocketFactory.class 2KB
DnsLookupTask$DnsLookupDesc.class 2KB
R$id.class 2KB
SystemConsoleActivity$1.class 2KB
PhoneUtils$LoggingLocationListener.class 2KB
ResultsConsoleActivity$2.class 2KB
PhoneUtils$SignalStrengthChangeListener.class 2KB
AccountSelector$1.class 2KB
SpeedometerApp$2.class 1KB
DeviceProperty.class 1KB
MeasurementScheduleConsoleActivity$2.class 1KB
MeasurementCreationActivity$EditBoxFocusChangeListener.class 1KB
MeasurementScheduler$TaskComparator.class 1KB
WatchdogBootReceiver.class 1KB
MeasurementCreationActivity$RadioOnClickListener.class 1KB
Checkin$MySSLSocketFactory$1.class 1KB
BatteryCapPowerManager.class 1KB
PhoneUtils$InterfaceType.class 1KB
ResultsConsoleActivity$1.class 1KB
SplashScreenActivity$1.class 1KB
PhoneUtils$PowerStateChangeReceiver.class 1KB
Logger.class 1KB
MeasurementScheduleConsoleActivity$3.class 1KB
SpeedometerApp$3.class 1KB
MeasurementScheduleConsoleActivity$1.class 1KB
R$drawable.class 1018B
SpeedometerApp$4.class 999B
TracerouteTask$HopInfo.class 969B
AboutActivity.class 961B
SplashScreenActivity.class 929B
R.class 926B
MeasurementScheduler$2.class 841B
MeasurementScheduler$3.class 837B
MeasurementScheduler$SchedulerBinder.class 807B
R$layout.class 748B
DeviceProperty$GeoLocation.class 740B
R$dimen.class 705B
MeasurementSkippedException.class 653B
MeasurementError.class 585B
R$color.class 510B
R$menu.class 504B
R$xml.class 448B
DeviceInfo.class 438B
R$attr.class 397B
BuildConfig.class 383B
.classpath 566B
classes.dex 361KB
.gitignore 110B
myjson-1.7.1.jar 173KB
myjson-1.7.1-9e2244a48332be73ead6925f47a97d3d.jar 64KB
MeasurementScheduler.java 37KB
PhoneUtils.java 25KB
PingTask.java 17KB
UDPBurstTask.java 16KB
Checkin.java 14KB
TracerouteTask.java 14KB
MeasurementCreationActivity.java 13KB
SpeedometerApp.java 13KB
HttpTask.java 11KB
AccountSelector.java 10KB
MeasurementResult.java 9KB
共 200 条
- 1
- 2
资源评论
bin471
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Spring Bootd实现的图像去雾系统端,完成主要的前后端交互+源代码+文档说明
- 企业网站建设-PPT.ppt
- 办公自动化Microsoft-Office学习教程.doc
- 办公软件ECEL技巧培训课件-PPT.pptx
- 办公软件Word快捷键大全.doc
- Springboot集成SpringbootAdmin实现服务监控管理-源码
- 办公软件应用-计算机一级考试试题.doc
- 毕业设计-图像去雾,基于matlab实现的暗通道先验算法和Retinex图像增强算法制作的图形化界面程序仿真源码
- 基于python+opencv实现的暗通道先验的单幅图像去雾算法python仿真源代码+文档说明+截图演示(高分课程设计)
- 基于STM32G4与智能摄像头的疲劳驾驶检测系统源代码(96分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功