use of im.tny.segvault.subway.Network in project underlx by underlx.
the class HomeStatsFragment method redraw.
private void redraw(Context context) {
if (mListener == null || mListener.getMainService() == null) {
return;
}
StatsCache cache = mListener.getMainService().getStatsCache();
if (cache == null) {
return;
}
StatsCache.Stats stats = cache.getNetworkStats(networkId);
if (stats == null) {
return;
}
// Calculate days difference like the website
Calendar today = Calendar.getInstance();
today.setTime(new Date());
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
Calendar lastDisturbance = Calendar.getInstance();
lastDisturbance.setTime(stats.lastDisturbance);
lastDisturbance.set(Calendar.HOUR_OF_DAY, 0);
lastDisturbance.set(Calendar.MINUTE, 0);
lastDisturbance.set(Calendar.SECOND, 0);
lastDisturbance.set(Calendar.MILLISECOND, 0);
SharedPreferences sharedPref = context.getSharedPreferences("settings", MODE_PRIVATE);
boolean locationEnabled = sharedPref.getBoolean(PreferenceNames.LocationEnable, true);
if (locationEnabled) {
if (stats.curOnInTransit == 0) {
usersOnlineView.setText(R.string.frag_stats_few_users_in_network);
} else {
usersOnlineView.setText(String.format(getString(R.string.frag_stats_users_in_network), stats.curOnInTransit));
}
usersOnlineView.setVisibility(View.VISIBLE);
} else {
usersOnlineView.setVisibility(View.GONE);
}
long days = (today.getTime().getTime() - lastDisturbance.getTime().getTime()) / (24 * 60 * 60 * 1000);
if (days < 2) {
long hours = (new Date().getTime() - stats.lastDisturbance.getTime()) / (60 * 60 * 1000);
lastDisturbanceView.setHtml(String.format(getString(R.string.frag_stats_last_disturbance_hours), hours));
} else {
lastDisturbanceView.setHtml(String.format(getString(R.string.frag_stats_last_disturbance_days), days));
}
Network net = mListener.getMainService().getNetwork(networkId);
if (net == null) {
return;
}
List<Line> lines = new ArrayList<>(net.getLines());
Collections.sort(lines, new Comparator<Line>() {
@Override
public int compare(Line t0, Line t1) {
return Integer.valueOf(t0.getOrder()).compareTo(t1.getOrder());
}
});
lineStatsLayout.removeAllViews();
TableRow header = (TableRow) getActivity().getLayoutInflater().inflate(R.layout.line_stats_header, lineStatsLayout, false);
lineStatsLayout.addView(header);
for (Line line : lines) {
double weekAvail;
if (stats.weekLineStats.get(line.getId()) == null) {
continue;
} else {
weekAvail = stats.weekLineStats.get(line.getId()).availability;
}
double monthAvail;
if (stats.monthLineStats.get(line.getId()) == null) {
continue;
} else {
monthAvail = stats.monthLineStats.get(line.getId()).availability;
}
TableRow row = (TableRow) getActivity().getLayoutInflater().inflate(R.layout.line_stats_row, lineStatsLayout, false);
TextView lineNameView = (TextView) row.findViewById(R.id.line_name_view);
lineNameView.setText(Util.getLineNames(getContext(), line)[0]);
lineNameView.setTextColor(line.getColor());
((TextView) row.findViewById(R.id.week_availability_view)).setText(String.format("%.3f%%", weekAvail * 100));
((TextView) row.findViewById(R.id.month_availability_view)).setText(String.format("%.3f%%", monthAvail * 100));
lineStatsLayout.addView(row);
}
if (new Date().getTime() - stats.updated.getTime() > java.util.concurrent.TimeUnit.MINUTES.toMillis(5)) {
lineStatsLayout.setAlpha(0.6f);
lastDisturbanceView.setAlpha(0.6f);
usersOnlineView.setAlpha(0.6f);
updateInformationView.setTypeface(null, Typeface.BOLD);
} else {
lineStatsLayout.setAlpha(1f);
lastDisturbanceView.setAlpha(1f);
usersOnlineView.setAlpha(1f);
updateInformationView.setTypeface(null, Typeface.NORMAL);
}
updateInformationView.setText(String.format(getString(R.string.frag_stats_updated), DateUtils.getRelativeTimeSpanString(context, stats.updated.getTime(), true)));
}
use of im.tny.segvault.subway.Network in project underlx by underlx.
the class TripFragment method refreshUI.
private void refreshUI() {
Network network = this.network;
if (network == null) {
return;
}
Realm realm = Application.getDefaultRealmInstance(getContext());
Trip trip = realm.where(Trip.class).equalTo("id", tripId).findFirst();
if (trip == null) {
return;
}
Path path = trip.toConnectionPath(network);
Station origin = path.getStartVertex().getStation();
Station dest = path.getEndVertex().getStation();
SpannableStringBuilder builder = new SpannableStringBuilder();
if (path.getEdgeList().size() == 0) {
builder.append("#");
builder.setSpan(new ImageSpan(getActivity(), R.drawable.ic_beenhere_black_24dp), builder.length() - 1, builder.length(), 0);
builder.append(" " + origin.getName());
} else {
builder.append(origin.getName() + " ").append("#");
builder.setSpan(new ImageSpan(getActivity(), R.drawable.ic_arrow_forward_black_24dp), builder.length() - 1, builder.length(), 0);
builder.append(" " + dest.getName());
}
stationNamesView.setText(builder);
dateView.setText(DateUtils.formatDateTime(getContext(), path.getEntryTime(0).getTime(), DateUtils.FORMAT_SHOW_DATE));
int length = path.getTimeablePhysicalLength();
long time = path.getMovementMilliseconds();
if (length == 0 || time == 0) {
statsLayout.setVisibility(View.GONE);
} else {
statsView.setText(String.format(getString(R.string.frag_trip_stats), length, DateUtils.formatElapsedTime(time / 1000), (((double) length / (double) (time / 1000)) * 3.6)));
}
populatePathView(getContext(), inflater, path, layoutRoute, true);
if (!trip.canBeCorrected()) {
correctButton.setVisibility(View.GONE);
}
realm.close();
}
use of im.tny.segvault.subway.Network in project underlx by underlx.
the class MainService method cacheAllExtras.
public void cacheAllExtras(String... network_ids) {
ExtraContentCache.clearAllCachedExtras(this);
for (String id : network_ids) {
Network network = getNetwork(id);
ExtraContentCache.cacheAllExtras(this, new ExtraContentCache.OnCacheAllListener() {
@Override
public void onSuccess() {
Intent intent = new Intent(ACTION_CACHE_EXTRAS_FINISHED);
intent.putExtra(EXTRA_CACHE_EXTRAS_FINISHED, true);
LocalBroadcastManager bm = LocalBroadcastManager.getInstance(MainService.this);
bm.sendBroadcast(intent);
}
@Override
public void onProgress(int current, int total) {
Intent intent = new Intent(ACTION_CACHE_EXTRAS_PROGRESS);
intent.putExtra(EXTRA_CACHE_EXTRAS_PROGRESS_CURRENT, current);
intent.putExtra(EXTRA_CACHE_EXTRAS_PROGRESS_TOTAL, total);
LocalBroadcastManager bm = LocalBroadcastManager.getInstance(MainService.this);
bm.sendBroadcast(intent);
}
@Override
public void onFailure() {
Intent intent = new Intent(ACTION_CACHE_EXTRAS_FINISHED);
intent.putExtra(EXTRA_CACHE_EXTRAS_FINISHED, false);
LocalBroadcastManager bm = LocalBroadcastManager.getInstance(MainService.this);
bm.sendBroadcast(intent);
}
}, network);
}
}
use of im.tny.segvault.subway.Network in project underlx by underlx.
the class MainService method loadNetworks.
private void loadNetworks() {
synchronized (lock) {
try {
Network net = TopologyCache.loadNetwork(this, PRIMARY_NETWORK_ID, api.getEndpoint().toString());
putNetwork(net);
S2LS loc = locServices.get(PRIMARY_NETWORK_ID);
Log.d("loadNetworks", String.format("In network? %b", loc.inNetwork()));
Log.d("loadNetworks", String.format("Near network? %b", loc.nearNetwork()));
Zone z = loc.getLocation();
for (Stop s : z.vertexSet()) {
Log.d("loadNetworks", String.format("May be in station %s", s));
}
} catch (CacheException e) {
// cache invalid, attempt to reload topology
updateTopology();
}
}
}
use of im.tny.segvault.subway.Network in project underlx by underlx.
the class MainService method handleDisturbanceNotification.
private void handleDisturbanceNotification(String network, String line, String id, String status, boolean downtime, long msgtime) {
Log.d("MainService", "handleDisturbanceNotification");
SharedPreferences sharedPref = getSharedPreferences("notifsettings", MODE_PRIVATE);
Set<String> linePref = sharedPref.getStringSet(PreferenceNames.NotifsLines, null);
Network snetwork;
synchronized (lock) {
if (!networks.containsKey(network)) {
return;
}
snetwork = networks.get(network);
}
Line sline = snetwork.getLine(line);
if (sline == null) {
return;
}
if (downtime) {
lineStatusCache.markLineAsDown(sline, new Date(msgtime));
} else {
lineStatusCache.markLineAsUp(sline);
}
if (linePref != null && !linePref.contains(line)) {
// notifications disabled for this line
return;
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (!downtime && !sharedPref.getBoolean(PreferenceNames.NotifsServiceResumed, true)) {
// notifications for normal service resumed disabled
notificationManager.cancel(id.hashCode());
return;
}
Realm realm = Application.getDefaultRealmInstance(this);
for (NotificationRule rule : realm.where(NotificationRule.class).findAll()) {
if (rule.isEnabled() && rule.applies(new Date(msgtime))) {
realm.close();
return;
}
}
realm.close();
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_INITIAL_FRAGMENT, "nav_disturbances");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
String title = String.format(getString(R.string.notif_disturbance_title), Util.getLineNames(this, sline)[0]);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(status);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setStyle(bigTextStyle).setSmallIcon(R.drawable.ic_disturbance_notif).setColor(sline.getColor()).setContentTitle(title).setContentText(status).setAutoCancel(true).setWhen(msgtime).setSound(Uri.parse(sharedPref.getString(downtime ? PreferenceNames.NotifsRingtone : PreferenceNames.NotifsRegularizationRingtone, "content://settings/system/notification_sound"))).setVisibility(Notification.VISIBILITY_PUBLIC).setContentIntent(pendingIntent);
if (sharedPref.getBoolean(downtime ? PreferenceNames.NotifsVibrate : PreferenceNames.NotifsRegularizationVibrate, false)) {
notificationBuilder.setVibrate(new long[] { 0, 100, 100, 150, 150, 200 });
} else {
notificationBuilder.setVibrate(new long[] { 0l });
}
notificationManager.notify(id.hashCode(), notificationBuilder.build());
}
Aggregations