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.content.Context; 20 import android.content.SharedPreferences; 21 import android.os.Bundle; 22 23 import com.android.car.settings.common.SettingsFragment; 24 25 /** 26 * Fragment base class for use in cases where a list of applications is displayed. Includes a 27 * a shared preference instance that can be used to show/hide system apps in the list. 28 */ 29 public abstract class AppListFragment extends SettingsFragment { 30 31 protected static final String SHARED_PREFERENCE_PATH = 32 "com.android.car.settings.applications.AppListFragment"; 33 protected static final String KEY_HIDE_SYSTEM = 34 "com.android.car.settings.applications.HIDE_SYSTEM"; 35 36 private boolean mHideSystem = true; 37 38 private SharedPreferences mSharedPreferences; 39 private SharedPreferences.OnSharedPreferenceChangeListener mSharedPreferenceChangeListener = 40 (sharedPreferences, key) -> { 41 if (KEY_HIDE_SYSTEM.equals(key)) { 42 mHideSystem = sharedPreferences.getBoolean(KEY_HIDE_SYSTEM, 43 /* defaultValue= */ true); 44 onToggleShowSystemApps(!mHideSystem); 45 } 46 }; 47 48 @Override onCreate(Bundle savedInstanceState)49 public void onCreate(Bundle savedInstanceState) { 50 super.onCreate(savedInstanceState); 51 mSharedPreferences = 52 getContext().getSharedPreferences(SHARED_PREFERENCE_PATH, Context.MODE_PRIVATE); 53 if (savedInstanceState != null) { 54 mHideSystem = savedInstanceState.getBoolean(KEY_HIDE_SYSTEM, 55 /* defaultValue= */ true); 56 mSharedPreferences.edit().putBoolean(KEY_HIDE_SYSTEM, mHideSystem).apply(); 57 } else { 58 mSharedPreferences.edit().putBoolean(KEY_HIDE_SYSTEM, true).apply(); 59 } 60 } 61 62 @Override onStart()63 public void onStart() { 64 super.onStart(); 65 onToggleShowSystemApps(!mHideSystem); 66 mSharedPreferences.registerOnSharedPreferenceChangeListener( 67 mSharedPreferenceChangeListener); 68 } 69 70 @Override onStop()71 public void onStop() { 72 super.onStop(); 73 mSharedPreferences.unregisterOnSharedPreferenceChangeListener( 74 mSharedPreferenceChangeListener); 75 } 76 77 /** Called when a user toggles the option to show system applications in the list. */ onToggleShowSystemApps(boolean showSystem)78 protected abstract void onToggleShowSystemApps(boolean showSystem); 79 80 /** Returns {@code true} if system applications should be shown in the list. */ shouldShowSystemApps()81 protected final boolean shouldShowSystemApps() { 82 return !mHideSystem; 83 } 84 85 @Override onSaveInstanceState(Bundle outState)86 public void onSaveInstanceState(Bundle outState) { 87 super.onSaveInstanceState(outState); 88 outState.putBoolean(KEY_HIDE_SYSTEM, mHideSystem); 89 } 90 } 91