• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.systemui.volume;
18 
19 import static android.app.slice.Slice.HINT_ERROR;
20 import static android.app.slice.SliceItem.FORMAT_SLICE;
21 
22 import android.content.Context;
23 import android.net.Uri;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 
28 import androidx.annotation.NonNull;
29 import androidx.lifecycle.LifecycleOwner;
30 import androidx.lifecycle.LiveData;
31 import androidx.recyclerview.widget.RecyclerView;
32 import androidx.slice.Slice;
33 import androidx.slice.SliceItem;
34 import androidx.slice.widget.SliceView;
35 
36 import com.android.systemui.R;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Map;
41 
42 /**
43  * RecyclerView adapter for Slices in Settings Panels.
44  */
45 public class VolumePanelSlicesAdapter extends
46         RecyclerView.Adapter<VolumePanelSlicesAdapter.SliceRowViewHolder> {
47 
48     private final List<LiveData<Slice>> mSliceLiveData;
49     private final LifecycleOwner mLifecycleOwner;
50     private SliceView.OnSliceActionListener mOnSliceActionListener;
51 
VolumePanelSlicesAdapter(LifecycleOwner lifecycleOwner, Map<Uri, LiveData<Slice>> sliceLiveData)52     public VolumePanelSlicesAdapter(LifecycleOwner lifecycleOwner,
53             Map<Uri, LiveData<Slice>> sliceLiveData) {
54         mLifecycleOwner = lifecycleOwner;
55         mSliceLiveData = new ArrayList<>(sliceLiveData.values());
56     }
57 
58     @NonNull
59     @Override
onCreateViewHolder(@onNull ViewGroup viewGroup, int viewType)60     public SliceRowViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
61         final Context context = viewGroup.getContext();
62         final LayoutInflater inflater = LayoutInflater.from(context);
63         View view = inflater.inflate(R.layout.volume_panel_slice_slider_row, viewGroup, false);
64         return new SliceRowViewHolder(view);
65     }
66 
67     @Override
onBindViewHolder(@onNull SliceRowViewHolder sliceRowViewHolder, int position)68     public void onBindViewHolder(@NonNull SliceRowViewHolder sliceRowViewHolder, int position) {
69         sliceRowViewHolder.onBind(mSliceLiveData.get(position), position);
70     }
71 
72     @Override
getItemCount()73     public int getItemCount() {
74         return mSliceLiveData.size();
75     }
76 
77     @Override
getItemViewType(int position)78     public int getItemViewType(int position) {
79         return position;
80     }
81 
setOnSliceActionListener(SliceView.OnSliceActionListener listener)82     void setOnSliceActionListener(SliceView.OnSliceActionListener listener) {
83         mOnSliceActionListener = listener;
84     }
85 
updateDataSet(ArrayList<LiveData<Slice>> list)86     void updateDataSet(ArrayList<LiveData<Slice>> list) {
87         mSliceLiveData.clear();
88         mSliceLiveData.addAll(list);
89         notifyDataSetChanged();
90     }
91 
92     /**
93      * ViewHolder for binding Slices to SliceViews.
94      */
95     public class SliceRowViewHolder extends RecyclerView.ViewHolder {
96 
97         private final SliceView mSliceView;
98 
SliceRowViewHolder(View view)99         public SliceRowViewHolder(View view) {
100             super(view);
101             mSliceView = view.findViewById(R.id.slice_view);
102             mSliceView.setMode(SliceView.MODE_LARGE);
103             mSliceView.setShowTitleItems(true);
104             mSliceView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
105             mSliceView.setOnSliceActionListener(mOnSliceActionListener);
106         }
107 
108         /**
109          * Called when the view is displayed.
110          */
onBind(LiveData<Slice> sliceLiveData, int position)111         public void onBind(LiveData<Slice> sliceLiveData, int position) {
112             sliceLiveData.observe(mLifecycleOwner, mSliceView);
113 
114             // Do not show the divider above media devices switcher slice per request
115             final Slice slice = sliceLiveData.getValue();
116 
117             // Hides slice which reports with error hint or not contain any slice sub-item.
118             if (slice == null || !isValidSlice(slice)) {
119                 mSliceView.setVisibility(View.GONE);
120             } else {
121                 mSliceView.setVisibility(View.VISIBLE);
122             }
123         }
124 
isValidSlice(Slice slice)125         private boolean isValidSlice(Slice slice) {
126             if (slice.getHints().contains(HINT_ERROR)) {
127                 return false;
128             }
129             for (SliceItem item : slice.getItems()) {
130                 if (item.getFormat().equals(FORMAT_SLICE)) {
131                     return true;
132                 }
133             }
134             return false;
135         }
136     }
137 }
138