2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
2015-12-06 17:23:59 +00:00
|
|
|
import android.Manifest;
|
2015-08-10 17:48:36 +00:00
|
|
|
import android.content.Context;
|
2015-12-06 17:23:59 +00:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.os.Build;
|
2015-08-10 17:48:36 +00:00
|
|
|
import android.os.PowerManager;
|
2017-01-12 15:02:09 +00:00
|
|
|
import android.os.SystemClock;
|
2015-07-31 23:19:16 +00:00
|
|
|
import android.util.Pair;
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2018-10-03 16:14:41 +00:00
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.InvalidKeyException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchProviderException;
|
2017-01-12 15:02:09 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2015-07-31 23:19:16 +00:00
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
import javax.crypto.CipherInputStream;
|
|
|
|
import javax.crypto.CipherOutputStream;
|
2018-10-03 16:14:41 +00:00
|
|
|
import javax.crypto.NoSuchPaddingException;
|
2015-07-31 23:19:16 +00:00
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
2017-06-30 19:22:35 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2015-07-31 23:19:16 +00:00
|
|
|
import eu.siacs.conversations.entities.DownloadableFile;
|
2018-09-17 15:47:51 +00:00
|
|
|
import eu.siacs.conversations.utils.Compatibility;
|
2015-07-31 23:19:16 +00:00
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public class AbstractConnectionManager {
|
2018-09-17 15:47:51 +00:00
|
|
|
|
|
|
|
private static final String KEYTYPE = "AES";
|
|
|
|
private static final String CIPHERMODE = "AES/GCM/NoPadding";
|
|
|
|
private static final String PROVIDER = "BC";
|
|
|
|
private static final int UI_REFRESH_THRESHOLD = 250;
|
|
|
|
private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
|
|
|
|
protected XmppConnectionService mXmppConnectionService;
|
|
|
|
|
|
|
|
public AbstractConnectionManager(XmppConnectionService service) {
|
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:14:41 +00:00
|
|
|
public static InputStream upgrade(DownloadableFile file, InputStream is) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, NoSuchProviderException {
|
|
|
|
if (file.getKey() != null && file.getIv() != null) {
|
|
|
|
Cipher cipher = Compatibility.twentyTwo() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER);
|
2018-10-03 10:50:54 +00:00
|
|
|
SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
|
|
|
|
IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
|
2018-10-03 16:14:41 +00:00
|
|
|
return new CipherInputStream(is, cipher);
|
|
|
|
} else {
|
|
|
|
return is;
|
2018-09-17 15:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:14:41 +00:00
|
|
|
|
2018-09-17 15:47:51 +00:00
|
|
|
public static OutputStream createAppendedOutputStream(DownloadableFile file) {
|
2018-09-30 11:48:11 +00:00
|
|
|
return createOutputStream(file, true);
|
2018-09-17 15:47:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 11:48:11 +00:00
|
|
|
public static OutputStream createOutputStream(DownloadableFile file) {
|
|
|
|
return createOutputStream(file, false);
|
2018-09-17 15:47:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 11:48:11 +00:00
|
|
|
private static OutputStream createOutputStream(DownloadableFile file, boolean append) {
|
2018-09-17 15:47:51 +00:00
|
|
|
FileOutputStream os;
|
|
|
|
try {
|
|
|
|
os = new FileOutputStream(file, append);
|
|
|
|
if (file.getKey() == null) {
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
2018-09-30 11:48:11 +00:00
|
|
|
Cipher cipher = Cipher.getInstance(CIPHERMODE);
|
|
|
|
SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
|
|
|
|
IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
|
|
|
|
return new CipherOutputStream(os, cipher);
|
2018-09-17 15:47:51 +00:00
|
|
|
} catch (Exception e) {
|
2018-09-30 11:48:11 +00:00
|
|
|
return null;
|
2018-09-17 15:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public XmppConnectionService getXmppConnectionService() {
|
|
|
|
return this.mXmppConnectionService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getAutoAcceptFileSize() {
|
|
|
|
return this.mXmppConnectionService.getLongPreference("auto_accept_file_size", R.integer.auto_accept_filesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasStoragePermission() {
|
2018-09-17 19:24:25 +00:00
|
|
|
return Compatibility.hasStoragePermission(mXmppConnectionService);
|
2018-09-17 15:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateConversationUi(boolean force) {
|
|
|
|
synchronized (LAST_UI_UPDATE_CALL) {
|
|
|
|
if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
|
|
|
|
LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public PowerManager.WakeLock createWakeLock(String name) {
|
|
|
|
PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
|
|
|
|
return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|