11package org .wordpress .android .util .ptr ;
22
33import android .app .Activity ;
4- import android .content .BroadcastReceiver ;
54import android .content .Context ;
6- import android .content .Intent ;
7- import android .content .IntentFilter ;
8- import android .content .SharedPreferences ;
9- import android .content .SharedPreferences .Editor ;
105import android .content .res .TypedArray ;
11- import android .preference .PreferenceManager ;
12- import android .support .v4 .content .LocalBroadcastManager ;
136import android .support .v4 .widget .SwipeRefreshLayout ;
147import android .support .v4 .widget .SwipeRefreshLayout .OnRefreshListener ;
158import android .util .TypedValue ;
169
1710import org .wordpress .android .util .R ;
18- import org .wordpress .android .util .ToastUtils ;
19- import org .wordpress .android .util .ToastUtils .Duration ;
20-
21- import java .lang .ref .WeakReference ;
22- import java .util .Arrays ;
23- import java .util .HashSet ;
24- import java .util .Set ;
2511
2612public class SwipeToRefreshHelper implements OnRefreshListener {
27- public static final String BROADCAST_ACTION_REFRESH_MENU_PRESSED = "REFRESH_MENU_PRESSED" ;
28- private static final String REFRESH_BUTTON_HIT_COUNT = "REFRESH_BUTTON_HIT_COUNT" ;
29- private static final Set <Integer > TOAST_FREQUENCY = new HashSet <Integer >(Arrays .asList (1 , 5 , 10 , 20 , 40 , 80 , 160 ,
30- 320 , 640 ));
3113 private SwipeRefreshLayout mSwipeRefreshLayout ;
3214 private RefreshListener mRefreshListener ;
33- private WeakReference <Activity > mActivityRef ;
3415
3516 public interface RefreshListener {
3617 public void onRefreshStarted ();
@@ -41,7 +22,6 @@ public SwipeToRefreshHelper(Activity activity, SwipeRefreshLayout swipeRefreshLa
4122 }
4223
4324 public void init (Activity activity , SwipeRefreshLayout swipeRefreshLayout , RefreshListener listener ) {
44- mActivityRef = new WeakReference <Activity >(activity );
4525 mRefreshListener = listener ;
4626 mSwipeRefreshLayout = swipeRefreshLayout ;
4727 mSwipeRefreshLayout .setOnRefreshListener (this );
@@ -69,62 +49,10 @@ public void setEnabled(boolean enabled) {
6949 mSwipeRefreshLayout .setEnabled (enabled );
7050 }
7151
72- public void refreshAction () {
73- Activity activity = mActivityRef .get ();
74- if (activity == null ) {
75- return ;
76- }
77- setRefreshing (true );
78- mRefreshListener .onRefreshStarted ();
79- SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences (activity );
80- int refreshHits = preferences .getInt (REFRESH_BUTTON_HIT_COUNT , 0 );
81- refreshHits += 1 ;
82- if (TOAST_FREQUENCY .contains (refreshHits )) {
83- ToastUtils .showToast (activity , R .string .ptr_tip_message , Duration .LONG );
84- }
85- Editor editor = preferences .edit ();
86- editor .putInt (REFRESH_BUTTON_HIT_COUNT , refreshHits );
87- editor .apply ();
88- }
89-
90- public void registerReceiver (Context context ) {
91- if (context == null ) {
92- return ;
93- }
94- IntentFilter filter = new IntentFilter ();
95- filter .addAction (BROADCAST_ACTION_REFRESH_MENU_PRESSED );
96- LocalBroadcastManager lbm = LocalBroadcastManager .getInstance (context );
97- lbm .registerReceiver (mReceiver , filter );
98- }
99-
100- public void unregisterReceiver (Context context ) {
101- if (context == null ) {
102- return ;
103- }
104- try {
105- LocalBroadcastManager lbm = LocalBroadcastManager .getInstance (context );
106- lbm .unregisterReceiver (mReceiver );
107- } catch (IllegalArgumentException e ) {
108- // exception occurs if receiver already unregistered (safe to ignore)
109- }
110- }
111-
11252 public static TypedArray obtainStyledAttrsFromThemeAttr (Context context , int themeAttr , int [] styleAttrs ) {
11353 TypedValue outValue = new TypedValue ();
11454 context .getTheme ().resolveAttribute (themeAttr , outValue , true );
11555 int styleResId = outValue .resourceId ;
11656 return context .obtainStyledAttributes (styleResId , styleAttrs );
11757 }
118-
119- private final BroadcastReceiver mReceiver = new BroadcastReceiver () {
120- @ Override
121- public void onReceive (Context context , Intent intent ) {
122- if (intent == null || intent .getAction () == null ) {
123- return ;
124- }
125- if (intent .getAction ().equals (BROADCAST_ACTION_REFRESH_MENU_PRESSED )) {
126- refreshAction ();
127- }
128- }
129- };
13058}
0 commit comments