1/* 2 * Copyright (C) 2017 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 17package com.android.settings.widget; 18 19import android.os.Handler; 20import android.os.Looper; 21import android.view.View; 22import android.view.animation.Animation; 23import android.view.animation.AnimationUtils; 24 25/** 26 * A helper class that manages show/hide loading spinner. 27 */ 28public class LoadingViewController { 29 30 private static final long DELAY_SHOW_LOADING_CONTAINER_THRESHOLD_MS = 100L; 31 32 public final Handler mFgHandler; 33 public final View mLoadingView; 34 public final View mContentView; 35 36 public LoadingViewController(View loadingView, View contentView) { 37 mLoadingView = loadingView; 38 mContentView = contentView; 39 mFgHandler = new Handler(Looper.getMainLooper()); 40 } 41 42 private Runnable mShowLoadingContainerRunnable = new Runnable() { 43 public void run() { 44 handleLoadingContainer(false /* done */, false /* animate */); 45 } 46 }; 47 48 public void showContent(boolean animate) { 49 // Cancel any pending task to show the loading animation and show the list of 50 // apps directly. 51 mFgHandler.removeCallbacks(mShowLoadingContainerRunnable); 52 handleLoadingContainer(true /* show */, animate); 53 } 54 55 public void showLoadingViewDelayed() { 56 mFgHandler.postDelayed( 57 mShowLoadingContainerRunnable, DELAY_SHOW_LOADING_CONTAINER_THRESHOLD_MS); 58 } 59 60 public void handleLoadingContainer(boolean done, boolean animate) { 61 handleLoadingContainer(mLoadingView, mContentView, done, animate); 62 } 63 64 /** 65 * Show/hide loading view and content view. 66 * 67 * @param loading The loading spinner view 68 * @param content The content view 69 * @param done If true, content is set visible and loading is set invisible. 70 * @param animate Whether or not content/loading views should animate in/out. 71 */ 72 public static void handleLoadingContainer(View loading, View content, boolean done, 73 boolean animate) { 74 setViewShown(loading, !done, animate); 75 setViewShown(content, done, animate); 76 } 77 78 private static void setViewShown(final View view, boolean shown, boolean animate) { 79 if (animate) { 80 Animation animation = AnimationUtils.loadAnimation(view.getContext(), 81 shown ? android.R.anim.fade_in : android.R.anim.fade_out); 82 if (shown) { 83 view.setVisibility(View.VISIBLE); 84 } else { 85 animation.setAnimationListener(new Animation.AnimationListener() { 86 @Override 87 public void onAnimationStart(Animation animation) { 88 } 89 90 @Override 91 public void onAnimationRepeat(Animation animation) { 92 } 93 94 @Override 95 public void onAnimationEnd(Animation animation) { 96 view.setVisibility(View.INVISIBLE); 97 } 98 }); 99 } 100 view.startAnimation(animation); 101 } else { 102 view.clearAnimation(); 103 view.setVisibility(shown ? View.VISIBLE : View.INVISIBLE); 104 } 105 } 106} 107