1 /*
2  * Copyright (C) 2021 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.privacy;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 
22 import androidx.annotation.Nullable;
23 
24 import com.android.car.settings.R;
25 import com.android.car.settings.common.SettingsFragment;
26 import com.android.car.ui.toolbar.MenuItem;
27 
28 import java.util.Arrays;
29 import java.util.List;
30 
31 /**
32  * All apps that have recently accessed microphone.
33  */
34 public class MicrophoneRecentAccessViewAllFragment extends SettingsFragment {
35 
36     private boolean mShowSystem = false;
37     private MenuItem mShowHideSystemMenu;
38     private MicrophoneRecentAccessViewAllPreferenceController mController;
39 
40     @Override
getPreferenceScreenResId()41     protected int getPreferenceScreenResId() {
42         return R.xml.microphone_recent_requests_view_all_fragment;
43     }
44 
45     @Override
onCreate(@ullable Bundle savedInstanceState)46     public void onCreate(@Nullable Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         mShowHideSystemMenu = new MenuItem.Builder(getContext())
49                 .setTitle(R.string.show_system)
50                 .setOnClickListener(i -> setShowSystem(!mShowSystem))
51                 .build();
52     }
53 
54     @Override
onAttach(Context context)55     public void onAttach(Context context) {
56         super.onAttach(context);
57         mController = use(MicrophoneRecentAccessViewAllPreferenceController.class,
58                 R.string.pk_microphone_recent_requests);
59     }
60 
61     @Override
getToolbarMenuItems()62     protected List<MenuItem> getToolbarMenuItems() {
63         return Arrays.asList(mShowHideSystemMenu);
64     }
65 
setShowSystem(boolean showSystem)66     private void setShowSystem(boolean showSystem) {
67         if (showSystem != mShowSystem) {
68             mShowSystem = showSystem;
69             mController.setShowSystem(showSystem);
70             updateMenu();
71         }
72     }
73 
updateMenu()74     private void updateMenu() {
75         mShowHideSystemMenu.setTitle(mShowSystem ? R.string.hide_system : R.string.show_system);
76     }
77 }
78