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