public byte[] getResource(String url) { Log.d("PCL", "getResource('" + url + "')"); AndroidResource r = getDB().getResource(url); if (r == null) { r = registerResource(url, 0); } return r.getContent(); }
@Override public void run() { Log.d("PCL", "CacherRunnable"); AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); List<AndroidResource> resourceList = getDB().getNullResources(); if ((resourceList == null) || (resourceList.size() == 0)) { alarmMgr.cancel(runCacherPendingIntent); return; } int countFetched = 0; for (AndroidResource r : resourceList) { try { byte[] b = fetchURL(r.getUrl()); if (b != null) { r.setContent(b); if ((r.getFlags() & RESOURCE_FLAG_QUESTIONNAIRE) != 0) { parseQuestionaire( new ByteArrayInputStream(b), new UrlScanningHandler() { @Override public void registerResource(String url) { QuestionnaireManager.this.registerResource(url, 0); } }); } countFetched++; } } catch (Exception e) { e.printStackTrace(); } } List<AndroidResource> after = getDB().getNullResources(); if ((after == null) || (after.size() == 0)) { Log.d("PCL", "CacherRunnable cancelling"); alarmMgr.cancel(runCacherPendingIntent); return; } /* if (after.size() != resourceList.size() - countFetched) { // Stop cycling if it looks like something is wrong and we aren't making progress // ... to avoid cycling endlessly and draining battery alarmMgr.cancel(runCacherPendingIntent); return; } */ Log.d("PCL", "CacherRunnable rescheduling"); alarmMgr.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + QUESTIONNAIRE_CACHER_RETRY_DELAY, runCacherPendingIntent); }
public Questionnaire getQuestionaire(String url) { Log.d("PCL", "getQuestionaire('" + url + "')"); AndroidResource r = getDB().getResource(url); if (r == null) { r = registerResource(url, RESOURCE_FLAG_QUESTIONNAIRE); } if (r.getContent() == null) { Log.d("PCL", "Fetching content, not in cache"); byte[] content = fetchURL(url); if (content != null) { r.setContent(content); } } if (r.getContent() == null) return null; QuestionnaireHandler handler = new QuestionnaireHandler(); parseQuestionaire(new ByteArrayInputStream(r.getContent()), handler); return handler.getQuestionaire(); }
@Override public ImmutableList<Step> getBuildSteps( BuildContext context, BuildableContext buildableContext) { ImmutableList.Builder<Step> commands = ImmutableList.builder(); // Create temp folder to store the files going to be zipped commands.add(new MakeCleanDirectoryStep(getProjectFilesystem(), temp)); // Remove the output .aar file commands.add(new RmStep(getProjectFilesystem(), pathToOutputFile, /* force delete */ true)); // put manifest into tmp folder commands.add( CopyStep.forFile( getProjectFilesystem(), manifest.getPathToOutput(), temp.resolve("AndroidManifest.xml"))); // put R.txt into tmp folder commands.add( CopyStep.forFile( getProjectFilesystem(), Preconditions.checkNotNull(androidResource.getPathToOutput()), temp.resolve("R.txt"))); // put res/ and assets/ into tmp folder commands.add( CopyStep.forDirectory( getProjectFilesystem(), assembledResourceDirectory, temp.resolve("res"), CopyStep.DirectoryMode.CONTENTS_ONLY)); commands.add( CopyStep.forDirectory( getProjectFilesystem(), assembledAssetsDirectory, temp.resolve("assets"), CopyStep.DirectoryMode.CONTENTS_ONLY)); // Create our Uber-jar, and place it in the tmp folder. commands.add( new JarDirectoryStep( getProjectFilesystem(), temp.resolve("classes.jar"), ImmutableSet.copyOf(getTransitiveClasspathEntries().values()), null, null)); // move native libs into tmp folder under jni/ if (assembledNativeLibs.isPresent()) { commands.add( CopyStep.forDirectory( getProjectFilesystem(), assembledNativeLibs.get(), temp.resolve("jni"), CopyStep.DirectoryMode.CONTENTS_ONLY)); } // move native assets into tmp folder under assets/lib/ for (SourcePath dir : nativeLibAssetsDirectories) { CopyNativeLibraries.copyNativeLibrary( getProjectFilesystem(), getResolver().getPath(dir), temp.resolve("assets").resolve("lib"), ImmutableSet.<TargetCpuType>of(), commands); } // do the zipping commands.add( new ZipStep( getProjectFilesystem(), pathToOutputFile, ImmutableSet.<Path>of(), false, ZipStep.DEFAULT_COMPRESSION_LEVEL, temp)); return commands.build(); }