Skip to content

Commit

Permalink
Changes to comparison app to take local images
Browse files Browse the repository at this point in the history
  • Loading branch information
lukkm authored and tyronen committed Jun 15, 2015
1 parent 96d74f3 commit 90be464
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 39 deletions.
1 change: 1 addition & 0 deletions samples/comparison/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.facebook.samples.comparison" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-sdk
android:minSdkVersion="9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@

package com.facebook.samples.comparison;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -41,6 +44,7 @@
import com.facebook.samples.comparison.urlsfetcher.ImageUrlsFetcher;
import com.facebook.samples.comparison.urlsfetcher.ImageUrlsRequestBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

Expand All @@ -56,29 +60,38 @@ public class MainActivity extends ActionBarActivity {
public static final int UIL_INDEX = 5;
public static final int VOLLEY_INDEX = 6;

// These need to be in sync with {@link R.array.image_sources}
public static final int NETWORK_INDEX = 1;
public static final int LOCAL_INDEX = 2;

private static final long STATS_CLOCK_INTERVAL_MS = 1000;
private static final int DEFAULT_MESSAGE_SIZE = 1024;
private static final int BYTES_IN_MEGABYTE = 1024 * 1024;

private static final String EXTRA_ALLOW_ANIMATIONS = "allow_animations";
private static final String EXTRA_USE_DRAWEE = "use_drawee";
private static final String EXTRA_CURRENT_ADAPTER_INDEX = "current_adapter_index";
private static final String EXTRA_CURRENT_SOURCE_ADAPTER_INDEX = "current_source_adapter_index";

private Handler mHandler;
private Runnable mStatsClockTickRunnable;

private TextView mStatsDisplay;
private Spinner mLoaderSelect;
private Spinner mSourceSelect;
private ListView mImageList;

private boolean mUseDrawee;
private boolean mAllowAnimations;
private int mCurrentAdapterIndex;
private int mCurrentLoaderAdapterIndex;
private int mCurrentSourceAdapterIndex;

private ImageListAdapter mCurrentAdapter;
private PerfListener mPerfListener;

private List<String> mImageUrls;
private List<String> mImageUrls = new ArrayList<>();

private boolean mUrlsLocal = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -89,11 +102,13 @@ protected void onCreate(Bundle savedInstanceState) {

mAllowAnimations = true;
mUseDrawee = true;
mCurrentAdapterIndex = 0;
mCurrentLoaderAdapterIndex = 0;
mCurrentSourceAdapterIndex = 0;
if (savedInstanceState != null) {
mAllowAnimations = savedInstanceState.getBoolean(EXTRA_ALLOW_ANIMATIONS);
mUseDrawee = savedInstanceState.getBoolean(EXTRA_USE_DRAWEE);
mCurrentAdapterIndex = savedInstanceState.getInt(EXTRA_CURRENT_ADAPTER_INDEX);
mCurrentLoaderAdapterIndex = savedInstanceState.getInt(EXTRA_CURRENT_ADAPTER_INDEX);
mCurrentSourceAdapterIndex = savedInstanceState.getInt(EXTRA_CURRENT_SOURCE_ADAPTER_INDEX);
}

mHandler = new Handler(Looper.getMainLooper());
Expand All @@ -114,21 +129,34 @@ public void run() {
mLoaderSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setAdapter(position);
setLoaderAdapter(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
mLoaderSelect.setSelection(mCurrentAdapterIndex);
mLoaderSelect.setSelection(mCurrentLoaderAdapterIndex);

mSourceSelect = (Spinner) findViewById(R.id.source_select);
mSourceSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setSourceAdapter(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
mSourceSelect.setSelection(mCurrentSourceAdapterIndex);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(EXTRA_ALLOW_ANIMATIONS, mAllowAnimations);
outState.putBoolean(EXTRA_USE_DRAWEE, mUseDrawee);
outState.putInt(EXTRA_CURRENT_ADAPTER_INDEX, mCurrentAdapterIndex);
outState.putInt(EXTRA_CURRENT_ADAPTER_INDEX, mCurrentLoaderAdapterIndex);
outState.putInt(EXTRA_CURRENT_SOURCE_ADAPTER_INDEX, mCurrentSourceAdapterIndex);
}

@Override
Expand Down Expand Up @@ -178,25 +206,30 @@ public void setAllowAnimations(boolean allowAnimations) {
mAllowAnimations = allowAnimations;
supportInvalidateOptionsMenu();
updateAdapter(null);
reloadUrls();
loadUrls();
}

@VisibleForTesting
public void setUseDrawee(boolean useDrawee) {
mUseDrawee = useDrawee;
supportInvalidateOptionsMenu();
setAdapter(mCurrentAdapterIndex);
setLoaderAdapter(mCurrentLoaderAdapterIndex);
setSourceAdapter(mCurrentSourceAdapterIndex);
}

private void setAdapter(int index) {
FLog.w(TAG, "onImageLoaderSelect: %d", index);
private void resetAdapter() {
if (mCurrentAdapter != null) {
mCurrentAdapter.shutDown();
mCurrentAdapter = null;
System.gc();
}
}

mCurrentAdapterIndex = index;
private void setLoaderAdapter(int index) {
FLog.v(TAG, "onImageLoaderSelect: %d", index);
resetAdapter();

mCurrentLoaderAdapterIndex = index;
mPerfListener = new PerfListener();
switch (index) {
case FRESCO_INDEX:
Expand All @@ -206,8 +239,8 @@ private void setAdapter(int index) {
R.id.image_list,
mPerfListener,
index == FRESCO_INDEX ?
ImagePipelineConfigFactory.getImagePipelineConfig(this) :
ImagePipelineConfigFactory.getOkHttpImagePipelineConfig(this));
ImagePipelineConfigFactory.getImagePipelineConfig(this) :
ImagePipelineConfigFactory.getOkHttpImagePipelineConfig(this));
break;
case GLIDE_INDEX:
mCurrentAdapter = new GlideAdapter(this, R.id.image_list, mPerfListener);
Expand All @@ -220,23 +253,47 @@ private void setAdapter(int index) {
break;
case VOLLEY_INDEX:
mCurrentAdapter = mUseDrawee ?
new VolleyDraweeAdapter(this, R.id.image_list, mPerfListener) :
new VolleyAdapter(this, R.id.image_list, mPerfListener);
new VolleyDraweeAdapter(this, R.id.image_list, mPerfListener) :
new VolleyAdapter(this, R.id.image_list, mPerfListener);
break;
default:
mCurrentAdapter = null;
return;
}

mImageList.setAdapter(mCurrentAdapter);
updateAdapter(mImageUrls);

if (mImageUrls != null && !mImageUrls.isEmpty()) {
updateAdapter(mImageUrls);
} else {
reloadUrls();
updateStats();
}

private void setSourceAdapter(int index) {
FLog.v(TAG, "onImageSourceSelect: %d", index);

mCurrentSourceAdapterIndex = index;
switch (index) {
case NETWORK_INDEX:
mUrlsLocal = false;
break;
case LOCAL_INDEX:
mUrlsLocal = true;
break;
default:
resetAdapter();
mImageUrls.clear();
return;
}

updateStats();
loadUrls();
setLoaderAdapter(mCurrentLoaderAdapterIndex);
}

private void loadUrls() {
if (mUrlsLocal) {
loadLocalUrls();
} else {
loadNetworkUrls();
}
}

private void scheduleNextStatsClockTick() {
Expand All @@ -247,29 +304,51 @@ private void cancelNextStatsClockTick() {
mHandler.removeCallbacks(mStatsClockTickRunnable);
}

private void reloadUrls() {
private void loadNetworkUrls() {
String url = "https://api.imgur.com/3/gallery/hot/viral/0.json";
ImageUrlsRequestBuilder builder = new ImageUrlsRequestBuilder(url)
.addImageFormat(
ImageFormat.JPEG,
ImageSize.LARGE_THUMBNAIL)
.addImageFormat(
ImageFormat.PNG,
ImageSize.LARGE_THUMBNAIL);
ImageFormat.PNG,
ImageSize.LARGE_THUMBNAIL);
if (mAllowAnimations) {
builder.addImageFormat(
ImageFormat.GIF,
ImageSize.ORIGINAL_IMAGE);
}
ImageUrlsFetcher.getImageUrls(
builder.build(),
new ImageUrlsFetcher.Callback() {
@Override
public void onFinish(List<String> result) {
mImageUrls = result;
updateAdapter(mImageUrls);
}
});
builder.build(),
new ImageUrlsFetcher.Callback() {
@Override
public void onFinish(List<String> result) {
// If the user changes to local images before the call comes back, then this should
// be ignored
if (!mUrlsLocal) {
mImageUrls = result;
updateAdapter(mImageUrls);
}
}
});
}

private void loadLocalUrls() {
Uri externalContentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.Media._ID};
Cursor cursor = getContentResolver().query(externalContentUri, projection, null, null, null);

mImageUrls.clear();

int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);

String imageId;
Uri imageUri;
while (cursor.moveToNext()) {
imageId = cursor.getString(columnIndex);
imageUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageId);
mImageUrls.add(imageUri.toString());
}
}

private void updateAdapter(List<String> urls) {
Expand Down
33 changes: 26 additions & 7 deletions samples/comparison/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,32 @@
android:layout_height="wrap_content"
android:typeface="monospace"/>

<Spinner
android:id="@+id/loader_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/image_loaders"
android:prompt="@string/select_image_loader"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp"
android:weightSum="1">

<Spinner
android:id="@+id/loader_select"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:entries="@array/image_loaders"
android:prompt="@string/select_image_loader"
/>

<Spinner
android:id="@+id/source_select"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:entries="@array/image_sources"
android:prompt="@string/select_image_loader"
/>

</LinearLayout>

<ListView
android:id="@+id/image_list"
Expand Down
8 changes: 8 additions & 0 deletions samples/comparison/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<string name="app_name">Fresco Comparison App</string>
<string name="action_settings">Settings</string>
<string name="select_image_loader">Select image loader</string>
<string name="select_image_source">Select image source</string>

<!-- If you change the order of the elements, update the indices in the MainActivity.java -->
<string-array name="image_loaders">
Expand All @@ -14,4 +15,11 @@
<item>Universal Image Loader</item>
<item>Volley</item>
</string-array>

<!-- If you change the order of the elements, update the indices in the MainActivity.java -->
<string-array name="image_sources">
<item>None</item>
<item>Network</item>
<item>Local</item>
</string-array>
</resources>

0 comments on commit 90be464

Please sign in to comment.