use of java.util.TimerTask in project Space-Station-Tracker by Kiarasht.
the class Alert method onStartCommand.
/**
* Starting a service for ISS Location updater.
*
* @param intent N/A
* @param startId N/A
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return START_STICKY;
}
// (30 minutes) minimum time interval between mLocation updates, in milliseconds
final int locationTime = 1800000;
// (1500 meters) minimum distance between mLocation updates, in meters
final int locationDistance = 500;
// (59 minutes) Time to repeat a compare between ISS and user's mLocation
final int timerRepeat = 3540000;
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, locationTime, locationDistance, locationListener);
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Check if permission are granted
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
// Find user's last known mLocation
mLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (mLocation == null) {
return;
}
dates = null;
for (int i = 0; i < 30 && sharedPreferences.getBoolean("notification_ISS", false) && (dates == null || dates.get(0) == null); ++i) {
// Get ISSs passes, saving them in an array of dates
dates = locations.displayPasses(String.valueOf(mLocation.getLatitude()), String.valueOf(mLocation.getLongitude()), context);
// Try 30 times for 10 minutes.
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Get user's current date
Date date = new Date();
long last = sharedPreferences.getLong("time", 0);
if (last == 0) {
accept = new Date(System.currentTimeMillis() - 3600 * 1000);
} else {
accept = new Date(last);
}
// Compare dates from displayPasses to user's current date
for (Date date1 : dates) {
if (date1 != null) {
// 1 hour
boolean withinHour = Math.abs(date.getTime() - date1.getTime()) < 3600000L;
// 59 minutes
boolean duplicate = Math.abs(accept.getTime() - date.getTime()) < 3540000L;
if (withinHour && !duplicate) {
sharedPreferences.edit().putLong("time", date.getTime()).apply();
notification(Math.abs(date.getTime() - date1.getTime()));
break;
}
}
}
}
}, 0, timerRepeat);
return START_STICKY;
}
use of java.util.TimerTask in project Space-Station-Tracker by Kiarasht.
the class MapsActivity method onMapReady.
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMaptype();
if (mTimer == null) {
mTimer = new Timer();
}
// Every 90 minutes, update the polylines automatically
if (mPolyTimer == null) {
mPolyTimer = new Timer();
TimerTask hourlyTask = new TimerTask() {
@Override
public void run() {
asyncTaskPolyline();
}
};
// 90 minutes
mPolyTimer.schedule(hourlyTask, 0L, 5400000);
}
mMarkerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.iss_2011));
mMarkerOptions.anchor(0.5f, 0.5f);
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
trackISS();
}
}, 0, mRefreshrate);
}
use of java.util.TimerTask in project android_frameworks_base by AOSPA.
the class LocationBasedCountryDetector method detectCountry.
/**
* Start detecting the country.
* <p>
* Queries the location from all location providers, then starts a thread to query the
* country from GeoCoder.
*/
@Override
public synchronized Country detectCountry() {
if (mLocationListeners != null) {
throw new IllegalStateException();
}
// Request the location from all enabled providers.
List<String> enabledProviders = getEnabledProviders();
int totalProviders = enabledProviders.size();
if (totalProviders > 0) {
mLocationListeners = new ArrayList<LocationListener>(totalProviders);
for (int i = 0; i < totalProviders; i++) {
String provider = enabledProviders.get(i);
if (isAcceptableProvider(provider)) {
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
LocationBasedCountryDetector.this.stop();
queryCountryCode(location);
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
mLocationListeners.add(listener);
registerListener(provider, listener);
}
}
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
mTimer = null;
LocationBasedCountryDetector.this.stop();
// Looks like no provider could provide the location, let's try the last
// known location.
queryCountryCode(getLastKnownLocation());
}
}, getQueryLocationTimeout());
} else {
// There is no provider enabled.
queryCountryCode(getLastKnownLocation());
}
return mDetectedCountry;
}
use of java.util.TimerTask in project lucene-solr by apache.
the class HdfsTestUtil method setupClass.
public static MiniDFSCluster setupClass(String dir, boolean safeModeTesting, boolean haTesting) throws Exception {
LuceneTestCase.assumeFalse("HDFS tests were disabled by -Dtests.disableHdfs", Boolean.parseBoolean(System.getProperty("tests.disableHdfs", "false")));
savedLocale = Locale.getDefault();
// TODO: we HACK around HADOOP-9643
Locale.setDefault(Locale.ENGLISH);
if (!HA_TESTING_ENABLED)
haTesting = false;
// keep netty from using secure random on startup: SOLR-10098
ThreadLocalRandom.setInitialSeedUniquifier(1L);
int dataNodes = Integer.getInteger("tests.hdfs.numdatanodes", 2);
Configuration conf = new Configuration();
conf.set("dfs.block.access.token.enable", "false");
conf.set("dfs.permissions.enabled", "false");
conf.set("hadoop.security.authentication", "simple");
conf.set("hdfs.minidfs.basedir", dir + File.separator + "hdfsBaseDir");
conf.set("dfs.namenode.name.dir", dir + File.separator + "nameNodeNameDir");
conf.setBoolean("fs.hdfs.impl.disable.cache", true);
System.setProperty("test.build.data", dir + File.separator + "hdfs" + File.separator + "build");
System.setProperty("test.cache.data", dir + File.separator + "hdfs" + File.separator + "cache");
System.setProperty("solr.lock.type", DirectoryFactory.LOCK_TYPE_HDFS);
System.setProperty("solr.hdfs.blockcache.global", Boolean.toString(LuceneTestCase.random().nextBoolean()));
final MiniDFSCluster dfsCluster;
if (!haTesting) {
dfsCluster = new MiniDFSCluster(conf, dataNodes, true, null);
System.setProperty("solr.hdfs.home", getDataDir(dfsCluster, "solr_hdfs_home"));
} else {
dfsCluster = new MiniDFSCluster.Builder(conf).nnTopology(MiniDFSNNTopology.simpleHATopology()).numDataNodes(dataNodes).build();
Configuration haConfig = getClientConfiguration(dfsCluster);
HdfsUtil.TEST_CONF = haConfig;
System.setProperty("solr.hdfs.home", getDataDir(dfsCluster, "solr_hdfs_home"));
}
dfsCluster.waitActive();
if (haTesting)
dfsCluster.transitionToActive(0);
int rndMode = LuceneTestCase.random().nextInt(3);
if (safeModeTesting && rndMode == 1) {
NameNodeAdapter.enterSafeMode(dfsCluster.getNameNode(), false);
int rnd = LuceneTestCase.random().nextInt(10000);
Timer timer = new Timer();
timers.put(dfsCluster, timer);
timer.schedule(new TimerTask() {
@Override
public void run() {
NameNodeAdapter.leaveSafeMode(dfsCluster.getNameNode());
}
}, rnd);
} else if (haTesting && rndMode == 2) {
int rnd = LuceneTestCase.random().nextInt(30000);
Timer timer = new Timer();
timers.put(dfsCluster, timer);
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO: randomly transition to standby
// try {
// dfsCluster.transitionToStandby(0);
// dfsCluster.transitionToActive(1);
// } catch (IOException e) {
// throw new RuntimeException();
// }
}
}, rnd);
} else {
// TODO: we could do much better at testing this
// force a lease recovery by creating a tlog file and not closing it
URI uri = dfsCluster.getURI();
Path hdfsDirPath = new Path(uri.toString() + "/solr/collection1/core_node1/data/tlog/tlog.0000000000000000000");
// tran log already being created testing
badTlogOutStreamFs = FileSystem.get(hdfsDirPath.toUri(), conf);
badTlogOutStream = badTlogOutStreamFs.create(hdfsDirPath);
}
SolrTestCaseJ4.useFactory("org.apache.solr.core.HdfsDirectoryFactory");
return dfsCluster;
}
use of java.util.TimerTask in project sling by apache.
the class TestInitDelayingTopologyEventListener method createScheduler.
private Scheduler createScheduler() {
return new Scheduler() {
@Override
public boolean unschedule(String jobName) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean schedule(final Object job, ScheduleOptions options) {
if (job instanceof Runnable) {
final Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
((Runnable) job).run();
}
}, 300);
return true;
}
return false;
}
@Override
public void removeJob(String name) throws NoSuchElementException {
// TODO Auto-generated method stub
}
@Override
public boolean fireJobAt(String name, Object job, Map<String, Serializable> config, Date date, int times, long period) {
// TODO Auto-generated method stub
return false;
}
@Override
public void fireJobAt(String name, Object job, Map<String, Serializable> config, Date date) throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean fireJob(Object job, Map<String, Serializable> config, int times, long period) {
// TODO Auto-generated method stub
return false;
}
@Override
public void fireJob(Object job, Map<String, Serializable> config) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void addPeriodicJob(String name, Object job, Map<String, Serializable> config, long period, boolean canRunConcurrently, boolean startImmediate) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void addPeriodicJob(String name, Object job, Map<String, Serializable> config, long period, boolean canRunConcurrently) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void addJob(String name, Object job, Map<String, Serializable> config, String schedulingExpression, boolean canRunConcurrently) throws Exception {
// TODO Auto-generated method stub
}
@Override
public ScheduleOptions NOW(int times, long period) {
// TODO Auto-generated method stub
return null;
}
@Override
public ScheduleOptions NOW() {
// TODO Auto-generated method stub
return null;
}
@Override
public ScheduleOptions EXPR(String expression) {
// TODO Auto-generated method stub
return null;
}
@Override
public ScheduleOptions AT(Date date, int times, long period) {
// TODO Auto-generated method stub
return null;
}
@Override
public ScheduleOptions AT(Date date) {
// TODO Auto-generated method stub
return null;
}
};
}
Aggregations