xref: /aosp_15_r20/cts/common/device-side/bedstead/nene/src/main/java/com/android/bedstead/nene/content/Suggestions.kt (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
<lambda>null2  * Copyright (C) 2023 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 package com.android.bedstead.nene.content
17 
18 import android.app.contentsuggestions.ContentSuggestionsManager
19 import android.cts.testapisreflection.*
20 import com.android.bedstead.nene.TestApis
21 import com.android.bedstead.nene.exceptions.AdbException
22 import com.android.bedstead.nene.exceptions.NeneException
23 import com.android.bedstead.nene.packages.ComponentReference
24 import com.android.bedstead.nene.users.UserReference
25 import com.android.bedstead.nene.utils.ShellCommand
26 import com.android.bedstead.nene.utils.UndoableContext
27 import com.android.bedstead.permissions.CommonPermissions.MANAGE_CONTENT_SUGGESTIONS
28 
29 import com.google.errorprone.annotations.CanIgnoreReturnValue
30 
31 /**
32  * Helper methods related to content suggestions.
33  */
34 object Suggestions {
35 
36     private val contentSuggestionsManager =
37             TestApis.context().instrumentedContext().getSystemService<ContentSuggestionsManager>(
38                     ContentSuggestionsManager::class.java)!!
39     private const val TEMPORARY_SERVICE_DURATION_MS = 3600000 // 1 hour
40 
41     @CanIgnoreReturnValue
42     @JvmOverloads
43     fun setDefaultServiceEnabled(user: UserReference = TestApis.users().instrumented(),
44                                  value: Boolean): UndoableContext {
45         val currentValue = defaultServiceEnabled(user)
46         if (currentValue == value) {
47             // Nothing to do
48             return UndoableContext.EMPTY
49         }
50         TestApis.permissions().withPermission(MANAGE_CONTENT_SUGGESTIONS).use {
51             contentSuggestionsManager.setDefaultServiceEnabled(user.id(),
52                     value)
53         }
54         return UndoableContext {
55             TestApis.permissions().withPermission(MANAGE_CONTENT_SUGGESTIONS).use {
56                 contentSuggestionsManager.setDefaultServiceEnabled(user.id(), currentValue)
57             }
58         }
59     }
60 
61     @JvmOverloads
62     fun defaultServiceEnabled(user: UserReference = TestApis.users().instrumented()): Boolean {
63         return try {
64             ShellCommand.builder("cmd content_suggestions")
65                     .addOperand("get default-service-enabled")
66                     .addOperand(user.id())
67                     .executeAndParseOutput { s: String -> s.contains("true") }
68         } catch (e: AdbException) {
69             throw NeneException("Error checking default-service-enabled", e)
70         }
71     }
72 
73     @CanIgnoreReturnValue
74     @JvmOverloads
75     fun setTemporaryService(user: UserReference, component: ComponentReference): UndoableContext {
76         TestApis.permissions().withPermission(MANAGE_CONTENT_SUGGESTIONS).use {
77             contentSuggestionsManager.setTemporaryService(user.id(), component.flattenToString(),
78                     TEMPORARY_SERVICE_DURATION_MS)
79         }
80         return UndoableContext(
81                 Runnable {
82                     TestApis.permissions().withPermission(MANAGE_CONTENT_SUGGESTIONS).use {
83                         TestApis.content().suggestions().clearTemporaryService(user)
84                     }
85                 })
86     }
87 
88     @JvmOverloads
89     fun clearTemporaryService(user: UserReference = TestApis.users().instrumented()) {
90         TestApis.permissions().withPermission(MANAGE_CONTENT_SUGGESTIONS).use {
91             contentSuggestionsManager.resetTemporaryService(user.id())
92         }
93     }
94 }
95