diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/TestDescriptorManager.kt b/app/src/main/java/org/openobservatory/ooniprobe/common/TestDescriptorManager.kt index ba764d443..363d973aa 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/common/TestDescriptorManager.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/TestDescriptorManager.kt @@ -49,37 +49,47 @@ class TestDescriptorManager @Inject constructor( * @param runId the run id of the descriptor to fetch * @param context the context to use for the request */ - fun fetchDescriptorFromRunId(runId: Long, context: Context): TestDescriptor { + fun fetchDescriptorFromRunId(runId: Long, context: Context): TestDescriptor? { val session = EngineProvider.get().newSession( - EngineProvider.get().getDefaultSessionConfig( - context, - BuildConfig.SOFTWARE_NAME, - BuildConfig.VERSION_NAME, - LoggerArray(), - (context.applicationContext as Application).preferenceManager.proxyURL - ) + EngineProvider.get().getDefaultSessionConfig( + context, + BuildConfig.SOFTWARE_NAME, + BuildConfig.VERSION_NAME, + LoggerArray(), + (context.applicationContext as Application).preferenceManager.proxyURL + ) ) val ooniContext = session.newContextWithTimeout(300) - val response: OONIRunDescriptor = + runCatching { + // Fetch the descriptor from the ooni server session.getLatestOONIRunLink(ooniContext, BuildConfig.OONI_API_BASE_URL, runId) - - var revisions: OONIRunRevisions? = null - - try { - if (Integer.parseInt(response.revision) > 1) { - revisions = session.getOONIRunLinkRevisions( - ooniContext, - BuildConfig.OONI_API_BASE_URL, - runId - ) + }.fold( + onSuccess = { ooniRunDescriptor -> + + var revisions: OONIRunRevisions? = null + + try { + if (ooniRunDescriptor.revision.toInt() > 1) { + revisions = session.getOONIRunLinkRevisions( + ooniContext, + BuildConfig.OONI_API_BASE_URL, + runId + ) + } + } catch (e: Exception) { + ThirdPartyServices.logException(e) + } + return ooniRunDescriptor.toTestDescriptor().apply { + previousRevision = Gson().toJson(revisions) + } + }, + onFailure = { throwable -> + // Log the error or handle it appropriately + ThirdPartyServices.logException(Exception(throwable)) + return null } - } catch (e: Exception) { - ThirdPartyServices.logException(e) - } - return response.toTestDescriptor().apply { - previousRevision = Gson().toJson(revisions) - } + ) } fun addDescriptor( diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/worker/UpdateDescriptorsWorker.kt b/app/src/main/java/org/openobservatory/ooniprobe/common/worker/UpdateDescriptorsWorker.kt index d861c00c1..5e4e9d244 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/common/worker/UpdateDescriptorsWorker.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/worker/UpdateDescriptorsWorker.kt @@ -17,8 +17,8 @@ var d: UpdateDescriptorsWorkerDependencies = UpdateDescriptorsWorkerDependencies const val PROGRESS = "PROGRESS" class AutoUpdateDescriptorsWorker( - context: Context, - workerParams: WorkerParameters + context: Context, + workerParams: WorkerParameters ) : Worker(context, workerParams) { override fun doWork(): Result { @@ -30,22 +30,22 @@ class AutoUpdateDescriptorsWorker( val updatedDescriptors: ArrayList = ArrayList() for (descriptor in d.testDescriptorManager.getDescriptorWithAutoUpdateEnabled()) { + Log.d(TAG, "Fetching updates for ${descriptor.runId}") - val updatedDescriptor: TestDescriptor = - d.testDescriptorManager.fetchDescriptorFromRunId( + d.testDescriptorManager.fetchDescriptorFromRunId( descriptor.runId, applicationContext - ) - - if (descriptor.shouldUpdate(updatedDescriptor)) { - updatedDescriptor.isAutoUpdate = descriptor.isAutoUpdate - updatedDescriptor.isAutoRun = descriptor.isAutoRun + )?.let { updatedDescriptor -> + if (descriptor.shouldUpdate(updatedDescriptor)) { + updatedDescriptor.isAutoUpdate = descriptor.isAutoUpdate + updatedDescriptor.isAutoRun = descriptor.isAutoRun - Log.d(TAG, "Saving updates for ${descriptor.runId}") + Log.d(TAG, "Saving updates for ${descriptor.runId}") - updatedDescriptor.save() - updatedDescriptors.add(updatedDescriptor) + updatedDescriptor.save() + updatedDescriptors.add(updatedDescriptor) + } } } @@ -70,21 +70,21 @@ class AutoUpdateDescriptorsWorker( companion object { @JvmField var UPDATED_DESCRIPTORS_WORK_NAME = - "${AutoUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME" + "${AutoUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME" private val TAG = AutoUpdateDescriptorsWorker::class.java.simpleName private val UPDATE_DESCRIPTOR_CHANNEL: String = - AutoUpdateDescriptorsWorker::class.java.simpleName + AutoUpdateDescriptorsWorker::class.java.simpleName private val KEY_UPDATED_DESCRIPTORS = - "${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS" + "${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS" } } class ManualUpdateDescriptorsWorker( - context: Context, - workerParams: WorkerParameters + context: Context, + workerParams: WorkerParameters ) : Worker(context, workerParams) { override fun doWork(): Result { @@ -113,17 +113,18 @@ class ManualUpdateDescriptorsWorker( for (descriptor in descriptors) { Log.d(TAG, "Fetching updates for ${descriptor.runId}") - val updatedDescriptor: TestDescriptor = - d.testDescriptorManager.fetchDescriptorFromRunId( + d.testDescriptorManager.fetchDescriptorFromRunId( descriptor.runId, applicationContext - ) - /** - * NOTE(aanorbel): Refine this logic to only update if the descriptor has changed. - * Consider explicit version compare. - */ - if (descriptor.shouldUpdate(updatedDescriptor)) { - updatedDescriptors.add(updatedDescriptor) + )?.let { updatedDescriptor -> + + /** + * NOTE(aanorbel): Refine this logic to only update if the descriptor has changed. + * Consider explicit version compare. + */ + if (descriptor.shouldUpdate(updatedDescriptor)) { + updatedDescriptors.add(updatedDescriptor) + } } } val outputData = Data.Builder() @@ -153,19 +154,20 @@ class ManualUpdateDescriptorsWorker( companion object { @JvmField var UPDATED_DESCRIPTORS_WORK_NAME = - "${AutoUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME" + "${AutoUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME" private val TAG = AutoUpdateDescriptorsWorker::class.java.simpleName private val UPDATE_DESCRIPTOR_CHANNEL: String = - AutoUpdateDescriptorsWorker::class.java.simpleName + AutoUpdateDescriptorsWorker::class.java.simpleName @JvmField var KEY_UPDATED_DESCRIPTORS = - "${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS" + "${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS" + @JvmField var KEY_DESCRIPTOR_IDS = - "${AutoUpdateDescriptorsWorker::class.java.name}.KEY_DESCRIPTOR_IDS" + "${AutoUpdateDescriptorsWorker::class.java.name}.KEY_DESCRIPTOR_IDS" } }