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