1 /* 2 * Copyright (C) 2015 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.deskclock.widget; 18 19 import android.transition.Fade; 20 import android.transition.Transition; 21 import android.transition.TransitionManager; 22 import android.transition.TransitionSet; 23 import android.view.View; 24 import android.view.ViewGroup; 25 26 import com.android.deskclock.Utils; 27 28 /** 29 * Controller that displays empty view and handles animation appropriately. 30 */ 31 public final class EmptyViewController { 32 33 private static final int ANIMATION_DURATION = 300; 34 private static final boolean USE_TRANSITION_FRAMEWORK = Utils.isLOrLater(); 35 36 private final Transition mEmptyViewTransition; 37 private final ViewGroup mMainLayout; 38 private final View mContentView; 39 private final View mEmptyView; 40 private boolean mIsEmpty; 41 42 /** 43 * Constructor of the controller. 44 * 45 * @param contentView The view that should be displayed when empty view is hidden. 46 * @param emptyView The view that should be displayed when main view is empty. 47 */ EmptyViewController(ViewGroup mainLayout, View contentView, View emptyView)48 public EmptyViewController(ViewGroup mainLayout, View contentView, View emptyView) { 49 mMainLayout = mainLayout; 50 mContentView = contentView; 51 mEmptyView = emptyView; 52 if (USE_TRANSITION_FRAMEWORK) { 53 mEmptyViewTransition = new TransitionSet() 54 .setOrdering(TransitionSet.ORDERING_SEQUENTIAL) 55 .addTarget(contentView) 56 .addTarget(emptyView) 57 .addTransition(new Fade(Fade.OUT)) 58 .addTransition(new Fade(Fade.IN)) 59 .setDuration(ANIMATION_DURATION); 60 } else { 61 mEmptyViewTransition = null; 62 } 63 } 64 65 /** 66 * Sets the state for the controller. If it's empty, it will display the empty view. 67 * 68 * @param isEmpty Whether or not the controller should transition into empty state. 69 */ setEmpty(boolean isEmpty)70 public void setEmpty(boolean isEmpty) { 71 if (mIsEmpty == isEmpty) { 72 return; 73 } 74 mIsEmpty = isEmpty; 75 // State changed, perform transition. 76 if (USE_TRANSITION_FRAMEWORK) { 77 TransitionManager.beginDelayedTransition(mMainLayout, mEmptyViewTransition); 78 } 79 mEmptyView.setVisibility(mIsEmpty ? View.VISIBLE : View.GONE); 80 mContentView.setVisibility(mIsEmpty ? View.GONE : View.VISIBLE); 81 } 82 } 83