Android Open Source - apps-android-commons Contributions Sync Adapter






From Project

Back to project page apps-android-commons.

License

The source code is released under:

Apache License

If you think the Android project apps-android-commons 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 org.wikimedia.commons.contributions;
/*ww  w  .j  a  v a 2  s  .  co m*/
import android.content.*;
import android.database.Cursor;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import android.accounts.Account;
import android.os.Bundle;

import java.io.*;
import java.util.*;

import org.mediawiki.api.*;
import org.wikimedia.commons.Utils;
import org.wikimedia.commons.*;


public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
    private static int COMMIT_THRESHOLD = 10;
    public ContributionsSyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
    }

    private int getLimit() {
        return 500; // FIXME: Parameterize!
    }

    private static final String[] existsQuery = { Contribution.Table.COLUMN_FILENAME };
    private static final String existsSelection = Contribution.Table.COLUMN_FILENAME + " = ?";
    private boolean fileExists(ContentProviderClient client, String filename) {
        Cursor cursor = null;
        try {
            cursor = client.query(ContributionsContentProvider.BASE_URI,
                    existsQuery,
                    existsSelection,
                    new String[] { filename },
                    ""
            );
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
        return cursor != null && cursor.getCount() != 0;
    }

    @Override
    public void onPerformSync(Account account, Bundle bundle, String s, ContentProviderClient contentProviderClient, SyncResult syncResult) {
        // This code is fraught with possibilities of race conditions, but lalalalala I can't hear you!
        String user = account.name;
        MWApi api = CommonsApplication.createMWApi();
        SharedPreferences prefs = this.getContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
        String lastModified = prefs.getString("lastSyncTimestamp", "");
        Date curTime = new Date();
        ApiResult result;
        Boolean done = false;
        String queryContinue = null;
        while(!done) {

            try {
                MWApi.RequestBuilder builder = api.action("query")
                        .param("list", "logevents")
                        .param("letype", "upload")
                        .param("leprop", "title|timestamp")
                        .param("leuser", user)
                        .param("lelimit", getLimit());
                if(!TextUtils.isEmpty(lastModified)) {
                    builder.param("leend", lastModified);
                }
                if(!TextUtils.isEmpty(queryContinue)) {
                    builder.param("lestart", queryContinue);
                }
                result = builder.get();
            } catch (IOException e) {
                // There isn't really much we can do, eh?
                // FIXME: Perhaps add EventLogging?
                syncResult.stats.numIoExceptions += 1; // Not sure if this does anything. Shitty docs
                Log.d("Commons", "Syncing failed due to " + e.toString());
                return;
            }
            Log.d("Commons", "Last modified at " + lastModified);

            ArrayList<ApiResult> uploads = result.getNodes("/api/query/logevents/item");
            Log.d("Commons", uploads.size() + " results!");
            ArrayList<ContentValues> imageValues = new ArrayList<ContentValues>();
            for(ApiResult image: uploads) {
                String filename = image.getString("@title");
                if(fileExists(contentProviderClient, filename)) {
                    Log.d("Commons", "Skipping " + filename);
                    continue;
                }
                String thumbUrl = Utils.makeThumbBaseUrl(filename);
                Date dateUpdated = Utils.parseMWDate(image.getString("@timestamp"));
                Contribution contrib = new Contribution(null, thumbUrl, filename, "", -1, dateUpdated, dateUpdated, user, "");
                contrib.setState(Contribution.STATE_COMPLETED);
                imageValues.add(contrib.toContentValues());

                if(imageValues.size() % COMMIT_THRESHOLD == 0) {
                    try {
                        contentProviderClient.bulkInsert(ContributionsContentProvider.BASE_URI, imageValues.toArray(new ContentValues[]{}));
                    } catch (RemoteException e) {
                        throw new RuntimeException(e);
                    }
                    imageValues.clear();
                }
            }

            if(imageValues.size() != 0) {
                try {
                    contentProviderClient.bulkInsert(ContributionsContentProvider.BASE_URI, imageValues.toArray(new ContentValues[]{}));
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
            }
            queryContinue = result.getString("/api/query-continue/logevents/@lestart");
            if(TextUtils.isEmpty(queryContinue)) {
                done = true;
            }
        }
        prefs.edit().putString("lastSyncTimestamp", Utils.toMWDate(curTime)).apply();
        Log.d("Commons", "Oh hai, everyone! Look, a kitty!");


    }
}




Java Source Code List

com.gst_sdk.GStreamer.java
com.gst_sdk.GStreamer.java
org.wikimedia.commons.AboutActivity.java
org.wikimedia.commons.CommonsApplication.java
org.wikimedia.commons.EventLog.java
org.wikimedia.commons.HandlerService.java
org.wikimedia.commons.LicenseList.java
org.wikimedia.commons.License.java
org.wikimedia.commons.MediaDataExtractor.java
org.wikimedia.commons.MediaWikiImageView.java
org.wikimedia.commons.Media.java
org.wikimedia.commons.Prefs.java
org.wikimedia.commons.SettingsActivity.java
org.wikimedia.commons.Utils.java
org.wikimedia.commons.WelcomeActivity.java
org.wikimedia.commons.api.MWApi.java
org.wikimedia.commons.auth.AuthenticatedActivity.java
org.wikimedia.commons.auth.LoginActivity.java
org.wikimedia.commons.auth.WikiAccountAuthenticatorService.java
org.wikimedia.commons.auth.WikiAccountAuthenticator.java
org.wikimedia.commons.campaigns.CampaignActivity.java
org.wikimedia.commons.campaigns.CampaignContribution.java
org.wikimedia.commons.campaigns.Campaign.java
org.wikimedia.commons.campaigns.CampaignsContentProvider.java
org.wikimedia.commons.campaigns.CampaignsListAdapter.java
org.wikimedia.commons.campaigns.CampaignsSyncAdapter.java
org.wikimedia.commons.campaigns.CampaignsSyncService.java
org.wikimedia.commons.category.CategorizationFragment.java
org.wikimedia.commons.category.CategoryContentProvider.java
org.wikimedia.commons.category.Category.java
org.wikimedia.commons.contributions.ContributionController.java
org.wikimedia.commons.contributions.ContributionViewHolder.java
org.wikimedia.commons.contributions.Contribution.java
org.wikimedia.commons.contributions.ContributionsActivity.java
org.wikimedia.commons.contributions.ContributionsContentProvider.java
org.wikimedia.commons.contributions.ContributionsListAdapter.java
org.wikimedia.commons.contributions.ContributionsListFragment.java
org.wikimedia.commons.contributions.ContributionsSyncAdapter.java
org.wikimedia.commons.contributions.ContributionsSyncService.java
org.wikimedia.commons.contributions.MediaListAdapter.java
org.wikimedia.commons.data.DBOpenHelper.java
org.wikimedia.commons.media.CategoryImagesLoader.java
org.wikimedia.commons.media.MediaDetailFragment.java
org.wikimedia.commons.media.MediaDetailPagerFragment.java
org.wikimedia.commons.media.MediaDetailSpacer.java
org.wikimedia.commons.modifications.CategoryModifier.java
org.wikimedia.commons.modifications.ModificationsContentProvider.java
org.wikimedia.commons.modifications.ModificationsSyncAdapter.java
org.wikimedia.commons.modifications.ModificationsSyncService.java
org.wikimedia.commons.modifications.ModifierSequence.java
org.wikimedia.commons.modifications.PageModifier.java
org.wikimedia.commons.modifications.TemplateRemoveModifier.java
org.wikimedia.commons.upload.MultipleShareActivity.java
org.wikimedia.commons.upload.MultipleUploadListFragment.java
org.wikimedia.commons.upload.ShareActivity.java
org.wikimedia.commons.upload.SingleUploadFragment.java
org.wikimedia.commons.upload.UploadController.java
org.wikimedia.commons.upload.UploadService.java