Convert some SignalServiceAttachment* classes to kotlin.

This commit is contained in:
Greyson Parrelli
2024-08-02 16:45:06 -04:00
committed by mtang-signal
parent bb01c0501b
commit 8932eef991
40 changed files with 431 additions and 673 deletions

View File

@@ -1,194 +0,0 @@
/*
* Copyright 2023 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.signalservice.api.messages;
import org.whispersystems.signalservice.internal.push.http.CancelationSignal;
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nullable;
public abstract class SignalServiceAttachment {
private final String contentType;
protected SignalServiceAttachment(String contentType) {
this.contentType = contentType;
}
public String getContentType() {
return contentType;
}
public abstract boolean isStream();
public abstract boolean isPointer();
public SignalServiceAttachmentStream asStream() {
return (SignalServiceAttachmentStream)this;
}
public SignalServiceAttachmentPointer asPointer() {
return (SignalServiceAttachmentPointer)this;
}
public static Builder newStreamBuilder() {
return new Builder();
}
public static class Builder {
private InputStream inputStream;
private String contentType;
private String fileName;
private long length;
private ProgressListener listener;
private CancelationSignal cancelationSignal;
private boolean voiceNote;
private boolean borderless;
private boolean gif;
private boolean faststart;
private int width;
private int height;
private String caption;
private String blurHash;
private long uploadTimestamp;
private ResumableUploadSpec resumableUploadSpec;
private UUID uuid;
private Builder() {}
public Builder withStream(InputStream inputStream) {
this.inputStream = inputStream;
return this;
}
public Builder withContentType(String contentType) {
this.contentType = contentType;
return this;
}
public Builder withLength(long length) {
this.length = length;
return this;
}
public Builder withFileName(String fileName) {
this.fileName = fileName;
return this;
}
public Builder withListener(ProgressListener listener) {
this.listener = listener;
return this;
}
public Builder withCancelationSignal(CancelationSignal cancelationSignal) {
this.cancelationSignal = cancelationSignal;
return this;
}
public Builder withVoiceNote(boolean voiceNote) {
this.voiceNote = voiceNote;
return this;
}
public Builder withBorderless(boolean borderless) {
this.borderless = borderless;
return this;
}
public Builder withGif(boolean gif) {
this.gif = gif;
return this;
}
public Builder withFaststart(boolean faststart) {
this.faststart = faststart;
return this;
}
public Builder withWidth(int width) {
this.width = width;
return this;
}
public Builder withHeight(int height) {
this.height = height;
return this;
}
public Builder withCaption(String caption) {
this.caption = caption;
return this;
}
public Builder withBlurHash(String blurHash) {
this.blurHash = blurHash;
return this;
}
public Builder withUploadTimestamp(long uploadTimestamp) {
this.uploadTimestamp = uploadTimestamp;
return this;
}
public Builder withResumableUploadSpec(ResumableUploadSpec resumableUploadSpec) {
this.resumableUploadSpec = resumableUploadSpec;
return this;
}
public Builder withUuid(@Nullable UUID uuid) {
this.uuid = uuid;
return this;
}
public SignalServiceAttachmentStream build() {
if (inputStream == null) throw new IllegalArgumentException("Must specify stream!");
if (contentType == null) throw new IllegalArgumentException("No content type specified!");
if (length == 0) throw new IllegalArgumentException("No length specified!");
return new SignalServiceAttachmentStream(inputStream,
contentType,
length,
Optional.ofNullable(fileName),
voiceNote,
borderless,
gif,
faststart,
Optional.empty(),
width,
height,
uploadTimestamp,
Optional.ofNullable(caption),
Optional.ofNullable(blurHash),
listener,
cancelationSignal,
Optional.ofNullable(resumableUploadSpec),
uuid);
}
}
/**
* An interface to receive progress information on upload/download of
* an attachment.
*/
public interface ProgressListener {
/**
* Called on a progress change event.
*
* @param total The total amount to transmit/receive in bytes.
* @param progress The amount that has been transmitted/received in bytes thus far
*/
void onAttachmentProgress(long total, long progress);
boolean shouldCancel();
}
}

View File

