summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Copp <adamcopp@google.com>2013-06-27 14:20:51 +0100
committerAndrew Sapperstein <asapperstein@google.com>2013-07-18 16:57:19 +0000
commit7732839c0cd9d87954a07e166b52b93cabea1374 (patch)
tree92d11c351d4c10481422a5f995ae3dd2b6776b0e
parent0e3c85d4c62529beed821252355c4a61e21c6070 (diff)
downloadphotoviewer-7732839c0cd9d87954a07e166b52b93cabea1374.tar.gz
Clean up internet state intent receiver
Previously there were three things wrong here: 1. We never unregistered the intent receiver 2. The Intent receiver was being created even when there was no use for it. 3. The mConnected was not being set to false in some circumstances. This change addresses all three problems Change-Id: I6117080c434a7d1c99bcdf12787a66a9a6b2e055 (cherry picked from commit 4e0bb7ba9fad4bd479fa1ac2f3210f6dbc808ec3)
-rw-r--r--src/com/android/ex/photo/Intents.java19
-rw-r--r--src/com/android/ex/photo/fragments/PhotoViewFragment.java27
2 files changed, 38 insertions, 8 deletions
diff --git a/src/com/android/ex/photo/Intents.java b/src/com/android/ex/photo/Intents.java
index 35bf33f..611e3ae 100644
--- a/src/com/android/ex/photo/Intents.java
+++ b/src/com/android/ex/photo/Intents.java
@@ -36,6 +36,7 @@ public class Intents {
public static final String EXTRA_PROJECTION = "projection";
public static final String EXTRA_THUMBNAIL_URI = "thumbnail_uri";
public static final String EXTRA_MAX_INITIAL_SCALE = "max_scale";
+ public static final String EXTRA_WATCH_NETWORK = "watch_network";
/**
* Gets a photo view intent builder to display the photos from phone activity.
@@ -81,6 +82,10 @@ public class Intents {
private String mThumbnailUri;
/** The maximum scale to display images at before */
private Float mMaxInitialScale;
+ /**
+ * True if the PhotoViewFragments should watch for network changes to restart their loaders
+ */
+ private boolean mWatchNetwork;
private PhotoViewIntentBuilder(Context context, Class<?> cls) {
mIntent = new Intent(context, cls);
@@ -136,6 +141,16 @@ public class Intents {
return this;
}
+ /**
+ * Enable watching the network for connectivity changes.
+ *
+ * When a change is detected, bitmap loaders will be restarted if required.
+ */
+ public PhotoViewIntentBuilder watchNetworkConnectivityChanges() {
+ mWatchNetwork = true;
+ return this;
+ }
+
/** Build the intent */
public Intent build() {
mIntent.setAction(Intent.ACTION_VIEW);
@@ -175,6 +190,10 @@ public class Intents {
mIntent.putExtra(EXTRA_MAX_INITIAL_SCALE, mMaxInitialScale);
}
+ if (mWatchNetwork == true) {
+ mIntent.putExtra(EXTRA_WATCH_NETWORK, true);
+ }
+
return mIntent;
}
}
diff --git a/src/com/android/ex/photo/fragments/PhotoViewFragment.java b/src/com/android/ex/photo/fragments/PhotoViewFragment.java
index 998fbca..de549d3 100644
--- a/src/com/android/ex/photo/fragments/PhotoViewFragment.java
+++ b/src/com/android/ex/photo/fragments/PhotoViewFragment.java
@@ -106,6 +106,8 @@ public class PhotoViewFragment extends Fragment implements
protected PhotoViewCallbacks mCallback;
protected PhotoPagerAdapter mAdapter;
+ protected BroadcastReceiver mInternetStateReceiver;
+
protected PhotoView mPhotoView;
protected ImageView mPhotoPreviewImage;
protected TextView mEmptyText;
@@ -117,6 +119,11 @@ public class PhotoViewFragment extends Fragment implements
/** Whether or not the fragment should make the photo full-screen */
protected boolean mFullScreen;
+ /**
+ * True if the PhotoViewFragment should watch the network state in order to restart loaders.
+ */
+ protected boolean mWatchNetworkState;
+
/** Whether or not this fragment will only show the loading spinner */
protected boolean mOnlyShowSpinner;
@@ -163,11 +170,6 @@ public class PhotoViewFragment extends Fragment implements
if (mAdapter == null) {
throw new IllegalStateException("Callback reported null adapter");
}
-
- if (hasNetworkStatePermission()) {
- getActivity().registerReceiver(new InternetStateBroadcastReceiver(),
- new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
- }
// Don't call until we've setup the entire view
setViewVisibility();
}
@@ -221,6 +223,7 @@ public class PhotoViewFragment extends Fragment implements
if (mIntent != null) {
mResolvedPhotoUri = mIntent.getStringExtra(Intents.EXTRA_RESOLVED_PHOTO_URI);
mThumbnailUri = mIntent.getStringExtra(Intents.EXTRA_THUMBNAIL_URI);
+ mWatchNetworkState = mIntent.getBooleanExtra(Intents.EXTRA_WATCH_NETWORK, false);
}
}
@@ -260,7 +263,12 @@ public class PhotoViewFragment extends Fragment implements
mCallback.addScreenListener(mPosition, this);
mCallback.addCursorListener(this);
- if (hasNetworkStatePermission()) {
+ if (mWatchNetworkState) {
+ if (mInternetStateReceiver == null) {
+ mInternetStateReceiver = new InternetStateBroadcastReceiver();
+ }
+ getActivity().registerReceiver(mInternetStateReceiver,
+ new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
ConnectivityManager connectivityManager = (ConnectivityManager)
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
@@ -285,6 +293,9 @@ public class PhotoViewFragment extends Fragment implements
@Override
public void onPause() {
// Remove listeners
+ if (mWatchNetworkState) {
+ getActivity().unregisterReceiver(mInternetStateReceiver);
+ }
mCallback.removeCursorListener(this);
mCallback.removeScreenListener(mPosition);
resetPhotoView();
@@ -556,11 +567,11 @@ public class PhotoViewFragment extends Fragment implements
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
- if (activeNetInfo == null) {
+ if (activeNetInfo == null || !activeNetInfo.isConnected()) {
mConnected = false;
return;
}
- if (mConnected == false && activeNetInfo.isConnected() && !isPhotoBound()) {
+ if (mConnected == false && !isPhotoBound()) {
if (mThumbnailShown == false) {
getLoaderManager().restartLoader(LOADER_ID_THUMBNAIL, null,
PhotoViewFragment.this);