1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package com.example.android.leanback;
15
16import android.app.Activity;
17import android.app.Fragment;
18import android.os.Bundle;
19import android.os.Handler;
20import android.view.Gravity;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24import android.widget.FrameLayout;
25import android.widget.ProgressBar;
26
27public class BrowseErrorActivity extends Activity
28{
29    private ErrorFragment mErrorFragment;
30    private SpinnerFragment mSpinnerFragment;
31
32    /** Called when the activity is first created. */
33    @Override
34    public void onCreate(Bundle savedInstanceState)
35    {
36        super.onCreate(savedInstanceState);
37        setContentView(R.layout.browse);
38
39        testError();
40    }
41
42    @Override
43    public void onAttachedToWindow() {
44        super.onAttachedToWindow();
45        BackgroundHelper.attach(this);
46    }
47
48    @Override
49    public void onStop() {
50        BackgroundHelper.release(this);
51        super.onStop();
52    }
53
54    private void testError() {
55        mErrorFragment = new ErrorFragment();
56        getFragmentManager().beginTransaction().add(R.id.main_frame, mErrorFragment).commit();
57
58        mSpinnerFragment = new SpinnerFragment();
59        getFragmentManager().beginTransaction().add(R.id.main_frame, mSpinnerFragment).commit();
60
61        Handler handler = new Handler();
62        handler.postDelayed(new Runnable() {
63            @Override
64            public void run() {
65                if (getFragmentManager().isDestroyed()) {
66                    return;
67                }
68                getFragmentManager().beginTransaction().remove(mSpinnerFragment).commit();
69                mErrorFragment.setErrorContent(getResources());
70            }
71        }, 3000);
72    }
73
74    static public class SpinnerFragment extends Fragment {
75        @Override
76        public View onCreateView(LayoutInflater inflater, ViewGroup container,
77                    Bundle savedInstanceState) {
78            ProgressBar progressBar = new ProgressBar(container.getContext());
79            if (container instanceof FrameLayout) {
80                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100, 100, Gravity.CENTER);
81                progressBar.setLayoutParams(layoutParams);
82            }
83            return progressBar;
84        }
85    }
86}
87