1 /* <lambda>null2 * 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.content.pm.PackageManager 21 import android.util.Log 22 import androidx.work.Data 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 CollectV1Worker(appContext: Context, params: WorkerParameters) : 29 BaseRequestWorker(appContext, params) { 30 31 companion object { 32 private val TAG = CollectV1Worker::class.java.simpleName 33 private const val DEBUG = false 34 35 private const val VERIFICATION_ID_KEY = "verificationId" 36 private const val PACKAGE_NAME_KEY = "packageName" 37 38 fun buildRequest(verificationId: Int, packageName: String) = 39 OneTimeWorkRequestBuilder<CollectV1Worker>() 40 .setInputData( 41 Data.Builder() 42 .putInt(VERIFICATION_ID_KEY, verificationId) 43 .apply { 44 putString(PACKAGE_NAME_KEY, packageName) 45 } 46 .build() 47 ) 48 .build() 49 } 50 51 override suspend fun doWork() = coroutineScope { 52 if (!AndroidUtils.isReceiverV1Enabled(appContext)) { 53 //clear sp and commit here 54 val inputData = params.inputData 55 val packageName = inputData.getString(PACKAGE_NAME_KEY) 56 val deContext = appContext.createDeviceProtectedStorageContext() 57 val sp = deContext?.getSharedPreferences(packageName, Context.MODE_PRIVATE) 58 val editor = sp?.edit() 59 editor?.clear()?.commit() 60 //delete sp file 61 val retOfDel = deContext?.deleteSharedPreferences(packageName) 62 if (DEBUG) { 63 Log.d(TAG, "delete sp for $packageName return $retOfDel") 64 } 65 return@coroutineScope Result.success() 66 } 67 68 val inputData = params.inputData 69 val verificationId = inputData.getInt(VERIFICATION_ID_KEY, -1) 70 val successfulHosts = mutableListOf<String>() 71 val failedHosts = mutableListOf<String>() 72 val packageName = inputData.getString(PACKAGE_NAME_KEY) 73 val deContext = appContext.createDeviceProtectedStorageContext() 74 val sp = deContext?.getSharedPreferences(packageName, Context.MODE_PRIVATE) 75 sp?.all?.entries?.forEach { (key, _) -> 76 when { 77 key.startsWith(SingleV1RequestWorker.HOST_SUCCESS_PREFIX) -> 78 successfulHosts += key.removePrefix(SingleV1RequestWorker.HOST_SUCCESS_PREFIX) 79 key.startsWith(SingleV1RequestWorker.HOST_FAILURE_PREFIX) -> 80 failedHosts += key.removePrefix(SingleV1RequestWorker.HOST_FAILURE_PREFIX) 81 } 82 } 83 84 if (DEBUG) { 85 Log.d( 86 TAG, "Domain verification v1 request for $packageName: " + 87 "success = $successfulHosts, failed = $failedHosts" 88 ) 89 } 90 91 val resultCode = if (failedHosts.isEmpty()) { 92 PackageManager.INTENT_FILTER_VERIFICATION_SUCCESS 93 } else { 94 PackageManager.INTENT_FILTER_VERIFICATION_FAILURE 95 } 96 97 appContext.packageManager.verifyIntentFilter(verificationId, resultCode, failedHosts) 98 99 //clear sp and commit here 100 val editor = sp?.edit() 101 editor?.clear()?.commit() 102 //delete sp file 103 val retOfDel = deContext?.deleteSharedPreferences(packageName) 104 if (DEBUG) { 105 Log.d(TAG, "delete sp for $packageName return $retOfDel") 106 } 107 108 Result.success() 109 } 110 } 111