@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; } }
/** * 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"); }