1 /*
2  * Copyright (C) 2018 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.car.settings.home;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.provider.Settings;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.XmlRes;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.SettingsFragment;
30 import com.android.car.ui.toolbar.MenuItem;
31 import com.android.car.ui.toolbar.NavButtonMode;
32 import com.android.car.ui.toolbar.ToolbarController;
33 
34 import java.util.Collections;
35 import java.util.List;
36 
37 /**
38  * Homepage for settings for car.
39  */
40 public class HomepageFragment extends SettingsFragment {
41     private static final int REQUEST_CODE = 501;
42 
43     private MenuItem mSearchButton;
44 
45     @Override
46     @XmlRes
getPreferenceScreenResId()47     protected int getPreferenceScreenResId() {
48         return R.xml.homepage_fragment;
49     }
50 
51     @Override
onAttach(Context context)52     public void onAttach(Context context) {
53         super.onAttach(context);
54         // TODO: Re-enable suggestions once more use cases are supported.
55         // use(SuggestionsPreferenceController.class, R.string.pk_suggestions).setLoaderManager(
56         //        LoaderManager.getInstance(/* owner= */ this));
57     }
58 
59     @Override
getToolbarMenuItems()60     protected List<MenuItem> getToolbarMenuItems() {
61         return Collections.singletonList(mSearchButton);
62     }
63 
64     @Override
onCreate(Bundle savedInstanceState)65     public void onCreate(Bundle savedInstanceState) {
66         super.onCreate(savedInstanceState);
67 
68         mSearchButton = new MenuItem.Builder(getContext())
69                 .setToSearch()
70                 .setOnClickListener(i -> onSearchButtonClicked())
71                 .setUxRestrictions(CarUxRestrictions.UX_RESTRICTIONS_NO_KEYBOARD)
72                 .build();
73     }
74 
75     @Override
setupToolbar(@onNull ToolbarController toolbar)76     protected void setupToolbar(@NonNull ToolbarController toolbar) {
77         super.setupToolbar(toolbar);
78         toolbar.setNavButtonMode(NavButtonMode.BACK);
79     }
80 
onSearchButtonClicked()81     private void onSearchButtonClicked() {
82         Intent intent = new Intent(Settings.ACTION_APP_SEARCH_SETTINGS)
83                 .setPackage(getSettingsIntelligencePkgName(getContext()));
84         if (intent.resolveActivity(getContext().getPackageManager()) == null) {
85             return;
86         }
87         startActivityForResult(intent, REQUEST_CODE);
88     }
89 
getSettingsIntelligencePkgName(Context context)90     private String getSettingsIntelligencePkgName(Context context) {
91         return context.getString(R.string.config_settingsintelligence_package_name);
92     }
93 
94 }
95