1 /*
2  * Copyright (C) 2022 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.quicksearchbox.google
17 
18 import android.content.ComponentName
19 import android.content.Context
20 import android.content.Intent
21 import android.os.Bundle
22 import android.os.Handler
23 import com.android.quicksearchbox.AbstractInternalSource
24 import com.android.quicksearchbox.CursorBackedSourceResult
25 import com.android.quicksearchbox.R
26 import com.android.quicksearchbox.SourceResult
27 import com.android.quicksearchbox.SuggestionCursor
28 import com.android.quicksearchbox.util.NamedTaskExecutor
29 
30 /** Special source implementation for Google suggestions. */
31 abstract class AbstractGoogleSource(
32   context: Context?,
33   uiThread: Handler?,
34   iconLoader: NamedTaskExecutor
35 ) :
36   AbstractInternalSource(context, uiThread, iconLoader),
37   com.android.quicksearchbox.google.GoogleSource {
38   @get:Override abstract override val intentComponent: ComponentName?
39 
40   @Override
refreshShortcutnull41   abstract override fun refreshShortcut(shortcutId: String?, extraData: String?): SuggestionCursor?
42 
43   /** Called by QSB to get web suggestions for a query. */
44   @Override abstract override fun queryInternal(query: String?): SourceResult?
45 
46   /** Called by external apps to get web suggestions for a query. */
47   @Override abstract override fun queryExternal(query: String?): SourceResult?
48 
49   @Override
50   override fun createVoiceSearchIntent(appData: Bundle?): Intent? {
51     return createVoiceWebSearchIntent(appData)
52   }
53 
54   @get:Override
55   override val defaultIntentAction: String
56     get() = Intent.ACTION_WEB_SEARCH
57 
58   @get:Override
59   override val hint: CharSequence
60     get() = context!!.getString(R.string.google_search_hint)
61 
62   @get:Override
63   override val label: CharSequence
64     get() = context!!.getString(R.string.google_search_label)
65 
66   @get:Override
67   override val name: String
68     get() = AbstractGoogleSource.Companion.GOOGLE_SOURCE_NAME
69 
70   @get:Override
71   override val settingsDescription: CharSequence
72     get() = context!!.getString(R.string.google_search_description)
73 
74   @get:Override
75   override val sourceIconResource: Int
76     get() = R.mipmap.google_icon
77 
78   @Override
getSuggestionsnull79   override fun getSuggestions(query: String?, queryLimit: Int): SourceResult? {
80     return emptyIfNull(queryInternal(query), query)
81   }
82 
getSuggestionsExternalnull83   fun getSuggestionsExternal(query: String?): SourceResult {
84     return emptyIfNull(queryExternal(query), query)
85   }
86 
emptyIfNullnull87   private fun emptyIfNull(result: SourceResult?, query: String?): SourceResult {
88     return if (result == null) CursorBackedSourceResult(this, query) else result
89   }
90 
91   @Override
voiceSearchEnablednull92   override fun voiceSearchEnabled(): Boolean {
93     return true
94   }
95 
96   @Override
includeInAllnull97   override fun includeInAll(): Boolean {
98     return true
99   }
100 
101   companion object {
102     /*
103      * This name corresponds to what was used in previous version of quick search box. We use the
104      * same name so that shortcuts continue to work after an upgrade. (It also makes logging more
105      * consistent).
106      */
107     private const val GOOGLE_SOURCE_NAME = "com.android.quicksearchbox/.google.GoogleSearch"
108   }
109 }
110