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
17 
18 import android.content.Context
19 import android.content.Intent
20 import android.net.Uri
21 import android.view.Menu
22 import android.view.MenuInflater
23 import android.view.MenuItem
24 
25 /** Handles app help. */
26 class Help(context: Context?, config: Config) {
27   private val mContext: Context?
28   private val mConfig: Config
addHelpMenuItemnull29   fun addHelpMenuItem(menu: Menu, activityName: String?) {
30     addHelpMenuItem(menu, activityName, false)
31   }
32 
addHelpMenuItemnull33   fun addHelpMenuItem(menu: Menu, activityName: String?, showAsAction: Boolean) {
34     val helpIntent: Intent? = getHelpIntent(activityName)
35     if (helpIntent != null) {
36       val inflater = MenuInflater(mContext)
37       inflater.inflate(R.menu.help, menu)
38       val item: MenuItem = menu.findItem(R.id.menu_help)
39       item.setIntent(helpIntent)
40       if (showAsAction) {
41         item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
42       }
43     }
44   }
45 
getHelpIntentnull46   private fun getHelpIntent(activityName: String?): Intent? {
47     val helpUrl: Uri = mConfig.getHelpUrl(activityName) ?: return null
48     return Intent(Intent.ACTION_VIEW, helpUrl)
49   }
50 
51   init {
52     mContext = context
53     mConfig = config
54   }
55 }
56