content: /*
   * Copyright (C) 2015 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License
   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
   * or implied. See the License for the specific language governing permissions and limitations under
   * the License.
   */
  
  package androidx.leanback.leanbackshowcase.cards.presenters;
  
  import android.animation.ObjectAnimator;
  import android.content.Context;
  import android.graphics.drawable.Drawable;
  import androidx.leanback.leanbackshowcase.R;
  import androidx.leanback.widget.ImageCardView;
  import android.view.View;
  import android.widget.ImageView;
  
  /**
   * This Presenter will display cards which consists of a single icon which will be highlighted by a
   * surrounding circle when the card is focused. AndroidTV uses these cards for entering settings
   * menu.
   */
  public class IconCardInChannelPublishPresenter extends ImageCardViewPresenter {
      private static final int ANIMATION_DURATION = 200;
  
      /**
       * Use specific theme to render the setting card in current browse fragment
       */
      public IconCardInChannelPublishPresenter(Context context) {
          super(context, R.style.SettingIconCardTheme);
      }
  
      @Override
      protected ImageCardView onCreateView() {
          final ImageCardView imageCardView = super.onCreateView();
          final ImageView image = imageCardView.getMainImageView();
          image.setBackgroundResource(R.drawable.icon_focused);
          image.getBackground().setAlpha(0);
          imageCardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
              @Override
              public void onFocusChange(View v, boolean hasFocus) {
                  animateIconBackground(image.getBackground(), hasFocus);
              }
          });
          return imageCardView;
      }
  
      private void animateIconBackground(Drawable drawable, boolean hasFocus) {
          if (hasFocus) {
              ObjectAnimator.ofInt(drawable, "alpha", 0, 255).setDuration(ANIMATION_DURATION).start();
          } else {
              ObjectAnimator.ofInt(drawable, "alpha", 255, 0).setDuration(ANIMATION_DURATION).start();
          }
      }
  }
  