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.settings.slices; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.View; 22 23 import androidx.slice.Slice; 24 import androidx.slice.widget.SliceView; 25 26 import com.android.settings.R; 27 import com.android.settingslib.widget.LayoutPreference; 28 29 /** 30 * Preference for {@link SliceView} 31 */ 32 public class SlicePreference extends LayoutPreference { 33 private SliceView mSliceView; 34 SlicePreference(Context context, AttributeSet attrs)35 public SlicePreference(Context context, AttributeSet attrs) { 36 super(context, attrs, R.attr.slicePreferenceStyle); 37 init(); 38 } 39 SlicePreference(Context context, AttributeSet attrs, int defStyleAttr)40 public SlicePreference(Context context, AttributeSet attrs, int defStyleAttr) { 41 super(context, attrs, defStyleAttr); 42 init(); 43 } 44 init()45 private void init() { 46 mSliceView = findViewById(R.id.slice_view); 47 mSliceView.setShowTitleItems(true); 48 mSliceView.setScrollable(false); 49 mSliceView.setVisibility(View.GONE); 50 } 51 onSliceUpdated(Slice slice)52 public void onSliceUpdated(Slice slice) { 53 if (slice == null) { 54 mSliceView.setVisibility(View.GONE); 55 } else { 56 mSliceView.setVisibility(View.VISIBLE); 57 } 58 mSliceView.onChanged(slice); 59 notifyChanged(); 60 } 61 } 62