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) 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 Cipher cipher = Cipher.getInstance(CIPHERMODE);
52 SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
53 IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
54 cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
55 return new Pair<>(new CipherInputStream(is, cipher), cipher.getOutputSize(size));
56 } catch (Exception e) {
57 throw new AssertionError(e);
58 }
59 }
60
61 public static OutputStream createAppendedOutputStream(DownloadableFile file) {
62 return createOutputStream(file, true);
63 }
64
65 public static OutputStream createOutputStream(DownloadableFile file) {
66 return createOutputStream(file, false);
67 }
68
69 private static OutputStream createOutputStream(DownloadableFile file, boolean append) {
70 FileOutputStream os;
71 try {
72 os = new FileOutputStream(file, append);
73 if (file.getKey() == null) {
74 return os;
75 }
76 } catch (FileNotFoundException e) {
77 return null;
78 }
79 try {
80 Cipher cipher = Cipher.getInstance(CIPHERMODE);
81 SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
82 IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
83 cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
84 return new CipherOutputStream(os, cipher);
85 } catch (Exception e) {
86 return null;
87 }
88 }
89
90 public XmppConnectionService getXmppConnectionService() {
91 return this.mXmppConnectionService;
92 }
93
94 public long getAutoAcceptFileSize() {
95 return this.mXmppConnectionService.getLongPreference("auto_accept_file_size", R.integer.auto_accept_filesize);
96 }
97
98 public boolean hasStoragePermission() {
99 return Compatibility.hasStoragePermission(mXmppConnectionService);
100 }
101
102 public void updateConversationUi(boolean force) {
103 synchronized (LAST_UI_UPDATE_CALL) {
104 if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
105 LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
106 mXmppConnectionService.updateConversationUi();
107 }
108 }
109 }
110
111 public PowerManager.WakeLock createWakeLock(String name) {
112 PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
113 return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
114 }
115}