1package eu.siacs.conversations.services;
2
3import android.content.Context;
4import android.os.PowerManager;
5import android.os.SystemClock;
6import android.util.Log;
7
8import org.bouncycastle.crypto.engines.AESEngine;
9import org.bouncycastle.crypto.io.CipherInputStream;
10import org.bouncycastle.crypto.io.CipherOutputStream;
11import org.bouncycastle.crypto.modes.AEADBlockCipher;
12import org.bouncycastle.crypto.modes.GCMBlockCipher;
13import org.bouncycastle.crypto.params.AEADParameters;
14import org.bouncycastle.crypto.params.KeyParameter;
15
16import java.io.FileInputStream;
17import java.io.FileNotFoundException;
18import java.io.FileOutputStream;
19import java.io.IOException;
20import java.io.InputStream;
21import java.io.OutputStream;
22import java.util.concurrent.atomic.AtomicLong;
23
24import javax.annotation.Nullable;
25
26import eu.siacs.conversations.Config;
27import eu.siacs.conversations.R;
28import eu.siacs.conversations.entities.DownloadableFile;
29import eu.siacs.conversations.utils.Compatibility;
30import okhttp3.MediaType;
31import okhttp3.RequestBody;
32import okio.BufferedSink;
33import okio.Okio;
34import okio.Source;
35
36import static eu.siacs.conversations.entities.Transferable.VALID_CRYPTO_EXTENSIONS;
37
38public class AbstractConnectionManager {
39
40 private static final int UI_REFRESH_THRESHOLD = 250;
41 private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
42 protected XmppConnectionService mXmppConnectionService;
43
44 public AbstractConnectionManager(XmppConnectionService service) {
45 this.mXmppConnectionService = service;
46 }
47
48 public static InputStream upgrade(DownloadableFile file, InputStream is) {
49 if (file.getKey() != null && file.getIv() != null) {
50 AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
51 cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
52 return new CipherInputStream(is, cipher);
53 } else {
54 return is;
55 }
56 }
57
58
59 //For progress tracking see:
60 //https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/Progress.java
61
62 public static RequestBody requestBody(final DownloadableFile file, final ProgressListener progressListener) {
63 return new RequestBody() {
64
65 @Override
66 public long contentLength() {
67 return file.getSize() + (file.getKey() != null ? 16 : 0);
68 }
69
70 @Nullable
71 @Override
72 public MediaType contentType() {
73 return MediaType.parse(file.getMimeType());
74 }
75
76 @Override
77 public void writeTo(final BufferedSink sink) throws IOException {
78 long transmitted = 0;
79 try (final Source source = Okio.source(upgrade(file, new FileInputStream(file)))) {
80 long read;
81 while ((read = source.read(sink.buffer(), 8196)) != -1) {
82 transmitted += read;
83 sink.flush();
84 progressListener.onProgress(transmitted);
85 }
86 }
87 }
88 };
89 }
90
91 public interface ProgressListener {
92 void onProgress(long progress);
93 }
94
95 public static OutputStream createOutputStream(DownloadableFile file, boolean append, boolean decrypt) {
96 FileOutputStream os;
97 try {
98 os = new FileOutputStream(file, append);
99 if (file.getKey() == null || !decrypt) {
100 return os;
101 }
102 } catch (FileNotFoundException e) {
103 Log.d(Config.LOGTAG, "unable to create output stream", e);
104 return null;
105 }
106 try {
107 AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
108 cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
109 return new CipherOutputStream(os, cipher);
110 } catch (Exception e) {
111 Log.d(Config.LOGTAG, "unable to create cipher output stream", e);
112 return null;
113 }
114 }
115
116 public XmppConnectionService getXmppConnectionService() {
117 return this.mXmppConnectionService;
118 }
119
120 public long getAutoAcceptFileSize() {
121 return this.mXmppConnectionService.getLongPreference("auto_accept_file_size", R.integer.auto_accept_filesize);
122 }
123
124 public boolean hasStoragePermission() {
125 return Compatibility.hasStoragePermission(mXmppConnectionService);
126 }
127
128 public void updateConversationUi(boolean force) {
129 synchronized (LAST_UI_UPDATE_CALL) {
130 if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
131 LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
132 mXmppConnectionService.updateConversationUi();
133 }
134 }
135 }
136
137 public PowerManager.WakeLock createWakeLock(final Thread thread) {
138 return createWakeLock("conversations:" + thread.getName());
139 }
140
141 public PowerManager.WakeLock createWakeLock(final String name) {
142 final PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
143 return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
144 }
145
146 public static class Extension {
147 public final String main;
148 public final String secondary;
149
150 private Extension(String main, String secondary) {
151 this.main = main;
152 this.secondary = secondary;
153 }
154
155 public String getExtension() {
156 if (VALID_CRYPTO_EXTENSIONS.contains(main)) {
157 return secondary;
158 } else {
159 return main;
160 }
161 }
162
163 public static Extension of(String path) {
164 //TODO accept List<String> pathSegments
165 final int pos = path.lastIndexOf('/');
166 final String filename = path.substring(pos + 1).toLowerCase();
167 final String[] parts = filename.split("\\.");
168 final String main = parts.length >= 2 ? parts[parts.length - 1] : null;
169 final String secondary = parts.length >= 3 ? parts[parts.length - 2] : null;
170 return new Extension(main, secondary);
171 }
172 }
173}