1 /* 2 * Copyright (C) 2017 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.settings.support.actionbar; 18 19 import static com.android.settings.support.actionbar.HelpResourceProvider.HELP_URI_RESOURCE_KEY; 20 21 import android.app.Activity; 22 import android.os.Bundle; 23 import android.view.Menu; 24 import android.view.MenuInflater; 25 26 import androidx.annotation.NonNull; 27 import androidx.fragment.app.Fragment; 28 29 import com.android.settingslib.HelpUtils; 30 import com.android.settingslib.core.lifecycle.LifecycleObserver; 31 import com.android.settingslib.core.lifecycle.ObservableFragment; 32 import com.android.settingslib.core.lifecycle.ObservablePreferenceFragment; 33 import com.android.settingslib.core.lifecycle.events.OnCreateOptionsMenu; 34 35 /** 36 * A controller that adds help menu to any Settings page. 37 */ 38 public class HelpMenuController implements LifecycleObserver, OnCreateOptionsMenu { 39 40 private final Fragment mHost; 41 init(@onNull ObservablePreferenceFragment host)42 public static void init(@NonNull ObservablePreferenceFragment host) { 43 host.getSettingsLifecycle().addObserver(new HelpMenuController(host)); 44 } 45 init(@onNull ObservableFragment host)46 public static void init(@NonNull ObservableFragment host) { 47 host.getSettingsLifecycle().addObserver(new HelpMenuController(host)); 48 } 49 HelpMenuController(@onNull Fragment host)50 private HelpMenuController(@NonNull Fragment host) { 51 mHost = host; 52 } 53 54 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)55 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 56 final Bundle arguments = mHost.getArguments(); 57 int helpResourceId = 0; 58 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) { 59 helpResourceId = arguments.getInt(HELP_URI_RESOURCE_KEY); 60 } else if (mHost instanceof HelpResourceProvider) { 61 helpResourceId = ((HelpResourceProvider) mHost).getHelpResource(); 62 } 63 64 String helpUri = null; 65 if (helpResourceId != 0) { 66 helpUri = mHost.getContext().getString(helpResourceId); 67 } 68 final Activity activity = mHost.getActivity(); 69 if (helpUri != null && activity != null) { 70 HelpUtils.prepareHelpMenuItem(activity, menu, helpUri, mHost.getClass().getName()); 71 } 72 } 73 } 74