Android Open Source - Dreamer Flickr Dream






From Project

Back to project page Dreamer.

License

The source code is released under:

Apache License

If you think the Android project Dreamer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.android.fancyblurdemo.app;
/*from ww w  .  ja  v a  2 s  . co  m*/
import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import android.service.dreams.DreamService;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.android.fancyblurdemo.volley.Response;
import com.android.fancyblurdemo.volley.VolleyError;

import java.util.List;

/**
 * Created by kevin.marlow on 4/4/14.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public class FlickrDream extends DreamService {

    private static final String FLICKR_API_KEY = "70fcd966ffa1676f8726a7b3fee51188";
    private static final String FLICKR_API_SECRET = "5b38c304eda85d6a";
    private static final String FLICKR_INTERESTING_PHOTOS_URL = "https://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=%s&format=json&per_page=50";

    private static final long SCROLL_DELAY = 5000; // 5 second scroll delay.

    /**
     * The {@link EdgeEffectViewPager} that will host the page contents.
     */
    private EdgeEffectViewPager mViewPager;
    private DreamAdapter mAdapter;

    /**
     * The view to show when there is no connectivity.
     */
    private RobotoTextView mNoConnectionView;

    /**
     * A flag to determine if we are on a tablet.
     */
    public static boolean sUseHighRes = false;
    private boolean mIsConnected = false;
    private ConnectionReceiver mConnectionReceiver;
    private boolean mForwardScroll = true;

    private Animation mFadeIn;
    private Animation mFadeOut;

    private Handler mScrollHandler = new Handler();
    private Runnable mScroller = new Runnable() {
        @Override
        public void run() {
            if (mViewPager != null && mAdapter != null && mAdapter.getCount() > 1) {
                if (mViewPager.getCurrentItem() + 1 >= mAdapter.getCount()) {
                    mForwardScroll = false;
                } else if (mForwardScroll == false && mViewPager.getCurrentItem() == 0) {
                    mForwardScroll = true;
                }
                if (mForwardScroll) {
                    mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1, true);
                } else {
                    mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1, true);
                }
                mScrollHandler.postDelayed(mScroller, SCROLL_DELAY);
            }
        }
    };

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        setContentView(R.layout.service_dream);
//        setInteractive(true);
        setScreenBright(true);
        setFullscreen(true);

        sUseHighRes = getResources().getBoolean(R.bool.isTablet);

        mViewPager = (EdgeEffectViewPager) findViewById(R.id.viewPager);
        mViewPager.setInterpolator(new AccelerateDecelerateInterpolator());
        mViewPager.setScrollDurationFactor(sUseHighRes ? 3 : 2);
        mViewPager.setOnPageChangeListener(new PageChangeListener());

        mNoConnectionView = (RobotoTextView) findViewById(R.id.noConnectionView);
        mFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        mFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        mIsConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        if (mIsConnected) {
            VolleyManager.getRequestQueue().add(new FlickrRequest(String.format(FLICKR_INTERESTING_PHOTOS_URL, FLICKR_API_KEY), sUseHighRes, new ResponseListener(), new ErrorListener()));
        } else {
            mConnectionReceiver = new ConnectionReceiver();
            mNoConnectionView.setVisibility(View.VISIBLE);
            mNoConnectionView.startAnimation(mFadeIn);
            // Only register for changes if we didn't start with connectivity.
            registerReceiver(mConnectionReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }
    }

    private class PageChangeListener extends ViewPager.SimpleOnPageChangeListener {

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // Do fade title callbacks.
            mAdapter.showTitle(mViewPager.getCurrentItem());
        }
    }

    @Override
    public void onDetachedFromWindow() {
        if (mConnectionReceiver != null) {
            final ConnectionReceiver receiver = mConnectionReceiver;
            unregisterReceiver(receiver);
            mConnectionReceiver = null;
        }
        mAdapter.cancelAllTasks();
        mScrollHandler.removeCallbacks(mScroller);
        mViewPager = null;
        mAdapter = null;
        super.onDetachedFromWindow();
    }

    @Override
    public void onDreamingStarted() {
        super.onDreamingStarted();
    }

    @Override
    public void onDreamingStopped() {
        mAdapter.cancelAllTasks();
        mScrollHandler.removeCallbacks(mScroller);
        super.onDreamingStopped();
    }

    /**
     * The network response listener for successful calls. Upon response,
     * the data model is repopulated and the adapter is notified.
     */
    private class ResponseListener implements Response.Listener<List<FlickrPhoto>> {

        @Override
        public void onResponse(List<FlickrPhoto> response) {
            mAdapter = new DreamAdapter(FlickrDream.this, response);
            mViewPager.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
            mAdapter.showTitle(0);
            mScrollHandler.postDelayed(mScroller, SCROLL_DELAY);
        }
    }

    /**
     * The network response listener for failed calls.
     * Normally we would provide feedback to the user and
     * handle the error. For now, lets just print out the error.
     */
    private class ErrorListener implements Response.ErrorListener {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("FAILED", error.networkResponse == null ? error.getMessage() : "Status Code: " + error.networkResponse.statusCode);
        }
    }

    /**
     * The {@link android.content.BroadcastReceiver} responsible for monitoring network changes.
     * This is used when the app is launched without network connectivity.
     */
    private class ConnectionReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            final boolean wasConnected = mIsConnected;
            ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            mIsConnected = activeNetwork != null && activeNetwork.isConnected();
            if (!wasConnected && mIsConnected) {
                mNoConnectionView.startAnimation(mFadeOut);
                VolleyManager.getRequestQueue().add(new FlickrRequest(String.format(FLICKR_INTERESTING_PHOTOS_URL, FLICKR_API_KEY), sUseHighRes, new ResponseListener(), new ErrorListener()));
                if (mConnectionReceiver != null) {
                    final ConnectionReceiver receiver = mConnectionReceiver;
                    unregisterReceiver(receiver);
                    mConnectionReceiver = null;
                }
            }
        }
    }
}




