AbstractConnectionManager.java

  1package eu.siacs.conversations.services;
  2
  3import android.content.Context;
  4import android.os.PowerManager;
  5import android.os.SystemClock;
  6import android.util.Log;
  7
  8import java.io.FileNotFoundException;
  9import java.io.FileOutputStream;
 10import java.io.InputStream;
 11import java.io.OutputStream;
 12import java.security.InvalidAlgorithmParameterException;
 13import java.security.InvalidKeyException;
 14import java.security.NoSuchAlgorithmException;
 15import java.security.NoSuchProviderException;
 16import java.util.concurrent.atomic.AtomicLong;
 17
 18import javax.crypto.Cipher;
 19import javax.crypto.CipherInputStream;
 20import javax.crypto.CipherOutputStream;
 21import javax.crypto.NoSuchPaddingException;
 22import javax.crypto.spec.IvParameterSpec;
 23import javax.crypto.spec.SecretKeySpec;
 24
 25import eu.siacs.conversations.Config;
 26import eu.siacs.conversations.R;
 27import eu.siacs.conversations.entities.DownloadableFile;
 28import eu.siacs.conversations.utils.Compatibility;
 29
 30public class AbstractConnectionManager {
 31
 32    private static final String KEYTYPE = "AES";
 33    private static final String CIPHERMODE = "AES/GCM/NoPadding";
 34    private static final String PROVIDER = "BC";
 35    private static final int UI_REFRESH_THRESHOLD = 250;
 36    private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
 37    protected XmppConnectionService mXmppConnectionService;
 38
 39    public AbstractConnectionManager(XmppConnectionService service) {
 40        this.mXmppConnectionService = service;
 41    }
 42
 43    public static InputStream upgrade(DownloadableFile file, InputStream is) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, NoSuchProviderException {
 44        if (file.getKey() != null && file.getIv() != null) {
 45            final Cipher cipher = Compatibility.twentyEight() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER);
 46            SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
 47            IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
 48            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
 49            return new CipherInputStream(is, cipher);
 50        } else {
 51            return is;
 52        }
 53    }
 54
 55
 56    public static OutputStream createAppendedOutputStream(DownloadableFile file) {
 57        return createOutputStream(file, true);
 58    }
 59
 60    public static OutputStream createOutputStream(DownloadableFile file) {
 61        return createOutputStream(file, false);
 62    }
 63
 64    private static OutputStream createOutputStream(DownloadableFile file, boolean append) {
 65        FileOutputStream os;
 66        try {
 67            os = new FileOutputStream(file, append);
 68            if (file.getKey() == null) {
 69                return os;
 70            }
 71        } catch (FileNotFoundException e) {
 72            Log.d(Config.LOGTAG,"unable to create output stream", e);
 73            return null;
 74        }
 75        try {
 76            final Cipher cipher = Compatibility.twentyEight() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER);
 77            SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
 78            IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
 79            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
 80            return new CipherOutputStream(os, cipher);
 81        } catch (Exception e) {
 82            Log.d(Config.LOGTAG,"unable to create cipher output stream", e);
 83            return null;
 84        }
 85    }
 86
 87    public XmppConnectionService getXmppConnectionService() {
 88        return this.mXmppConnectionService;
 89    }
 90
 91    public long getAutoAcceptFileSize() {
 92        return this.mXmppConnectionService.getLongPreference("auto_accept_file_size", R.integer.auto_accept_filesize);
 93    }
 94
 95    public boolean hasStoragePermission() {
 96        return Compatibility.hasStoragePermission(mXmppConnectionService);
 97    }
 98
 99    public void updateConversationUi(boolean force) {
100        synchronized (LAST_UI_UPDATE_CALL) {
101            if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
102                LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
103                mXmppConnectionService.updateConversationUi();
104            }
105        }
106    }
107
108    public PowerManager.WakeLock createWakeLock(String name) {
109        PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
110        return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
111    }
112
113    public static class Extension {
114        public final String main;
115        public final String secondary;
116
117        private Extension(String main, String secondary) {
118            this.main = main;
119            this.secondary = secondary;
120        }
121
122        public static Extension of(String path) {
123            final int pos = path.lastIndexOf('/');
124            final String filename = path.substring(pos + 1).toLowerCase();
125            final String[] parts = filename.split("\\.");
126            final String main = parts.length >= 2 ? parts[parts.length - 1] : null;
127            final String secondary = parts.length >= 3 ? parts[parts.length - 2] : null;
128            return new Extension(main, secondary);
129        }
130    }
131}