diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/SwipeToRefreshHelper.java b/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/SwipeToRefreshHelper.java index 9a1a4c74f53d..7488c2c85c78 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/SwipeToRefreshHelper.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/SwipeToRefreshHelper.java @@ -1,36 +1,17 @@ package org.wordpress.android.util.ptr; import android.app.Activity; -import android.content.BroadcastReceiver; import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; import android.content.res.TypedArray; -import android.preference.PreferenceManager; -import android.support.v4.content.LocalBroadcastManager; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; import android.util.TypedValue; import org.wordpress.android.util.R; -import org.wordpress.android.util.ToastUtils; -import org.wordpress.android.util.ToastUtils.Duration; - -import java.lang.ref.WeakReference; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; public class SwipeToRefreshHelper implements OnRefreshListener { - public static final String BROADCAST_ACTION_REFRESH_MENU_PRESSED = "REFRESH_MENU_PRESSED"; - private static final String REFRESH_BUTTON_HIT_COUNT = "REFRESH_BUTTON_HIT_COUNT"; - private static final Set TOAST_FREQUENCY = new HashSet(Arrays.asList(1, 5, 10, 20, 40, 80, 160, - 320, 640)); private SwipeRefreshLayout mSwipeRefreshLayout; private RefreshListener mRefreshListener; - private WeakReference mActivityRef; public interface RefreshListener { public void onRefreshStarted(); @@ -41,7 +22,6 @@ public SwipeToRefreshHelper(Activity activity, SwipeRefreshLayout swipeRefreshLa } public void init(Activity activity, SwipeRefreshLayout swipeRefreshLayout, RefreshListener listener) { - mActivityRef = new WeakReference(activity); mRefreshListener = listener; mSwipeRefreshLayout = swipeRefreshLayout; mSwipeRefreshLayout.setOnRefreshListener(this); @@ -69,62 +49,10 @@ public void setEnabled(boolean enabled) { mSwipeRefreshLayout.setEnabled(enabled); } - public void refreshAction() { - Activity activity = mActivityRef.get(); - if (activity == null) { - return; - } - setRefreshing(true); - mRefreshListener.onRefreshStarted(); - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); - int refreshHits = preferences.getInt(REFRESH_BUTTON_HIT_COUNT, 0); - refreshHits += 1; - if (TOAST_FREQUENCY.contains(refreshHits)) { - ToastUtils.showToast(activity, R.string.ptr_tip_message, Duration.LONG); - } - Editor editor = preferences.edit(); - editor.putInt(REFRESH_BUTTON_HIT_COUNT, refreshHits); - editor.apply(); - } - - public void registerReceiver(Context context) { - if (context == null) { - return; - } - IntentFilter filter = new IntentFilter(); - filter.addAction(BROADCAST_ACTION_REFRESH_MENU_PRESSED); - LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); - lbm.registerReceiver(mReceiver, filter); - } - - public void unregisterReceiver(Context context) { - if (context == null) { - return; - } - try { - LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); - lbm.unregisterReceiver(mReceiver); - } catch (IllegalArgumentException e) { - // exception occurs if receiver already unregistered (safe to ignore) - } - } - public static TypedArray obtainStyledAttrsFromThemeAttr(Context context, int themeAttr, int[] styleAttrs) { TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(themeAttr, outValue, true); int styleResId = outValue.resourceId; return context.obtainStyledAttributes(styleResId, styleAttrs); } - - private final BroadcastReceiver mReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - if (intent == null || intent.getAction() == null) { - return; - } - if (intent.getAction().equals(BROADCAST_ACTION_REFRESH_MENU_PRESSED)) { - refreshAction(); - } - } - }; }