@@ -0,0 +1,178 @@
/*
* Copyright 2023 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.signalservice.api.messages
import org.whispersystems.signalservice.internal.push.http.CancelationSignal
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec
import java.io.InputStream
import java.util.Optional
import java.util.UUID
abstract class SignalServiceAttachment protected constructor(val contentType: String?) {
abstract fun isStream(): Boolean
abstract fun isPointer(): Boolean
fun asStream(): SignalServiceAttachmentStream {
return this as SignalServiceAttachmentStream
}
fun asPointer(): SignalServiceAttachmentPointer {
return this as SignalServiceAttachmentPointer
}
class Builder {
private var inputStream: InputStream? = null
private var contentType: String? = null
private var fileName: String? = null
private var length: Long = 0
private var listener: ProgressListener? = null
private var cancelationSignal: CancelationSignal? = null
private var voiceNote = false
private var borderless = false
private var gif = false
private var faststart = false
private var width = 0
private var height = 0
private var caption: String? = null
private var blurHash: String? = null
private var uploadTimestamp: Long = 0
private var resumableUploadSpec: ResumableUploadSpec? = null
private var uuid: UUID? = null
fun withStream(inputStream: InputStream?): Builder {
this.inputStream = inputStream
return this
}
fun withContentType(contentType: String?): Builder {
this.contentType = contentType
return this
}
fun withLength(length: Long): Builder {
this.length = length
return this
}
fun withFileName(fileName: String?): Builder {
this.fileName = fileName
return this
}
fun withListener(listener: ProgressListener?): Builder {
this.listener = listener
return this
}
fun withCancelationSignal(cancelationSignal: CancelationSignal?): Builder {
this.cancelationSignal = cancelationSignal
return this
}
fun withVoiceNote(voiceNote: Boolean): Builder {
this.voiceNote = voiceNote
return this
}
fun withBorderless(borderless: Boolean): Builder {
this.borderless = borderless
return this
}
fun withGif(gif: Boolean): Builder {
this.gif = gif
return this
}
fun withFaststart(faststart: Boolean): Builder {
this.faststart = faststart
return this
}
fun withWidth(width: Int): Builder {
this.width = width
return this
}
fun withHeight(height: Int): Builder {
this.height = height
return this
}
fun withCaption(caption: String?): Builder {
this.caption = caption
return this
}
fun withBlurHash(blurHash: String?): Builder {
this.blurHash = blurHash
return this
}
fun withUploadTimestamp(uploadTimestamp: Long): Builder {
this.uploadTimestamp = uploadTimestamp
return this
}
fun withResumableUploadSpec(resumableUploadSpec: ResumableUploadSpec?): Builder {
this.resumableUploadSpec = resumableUploadSpec
return this
}
fun withUuid(uuid: UUID?): Builder {
this.uuid = uuid
return this
}
fun build(): SignalServiceAttachmentStream {
requireNotNull(inputStream) { "Must specify stream!" }
require(length != 0L) { "No length specified!" }
return SignalServiceAttachmentStream(
inputStream = inputStream!!,
contentType = contentType,
length = length,
fileName = Optional.ofNullable(fileName),
voiceNote = voiceNote,
isBorderless = borderless,
isGif = gif,
isFaststart = faststart,
preview = Optional.empty(),
width = width,
height = height,
uploadTimestamp = uploadTimestamp,
caption = Optional.ofNullable(caption),
blurHash = Optional.ofNullable(blurHash),
listener = listener,
cancelationSignal = cancelationSignal,
resumableUploadSpec = Optional.ofNullable(resumableUploadSpec),
uuid = uuid
)
}
}
/**
* An interface to receive progress information on upload/download of
* an attachment.
*/
interface ProgressListener {
/**
* Called on a progress change event.
*
* @param total The total amount to transmit/receive in bytes.
* @param progress The amount that has been transmitted/received in bytes thus far
*/
fun onAttachmentProgress(total: Long, progress: Long)
fun shouldCancel(): Boolean
}
companion object {
@JvmStatic
fun newStreamBuilder(): Builder {
return Builder()
}
}
}

View File

