1 package com.android.car.media; 2 3 import android.os.Bundle; 4 import android.os.Handler; 5 import android.support.v4.app.Fragment; 6 import android.util.Log; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.ImageView; 11 import android.widget.ProgressBar; 12 import android.widget.TextView; 13 14 import com.android.car.media.common.MediaSource; 15 import com.android.car.media.widgets.ViewUtils; 16 17 /** 18 * Empty fragment to show while we are loading content 19 */ 20 public class EmptyFragment extends Fragment { 21 private ProgressBar mProgressBar; 22 private ImageView mErrorIcon; 23 private TextView mErrorMessage; 24 25 private int mProgressBarDelay; 26 private Handler mHandler = new Handler(); 27 private int mFadeDuration; 28 private MediaActivity.BrowseState mState = MediaActivity.BrowseState.EMPTY; 29 private MediaSource mMediaSource; 30 private Runnable mProgressIndicatorRunnable = new Runnable() { 31 @Override 32 public void run() { 33 ViewUtils.showViewAnimated(mProgressBar, mFadeDuration); 34 } 35 }; 36 @Override 37 public View onCreateView(LayoutInflater inflater, final ViewGroup container, 38 Bundle savedInstanceState) { 39 View view = inflater.inflate(R.layout.fragment_empty, container, false); 40 mProgressBar = view.findViewById(R.id.loading_spinner); 41 mProgressBarDelay = getContext().getResources() 42 .getInteger(R.integer.progress_indicator_delay); 43 mFadeDuration = getContext().getResources().getInteger( 44 R.integer.new_album_art_fade_in_duration); 45 mErrorIcon = view.findViewById(R.id.error_icon); 46 mErrorMessage = view.findViewById(R.id.error_message); 47 update(); 48 return view; 49 } 50 51 @Override 52 public void onPause() { 53 super.onPause(); 54 mHandler.removeCallbacks(mProgressIndicatorRunnable); 55 } 56 57 /** 58 * Updates the state of this fragment 59 * 60 * @param state browsing state to display 61 * @param mediaSource media source currently being browsed 62 */ 63 public void setState(MediaActivity.BrowseState state, MediaSource mediaSource) { 64 mHandler.removeCallbacks(mProgressIndicatorRunnable); 65 mMediaSource = mediaSource; 66 mState = state; 67 if (this.getView() != null) { 68 update(); 69 } 70 } 71 72 private void update() { 73 switch (mState) { 74 case LOADING: 75 // Display the indicator after a certain time, to avoid flashing the indicator 76 // constantly, even when performance is acceptable. 77 mHandler.postDelayed(mProgressIndicatorRunnable, mProgressBarDelay); 78 mErrorIcon.setVisibility(View.GONE); 79 mErrorMessage.setVisibility(View.GONE); 80 break; 81 case ERROR: 82 mProgressBar.setVisibility(View.GONE); 83 mErrorIcon.setVisibility(View.VISIBLE); 84 mErrorMessage.setVisibility(View.VISIBLE); 85 mErrorMessage.setText(getContext().getString( 86 R.string.cannot_connect_to_app, 87 mMediaSource != null 88 ? mMediaSource.getName() 89 : getContext().getString(R.string.unknown_media_provider_name))); 90 break; 91 case EMPTY: 92 mProgressBar.setVisibility(View.GONE); 93 mErrorIcon.setVisibility(View.GONE); 94 mErrorMessage.setVisibility(View.VISIBLE); 95 mErrorMessage.setText(getContext().getString(R.string.nothing_to_play)); 96 break; 97 default: 98 // Fail fast on any other state. 99 throw new IllegalStateException("Invalid state for this fragment: " + mState); 100 } 101 } 102 } 103