Android Open Source - photo-paper Wallpaper Service






From Project

Back to project page photo-paper.

License

The source code is released under:

MIT License

If you think the Android project photo-paper 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.lukekorth.android_500px.services;
// w  w  w  .j a va 2s  .co  m
import android.app.IntentService;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.PowerManager;
import android.os.SystemClock;

import com.lukekorth.android_500px.WallpaperApplication;
import com.lukekorth.android_500px.helpers.Settings;
import com.lukekorth.android_500px.helpers.Utils;
import com.lukekorth.android_500px.models.Photos;
import com.lukekorth.android_500px.models.WallpaperChangedEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class WallpaperService extends IntentService {

    public static final String USER_PRESENT_RECEIVER_KEY = "com.lukekorth.android_500px.services.WallpaperService.SLEEP";

    public WallpaperService() {
        super("WallpaperService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Logger logger = LoggerFactory.getLogger("WallpaperService");

        if (Settings.isEnabled(this)) {
            PowerManager.WakeLock wakeLock = ((PowerManager) getSystemService(POWER_SERVICE))
                    .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "500pxApiService");
            wakeLock.acquire();

            if (intent.getBooleanExtra(USER_PRESENT_RECEIVER_KEY, false)) {
                logger.debug("User present, waiting 2 seconds and then setting wallpaper");

                SystemClock.sleep(2000);

                if (!Utils.shouldUpdateWallpaper(this)) {
                    logger.debug("Wallpaper does not need to be updated");
                    return;
                }
            }

            WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);

            int width = wallpaperManager.getDesiredMinimumWidth();
            int height = wallpaperManager.getDesiredMinimumHeight();
            if (Utils.supportsParallax(this) && !Settings.useParallax(this)) {
                width = width / 2;
            }

            logger.debug("Setting wallpaper to " + width + "px wide by " + height + "px tall");

            Photos photo = Photos.getNextPhoto(this);
            if (photo != null) {
                try {
                    Bitmap bitmap = WallpaperApplication.getPicasso(this)
                            .load(photo.imageUrl)
                            .centerCrop()
                            .resize(width, height)
                            .get();

                    wallpaperManager.setBitmap(bitmap);

                    photo.seen = true;
                    photo.seenAt = System.currentTimeMillis();
                    photo.save();

                    Settings.setUpdated(this);
                } catch (IOException e) {
                    logger.error(e.getMessage());
                    photo.failedCount = photo.failedCount + 1;
                    if (photo.failedCount > 3) {
                        photo.delete();
                    } else {
                        photo.save();
                    }
                    startService(new Intent(this, WallpaperService.class));
                }
            } else {
                logger.debug("Next photo was null");
            }

            if (Utils.needMorePhotos(this)) {
                logger.debug("Getting more photos via ApiService");
                startService(new Intent(this, ApiService.class));
            }

            WallpaperApplication.getBus().post(new WallpaperChangedEvent());

            wakeLock.release();
        } else {
            logger.debug("App not enabled");
        }
    }

}




Java Source Code List

com.lukekorth.android_500px.HistoryActivity.java
com.lukekorth.android_500px.PhotoFragment.java
com.lukekorth.android_500px.SearchActivity.java
com.lukekorth.android_500px.SettingsActivity.java
com.lukekorth.android_500px.ViewPhotoActivity.java
com.lukekorth.android_500px.WallpaperApplication.java
com.lukekorth.android_500px.helpers.Cache.java
com.lukekorth.android_500px.helpers.LogReporting.java
com.lukekorth.android_500px.helpers.Settings.java
com.lukekorth.android_500px.helpers.ThreadBus.java
com.lukekorth.android_500px.helpers.Utils.java
com.lukekorth.android_500px.models.ActivityResumedEvent.java
com.lukekorth.android_500px.models.EnableCategoriesEvent.java
com.lukekorth.android_500px.models.Photos.java
com.lukekorth.android_500px.models.SearchCompleteEvent.java
com.lukekorth.android_500px.models.UserUpdatedEvent.java
com.lukekorth.android_500px.models.User.java
com.lukekorth.android_500px.models.WallpaperChangedEvent.java
com.lukekorth.android_500px.receivers.UserPresentReceiver.java
com.lukekorth.android_500px.receivers.WifiReceiver.java
com.lukekorth.android_500px.services.ApiService.java
com.lukekorth.android_500px.services.ClearCacheIntentService.java
com.lukekorth.android_500px.services.UserInfoIntentService.java
com.lukekorth.android_500px.services.WallpaperService.java
com.lukekorth.android_500px.views.FeatureListPreference.java
com.lukekorth.android_500px.views.SquareImageView.java
com.squareup.picasso.PicassoTools.java