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.os.Handler;
22import android.support.annotation.LayoutRes;
23import android.support.v4.widget.SwipeRefreshLayout;
24import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
25import android.view.Menu;
26import android.view.MenuInflater;
27import android.view.MenuItem;
28
29import com.example.android.supportv4.R;
30
31/**
32 * Example of using the SwipeRefreshLayout.
33 */
34abstract class BaseSwipeRefreshLayoutActivity extends Activity implements OnRefreshListener {
35
36    public static final String[] TITLES = {
37            "Henry IV (1)",
38            "Henry V",
39            "Henry VIII",
40            "Richard II",
41            "Richard III",
42            "Merchant of Venice",
43            "Othello",
44            "King Lear",
45            "Henry IV (1)",
46            "Henry V",
47            "Henry VIII",
48            "Richard II",
49            "Richard III",
50            "Merchant of Venice",
51            "Othello",
52            "King Lear",
53            "Henry IV (1)",
54            "Henry V",
55            "Henry VIII",
56            "Richard II",
57            "Richard III",
58            "Merchant of Venice",
59            "Othello",
60            "King Lear",
61            "Henry IV (1)",
62            "Henry V",
63            "Henry VIII",
64            "Richard II",
65            "Richard III",
66            "Merchant of Venice",
67            "Othello",
68            "King Lear"
69    };
70
71    // Try a SUPER quick refresh to make sure we don't get extra refreshes
72    // while the user's finger is still down.
73    private static final boolean SUPER_QUICK_REFRESH = false;
74
75    private SwipeRefreshLayout mSwipeRefreshWidget;
76
77    private final Handler mHandler = new Handler();
78
79    private final Runnable mRefreshDone = new Runnable() {
80        @Override
81        public void run() {
82            mSwipeRefreshWidget.setRefreshing(false);
83        }
84
85    };
86
87    @Override
88    public void onCreate(Bundle bundle) {
89        super.onCreate(bundle);
90        setContentView(getLayoutId());
91
92        mSwipeRefreshWidget = findViewById(R.id.swipe_refresh_widget);
93        mSwipeRefreshWidget.setColorSchemeResources(R.color.color1, R.color.color2, R.color.color3,
94                R.color.color4);
95        mSwipeRefreshWidget.setOnRefreshListener(this);
96    }
97
98    @LayoutRes
99    protected abstract int getLayoutId();
100
101    @Override
102    public void onRefresh() {
103        refresh();
104    }
105
106    @Override
107    public boolean onCreateOptionsMenu(Menu menu) {
108        MenuInflater inflater = getMenuInflater();
109        inflater.inflate(R.menu.swipe_refresh_menu, menu);
110        return true;
111    }
112
113    /**
114     * Click handler for the menu item to force a refresh.
115     */
116    @Override
117    public boolean onOptionsItemSelected(MenuItem item) {
118        final int id = item.getItemId();
119        switch(id) {
120            case R.id.force_refresh:
121                mSwipeRefreshWidget.setRefreshing(true);
122                refresh();
123                return true;
124        }
125        return false;
126    }
127
128    private void refresh() {
129        mHandler.removeCallbacks(mRefreshDone);
130        mHandler.postDelayed(mRefreshDone, 1000);
131    }
132}