1 /* 2 * Copyright (C) 2020 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.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.content.SharedPreferences; 22 23 import com.android.car.settings.common.ColoredSwitchPreference; 24 import com.android.car.settings.common.FragmentController; 25 import com.android.car.settings.common.PreferenceController; 26 27 /** 28 * Hides/shows system apps from Application listings. Intended to be used inside instances of 29 * {@link AppListFragment}. 30 */ 31 public class HideSystemSwitchPreferenceController 32 extends PreferenceController<ColoredSwitchPreference> { 33 34 private final SharedPreferences mSharedPreferences = getContext().getSharedPreferences( 35 AppListFragment.SHARED_PREFERENCE_PATH, Context.MODE_PRIVATE); 36 HideSystemSwitchPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)37 public HideSystemSwitchPreferenceController(Context context, String preferenceKey, 38 FragmentController fragmentController, 39 CarUxRestrictions uxRestrictions) { 40 super(context, preferenceKey, fragmentController, uxRestrictions); 41 } 42 43 @Override getPreferenceType()44 protected Class<ColoredSwitchPreference> getPreferenceType() { 45 return ColoredSwitchPreference.class; 46 } 47 48 @Override handlePreferenceChanged(ColoredSwitchPreference preference, Object newValue)49 protected boolean handlePreferenceChanged(ColoredSwitchPreference preference, Object newValue) { 50 boolean checked = (Boolean) newValue; 51 mSharedPreferences.edit().putBoolean(AppListFragment.KEY_HIDE_SYSTEM, checked).apply(); 52 return true; 53 } 54 55 @Override onStartInternal()56 protected void onStartInternal() { 57 getPreference().setChecked(getSharedPreferenceHidden()); 58 } 59 getSharedPreferenceHidden()60 private boolean getSharedPreferenceHidden() { 61 return mSharedPreferences.getBoolean(AppListFragment.KEY_HIDE_SYSTEM, 62 /* defaultValue= */ true); 63 } 64 } 65