1 /*
2  * Copyright 2019 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.applications;
18 
19 import android.os.Bundle;
20 
21 import com.android.car.settings.R;
22 import com.android.car.settings.common.SettingsFragment;
23 import com.android.car.ui.toolbar.MenuItem;
24 
25 import java.util.Collections;
26 import java.util.List;
27 
28 /**
29  * Fragment base class for use in cases where a list of applications is displayed with the option to
30  * show/hide system apps in the list.
31  */
32 public abstract class AppListFragment extends SettingsFragment {
33 
34     private static final String KEY_SHOW_SYSTEM = "showSystem";
35 
36     private boolean mShowSystem;
37     private MenuItem mSystemButton;
38 
39     @Override
onCreate(Bundle savedInstanceState)40     public void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         if (savedInstanceState != null) {
43             mShowSystem = savedInstanceState.getBoolean(KEY_SHOW_SYSTEM, false);
44         }
45 
46         mSystemButton = new MenuItem.Builder(getContext())
47                 .setOnClickListener(i -> {
48                     mShowSystem = !mShowSystem;
49                     onToggleShowSystemApps(mShowSystem);
50                     i.setTitle(mShowSystem ? R.string.hide_system : R.string.show_system);
51                 })
52                 .setTitle(mShowSystem ? R.string.hide_system : R.string.show_system)
53                 .build();
54     }
55 
56     @Override
getToolbarMenuItems()57     public List<MenuItem> getToolbarMenuItems() {
58         return Collections.singletonList(mSystemButton);
59     }
60 
61     @Override
onStart()62     public void onStart() {
63         super.onStart();
64         onToggleShowSystemApps(mShowSystem);
65     }
66 
67     /** Called when a user toggles the option to show system applications in the list. */
onToggleShowSystemApps(boolean showSystem)68     protected abstract void onToggleShowSystemApps(boolean showSystem);
69 
70     /** Returns {@code true} if system applications should be shown in the list. */
shouldShowSystemApps()71     protected final boolean shouldShowSystemApps() {
72         return mShowSystem;
73     }
74 
75     @Override
onSaveInstanceState(Bundle outState)76     public void onSaveInstanceState(Bundle outState) {
77         super.onSaveInstanceState(outState);
78         outState.putBoolean(KEY_SHOW_SYSTEM, mShowSystem);
79     }
80 }
81