diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 58a553a..e270537 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,5 +1,9 @@ + + + + diff --git a/assets/faces.txt b/assets/faces.txt new file mode 100644 index 0000000..53f19bf --- /dev/null +++ b/assets/faces.txt @@ -0,0 +1,168 @@ +{ + "people":[ + { + "gravatar":"http://www.gravatar.com/avatar/feb86e26302b2efd0f937afa72a16fc6?d=monsterid&s=240", + "id":"0" + }, + { + "gravatar":"http://www.gravatar.com/avatar/487bb9d69c8795b5dd1e67332cfb8221?d=monsterid&s=240", + "id":"1" + }, + { + "gravatar":"http://www.gravatar.com/avatar/f31e2458313b36730a066ec703e51b11?d=monsterid&s=240", + "id":"2" + }, + { + "gravatar":"http://www.gravatar.com/avatar/7f8727786f6bb1a0c352acc4e4419a83?d=monsterid&s=240", + "id":"3" + }, + { + "gravatar":"http://www.gravatar.com/avatar/2dc3eab78ab131cc3a8acbc972c92b73?d=monsterid&s=240", + "id":"4" + }, + { + "gravatar":"http://www.gravatar.com/avatar/705da853dcc90e5aca80b4d085c1d214?d=monsterid&s=240", + "id":"5" + }, + { + "gravatar":"http://www.gravatar.com/avatar/28be0f28848e9b280845436bf3fad6ca?d=monsterid&s=240", + "id":"6" + }, + { + "gravatar":"http://www.gravatar.com/avatar/1c364f4cad4f61aa08dc8f112931e39f?d=monsterid&s=240", + "id":"7" + }, + { + "gravatar":"http://www.gravatar.com/avatar/bb03b2dafcda985bbeb52a4f446cc754?d=monsterid&s=240", + "id":"8" + }, + { + "gravatar":"http://www.gravatar.com/avatar/ec43363ba46bdb76aae3999edf692871?d=monsterid&s=240", + "id":"9" + }, + { + "gravatar":"http://www.gravatar.com/avatar/e32aee082c585f111fe3405d470112b0?d=monsterid&s=240", + "id":"10" + }, + { + "gravatar":"http://www.gravatar.com/avatar/c770015076fd08e56be5cfd4ca387298?d=monsterid&s=240", + "id":"11" + }, + { + "gravatar":"http://www.gravatar.com/avatar/53abc38c639a22f80174261df7f35f0d?d=monsterid&s=240", + "id":"12" + }, + { + "gravatar":"http://www.gravatar.com/avatar/cd68c5cafec543474541a2211dc7444f?d=monsterid&s=240", + "id":"13" + }, + { + "gravatar":"http://www.gravatar.com/avatar/2b7031546b140aa0612e4d2ce8f922c0?d=monsterid&s=240", + "id":"14" + }, + { + "gravatar":"http://www.gravatar.com/avatar/bcb735972933b10b1eb269886c7c799a?d=monsterid&s=240", + "id":"15" + }, + { + "gravatar":"http://www.gravatar.com/avatar/ab7fa42a14263894835ba1e5be031111?d=monsterid&s=240", + "id":"16" + }, + { + "gravatar":"http://www.gravatar.com/avatar/35287768ccb0b3c68067a511c4c3503f?d=monsterid&s=240", + "id":"17" + }, + { + "gravatar":"http://www.gravatar.com/avatar/45bba01ac921f96e49059da127800d6d?d=monsterid&s=240", + "id":"18" + }, + { + "gravatar":"http://www.gravatar.com/avatar/289040ff9f5916ba6d88c660334660ec?d=monsterid&s=240", + "id":"19" + }, + { + "gravatar":"http://www.gravatar.com/avatar/0808d58e098c64a5e9a1a411bce8d0d8?d=monsterid&s=240", + "id":"20" + }, + { + "gravatar":"http://www.gravatar.com/avatar/81801d4f62de321a0bd6903fe9459a5a?d=monsterid&s=240", + "id":"21" + }, + { + "gravatar":"http://www.gravatar.com/avatar/0cbc8aace3690ac2288754bfb12ef43c?d=monsterid&s=240", + "id":"22" + }, + { + "gravatar":"http://www.gravatar.com/avatar/dae071222608e84f665fdfd4678e8f2e?d=monsterid&s=240", + "id":"23" + }, + { + "gravatar":"http://www.gravatar.com/avatar/9b14143dfed8f4fa57428ad3e547bc0e?d=monsterid&s=240", + "id":"24" + }, + { + "gravatar":"http://www.gravatar.com/avatar/e0a27844ea90b44bcebfa3bec7a583bd?d=monsterid&s=240", + "id":"25" + }, + { + "gravatar":"http://www.gravatar.com/avatar/e60ecae6d42227896b877fe5ae80dcbf?d=monsterid&s=240", + "id":"26" + }, + { + "gravatar":"http://www.gravatar.com/avatar/cb8004fd244c846ef07902d30be3d584?d=monsterid&s=240", + "id":"27" + }, + { + "gravatar":"http://www.gravatar.com/avatar/5f68d5e3481bbd4e143454820f33d9b6?d=monsterid&s=240", + "id":"28" + }, + { + "gravatar":"http://www.gravatar.com/avatar/eae907f53220bbcaabfe0cda31be29d8?d=monsterid&s=240", + "id":"29" + }, + { + "gravatar":"http://www.gravatar.com/avatar/cd26f838cf344a14b8a1add418f8f2b8?d=monsterid&s=240", + "id":"30" + }, + { + "gravatar":"http://www.gravatar.com/avatar/35bbb9b6fbad099dcbb2901d281a8d8e?d=monsterid&s=240", + "id":"31" + }, + { + "gravatar":"http://www.gravatar.com/avatar/335ee931be900e9d28e769e5c9537084?d=monsterid&s=240", + "id":"32" + }, + { + "gravatar":"http://www.gravatar.com/avatar/547db7c434e8a83737d99f0f4eab2290?d=monsterid&s=240", + "id":"33" + }, + { + "gravatar":"http://www.gravatar.com/avatar/96dbb97764ca7f5d56073ccf401a9753?d=monsterid&s=240", + "id":"34" + }, + { + "gravatar":"http://www.gravatar.com/avatar/fedd24fdaae013b925eff616ad55fee8?d=monsterid&s=240", + "id":"35" + }, + { + "gravatar":"http://www.gravatar.com/avatar/fa0a22c3463a9ee3cb1acd6bc05b9a3e?d=monsterid&s=240", + "id":"36" + }, + { + "gravatar":"http://www.gravatar.com/avatar/674f1d9793c903baeb7607655cd0f787?d=monsterid&s=240", + "id":"37" + }, + { + "gravatar":"http://www.gravatar.com/avatar/ee5179d5ce0b3172b6b34c5ec94a61c0?d=monsterid&s=240", + "id":"38" + }, + { + "gravatar":"http://www.gravatar.com/avatar/fde11b1d20465b0dcb7c9879aca299a3?d=monsterid&s=240", + "id":"39" + }, + { + "gravatar":"http://www.gravatar.com/avatar/e6f6d95ae0cad8073ccac961c3c9e9d7?d=monsterid&s=240", + "id":"40" + } + ] +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 1df1c50..61cdc89 100644 --- a/build.gradle +++ b/build.gradle @@ -25,10 +25,19 @@ repositories { } } +dependencies { + compile 'com.squareup.picasso:picasso:2.3.3' +} + android { compileSdkVersion 19 buildToolsVersion "19.1.0" + defaultConfig { + minSdkVersion 5 + targetSdkVersion 18 + } + sourceSets { main { manifest.srcFile 'AndroidManifest.xml' diff --git a/src/pl/polidea/coverflow/AbstractCoverFlowImageAdapter.java b/src/pl/polidea/coverflow/AbstractCoverFlowImageAdapter.java index 8922e75..a185fed 100644 --- a/src/pl/polidea/coverflow/AbstractCoverFlowImageAdapter.java +++ b/src/pl/polidea/coverflow/AbstractCoverFlowImageAdapter.java @@ -1,9 +1,5 @@ package pl.polidea.coverflow; -import java.lang.ref.WeakReference; -import java.util.HashMap; -import java.util.Map; - import android.content.Context; import android.graphics.Bitmap; import android.util.Log; @@ -12,6 +8,10 @@ import android.widget.BaseAdapter; import android.widget.ImageView; +import java.lang.ref.WeakReference; +import java.util.HashMap; +import java.util.Map; + /** * This class is an adapter that provides base, abstract class for images * adapter. @@ -100,7 +100,7 @@ public final synchronized long getItemId(final int position) { * android.view.ViewGroup) */ @Override - public final synchronized ImageView getView(final int position, final View convertView, final ViewGroup parent) { + public synchronized ImageView getView(final int position, final View convertView, final ViewGroup parent) { ImageView imageView; if (convertView == null) { final Context context = parent.getContext(); diff --git a/src/pl/polidea/coverflow/NetworkImageAdapter.java b/src/pl/polidea/coverflow/NetworkImageAdapter.java new file mode 100644 index 0000000..32af6ff --- /dev/null +++ b/src/pl/polidea/coverflow/NetworkImageAdapter.java @@ -0,0 +1,145 @@ +package pl.polidea.coverflow; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.util.Log; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; + +import com.squareup.picasso.Picasso; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.ref.WeakReference; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import pl.polidea.coverflow.testingactivity.Result; + +/** + * This class is an adapter that provides images from a fixed set of resource + * ids. Bitmaps and ImageViews are kept as weak references so that they can be + * cleared by garbage collection when not needed. + * + */ +public class NetworkImageAdapter extends AbstractCoverFlowImageAdapter { + /** The Constant TAG. */ + private static final String TAG = NetworkImageAdapter.class.getSimpleName(); + + /** The bitmap map. */ + private final Map> bitmapMap = new HashMap>(); + + private final Context context; + + private static final LinkedList RESULTS = new LinkedList(); + + public NetworkImageAdapter(final Context context) + { + super(); + + this.context = context; + try { + setResources(parseJSONResult(readStringFromInputStream(context.getAssets().open("faces.txt")))); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + public final synchronized void setResources(List results) + { + RESULTS.clear(); + + for (Result r : results) + { + RESULTS.add(r); + } + + notifyDataSetChanged(); + } + + /* + * (non-Javadoc) + * + * @see android.widget.Adapter#getCount() + */ + @Override + public synchronized int getCount() + { + return RESULTS.size(); + } + + /* + * (non-Javadoc) + * + * @see pl.polidea.coverflow.AbstractCoverFlowImageAdapter#createBitmap(int) + */ + @Override + protected Bitmap createBitmap(final int position) { + return ((BitmapDrawable) context.getResources().getDrawable(R.drawable.image01)).getBitmap(); + } + + private String readStringFromInputStream(InputStream inputStream) throws IOException + { + StringBuilder builder = new StringBuilder(); + + int c; + + while((c = inputStream.read()) != -1) + { + builder.append((char)c); + } + + return builder.toString(); + } + + private LinkedList parseJSONResult(String result) + { + LinkedList results = new LinkedList(); + + try + { + JSONObject outerItem = new JSONObject(result); + JSONArray jArray = outerItem.getJSONArray("people"); + + for (int i = 0; i < jArray.length(); i++) + { + JSONObject employee = jArray.getJSONObject(i); + String id = employee.getString("id"); + + String url = employee.getString("gravatar"); + + Result employeeResult = new Result(url, id); + results.add(employeeResult); + } + } + catch (JSONException e) + { + Log.e("CoverFlowTestingActivity", "JSONException in parseJSONResult"); + e.printStackTrace(); + } + + return results; + } + + @Override + public synchronized ImageView getView(int position, View convertView, ViewGroup parent) + { + ImageView view = super.getView(position, convertView, parent); + + Log.d(TAG, "getView: " + RESULTS.get(position)); + + Picasso.with(context).load(RESULTS.get(position).getAvatarURL()).into(view); + + return view; + } +} \ No newline at end of file diff --git a/src/pl/polidea/coverflow/ResourceImageAdapter.java b/src/pl/polidea/coverflow/ResourceImageAdapter.java index eaa79c9..60d8cf6 100644 --- a/src/pl/polidea/coverflow/ResourceImageAdapter.java +++ b/src/pl/polidea/coverflow/ResourceImageAdapter.java @@ -1,16 +1,16 @@ package pl.polidea.coverflow; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.util.Log; + import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.drawable.BitmapDrawable; -import android.util.Log; - /** * This class is an adapter that provides images from a fixed set of resource * ids. Bitmaps and ImageViews are kept as weak references so that they can be diff --git a/src/pl/polidea/coverflow/testingactivity/CoverFlowTestingActivity.java b/src/pl/polidea/coverflow/testingactivity/CoverFlowTestingActivity.java index 40cd096..f07c8ec 100644 --- a/src/pl/polidea/coverflow/testingactivity/CoverFlowTestingActivity.java +++ b/src/pl/polidea/coverflow/testingactivity/CoverFlowTestingActivity.java @@ -1,9 +1,5 @@ package pl.polidea.coverflow.testingactivity; -import pl.polidea.coverflow.CoverFlow; -import pl.polidea.coverflow.R; -import pl.polidea.coverflow.ReflectingImageAdapter; -import pl.polidea.coverflow.ResourceImageAdapter; import android.app.Activity; import android.os.Bundle; import android.view.View; @@ -13,13 +9,18 @@ import android.widget.BaseAdapter; import android.widget.TextView; +import pl.polidea.coverflow.AbstractCoverFlowImageAdapter; +import pl.polidea.coverflow.CoverFlow; +import pl.polidea.coverflow.NetworkImageAdapter; +import pl.polidea.coverflow.R; +import pl.polidea.coverflow.ReflectingImageAdapter; + /**** * The Class CoverFlowTestingActivity. */ public class CoverFlowTestingActivity extends Activity { private TextView textView; - /* * (non-Javadoc) * @@ -32,6 +33,7 @@ protected void onCreate(final Bundle savedInstanceState) { setContentView(R.layout.main); textView = (TextView) findViewById(this.getResources() .getIdentifier("statusText", "id", "pl.polidea.coverflow")); + // note resources below are taken using getIdentifier to allow importing // this library as library. final CoverFlow coverFlow1 = (CoverFlow) findViewById(this.getResources().getIdentifier("coverflow", "id", @@ -52,10 +54,18 @@ protected void onCreate(final Bundle savedInstanceState) { */ private void setupCoverFlow(final CoverFlow mCoverFlow, final boolean reflect) { BaseAdapter coverImageAdapter; + + AbstractCoverFlowImageAdapter linkedAdapter; + + // select here which adapter to use + linkedAdapter = new ResourceImageAdapter(this); +// linkedAdapter = new NetworkImageAdapter(this); + + // ReflectingImageAdapter doesn't work with NetworkImageAdapter - you need to use Picasso there if (reflect) { - coverImageAdapter = new ReflectingImageAdapter(new ResourceImageAdapter(this)); + coverImageAdapter = new ReflectingImageAdapter(linkedAdapter); } else { - coverImageAdapter = new ResourceImageAdapter(this); + coverImageAdapter = linkedAdapter; } mCoverFlow.setAdapter(coverImageAdapter); mCoverFlow.setSelection(2, true); @@ -88,5 +98,4 @@ public void onNothingSelected(final AdapterView< ? > parent) { } }); } - } diff --git a/src/pl/polidea/coverflow/testingactivity/NetworkUtilities.java b/src/pl/polidea/coverflow/testingactivity/NetworkUtilities.java new file mode 100644 index 0000000..3e4fd9f --- /dev/null +++ b/src/pl/polidea/coverflow/testingactivity/NetworkUtilities.java @@ -0,0 +1,22 @@ +package pl.polidea.coverflow.testingactivity; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; + +/** + * Created by martawoldanska on 8/5/14. + */ +public class NetworkUtilities +{ + public static boolean isOnline(Context context) + { + ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = cm.getActiveNetworkInfo(); + + if (networkInfo != null && networkInfo.isConnected()) + return true; + + return false; + } +} diff --git a/src/pl/polidea/coverflow/testingactivity/Result.java b/src/pl/polidea/coverflow/testingactivity/Result.java new file mode 100644 index 0000000..eb43e68 --- /dev/null +++ b/src/pl/polidea/coverflow/testingactivity/Result.java @@ -0,0 +1,29 @@ +package pl.polidea.coverflow.testingactivity; + +/** + * Created by martawoldanska on 8/4/14. + */ +public class Result +{ + + private String avatarURL; + private String userId; + + public Result() + { + this.avatarURL = null; + this.userId = ""; + } + + public Result(String avatarURL, String userId) + { + this.avatarURL = avatarURL; + this.userId = userId; + } + + public String getAvatarURL() + { + return this.avatarURL; + } + +}