Home | History | Annotate | Download | only in demorenderer
      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 android.car.cluster.demorenderer;
     17 
     18 import android.animation.Animator;
     19 import android.animation.AnimatorListenerAdapter;
     20 import android.content.Context;
     21 import android.graphics.Bitmap;
     22 import android.util.Log;
     23 import android.view.View;
     24 import android.widget.FrameLayout;
     25 import android.widget.ImageView;
     26 import android.widget.TextView;
     27 
     28 /**
     29  * This class is responsible for drawing the whole instrument cluster.
     30  */
     31 public class DemoInstrumentClusterView extends FrameLayout {
     32 
     33     private final String TAG = DemoInstrumentClusterView.class.getSimpleName();
     34 
     35     private TextView mSpeedView;
     36     private TextView mEventTitleView;
     37     private TextView mDistanceView;
     38     private View mNavPanel;
     39     private TextView mMediaArtistView;
     40     private TextView mMediaAlbumView;
     41     private TextView mMediaTrackView;
     42     private ImageView mMediaImageView;
     43     private View mMediaPanel;
     44 
     45     private View mPhonePanel;
     46     private TextView mPhoneTitle;
     47     private TextView mPhoneSubtitle;
     48     private ImageView mPhoneImage;
     49 
     50     private final Integer mAnimationDurationMs;
     51 
     52     public DemoInstrumentClusterView(Context context) {
     53         super(context);
     54         mAnimationDurationMs = getResources().getInteger(android.R.integer.config_longAnimTime);
     55         init();
     56     }
     57 
     58     public void setSpeed(String speed) {
     59         Log.d(TAG, "setSpeed, meterPerSecond: " + speed);
     60         mSpeedView.setText(speed);
     61     }
     62 
     63     public void showNavigation() {
     64         Log.d(TAG, "showNavigation");
     65         mEventTitleView.setText("");
     66         mDistanceView.setText("");
     67         mNavPanel.setVisibility(VISIBLE);
     68     }
     69 
     70     public void hideNavigation() {
     71         Log.d(TAG, "hideNavigation");
     72         mNavPanel.setVisibility(INVISIBLE);
     73     }
     74 
     75     public void setNextTurn(Bitmap image, String title) {
     76         Log.d(TAG, "setNextTurn, image: " + image + ", title: " + title);
     77         mEventTitleView.setText(title);
     78     }
     79 
     80     public void setNextTurnDistance(String distance) {
     81         Log.d(TAG, "setNextTurnDistance, distance: " + distance);
     82         mDistanceView.setText(distance);
     83     }
     84 
     85     public void setMediaData(final CharSequence artist, final CharSequence album,
     86             final CharSequence track, final Bitmap image) {
     87         Log.d(TAG, "setMediaData" + " artist = " + artist + ", album: " + album + ", track: " +
     88                 track + ", bitmap: " + image);
     89 
     90         mMediaArtistView.setText(artist);
     91         mMediaAlbumView.setText(album);
     92         mMediaTrackView.setText(track);
     93         mMediaImageView.setImageBitmap(image);
     94     }
     95 
     96     private void showAnimated(final View view) {
     97         if (view.getVisibility() == VISIBLE && view.getAlpha() > 0) {
     98             return;
     99         }
    100         view.setAlpha(0);
    101         view.setVisibility(VISIBLE);
    102         view.animate()
    103                 .alpha(1f)
    104                 .setDuration(mAnimationDurationMs)
    105                 .setListener(null);
    106     }
    107 
    108     private void hideAnimated(final View view) {
    109         if (view.getVisibility() == GONE) {
    110             return;
    111         }
    112         view.animate()
    113                 .alpha(0f)
    114                 .setDuration(mAnimationDurationMs)
    115                 .setListener(new AnimatorListenerAdapter() {
    116                     @Override
    117                     public void onAnimationEnd(Animator animation) {
    118                         view.setVisibility(GONE);
    119                     }
    120                 });
    121     }
    122 
    123     public void showMedia() {
    124         Log.d(TAG, "showMedia");
    125         showAnimated(mMediaPanel);
    126     }
    127 
    128     public void hideMedia() {
    129         Log.d(TAG, "hideMedia");
    130         hideAnimated(mMediaPanel);
    131     }
    132 
    133     public void showPhone() {
    134         Log.d(TAG, "showPhone");
    135         mPhoneSubtitle.setText("");
    136         mPhoneImage.setImageResource(0); // To clear previous contact photo (if any).
    137         mPhoneTitle.setText("");
    138         showAnimated(mPhonePanel);
    139     }
    140 
    141     public void hidePhone() {
    142         Log.d(TAG, "hidePhone");
    143         hideAnimated(mPhonePanel);
    144     }
    145 
    146     public void setPhoneTitle(String number) {
    147         Log.d(TAG, "setPhoneTitle, number: " + number);
    148         mPhoneTitle.setText(number);
    149     }
    150 
    151     public void setPhoneSubtitle(String contact) {
    152         Log.d(TAG, "setPhoneContact, contact: " + contact);
    153         mPhoneSubtitle.setText(contact);
    154     }
    155 
    156     public void setPhoneImage(Bitmap photo) {
    157         Log.d(TAG, "setPhoneImage, photo: " + photo);
    158         mPhoneImage.setImageBitmap(photo);
    159     }
    160 
    161     private void init() {
    162         Log.d(TAG, "init");
    163         View rootView = inflate(getContext(), R.layout.instrument_cluster, null);
    164         mSpeedView = (TextView) rootView.findViewById(R.id.speed);
    165         mEventTitleView = (TextView) rootView.findViewById(R.id.nav_event_title);
    166         mDistanceView = (TextView) rootView.findViewById(R.id.nav_distance);
    167         mNavPanel = rootView.findViewById(R.id.nav_layout);
    168 
    169         mMediaPanel = rootView.findViewById(R.id.media_layout);
    170         mMediaArtistView = (TextView) rootView.findViewById(R.id.media_artist);
    171         mMediaAlbumView = (TextView) rootView.findViewById(R.id.media_album);
    172         mMediaTrackView = (TextView) rootView.findViewById(R.id.media_track);
    173         mMediaImageView = (ImageView) rootView.findViewById(R.id.media_image);
    174 
    175         mPhonePanel = rootView.findViewById(R.id.phone_layout);
    176         mPhoneImage = (ImageView) rootView.findViewById(R.id.phone_contact_photo);
    177         mPhoneSubtitle = (TextView) rootView.findViewById(R.id.phone_subtitle);
    178         mPhoneTitle = (TextView) rootView.findViewById(R.id.phone_title);
    179 
    180         setSpeed("0");
    181 
    182         addView(rootView);
    183     }
    184 }
    185