Skip to content
Snippets Groups Projects
Select Git revision
  • 897a1bec71af0d13434b88cac4196ce9138f5887
  • master default protected
  • release/202005
  • release/202001
  • release/201912
  • release/windows-test/201910
  • release/201908
  • release/201906
  • release/201905
  • release/201904
  • release/201903
  • release/201902
  • release/201901
  • release/201812
  • release/201811
  • release/201808
  • wip/patches_poly_2017/cedryk_doucet/abderahmane_bouziane
  • releases/beta1
  • android/release_463
  • android/release_462
  • android/release_461
  • android/release_460
  • android/release_459
  • android/release_458
  • android/release_457
  • android/release_456
  • android/release_455
  • android/release_454
  • android/release_453
  • android/release_452
  • android/release_451
  • android/release_450
  • android/release_449
  • android/release_448
  • android/release_447
  • android/release_446
  • android/release_445
  • android/release_444
38 results

DataTransfer.java

Blame
  • Andreas Traczyk's avatar
    Andreas Traczyk authored
    Change-Id: I3cc101982ab9bfaf36f7ba7a96f34dc80305f44f
    45903bac
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    DataTransfer.java 4.54 KiB
    /*
     *  Copyright (C) 2004-2019 Savoir-faire Linux Inc.
     *
     *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
     *          Rayan Osseiran <rayan.osseiran@savoirfairelinux.com>
     *
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 3 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program; if not, write to the Free Software
     *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     */
    package cx.ring.model;
    
    import java.util.Set;
    
    import cx.ring.utils.HashUtils;
    import cx.ring.utils.StringUtils;
    
    public class DataTransfer extends Interaction {
    
        private long mTotalSize;
        private long mBytesProgress;
        private String mPeerId;
        private String mExtension;
    
        private static final Set<String> IMAGE_EXTENSIONS = HashUtils.asSet("jpg", "jpeg", "png", "gif");
        private static final Set<String> AUDIO_EXTENSIONS = HashUtils.asSet("ogg", "mp3", "aac", "flac", "m4a");
        private static final Set<String> VIDEO_EXTENSIONS = HashUtils.asSet("webm", "mp4", "mkv");
        private static final int MAX_SIZE = 32 * 1024 * 1024;
        private static final int UNLIMITED_SIZE = 256 * 1024 * 1024;
    
        public DataTransfer(ConversationHistory conversation, String account, String displayName, boolean isOutgoing, long totalSize, long bytesProgress, long daemonId) {
            mAuthor = isOutgoing ? null : conversation.getParticipant();
            mAccount = account;
            mConversation = conversation;
            mPeerId = conversation.getParticipant();
            mTotalSize = totalSize;
            mBytesProgress = bytesProgress;
            mBody = displayName;
            mStatus = InteractionStatus.TRANSFER_CREATED.toString();
            mType = InteractionType.DATA_TRANSFER.toString();
            mTimestamp = System.currentTimeMillis();
            mIsRead = 1;
            mDaemonId = daemonId;
            mIsIncoming = mAuthor != null;
        }
    
    
        public DataTransfer(Interaction interaction) {
            mId = interaction.getId();
            mDaemonId = interaction.getDaemonId();
            mAuthor = interaction.getAuthor();
            mConversation = interaction.getConversation();
            mPeerId = interaction.getConversation().getParticipant();
            mBody = interaction.getBody();
            mStatus = interaction.getStatus().toString();
            mType = interaction.getType().toString();
            mTimestamp = interaction.getTimestamp();
            mAccount = interaction.getAccount();
            mContact = interaction.getContact();
            mIsRead = 1;
            mIsIncoming = mAuthor != null;
        }
    
        public String getExtension() {
            if (mExtension == null)
                mExtension = StringUtils.getFileExtension(getDisplayName()).toLowerCase();
            return mExtension;
        }
    
    
        public boolean isPicture() {
            return IMAGE_EXTENSIONS.contains(getExtension());
        }
        public boolean isAudio() {
            return AUDIO_EXTENSIONS.contains(getExtension());
        }
        public boolean isVideo() {
            return VIDEO_EXTENSIONS.contains(getExtension());
        }
    
        public boolean isComplete() {
            return isOutgoing() || InteractionStatus.TRANSFER_FINISHED.toString().equals(mStatus);
        }
        public boolean showPicture() {
            return isPicture() && isComplete();
        }
    
        public String getStoragePath() {
            String ext = StringUtils.getFileExtension(mBody);
            if (ext.length() > 8)
                ext = ext.substring(0, 8);
            return Long.toString(mId) + '_' + HashUtils.sha1(mBody) + '.' + ext;
        }
    
        public void setSize(long size) {
            mTotalSize = size;
        }
    
        public String getDisplayName() {
            return mBody;
        }
    
        public boolean isOutgoing() {
            return !mIsIncoming;
        }
    
        public long getTotalSize() {
            return mTotalSize;
        }
    
        public long getBytesProgress() {
            return mBytesProgress;
        }
    
        public void setBytesProgress(long bytesProgress) { mBytesProgress = bytesProgress;
        }
    
        public String getPeerId() {
            return mPeerId;
        }
    
    
        public boolean isError() {
            return getStatus().isError();
        }
    
        public boolean canAutoAccept(int maxSize) {
            return maxSize == UNLIMITED_SIZE || getTotalSize() <= maxSize;
        }
    
    
    }