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 
     17 package com.android.car.radio;
     18 
     19 import android.animation.ObjectAnimator;
     20 import android.os.Bundle;
     21 import android.support.v4.app.Fragment;
     22 import android.support.v4.view.animation.FastOutSlowInInterpolator;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 
     27 /**
     28  * A fragment that functions as the main display of the information relating to the current radio
     29  * station. It also displays controls that allows the user to switch to different radio stations.
     30  */
     31 public class MainRadioFragment extends Fragment implements FragmentWithFade {
     32     private static final FastOutSlowInInterpolator sInterpolator = new FastOutSlowInInterpolator();
     33     private static final int FADE_OUT_START_DELAY_MS = 150;
     34     private static final int FADE_ANIM_TIME_MS = 100;
     35 
     36     private RadioController mRadioController;
     37     private RadioPresetListClickListener mPresetListListener;
     38 
     39     private View mRootView;
     40     private View mMainDisplay;
     41 
     42     /**
     43      * Interface for a class that will be notified when the button to open the list of the user's
     44      * favorite radio stations has been clicked.
     45      */
     46     public interface RadioPresetListClickListener {
     47         /**
     48          * Method that will be called when the preset list button has been clicked. Clicking this
     49          * button should open a display of the user's presets.
     50          */
     51         void onPresetListClicked();
     52     }
     53 
     54     /**
     55      * Sets the {@link RadioController} that is responsible for updating the UI of this fragment
     56      * with the information of the current radio station.
     57      */
     58     private void setRadioController(RadioController radioController) {
     59         mRadioController = radioController;
     60     }
     61 
     62     /**
     63      * Sets the listener that will be notified when the button to start the display of the user's
     64      * presets has been clicked.
     65      */
     66     public void setPresetListClickListener(RadioPresetListClickListener starter) {
     67         mPresetListListener = starter;
     68     }
     69 
     70     @Override
     71     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     72             Bundle savedInstanceState) {
     73         mRootView = inflater.inflate(R.layout.radio_fragment, container, false);
     74 
     75         mMainDisplay = mRootView.findViewById(R.id.radio_station_display_container);
     76 
     77         mRootView.findViewById(R.id.radio_presets_list).setOnClickListener(v -> {
     78             if (mPresetListListener != null) {
     79                 mPresetListListener.onPresetListClicked();
     80             }
     81         });
     82 
     83         return mRootView;
     84     }
     85 
     86     @Override
     87     public void fadeOutContent() {
     88         ObjectAnimator containerAlphaAnimator =
     89                 ObjectAnimator.ofFloat(mMainDisplay, View.ALPHA, 1f, 0f);
     90         containerAlphaAnimator.setInterpolator(sInterpolator);
     91         containerAlphaAnimator.setStartDelay(FADE_OUT_START_DELAY_MS);
     92         containerAlphaAnimator.setDuration(FADE_ANIM_TIME_MS);
     93         containerAlphaAnimator.start();
     94     }
     95 
     96     @Override
     97     public void fadeInContent() {
     98         ObjectAnimator containerAlphaAnimator =
     99                 ObjectAnimator.ofFloat(mMainDisplay, View.ALPHA, 0f, 1f);
    100         containerAlphaAnimator.setInterpolator(sInterpolator);
    101         containerAlphaAnimator.setDuration(FADE_ANIM_TIME_MS);
    102         containerAlphaAnimator.start();
    103     }
    104 
    105     @Override
    106     public void onStart() {
    107         super.onStart();
    108 
    109         mRadioController.initialize(mRootView);
    110         mRadioController.setShouldColorStatusBar(true);
    111 
    112         fadeInContent();
    113     }
    114 
    115     @Override
    116     public void onStop() {
    117         super.onStop();
    118         fadeOutContent();
    119     }
    120 
    121     @Override
    122     public void onDestroy() {
    123         mPresetListListener = null;
    124         super.onDestroy();
    125     }
    126 
    127     /**
    128      * Returns a new instance of the {@link MainRadioFragment}.
    129      *
    130      * @param radioController The {@link RadioController} that is responsible for updating the UI
    131      *                        of the returned fragment.
    132      */
    133     public static MainRadioFragment newInstance(RadioController radioController) {
    134         MainRadioFragment fragment = new MainRadioFragment();
    135         fragment.setRadioController(radioController);
    136 
    137         return fragment;
    138     }
    139 }
    140