AbstractConnectionManager.java

  1package eu.siacs.conversations.services;
  2
  3import android.Manifest;
  4import android.content.Context;
  5import android.content.pm.PackageManager;
  6import android.os.Build;
  7import android.os.PowerManager;
  8import android.os.SystemClock;
  9import android.util.Pair;
 10
 11import java.io.FileInputStream;
 12import java.io.FileNotFoundException;
 13import java.io.FileOutputStream;
 14import java.io.InputStream;
 15import java.io.OutputStream;
 16import java.util.concurrent.atomic.AtomicLong;
 17
 18import javax.crypto.Cipher;
 19import javax.crypto.CipherInputStream;
 20import javax.crypto.CipherOutputStream;
 21import javax.crypto.spec.IvParameterSpec;
 22import javax.crypto.spec.SecretKeySpec;
 23
 24import eu.siacs.conversations.Config;
 25import eu.siacs.conversations.R;
 26import eu.siacs.conversations.entities.DownloadableFile;
 27import eu.siacs.conversations.utils.Compatibility;
 28
 29public class AbstractConnectionManager {
 30
 31    private static final String KEYTYPE = "AES";
 32    private static final String CIPHERMODE = "AES/GCM/NoPadding";
 33    private static final String PROVIDER = "BC";
 34    private static final int UI_REFRESH_THRESHOLD = 250;
 35    private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
 36    protected XmppConnectionService mXmppConnectionService;
 37
 38    public AbstractConnectionManager(XmppConnectionService service) {
 39        this.mXmppConnectionService = service;
 40    }
 41
 42    public static Pair<InputStream, Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException {
 43        FileInputStream is;
 44        int size;
 45        is = new FileInputStream(file);
 46        size = (int) file.getSize();
 47        if (file.getKey() == null) {
 48            return new Pair<>(is, size);
 49        }
 50        try {
 51            if (gcm) {
 52                Cipher cipher = Compatibility.twentyTwo() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER);
 53                SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
 54                IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
 55                cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
 56                return new Pair<>(new CipherInputStream(is, cipher), cipher.getOutputSize(size));
 57            } else {
 58                IvParameterSpec ips = new IvParameterSpec(file.getIv());
 59                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
 60                cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), KEYTYPE), ips);
 61                return new Pair<>(new CipherInputStream(is, cipher), (size / 16 + 1) * 16);
 62            }
 63        } catch (Exception e) {
 64            throw new AssertionError(e);
 65        }
 66    }
 67
 68    public static OutputStream createAppendedOutputStream(DownloadableFile file) {
 69        return createOutputStream(file, true);
 70    }
 71
 72    public static OutputStream createOutputStream(DownloadableFile file) {
 73        return createOutputStream(file, false);
 74    }
 75
 76    private static OutputStream createOutputStream(DownloadableFile file, boolean append) {
 77        FileOutputStream os;
 78        try {
 79            os = new FileOutputStream(file, append);
 80            if (file.getKey() == null) {
 81                return os;
 82            }
 83        } catch (FileNotFoundException e) {
 84            return null;
 85        }
 86        try {
 87            Cipher cipher = Cipher.getInstance(CIPHERMODE);
 88            SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
 89            IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
 90            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
 91            return new CipherOutputStream(os, cipher);
 92        } catch (Exception e) {
 93            return null;
 94        }
 95    }
 96
 97    public XmppConnectionService getXmppConnectionService() {
 98        return this.mXmppConnectionService;
 99    }
100
101    public long getAutoAcceptFileSize() {
102        return this.mXmppConnectionService.getLongPreference("auto_accept_file_size", R.integer.auto_accept_filesize);
103    }
104
105    public boolean hasStoragePermission() {
106        return Compatibility.hasStoragePermission(mXmppConnectionService);
107    }
108
109    public void updateConversationUi(boolean force) {
110        synchronized (LAST_UI_UPDATE_CALL) {
111            if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
112                LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
113                mXmppConnectionService.updateConversationUi();
114            }
115        }
116    }
117
118    public PowerManager.WakeLock createWakeLock(String name) {
119        PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
120        return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
121    }
122}