1/*
2 * Copyright (C) 2013 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.example.android.supportv4.widget;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.support.v4.widget.ContentLoadingProgressBar;
22import android.view.View;
23import android.view.View.OnClickListener;
24import android.view.ViewTreeObserver;
25import android.widget.Button;
26import android.widget.TextView;
27
28import com.example.android.supportv4.R;
29
30/**
31 * Demonstrates how to use the ContentLoadingProgressBar. By default, the
32 * developer should start the ContentLoadingProgressBar with visibility of
33 * "gone" or "invisible". The ContentLoadingProgressBar will be shown after the
34 * default delay for at least a minimum time regardless of when the "hide"
35 * button is pressed.
36 */
37public class ContentLoadingProgressBarActivity extends Activity implements
38        OnClickListener, ViewTreeObserver.OnGlobalLayoutListener {
39
40    private Button mShowButton;
41    private Button mHideButton;
42    private ContentLoadingProgressBar mBar;
43    private long mShowTime;
44    private long mHideTime;
45    private TextView mShowText;
46    private TextView mShowTextDone;
47    private TextView mHideText;
48    private TextView mHideTextDone;
49    private int mLastVisibility;
50    private long mVisibilityChangedTime;
51
52    @Override
53    protected void onCreate(Bundle savedInstanceState) {
54        super.onCreate(savedInstanceState);
55        setContentView(R.layout.content_loading_progressbar);
56
57        mBar = (ContentLoadingProgressBar)findViewById(R.id.progressbar);
58        mShowButton = (Button)findViewById(R.id.show);
59        mShowButton.setOnClickListener(this);
60        mHideButton = (Button)findViewById(R.id.hide);
61        mHideButton.setOnClickListener(this);
62
63        mShowText = (TextView)findViewById(R.id.show_text);
64        mShowTextDone = (TextView)findViewById(R.id.show_text_done);
65        mHideText = (TextView)findViewById(R.id.hide_text);
66        mHideTextDone = (TextView)findViewById(R.id.hide_text_done);
67
68        mLastVisibility = mBar.getVisibility();
69
70        mBar.getViewTreeObserver().addOnGlobalLayoutListener(this);
71    }
72
73    @Override
74    public void onClick(View v) {
75        switch (v.getId()) {
76            case R.id.show:
77                mBar.show();
78                mShowTime = System.currentTimeMillis();
79                mShowText.setText("Show clicked at " + mShowTime);
80                break;
81            case R.id.hide:
82                mBar.hide();
83                mHideTime = System.currentTimeMillis();
84                mHideText.setText("Hide clicked at " + mHideTime);
85                break;
86        }
87    }
88
89    @Override
90    public void onGlobalLayout() {
91        final int visibility = mBar.getVisibility();
92
93        if (mLastVisibility != visibility) {
94            if (visibility == View.VISIBLE) {
95                mVisibilityChangedTime = System.currentTimeMillis();
96                mShowTextDone.setText("Shown at "
97                    + (mVisibilityChangedTime - mShowTime));
98            } else {
99                mHideTextDone.setText("Hidden after "
100                    + (System.currentTimeMillis() - mVisibilityChangedTime));
101            }
102            mLastVisibility = mBar.getVisibility();
103        }
104    }
105}
106