@@ -1,166 +0,0 @@
/*
* Copyright (C) 2014-2017 Open Whisper Systems
*
* Licensed according to the LICENSE file in this repository.
*/
package org.whispersystems.signalservice.api.messages;
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nullable;
/**
* Represents a received SignalServiceAttachment "handle." This
* is a pointer to the actual attachment content, which needs to be
* retrieved using {@link SignalServiceMessageReceiver#retrieveAttachment(SignalServiceAttachmentPointer, java.io.File, long)}
*
* @author Moxie Marlinspike
*/
public class SignalServiceAttachmentPointer extends SignalServiceAttachment {
private final int cdnNumber;
private final SignalServiceAttachmentRemoteId remoteId;
private final byte[] key;
private final Optional<Integer> size;
private final Optional<byte[]> preview;
private final Optional<byte[]> digest;
private final Optional<byte[]> incrementalDigest;
private final int incrementalMacChunkSize;
private final Optional<String> fileName;
private final boolean voiceNote;
private final boolean borderless;
private final boolean gif;
private final int width;
private final int height;
private final Optional<String> caption;
private final Optional<String> blurHash;
private final long uploadTimestamp;
private final UUID uuid;
public SignalServiceAttachmentPointer(int cdnNumber,
SignalServiceAttachmentRemoteId remoteId,
String contentType,
byte[] key,
Optional<Integer> size,
Optional<byte[]> preview,
int width,
int height,
Optional<byte[]> digest,
Optional<byte[]> incrementalDigest,
int incrementalMacChunkSize,
Optional<String> fileName,
boolean voiceNote,
boolean borderless,
boolean gif,
Optional<String> caption,
Optional<String> blurHash,
long uploadTimestamp,
@Nullable UUID uuid)
{
super(contentType);
this.cdnNumber = cdnNumber;
this.remoteId = remoteId;
this.key = key;
this.size = size;
this.preview = preview;
this.width = width;
this.height = height;
this.incrementalMacChunkSize = incrementalMacChunkSize;
this.digest = digest;
this.incrementalDigest = incrementalDigest;
this.fileName = fileName;
this.voiceNote = voiceNote;
this.borderless = borderless;
this.caption = caption;
this.blurHash = blurHash;
this.uploadTimestamp = uploadTimestamp;
this.gif = gif;
this.uuid = uuid;
}
public int getCdnNumber() {
return cdnNumber;
}
public SignalServiceAttachmentRemoteId getRemoteId() {
return remoteId;
}
public byte[] getKey() {
return key;
}
@Override
public boolean isStream() {
return false;
}
@Override
public boolean isPointer() {
return true;
}
public Optional<Integer> getSize() {
return size;
}
public Optional<String> getFileName() {
return fileName;
}
public Optional<byte[]> getPreview() {
return preview;
}
public Optional<byte[]> getDigest() {
return digest;
}
public Optional<byte[]> getIncrementalDigest() {
return incrementalDigest;
}
public boolean getVoiceNote() {
return voiceNote;
}
public boolean isBorderless() {
return borderless;
}
public boolean isGif() {
return gif;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getIncrementalMacChunkSize() {
return incrementalMacChunkSize;
}
public Optional<String> getCaption() {
return caption;
}
public Optional<String> getBlurHash() {
return blurHash;
}
public long getUploadTimestamp() {
return uploadTimestamp;
}
public @Nullable UUID getUuid() {
return uuid;
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (C) 2014-2017 Open Whisper Systems
*
* Licensed according to the LICENSE file in this repository.
*/
package org.whispersystems.signalservice.api.messages
import java.util.Optional
import java.util.UUID
/**
* Represents a received SignalServiceAttachment "handle." This
* is a pointer to the actual attachment content, which needs to be
* retrieved using [SignalServiceMessageReceiver.retrieveAttachment]
*
* @author Moxie Marlinspike
*/
class SignalServiceAttachmentPointer(
val cdnNumber: Int,
val remoteId: SignalServiceAttachmentRemoteId,
contentType: String?,
val key: ByteArray?,
val size: Optional<Int>,
val preview: Optional<ByteArray>,
val width: Int,
val height: Int,
val digest: Optional<ByteArray>,
val incrementalDigest: Optional<ByteArray>,
val incrementalMacChunkSize: Int,
val fileName: Optional<String>,
val voiceNote: Boolean,
val isBorderless: Boolean,
val isGif: Boolean,
val caption: Optional<String>,
val blurHash: Optional<String>,
val uploadTimestamp: Long,
val uuid: UUID?
) : SignalServiceAttachment(contentType) {
override fun isStream() = false
override fun isPointer() = true
}

View File

@@ -1,179 +0,0 @@
/**
* Copyright (C) 2014-2016 Open Whisper Systems
*
* Licensed according to the LICENSE file in this repository.
*/
package org.whispersystems.signalservice.api.messages;
import org.whispersystems.signalservice.internal.push.http.CancelationSignal;
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nullable;
/**
* Represents a local SignalServiceAttachment to be sent.
*/
public class SignalServiceAttachmentStream extends SignalServiceAttachment implements Closeable {
private final InputStream inputStream;
private final long length;
private final Optional<String> fileName;
private final ProgressListener listener;
private final CancelationSignal cancelationSignal;
private final Optional<byte[]> preview;
private final boolean voiceNote;
private final boolean borderless;
private final boolean gif;
private final boolean faststart;
private final int width;
private final int height;
private final long uploadTimestamp;
private final Optional<String> caption;
private final Optional<String> blurHash;
private final Optional<ResumableUploadSpec> resumableUploadSpec;
private final UUID uuid;
public SignalServiceAttachmentStream(InputStream inputStream,
String contentType,
long length,
Optional<String> fileName,
boolean voiceNote,
boolean borderless,
boolean gif,
boolean faststart,
ProgressListener listener,
CancelationSignal cancelationSignal)
{
this(inputStream, contentType, length, fileName, voiceNote, borderless, gif, faststart, Optional.empty(), 0, 0, System.currentTimeMillis(), Optional.empty(), Optional.empty(), listener, cancelationSignal, Optional.empty(), UUID.randomUUID());
}
public SignalServiceAttachmentStream(InputStream inputStream,
String contentType,
long length,
Optional<String> fileName,
boolean voiceNote,
boolean borderless,
boolean gif,
boolean faststart,
Optional<byte[]> preview,
int width,
int height,
long uploadTimestamp,
Optional<String> caption,
Optional<String> blurHash,
ProgressListener listener,
CancelationSignal cancelationSignal,
Optional<ResumableUploadSpec> resumableUploadSpec,
@Nullable UUID uuid)
{
super(contentType);
this.inputStream = inputStream;
this.length = length;
this.fileName = fileName;
this.listener = listener;
this.voiceNote = voiceNote;
this.borderless = borderless;
this.gif = gif;
this.preview = preview;
this.faststart = faststart;
this.width = width;
this.height = height;
this.uploadTimestamp = uploadTimestamp;
this.caption = caption;
this.blurHash = blurHash;
this.cancelationSignal = cancelationSignal;
this.resumableUploadSpec = resumableUploadSpec;
this.uuid = uuid;
}
@Override
public boolean isStream() {
return true;
}
@Override
public boolean isPointer() {
return false;
}
public InputStream getInputStream() {
return inputStream;
}
public long getLength() {
return length;
}
public Optional<String> getFileName() {
return fileName;
}
public ProgressListener getListener() {
return listener;
}
public CancelationSignal getCancelationSignal() {
return cancelationSignal;
}
public Optional<byte[]> getPreview() {
return preview;
}
public boolean getVoiceNote() {
return voiceNote;
}
public boolean isBorderless() {
return borderless;
}
public boolean isGif() {
return gif;
}
public boolean isFaststart() {
return faststart;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Optional<String> getCaption() {
return caption;
}
public Optional<String> getBlurHash() {
return blurHash;
}
public long getUploadTimestamp() {
return uploadTimestamp;
}
public Optional<ResumableUploadSpec> getResumableUploadSpec() {
return resumableUploadSpec;
}
public @Nullable UUID getUuid() {
return uuid;
}
@Override
public void close() throws IOException {
inputStream.close();
}
}

View File

@@ -0,0 +1,78 @@
/**
* Copyright (C) 2014-2016 Open Whisper Systems
*
* Licensed according to the LICENSE file in this repository.
*/
package org.whispersystems.signalservice.api.messages
import org.whispersystems.signalservice.internal.push.http.CancelationSignal
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec
import java.io.Closeable
import java.io.IOException
import java.io.InputStream
import java.util.Optional
import java.util.UUID
/**
* Represents a local SignalServiceAttachment to be sent.
*/
class SignalServiceAttachmentStream(
val inputStream: InputStream,
contentType: String?,
val length: Long,
val fileName: Optional<String>,
val voiceNote: Boolean,
val isBorderless: Boolean,
val isGif: Boolean,
val isFaststart: Boolean,
val preview: Optional<ByteArray>,
val width: Int,
val height: Int,
val uploadTimestamp: Long,
val caption: Optional<String>,
val blurHash: Optional<String>,
val listener: ProgressListener?,
val cancelationSignal: CancelationSignal?,
val resumableUploadSpec: Optional<ResumableUploadSpec>,
val uuid: UUID?
) : SignalServiceAttachment(contentType!!), Closeable {
constructor(
inputStream: InputStream,
contentType: String?,
length: Long,
fileName: Optional<String>,
voiceNote: Boolean,
borderless: Boolean,
gif: Boolean,
faststart: Boolean,
listener: ProgressListener?,
cancelationSignal: CancelationSignal?
) : this(
inputStream = inputStream,
contentType = contentType,
length = length,
fileName = fileName,
voiceNote = voiceNote,
isBorderless = borderless,
isGif = gif,
isFaststart = faststart,
preview = Optional.empty<ByteArray>(),
width = 0,
height = 0,
uploadTimestamp = System.currentTimeMillis(),
caption = Optional.empty<String>(),
blurHash = Optional.empty<String>(),
listener = listener,
cancelationSignal = cancelationSignal,
resumableUploadSpec = Optional.empty<ResumableUploadSpec>(),
uuid = UUID.randomUUID()
)
override fun isStream() = true
override fun isPointer() = false
@Throws(IOException::class)
override fun close() {
inputStream.close()
}
}