Java Source Code List

com.android.fancyblurdemo.app.BitmapLruImageCache.java
com.android.fancyblurdemo.app.BlurImageView.java
com.android.fancyblurdemo.app.ContextWrapperEdgeEffect.java
com.android.fancyblurdemo.app.DreamAdapter.java
com.android.fancyblurdemo.app.EdgeEffectViewPager.java
com.android.fancyblurdemo.app.FlickrDream.java
com.android.fancyblurdemo.app.FlickrPhoto.java
com.android.fancyblurdemo.app.FlickrRequest.java
com.android.fancyblurdemo.app.FlickrScroller.java
com.android.fancyblurdemo.app.MainActivity.java
com.android.fancyblurdemo.app.MainApplication.java
com.android.fancyblurdemo.app.PhotoFragment.java
com.android.fancyblurdemo.app.PhotoSizeRequest.java
com.android.fancyblurdemo.app.PreCompositeTextTask.java
com.android.fancyblurdemo.app.RobotoTextView.java
com.android.fancyblurdemo.app.TopSwipeLayout.java
com.android.fancyblurdemo.app.VolleyManager.java
com.android.fancyblurdemo.app.imageblur.BlurDispatcher.java
com.android.fancyblurdemo.app.imageblur.BlurError.java
com.android.fancyblurdemo.app.imageblur.BlurManager.java
com.android.fancyblurdemo.app.imageblur.BlurQueue.java
com.android.fancyblurdemo.app.imageblur.BlurRequest.java
com.android.fancyblurdemo.app.imageblur.BlurResponseDelivery.java
com.android.fancyblurdemo.app.imageblur.BlurResponse.java
com.android.fancyblurdemo.app.imageblur.DiskLruImageCache.java
com.android.fancyblurdemo.app.imageblur.ImageBlurrer.java
com.android.fancyblurdemo.volley.AuthFailureError.java
com.android.fancyblurdemo.volley.CacheDispatcher.java
com.android.fancyblurdemo.volley.Cache.java
com.android.fancyblurdemo.volley.DefaultRetryPolicy.java
com.android.fancyblurdemo.volley.ExecutorDelivery.java
com.android.fancyblurdemo.volley.NetworkDispatcher.java
com.android.fancyblurdemo.volley.NetworkError.java
com.android.fancyblurdemo.volley.NetworkResponse.java
com.android.fancyblurdemo.volley.Network.java
com.android.fancyblurdemo.volley.NoConnectionError.java
com.android.fancyblurdemo.volley.ParseError.java
com.android.fancyblurdemo.volley.RequestQueue.java
com.android.fancyblurdemo.volley.Request.java
com.android.fancyblurdemo.volley.ResponseDelivery.java
com.android.fancyblurdemo.volley.Response.java
com.android.fancyblurdemo.volley.RetryPolicy.java
com.android.fancyblurdemo.volley.ServerError.java
com.android.fancyblurdemo.volley.TimeoutError.java
com.android.fancyblurdemo.volley.VolleyError.java
com.android.fancyblurdemo.volley.VolleyLog.java
com.android.fancyblurdemo.volley.toolbox.AndroidAuthenticator.java
com.android.fancyblurdemo.volley.toolbox.Authenticator.java
com.android.fancyblurdemo.volley.toolbox.BasicNetwork.java
com.android.fancyblurdemo.volley.toolbox.ByteArrayPool.java
com.android.fancyblurdemo.volley.toolbox.ClearCacheRequest.java
com.android.fancyblurdemo.volley.toolbox.DiskBasedCache.java
com.android.fancyblurdemo.volley.toolbox.HttpClientStack.java
com.android.fancyblurdemo.volley.toolbox.HttpHeaderParser.java
com.android.fancyblurdemo.volley.toolbox.HttpStack.java
com.android.fancyblurdemo.volley.toolbox.HurlStack.java
com.android.fancyblurdemo.volley.toolbox.ImageLoader.java
com.android.fancyblurdemo.volley.toolbox.ImageRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonArrayRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonObjectRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonRequest.java
com.android.fancyblurdemo.volley.toolbox.NetworkImageView.java
com.android.fancyblurdemo.volley.toolbox.NoCache.java
com.android.fancyblurdemo.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.fancyblurdemo.volley.toolbox.RequestFuture.java
com.android.fancyblurdemo.volley.toolbox.StringRequest.java
com.android.fancyblurdemo.volley.toolbox.Volley.java