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