@SuppressLint("SimpleDateFormat") @Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { StudyRoom studyRoom = StudyRoomGroupManager.getStudyRoomFromCursor(cursor); if (view.getId() == android.R.id.text1) { TextView tv = (TextView) view; tv.setText(studyRoom.name); } else if (view.getId() == android.R.id.text2) { StringBuilder stringBuilder = new StringBuilder(studyRoom.location).append("<br>"); if (studyRoom.occupiedTill.compareTo(new Date()) < 0) { stringBuilder.append(getString(R.string.free)); } else { stringBuilder .append(getString(R.string.occupied)) .append(" <b>") .append( new SimpleDateFormat("HH:mm", Locale.getDefault()).format(studyRoom.occupiedTill)) .append("</b>"); } TextView tv = (TextView) view; tv.setText(Utils.fromHtml(stringBuilder.toString())); } else if (view.getId() == R.id.text3) { TextView tv = (TextView) view; tv.setText(studyRoom.code); } return true; }
@Override public void onCreate() { super.onCreate(); Utils.log("DownloadService service has started"); broadcastManager = LocalBroadcastManager.getInstance(this); // Init sync table new SyncManager(this); }
private boolean downloadNews(boolean force) { try { NewsManager nm = new NewsManager(this); nm.downloadFromExternal(force); return true; } catch (JSONException e) { Utils.log(e); return false; } }
private boolean downLoadKino(boolean force) { try { KinoManager km = new KinoManager(this); km.downloadFromExternal(force); return true; } catch (JSONException e) { Utils.log(e); return false; } }
private boolean downloadCafeterias(boolean force) { try { CafeteriaManager cm = new CafeteriaManager(this); CafeteriaMenuManager cmm = new CafeteriaMenuManager(this); cm.downloadFromExternal(force); cmm.downloadFromExternal(this, force); return true; } catch (JSONException e) { Utils.log(e); return false; } }
/** change presentation of locations in the list */ @Override public boolean setViewValue(View view, Cursor c, int index) { if (view.getId() == android.R.id.text2) { String transport = c.getString(c.getColumnIndex(Const.TRANSPORT_COLUMN)); String address = c.getString(c.getColumnIndex(Const.ADDRESS_COLUMN)); String hours = c.getString(c.getColumnIndex(Const.HOURS_COLUMN)); String remark = c.getString(c.getColumnIndex(Const.REMARK_COLUMN)); String room = c.getString(c.getColumnIndex(Const.ROOM_COLUMN)); StringBuilder sb = new StringBuilder(hours).append('\n').append(address); if (!room.isEmpty()) { sb.append(", ").append(room); } if (!transport.isEmpty()) { sb.append(" (").append(transport).append(')'); } if (!remark.isEmpty()) { sb.append('\n').append(COMPILE.matcher(remark).replaceAll("\n")); } TextView tv = (TextView) view; tv.setText(sb.toString()); // link email addresses and phone numbers (e.g. 089-123456) Linkify.addLinks(tv, Linkify.EMAIL_ADDRESSES); Linkify.addLinks(tv, Pattern.compile("[0-9-]{6,}"), "tel:"); return true; } else if (view.getId() == R.id.text3) { StringBuilder url = new StringBuilder(c.getString(c.getColumnIndex(Const.URL_COLUMN))); TextView tv = (TextView) view; if (url.toString().isEmpty()) { tv.setVisibility(View.GONE); } else { url.insert(0, "<a href=\"") .append("\">") .append(getString(R.string.website)) .append("</a>"); tv.setMovementMethod(LinkMovementMethod.getInstance()); tv.setText(Utils.fromHtml(url.toString())); } return true; } return false; }
/** Import default location and opening hours from assets */ private void importLocationsDefaults() throws IOException { OpenHoursManager lm = new OpenHoursManager(this); if (lm.empty()) { List<String[]> rows = Utils.readCsv(getAssets().open(CSV_LOCATIONS)); for (String[] row : rows) { lm.replaceIntoDb( new Location( Integer.parseInt(row[0]), row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8])); } } }
/** * Download the data for a specific intent note, that only one concurrent download() is possible * with a static synchronized method! */ private static synchronized void download(Intent intent, DownloadService service) { // Set the app version if not set PackageInfo pi = Util.getPackageInfo(service); if (pi != null) { G.appVersion = pi.versionName; // Version G.appPackage = pi.packageName; // Package name G.appVersionCode = pi.versionCode; // Version code e.g.: 45 } String action = intent.getStringExtra(Const.ACTION_EXTRA); // No action: leave service if (action == null) { return; } boolean successful = true; boolean force = intent.getBooleanExtra(Const.FORCE_DOWNLOAD, false); boolean launch = intent.getBooleanExtra(Const.APP_LAUNCHES, false); // Check if device has a internet connection boolean backgroundServicePermitted = Utils.isBackgroundServicePermitted(service); if (NetUtils.isConnected(service) && (launch || backgroundServicePermitted)) { Utils.logv("Handle action <" + action + ">"); switch (action) { case Const.NEWS: successful = service.downloadNews(force); break; case Const.FACULTIES: successful = service.downloadFacultiesAndSurveyData(); break; case Const.CAFETERIAS: successful = service.downloadCafeterias(force); break; case Const.KINO: successful = service.downLoadKino(force); break; case Const.DOWNLOAD_ALL_FROM_EXTERNAL: default: successful = service.downloadAll(force); boolean isSetup = Utils.getInternalSettingBool(service, Const.EVERYTHING_SETUP, false); if (isSetup) { break; } CacheManager cm = new CacheManager(service); cm.syncCalendar(); if (successful) { Utils.setInternalSetting(service, Const.EVERYTHING_SETUP, true); } break; } } // Update the last run time saved in shared prefs if (action.equals(Const.DOWNLOAD_ALL_FROM_EXTERNAL)) { try { service.importLocationsDefaults(); } catch (IOException e) { Utils.log(e); successful = false; } if (successful) { SharedPreferences prefs = service.getSharedPreferences(Const.INTERNAL_PREFS, 0); prefs.edit().putLong(LAST_UPDATE, System.currentTimeMillis()).apply(); } CardManager.update(service); successful = true; } // After done the job, create an broadcast intent and send it. The receivers will be informed // that the download service has finished. Utils.logv("Downloadservice was " + (successful ? "" : "not ") + "successful"); if (successful) { service.broadcastDownloadCompleted(); } else { service.broadcastError(service.getResources().getString(R.string.exception_unknown)); } // Do all other import stuff that is not relevant for creating the viewing the start page if (action.equals(Const.DOWNLOAD_ALL_FROM_EXTERNAL)) { service.startService(new Intent(service, FillCacheService.class)); } }
@Override public void onDestroy() { super.onDestroy(); Utils.log("DownloadService service has stopped"); }