0

I'm in the middle of making a wallpaper app for android and I'm have a problem with Muzei support and hoping someone here can help me see what I'm missing.

I have a JSON file which my app uses to get the wallpaper URLs and display the pictures from. It works fine and it gets the right images. However if I update the JSON with more entries and more images then the Muzei extension still uses the old database. I was thinking that maybe it caches the database and just doesn't update it for whatever reason. The only way to get it to use the updated JSON file is to clear the data of my app and set a different extension in Muzei and the reset Muzei with my extension. Which would not be very user friendly.

Probably just being blind but help would be appreciated.

ArtSource.java:

package com.main.walls.muzei;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.google.android.apps.muzei.api.Artwork;
import com.google.android.apps.muzei.api.RemoteMuzeiArtSource;
import com.google.android.apps.muzei.api.UserCommand;
import com.main.walls.utilities.Preferences;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Random;

import walls.wallpaper.R;

public class ArtSource extends RemoteMuzeiArtSource {

    private WallsDatabase wdb;
    private ArrayList<WallpaperInfo> wallslist;
    private Preferences mPrefs;

    private static final String ARTSOURCE_NAME = "Walls";
    private static final String JSON_URL = "http://pastebin.com/raw.php?i=VWTzhJ0N";
    private static final String MARKET_URL = "https://play.google.com/store/apps/details?id=";
    private static final int COMMAND_ID_SHARE = 1337;

    public ArtSource() {
        super(ARTSOURCE_NAME);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String command = intent.getExtras().getString("service");
        if (command != null) {
            try {
                onTryUpdate(UPDATE_REASON_USER_NEXT);
            } catch (RetryException e) {
                Log.d("MuzeiArtSource", Log.getStackTraceString(e));
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        wdb = new WallsDatabase(getApplicationContext());
        wallslist = new ArrayList<>();

        mPrefs = new Preferences(ArtSource.this);

        ArrayList<UserCommand> commands = new ArrayList<>();
        commands.add(new UserCommand(BUILTIN_COMMAND_ID_NEXT_ARTWORK));
        commands.add(new UserCommand(COMMAND_ID_SHARE, getString(R.string.justshare)));

        setUserCommands(commands);

    }

    @Override
    public void onCustomCommand(int id) {
        super.onCustomCommand(id);
        if (id == COMMAND_ID_SHARE) {
            Artwork currentArtwork = getCurrentArtwork();
            Intent shareWall = new Intent(Intent.ACTION_SEND);
            shareWall.setType("text/plain");

            String authorName = currentArtwork.getByline();
            String storeUrl = MARKET_URL + getResources().getString(R.string.package_name);
            String iconPackName = getString(R.string.app_name);

            shareWall.putExtra(Intent.EXTRA_TEXT,
                    getString(R.string.partone) + authorName +
                            getString(R.string.parttwo) + iconPackName +
                            getString(R.string.partthree) + storeUrl);

            shareWall = Intent.createChooser(shareWall, getString(R.string.share_title));
            shareWall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(shareWall);
        }
    }

    @Override
    protected void onTryUpdate(int reason) throws RetryException {
        if (mPrefs.isFeaturesEnabled()) {
            if (wallslist.size() == 0)
                getWallpapersFromUrl(JSON_URL);
            int i = getRandomInt();
            String token = wallslist.get(i).getWallURL();
            publishArtwork(new Artwork.Builder()
                    .byline(wallslist.get(i).getWallAuthor())
                    .imageUri(Uri.parse(wallslist.get(i).getWallURL()))
                    .token(token)
                    .viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(wallslist.get(i).getWallURL())))
                    .build());
            scheduleUpdate(System.currentTimeMillis() + mPrefs.getRotateTime());
        }
    }

    private int getRandomInt() {
        return new Random().nextInt(wallslist.size());
    }

    private void getWallpapersFromUrl(String url) {
        wallslist.clear();
        wallslist = wdb.getAllWalls();

        if (wallslist.size() == 0) {
            try {
                HttpClient cl = new DefaultHttpClient();
                HttpResponse response = cl.execute(new HttpGet(url));
                if (response.getStatusLine().getStatusCode() == 200) {
                    final String data = EntityUtils.toString(response.getEntity());
                    JSONObject jsonobject = new JSONObject(data);
                    final JSONArray jsonarray = jsonobject.getJSONArray("wallpapers");
                    wallslist.clear();
                    wdb.deleteAllWallpapers();
                    for (int i = 0; i < jsonarray.length(); i++) {
                        jsonobject = jsonarray.getJSONObject(i);
                        WallpaperInfo jsondata = new WallpaperInfo(
                                jsonobject.getString("author"),
                                jsonobject.getString("url")
                        );
                        wdb.addWallpaper(jsondata);
                        wallslist.add(jsondata);
                    }
                }
            } catch (Exception e) {
                Log.d("Wallpapers", Log.getStackTraceString(e));
            }
        }
    }
}

Not sure if I need any other code in here or not and the logcat doesn't say anything, it's as though it's it's working as normal. If you need to see more code just let me know.

Thanks for any help.

Lewis Broadbent
  • 769
  • 1
  • 7
  • 14

1 Answers1

0

Basically I was right and I was just being stupid and missing the simplest thing, it didn't even have anything to do with ArtSource.java.

In WallsDatabase.Java I wasn't updating DATABASE_VERSION. I was leaving it at 1 and so when I updated the app it didn't bother to update the database because the version was the same.

So just change the value of private static final int DATABASE_VERSION = 1 to a higher number and it should work great.

Simple mistake but an easy one to make I guess.

Lewis Broadbent
  • 769
  • 1
  • 7
  • 14