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 static com.android.car.ui.core.CarUi.requireInsets;
20 
21 import android.hardware.radio.RadioManager.ProgramInfo;
22 import android.os.Bundle;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 
27 import androidx.fragment.app.Fragment;
28 
29 import com.android.car.radio.bands.ProgramType;
30 import com.android.car.radio.util.Log;
31 import com.android.car.ui.baselayout.Insets;
32 import com.android.car.ui.baselayout.InsetsChangedListener;
33 
34 /**
35  * Fragment that allows tuning to a specific frequency using a keypad
36  */
37 public class ManualTunerFragment extends Fragment implements InsetsChangedListener {
38     private static final String TAG = "BcRadioApp.TunFrg";
39 
40     private ManualTunerController mController;
41     private RadioController mRadioController;
42 
43     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)44     public View onCreateView(LayoutInflater inflater, ViewGroup container,
45             Bundle savedInstanceState) {
46         View view = inflater.inflate(R.layout.tuner_fragment, container, false);
47         mController = new ManualTunerController(getContext(), view,
48                 mRadioController.getRegionConfig(), mRadioController::tune);
49 
50         return view;
51     }
52 
53     @Override
setUserVisibleHint(boolean isVisibleToUser)54     public void setUserVisibleHint(boolean isVisibleToUser) {
55         super.setUserVisibleHint(isVisibleToUser);
56 
57         if (!isVisibleToUser) return;
58 
59         ProgramInfo current = mRadioController.getCurrentProgram().getValue();
60         if (current != null) {
61             mController.switchProgramType(ProgramType.fromSelector(current.getSelector()));
62         }
63 
64         try {
65             mRadioController.setSkipMode(SkipMode.TUNE);
66         } catch (IllegalStateException e) {
67             Log.e(TAG, "Can't set skip mode", e);
68         }
69     }
70 
71     @Override
onCarUiInsetsChanged(Insets insets)72     public void onCarUiInsetsChanged(Insets insets) {
73         View view = requireView();
74         view.setPadding(insets.getLeft(), insets.getTop(),
75                 insets.getRight(), insets.getBottom());
76     }
77 
78     @Override
onStart()79     public void onStart() {
80         super.onStart();
81 
82         // This is needed to apply the inset changes that happened before this fragment was visible
83         onCarUiInsetsChanged(requireInsets(getActivity()));
84     }
85 
newInstance(RadioController radioController)86     static ManualTunerFragment newInstance(RadioController radioController) {
87         ManualTunerFragment fragment = new ManualTunerFragment();
88         fragment.mRadioController = radioController;
89         return fragment;
90     }
91 }
92