OSDN Git Service

445887f62ba7d89b24e6ca2c22fd24d170f87ba7
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / widgets / LoadingEmptyContainer.java
1 /*
2 * Copyright (C) 2014 The CyanogenMod 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 org.lineageos.eleven.widgets;
17
18 import android.content.Context;
19 import android.os.Handler;
20 import android.util.AttributeSet;
21 import android.view.View;
22 import android.widget.FrameLayout;
23
24 import org.lineageos.eleven.R;
25
26 /**
27  * This class is the default empty state view for most listviews/fragments
28  * It allows the ability to set a main text, a main highlight text and a secondary text
29  * By default this container has some strings loaded, but other classes can call the apis to change
30  * the text
31  */
32 public class LoadingEmptyContainer extends FrameLayout {
33     private static final int LOADING_DELAY = 300;
34
35     private Handler mHandler;
36     private Runnable mShowLoadingRunnable;
37
38     public LoadingEmptyContainer(Context context, AttributeSet attrs) {
39         super(context, attrs);
40
41         mHandler = new Handler();
42         mShowLoadingRunnable = new Runnable() {
43             @Override
44             public void run() {
45                 findViewById(R.id.progressbar).setVisibility(View.VISIBLE);
46                 getNoResultsContainer().setVisibility(View.INVISIBLE);
47             }
48         };
49     }
50
51     @Override
52     protected void onFinishInflate() {
53         super.onFinishInflate();
54
55         hideAll();
56     }
57
58     public void hideAll() {
59         findViewById(R.id.progressbar).setVisibility(View.INVISIBLE);
60         getNoResultsContainer().setVisibility(View.INVISIBLE);
61     }
62
63     public void showLoading() {
64         hideAll();
65
66         if (!mHandler.hasCallbacks(mShowLoadingRunnable)) {
67             mHandler.postDelayed(mShowLoadingRunnable, LOADING_DELAY);
68         }
69     }
70
71     public void showNoResults() {
72         mHandler.removeCallbacks(mShowLoadingRunnable);
73
74         findViewById(R.id.progressbar).setVisibility(View.INVISIBLE);
75         getNoResultsContainer().setVisibility(View.VISIBLE);
76     }
77
78     public NoResultsContainer getNoResultsContainer() {
79         return (NoResultsContainer)findViewById(R.id.no_results_container);
80     }
81 }