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.graphics.drawable.Drawable;
21 
22 import androidx.fragment.app.Fragment;
23 import androidx.fragment.app.FragmentManager;
24 import androidx.fragment.app.FragmentPagerAdapter;
25 
26 /**
27  * Adapter containing all fragments used in the view pager
28  */
29 public class RadioPagerAdapter extends FragmentPagerAdapter {
30 
31     private static final int DEFAULT_PAGE_COUNT = 2;
32     private static final int[] TAB_LABELS =
33             new int[]{R.string.favorites_tab, R.string.tune_tab, R.string.browse_tab};
34     private static final int[] TAB_ICONS = new int[]{R.drawable.ic_star_filled,
35             R.drawable.ic_input_antenna, R.drawable.ic_list};
36 
37     private RadioController mRadioController;
38     private Context mContext;
39     private int mPageCount;
40     private Fragment mBrowseFragment;
41 
RadioPagerAdapter(Context context, FragmentManager fragmentManager, RadioController controller)42     public RadioPagerAdapter(Context context, FragmentManager fragmentManager,
43             RadioController controller) {
44         super(fragmentManager);
45         mRadioController = controller;
46         mContext = context;
47         mPageCount = DEFAULT_PAGE_COUNT;
48     }
49 
50     @Override
getItem(int i)51     public Fragment getItem(int i) {
52         switch (i) {
53             case 0:
54                 return FavoritesFragment.newInstance(mRadioController);
55             case 1:
56                 return ManualTunerFragment.newInstance(mRadioController);
57             case 2:
58                 return mBrowseFragment;
59         }
60         return null;
61     }
62 
63     @Override
getCount()64     public int getCount() {
65         return mPageCount;
66     }
67 
68     @Override
getPageTitle(int position)69     public CharSequence getPageTitle(int position) {
70         return mContext.getResources().getString(TAB_LABELS[position]);
71     }
72 
73     /**
74      * Returns drawable of the icon to use for the tab at position
75      */
getPageIcon(int position)76     public Drawable getPageIcon(int position) {
77         return mContext.getDrawable(TAB_ICONS[position]);
78     }
79 
80     /**
81      * @return true if browse tab is added to this adapter, false if tab already exists
82      */
addBrowseTab()83     public boolean addBrowseTab() {
84         if (mBrowseFragment == null) {
85             mPageCount++;
86             mBrowseFragment = BrowseFragment.newInstance(mRadioController);
87             notifyDataSetChanged();
88             return true;
89         }
90         return false;
91     }
92 }
93