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     @Override
     55     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     56             Bundle savedInstanceState) {
     57         mRootView = inflater.inflate(R.layout.radio_fragment, container, false);
     58 
     59         mMainDisplay = mRootView.findViewById(R.id.radio_station_display_container);
     60 
     61         mRootView.findViewById(R.id.radio_presets_list).setOnClickListener(v -> {
     62             if (mPresetListListener != null) {
     63                 mPresetListListener.onPresetListClicked();
     64             }
     65         });
     66 
     67         return mRootView;
     68     }
     69 
     70     @Override
     71     public void fadeOutContent() {
     72         ObjectAnimator containerAlphaAnimator =
     73                 ObjectAnimator.ofFloat(mMainDisplay, View.ALPHA, 1f, 0f);
     74         containerAlphaAnimator.setInterpolator(sInterpolator);
     75         containerAlphaAnimator.setStartDelay(FADE_OUT_START_DELAY_MS);
     76         containerAlphaAnimator.setDuration(FADE_ANIM_TIME_MS);
     77         containerAlphaAnimator.start();
     78     }
     79 
     80     @Override
     81     public void fadeInContent() {
     82         ObjectAnimator containerAlphaAnimator =
     83                 ObjectAnimator.ofFloat(mMainDisplay, View.ALPHA, 0f, 1f);
     84         containerAlphaAnimator.setInterpolator(sInterpolator);
     85         containerAlphaAnimator.setDuration(FADE_ANIM_TIME_MS);
     86         containerAlphaAnimator.start();
     87     }
     88 
     89     @Override
     90     public void onStart() {
     91         super.onStart();
     92 
     93         mRadioController.initialize(mRootView);
     94         mRadioController.setShouldColorStatusBar(true);
     95 
     96         fadeInContent();
     97     }
     98 
     99     @Override
    100     public void onStop() {
    101         super.onStop();
    102         fadeOutContent();
    103     }
    104 
    105     @Override
    106     public void onDestroy() {
    107         mPresetListListener = null;
    108         super.onDestroy();
    109     }
    110 
    111     /**
    112      * Returns a new instance of the {@link MainRadioFragment}.
    113      *
    114      * @param radioController The {@link RadioController} that is responsible for updating the UI
    115      *                        of the returned fragment.
    116      */
    117     static MainRadioFragment newInstance(RadioController radioController,
    118             RadioPresetListClickListener clickListener) {
    119         MainRadioFragment fragment = new MainRadioFragment();
    120         fragment.mRadioController = radioController;
    121         fragment.mPresetListListener = clickListener;
    122 
    123         return fragment;
    124     }
    125 }
    126