1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.statementservice.domain.worker 18 19 import android.content.Context 20 import android.util.Log 21 import androidx.work.Data 22 import androidx.work.OneTimeWorkRequest 23 import androidx.work.OneTimeWorkRequestBuilder 24 import androidx.work.WorkerParameters 25 import com.android.statementservice.utils.AndroidUtils 26 import kotlinx.coroutines.coroutineScope 27 28 class SingleV1RequestWorker(appContext: Context, params: WorkerParameters) : 29 BaseRequestWorker(appContext, params) { 30 31 companion object { 32 private val TAG = SingleV1RequestWorker::class.java.simpleName 33 private const val DEBUG = false 34 35 private const val PACKAGE_NAME_KEY = "packageName" 36 private const val HOST_KEY = "host" 37 const val HOST_SUCCESS_PREFIX = "hostSuccess:" 38 const val HOST_FAILURE_PREFIX = "hostFailure:" 39 buildRequestnull40 fun buildRequest( 41 packageName: String, 42 host: String, 43 block: OneTimeWorkRequest.Builder.() -> Unit = {} 44 ) = OneTimeWorkRequestBuilder<SingleV1RequestWorker>() 45 .setInputData( 46 Data.Builder() 47 .putString(PACKAGE_NAME_KEY, packageName) 48 .putString(HOST_KEY, host) 49 .build() 50 ) 51 .apply(block) 52 .build() 53 } 54 <lambda>null55 override suspend fun doWork() = coroutineScope { 56 if (!AndroidUtils.isReceiverV1Enabled(appContext)) { 57 return@coroutineScope Result.success() 58 } 59 60 val packageName = params.inputData.getString(PACKAGE_NAME_KEY)!! 61 val host = params.inputData.getString(HOST_KEY)!! 62 63 val (result, status) = verifier.verifyHost(host, packageName, params.network) 64 65 if (DEBUG) { 66 Log.d( 67 TAG, "Domain verification v1 request for $packageName: " + 68 "host = $host, status = $status" 69 ) 70 } 71 72 // Coerce failure results into success so that final collection task gets a chance to run 73 when (result) { 74 is Result.Success -> { 75 val deContext = appContext.createDeviceProtectedStorageContext() 76 val sp = deContext?.getSharedPreferences(packageName, Context.MODE_PRIVATE) 77 sp?.edit()?.putInt("$HOST_SUCCESS_PREFIX$host", status.value)?.apply() 78 Result.success() 79 } 80 is Result.Failure -> { 81 val deContext = appContext.createDeviceProtectedStorageContext() 82 val sp = deContext?.getSharedPreferences(packageName, Context.MODE_PRIVATE) 83 sp?.edit()?.putInt("$HOST_FAILURE_PREFIX$host", status.value)?.apply() 84 Result.success() 85 } 86 else -> result 87 } 88 } 89 } 90