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
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.content.pm.PackageManager
22 import androidx.work.ExistingWorkPolicy
23 import androidx.work.WorkManager
24 import com.android.statementservice.domain.worker.CollectV1Worker
25 import com.android.statementservice.domain.worker.SingleV1RequestWorker
26 
27 /**
28  * Receiver for V1 API. Separated so that the receiver permission can be declared for only the
29  * v1 and v2 permissions individually, exactly matching the intended usage.
30  */
31 class DomainVerificationReceiverV1 : BaseDomainVerificationReceiver() {
32 
33     companion object {
34         private const val ENABLE_V1 = true
35         private const val PACKAGE_WORK_PREFIX_V1 = "package_request_v1-"
36     }
37 
38     override val tag = DomainVerificationReceiverV1::class.java.simpleName
39 
onReceivenull40     override fun onReceive(context: Context, intent: Intent) {
41         when (intent.action) {
42             Intent.ACTION_INTENT_FILTER_NEEDS_VERIFICATION ->
43                 scheduleUnlockedV1(context, intent)
44             else -> debugLog { "Received invalid broadcast: $intent" }
45         }
46     }
47 
scheduleUnlockedV1null48     private fun scheduleUnlockedV1(context: Context, intent: Intent) {
49         if (!ENABLE_V1) {
50             return
51         }
52 
53         val verificationId =
54             intent.getIntExtra(PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_ID, -1)
55         val hosts =
56             (intent.getStringExtra(PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_HOSTS) ?: return)
57                 .split(" ")
58         val packageName =
59             intent.getStringExtra(PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_PACKAGE_NAME)
60                 ?: return
61 
62         debugLog { "Attempting v1 verification for $packageName" }
63 
64         val workRequests = hosts.map {
65             SingleV1RequestWorker.buildRequest(packageName, it) {
66                 setConstraints(networkConstraints)
67             }
68         }
69 
70         //clear sp before enqueue unique work since policy is REPLACE
71         val deContext = context.createDeviceProtectedStorageContext()
72         val editor = deContext?.getSharedPreferences(packageName, Context.MODE_PRIVATE)?.edit()
73         editor?.clear()?.apply()
74         WorkManager.getInstance(context)
75             .beginUniqueWork(
76                 "$PACKAGE_WORK_PREFIX_V1$packageName",
77                 ExistingWorkPolicy.REPLACE,
78                 workRequests
79             )
80             .then(CollectV1Worker.buildRequest(verificationId, packageName))
81             .enqueue()
82     }
83 }
84