Home | History | Annotate | Download | only in radio
      1 /*
      2  * Copyright (c) 2016, 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 package com.android.car.radio;
     17 
     18 import android.annotation.Nullable;
     19 import android.hardware.radio.ProgramSelector;
     20 import android.hardware.radio.RadioManager;
     21 import android.os.Bundle;
     22 import android.support.v4.app.Fragment;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 
     27 /**
     28  * A fragment that allows the user to manually input a radio station to tune to.
     29  */
     30 public class ManualTunerFragment extends Fragment implements
     31         ManualTunerController.ManualTunerClickListener {
     32     public static final String RADIO_BAND_ARG = "radio_band_arg";
     33 
     34     private static final int DEFAULT_RADIO_BAND = RadioManager.BAND_FM;
     35 
     36     private ManualTunerController mController;
     37     private ManualTunerCompletionListener mListener;
     38 
     39     /**
     40      * Interface for a class that will notified on completion of a manual tune.
     41      */
     42     public interface ManualTunerCompletionListener {
     43         /**
     44          * Called when the user has finished selected a radio station on the manual tuner. If the
     45          * user exits the manual tuner without a station being selected, then {@code null} will
     46          * be passed to this method.
     47          *
     48          * @param station The {@link ProgramSelector} that was selected or {@code null} if the user
     49          *                exits before selecting one.
     50          */
     51         void onStationSelected(@Nullable ProgramSelector sel);
     52     }
     53 
     54     @Override
     55     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     56             Bundle savedInstanceState) {
     57         View view = inflater.inflate(R.layout.manual_tuner, container, false);
     58 
     59         int radioBand = getArguments().getInt(RADIO_BAND_ARG, DEFAULT_RADIO_BAND);
     60 
     61         mController = new ManualTunerController(getContext(), view, radioBand);
     62         mController.setDoneButtonListener(this);
     63 
     64         return view;
     65     }
     66 
     67     /**
     68      * Sets the listener that will be notified upon completion of manual tuning functions.
     69      */
     70     public void setManualTunerCompletionListener(ManualTunerCompletionListener listener) {
     71         mListener = listener;
     72     }
     73 
     74     @Override
     75     public void onBack() {
     76        if (mListener != null) {
     77            mListener.onStationSelected(null);
     78        }
     79     }
     80 
     81     @Override
     82     public void onDone(ProgramSelector sel) {
     83         if (mListener != null) {
     84             mListener.onStationSelected(sel);
     85         }
     86     }
     87 
     88     /**
     89      * Creates a new {@link ManualTunerFragment} that is defaulted to the given radio band.
     90      */
     91     public static ManualTunerFragment newInstance(int radioBand) {
     92         ManualTunerFragment fragment = new ManualTunerFragment();
     93 
     94         Bundle args = new Bundle();
     95         args.putInt(RADIO_BAND_ARG, radioBand);
     96         fragment.setArguments(args);
     97 
     98         return fragment;
     99     }
    100 }
    101