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.car.radio; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 25 import androidx.fragment.app.Fragment; 26 import androidx.recyclerview.widget.LinearLayoutManager; 27 import androidx.recyclerview.widget.RecyclerView; 28 29 import com.android.car.broadcastradio.support.Program; 30 import com.android.car.radio.storage.RadioStorage; 31 32 /** 33 * Fragment that shows a list of all the current favorite radio stations 34 */ 35 public class FavoritesFragment extends Fragment { 36 37 private RadioController mRadioController; 38 private BrowseAdapter mBrowseAdapter; 39 private RadioStorage mRadioStorage; 40 private RecyclerView mBrowseList; 41 42 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)43 public View onCreateView(LayoutInflater inflater, ViewGroup container, 44 Bundle savedInstanceState) { 45 View root = inflater.inflate(R.layout.browse_fragment, container, false); 46 return root; 47 } 48 49 @Override onViewCreated(View view, Bundle savedInstanceState)50 public void onViewCreated(View view, Bundle savedInstanceState) { 51 Context context = getContext(); 52 53 mRadioStorage = RadioStorage.getInstance(context); 54 mBrowseAdapter = new BrowseAdapter(this, mRadioController.getCurrentProgram(), 55 mRadioStorage.getFavorites()); 56 57 mBrowseAdapter.setOnItemClickListener(mRadioController::tune); 58 mBrowseAdapter.setOnItemFavoriteListener(this::handlePresetItemFavoriteChanged); 59 60 mBrowseList = view.findViewById(R.id.browse_list); 61 mBrowseList.setLayoutManager(new LinearLayoutManager(context)); 62 mBrowseList.setAdapter(mBrowseAdapter); 63 mBrowseList.setVerticalFadingEdgeEnabled(true); 64 mBrowseList.setFadingEdgeLength(getResources() 65 .getDimensionPixelSize(R.dimen.browse_list_fading_edge_length)); 66 } 67 68 @Override setUserVisibleHint(boolean isVisibleToUser)69 public void setUserVisibleHint(boolean isVisibleToUser) { 70 super.setUserVisibleHint(isVisibleToUser); 71 if (!isVisibleToUser && mBrowseAdapter != null) { 72 mBrowseAdapter.removeFormerFavorites(); 73 } 74 } 75 handlePresetItemFavoriteChanged(Program program, boolean saveAsFavorite)76 private void handlePresetItemFavoriteChanged(Program program, boolean saveAsFavorite) { 77 if (saveAsFavorite) { 78 mRadioStorage.addFavorite(program); 79 } else { 80 mRadioStorage.removeFavorite(program.getSelector()); 81 } 82 } 83 newInstance(RadioController radioController)84 static FavoritesFragment newInstance(RadioController radioController) { 85 FavoritesFragment fragment = new FavoritesFragment(); 86 fragment.mRadioController = radioController; 87 return fragment; 88 } 89 } 90