I am trying to fire the DownloadListener event in Android WebView, when I click the download button on the JSF page, these links load dynamically. It works fine in Google Chrome, but it doesn't work in WebView, and I couldn't figure it out.
I just need this event to fire when a button is clicked. Can you help me?
Download:
public void download(Download obj) throws IOException {
byte[] download = ....
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context
.getExternalContext().getResponse();
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("application/octet-stream");
response.setHeader("Content-Length", String.valueOf(download.length));
response.setHeader("Content-Disposition", "attachment;filename=\"download\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new ByteArrayInputStream(download),
DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(),
DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
input.close();
output.close();
}
context.responseComplete();
}
Data loading:
<p:dataTable value="#{downloadBean.downloads}" var="obj"
emptyMessage="#{erros.sem_registros}">
<p:column headerText="#{msgs.acoes}" styleClass="width50">
<h:form>
<h:commandLink value="#{msgs.download}" action="#{downloadBean.download(obj)}" />
</h:form>
</p:column>
</p:dataTable>
WebView Event:
viewer.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
try {
Toast.makeText(MainActivity.this, "Download ativo", Toast.LENGTH_SHORT).show();
String retorno = new DownloadUtil().getNew(url);
if (retorno != null) {
findBT();
openBT();
sendData(retorno);
closeBT();
} else {
Toast.makeText(MainActivity.this, "Não foi possível receber o arquivo do servidor", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Conexão mal sucedida", Toast.LENGTH_SHORT).show();
}
}
});
source
share