Differential D9069 Diff 38942 native/android/app/src/main/java/app/comm/android/commservices/CommAndroidBlobClient.java
Changeset View
Changeset View
Standalone View
Standalone View
native/android/app/src/main/java/app/comm/android/commservices/CommAndroidBlobClient.java
package app.comm.android.commservices; | package app.comm.android.commservices; | ||||
import android.content.Context; | |||||
import androidx.work.Constraints; | |||||
import androidx.work.Data; | |||||
import androidx.work.NetworkType; | |||||
import androidx.work.OneTimeWorkRequest; | |||||
import androidx.work.WorkManager; | |||||
import androidx.work.WorkRequest; | |||||
import app.comm.android.BuildConfig; | import app.comm.android.BuildConfig; | ||||
import app.comm.android.fbjni.CommSecureStore; | import app.comm.android.fbjni.CommSecureStore; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.lang.OutOfMemoryError; | import java.lang.OutOfMemoryError; | ||||
import java.util.Base64; | import java.util.Base64; | ||||
import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | ||||
import okhttp3.Call; | import okhttp3.Call; | ||||
import okhttp3.OkHttpClient; | import okhttp3.OkHttpClient; | ||||
import okhttp3.Request; | import okhttp3.Request; | ||||
import okhttp3.RequestBody; | import okhttp3.RequestBody; | ||||
import okhttp3.Response; | import okhttp3.Response; | ||||
import org.json.JSONException; | import org.json.JSONException; | ||||
import org.json.JSONObject; | import org.json.JSONObject; | ||||
public class CommAndroidBlobClient { | public class CommAndroidBlobClient { | ||||
private static final String BLOB_SERVICE_URL = BuildConfig.DEBUG | |||||
? "https://blob.staging.commtechnologies.org" | |||||
: "https://blob.commtechnologies.org"; | |||||
// The FirebaseMessagingService docs state that message | // The FirebaseMessagingService docs state that message | ||||
// processing should complete within at most 20 seconds | // processing should complete within at most 20 seconds | ||||
// window. Therefore we limit http time call to 15 seconds | // window. Therefore we limit http time call to 15 seconds | ||||
// https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages | // https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages | ||||
private static final int NOTIF_PROCESSING_TIME_LIMIT_SECONDS = 15; | private static final int NOTIF_PROCESSING_TIME_LIMIT_SECONDS = 15; | ||||
// OkHttp docs advise to share OkHttpClient instances | // OkHttp docs advise to share OkHttpClient instances | ||||
// https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/#okhttpclients-should-be-shared | // https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/#okhttpclients-should-be-shared | ||||
private static final OkHttpClient httpClient = | public static final OkHttpClient httpClient = | ||||
new OkHttpClient.Builder() | new OkHttpClient.Builder() | ||||
.callTimeout(NOTIF_PROCESSING_TIME_LIMIT_SECONDS, TimeUnit.SECONDS) | .callTimeout(NOTIF_PROCESSING_TIME_LIMIT_SECONDS, TimeUnit.SECONDS) | ||||
.build(); | .build(); | ||||
public static final String BLOB_SERVICE_URL = BuildConfig.DEBUG | |||||
? "https://blob.staging.commtechnologies.org" | |||||
: "https://blob.commtechnologies.org"; | |||||
public static final String BLOB_HASH_KEY = "blob_hash"; | |||||
public static final String BLOB_HOLDER_KEY = "holder"; | |||||
public byte[] getBlobSync(String blobHash) throws IOException, JSONException { | public byte[] getBlobSync(String blobHash) throws IOException, JSONException { | ||||
String authToken = getAuthToken(); | String authToken = getAuthToken(); | ||||
Request request = new Request.Builder() | Request request = new Request.Builder() | ||||
.get() | .get() | ||||
.url(BLOB_SERVICE_URL + "/blob/" + blobHash) | .url(BLOB_SERVICE_URL + "/blob/" + blobHash) | ||||
.header("Authorization", authToken) | .header("Authorization", authToken) | ||||
.build(); | .build(); | ||||
Response response = httpClient.newCall(request).execute(); | Response response = httpClient.newCall(request).execute(); | ||||
if (!response.isSuccessful()) { | if (!response.isSuccessful()) { | ||||
throw new RuntimeException( | throw new RuntimeException( | ||||
"Failed to download blob from blob service. Response error code: " + | "Failed to download blob from blob service. Response error code: " + | ||||
response); | response); | ||||
} | } | ||||
return response.body().bytes(); | return response.body().bytes(); | ||||
} | } | ||||
private String getAuthToken() throws JSONException { | public void scheduleDeferredBlobDeletion( | ||||
String blobHash, | |||||
String blobHolder, | |||||
Context context) { | |||||
Constraints constraints = new Constraints.Builder() | |||||
.setRequiredNetworkType(NetworkType.CONNECTED) | |||||
.build(); | |||||
WorkRequest deleteBlobWorkRequest = | |||||
new OneTimeWorkRequest.Builder(CommAndroidDeleteBlobWork.class) | |||||
.setConstraints(constraints) | |||||
.setInitialDelay( | |||||
NOTIF_PROCESSING_TIME_LIMIT_SECONDS, TimeUnit.SECONDS) | |||||
.setInputData(new Data.Builder() | |||||
.putString(BLOB_HASH_KEY, blobHash) | |||||
.putString(BLOB_HOLDER_KEY, blobHolder) | |||||
.build()) | |||||
.build(); | |||||
WorkManager.getInstance(context).enqueue(deleteBlobWorkRequest); | |||||
} | |||||
public static String getAuthToken() throws JSONException { | |||||
// Authentication data are retrieved on every request | // Authentication data are retrieved on every request | ||||
// since they might change while CommNotificationsHandler | // since they might change while CommNotificationsHandler | ||||
// thread is running so we should not rely on caching | // thread is running so we should not rely on caching | ||||
// them in memory. | // them in memory. | ||||
String userID = CommSecureStore.get("userID"); | String userID = CommSecureStore.get("userID"); | ||||
String accessToken = CommSecureStore.get("accessToken"); | String accessToken = CommSecureStore.get("accessToken"); | ||||
String deviceID = CommSecureStore.get("deviceID"); | String deviceID = CommSecureStore.get("deviceID"); | ||||
Show All 17 Lines |