private void startTrackingService() { if (!DateTimes.isSameDay(mDate, Calendar.getInstance())) { mDate = Calendar.getInstance(); TextView txtDate = (TextView) findViewById(R.id.txtDate); txtDate.setText(DateTimes.dateFormat().format(mDate)); GPSHelper.loadTrackings(mDate); updateList(); } IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(PresenceManagerUtil.CHANGE_STATUS); getApplicationContext().registerReceiver(mIspReceiver, intentFilter); mPresenceManager.startPresence(); for (String p : mLocationManager.getAllProviders()) { LocationListener ll = new MyLocationListener(); mLocationManager.requestLocationUpdates( p, 60000, // 通知のための最小時間間隔(ミリ秒) 30, // 通知のための最小距離間隔(メートル) ll); locationListeners.add(ll); } buttonVisible(true); }
public static void setState(ActivityRecognitionResult result) { DetectedActivity da = result.getMostProbableActivity(); int state = da.getType(); log.debug( "DetectedActivity: " + DateTimes.timeFormat().format(result.getTime()) + " - " + getStateText(state) + ", " + da.getConfidence() + ", " + DateTimes.timeFormat().format(result.getElapsedRealtimeMillis())); if (Collections.isEmpty(states)) { states.put(state, 0L); lastState = state; lastStart = System.currentTimeMillis(); return; } Long ltime = states.get(lastState); if (ltime != null) { ltime += (System.currentTimeMillis() - lastStart); states.put(lastState, ltime); } lastState = state; lastStart = System.currentTimeMillis(); if (!states.containsKey(state)) { states.put(state, 0L); } }
public static String getTrackingFile(Calendar c) { String fn = GPSHelper.class.getPackage().getName() + "/gpstracking." + DateTimes.dateLogFormat().format(c) + ".txt"; return FileNames.concat(Environment.getExternalStorageDirectory().getAbsolutePath(), fn); }
public static void loadTrackings(Calendar c) { trackings.clear(); File file = new File(getTrackingFile(c)); if (!file.exists()) { log.warn(file + "does not exist"); return; } log.info("Loading " + file); float[] results = new float[1]; LineIterator li = null; try { li = Files.lineIterator(file); while (li.hasNext()) { String line = li.next(); if (Strings.isEmpty(line)) { continue; } TrackingData td = Jsons.fromJson(line, TrackingData.class); TrackingData ltd = getLastLocation(); if (ltd != null) { Location.distanceBetween( ltd.getLatitude(), ltd.getLongitude(), td.getLatitude(), td.getLongitude(), results); td.setDistance(results[0]); td.setSpeed(td.getDistance() / DateTimes.subSeconds(td.getDate(), ltd.getDate())); } trackings.add(td); } } catch (IOException e) { log.error(e); } finally { Streams.safeClose(li); } }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDate = Calendar.getInstance(); findViewById(R.id.btnStart).setOnClickListener(this); findViewById(R.id.btnStop).setOnClickListener(this); TextView txtDate = (TextView) findViewById(R.id.txtDate); txtDate.setText(DateTimes.dateFormat().format(mDate)); txtDate.setOnClickListener(this); buttonVisible(false); mHandler = new Handler(getMainLooper()); mGeocoder = new Geocoder(this, Locale.getDefault()); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // // Criteriaオブジェクトを生成 // Criteria criteria = new Criteria(); // criteria.setAccuracy(Criteria.ACCURACY_FINE); // criteria.setPowerRequirement(Criteria.POWER_LOW); // locationProvider = mLocationManager.getBestProvider(criteria, true); // log.debug("Location Provider: " + locationProvider); // setTitle(getTitle() + " - " + locationProvider); mPresenceManager = PresenceManager.getInstance(getApplicationContext()); MyAdapter adapter = new MyAdapter(this, GPSHelper.getTrackings()); ListView listView = (ListView) findViewById(R.id.listTracking); listView.setAdapter(adapter); listView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // ListView listView = (ListView)parent; // TrackingData item = (TrackingData)listView.getItemAtPosition(position); if (position % 2 == 0) { startActivity(new Intent(MainActivity.this, MapLineActivity.class)); } else { startActivity(new Intent(MainActivity.this, MapCircleActivity.class)); } } }); mHandler.postDelayed( new Runnable() { @Override public void run() { GPSHelper.loadTrackings(mDate); updateList(); } }, 100); }
public static boolean addLocation(Location location, Geocoder geocoder) { TrackingData ltd = getLastLocation(); float distance = 0.0f; float speed = 0.0f; if (ltd != null) { float[] results = new float[1]; Location.distanceBetween( ltd.getLatitude(), ltd.getLongitude(), location.getLatitude(), location.getLongitude(), results); distance = results[0]; if (distance < 100) { log.debug("SKIP SAME " + location + ": " + distance); return false; } long delta = DateTimes.subSeconds(new Date(location.getTime()), ltd.getDate()); // less than 30 minutes if (delta < 30 * 60) { speed = distance / delta; // if (lastState == DetectedActivity.STILL) { // // } // great than 120km/h if (speed >= 33) { log.debug("SKIP FAST " + location + ": " + distance); return false; } } } String address = ""; try { List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (Collections.isNotEmpty(addresses)) { Address a = addresses.get(0); address = toAddress(a); } // if (ltd != null && Strings.isNotEmpty(address)) { // if (ltd.getAddress().equals(address)) { // log.debug("Skip (" + location + "): " + address); // return; // } // } } catch (Exception e) { // Catch network or other I/O problems. log.error( "Failed to get address of (" + location.getLatitude() + ", " + location.getLongitude(), e); } TrackingData td = new TrackingData(); td.setDate(new Date(location.getTime())); td.setState(getBestState()); td.setLatitude(location.getLatitude()); td.setLongitude(location.getLongitude()); td.setAddress(address); if (ltd != null) { float[] results = new float[1]; Location.distanceBetween( ltd.getLatitude(), ltd.getLongitude(), td.getLatitude(), td.getLongitude(), results); td.setDistance(results[0]); td.setSpeed(td.getDistance() / DateTimes.subSeconds(td.getDate(), ltd.getDate())); } trackings.add(td); saveTrackingData(td); return true; }