diff options
author | Christian Lohmaier <lohmaier+LibreOffice@googlemail.com> | 2020-11-12 14:02:43 +0100 |
---|---|---|
committer | Christian Lohmaier <lohmaier+LibreOffice@googlemail.com> | 2020-11-12 14:02:43 +0100 |
commit | 0430a85322d819a315c2a8673f7672a9a7aa600a (patch) | |
tree | 5e407ded48fd2d265cff69c808e771b3d1b57efe | |
parent | 673e8591d89bcb20717b9b5f55b9759336d68d6b (diff) |
fix potential memleak w/ non-static AsyncTask
and don't shoot ourselves in the foot (crash) by throwing a
RuntimeException when the slide commands fail for some reason
Also bother to log the actual error instead of a generic "something
failed"
-rw-r--r-- | android/sdremote/mobile/src/main/java/org/libreoffice/impressremote/communication/CommandsTransmitter.java | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/android/sdremote/mobile/src/main/java/org/libreoffice/impressremote/communication/CommandsTransmitter.java b/android/sdremote/mobile/src/main/java/org/libreoffice/impressremote/communication/CommandsTransmitter.java index 6008e3e..e7fe8dd 100644 --- a/android/sdremote/mobile/src/main/java/org/libreoffice/impressremote/communication/CommandsTransmitter.java +++ b/android/sdremote/mobile/src/main/java/org/libreoffice/impressremote/communication/CommandsTransmitter.java @@ -9,6 +9,7 @@ package org.libreoffice.impressremote.communication; import android.os.AsyncTask; +import android.util.Log; import java.io.BufferedWriter; import java.io.IOException; @@ -30,7 +31,7 @@ public class CommandsTransmitter { return new BufferedWriter( new OutputStreamWriter(aCommandsStream, Protocol.CHARSET)); } catch (UnsupportedEncodingException e) { - throw new RuntimeException("Unable to create commands writer."); + throw new RuntimeException("Unable to create commands writer:" + e.getMessage()); } } @@ -43,19 +44,7 @@ public class CommandsTransmitter { private void writeCommand(final String aCommand) { // TODO: We should ensure that all communication happens on one Thread. By default AsyncTask // executes on a thread pool (at least on modern devices). See tdf#111398 - (new AsyncTask<Void, Void, Void>() { - @Override - protected Void doInBackground(Void... params) { - try { - mCommandsWriter.write(aCommand); - mCommandsWriter.flush(); - } catch (IOException e) { - throw new RuntimeException("Unable to write command."); - } - - return null; - } - }).execute(); + new WriteCommandTask(mCommandsWriter).execute(aCommand); } public void performNextTransition() { @@ -110,6 +99,26 @@ public class CommandsTransmitter { writeCommand(Protocol.Commands .prepareCommand(Protocol.Commands.POINTER_DISMISSED)); } + + // non-static AsyncTask can leak memory + private static class WriteCommandTask extends AsyncTask<String, Void, Void> { + private final BufferedWriter mCommandsWriter; + + public WriteCommandTask(BufferedWriter writer) { + mCommandsWriter = writer; + } + + @Override + protected Void doInBackground(String... commands) { + try { + mCommandsWriter.write(commands[0]); + mCommandsWriter.flush(); + } catch (IOException e) { + Log.e("CommandsTransmitter", "Unable to write command:" + e.getMessage()); + } + return null; + } + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |