Search in sources :

Example 6 with Contribution

use of fr.free.nrw.commons.contributions.Contribution in project apps-android-commons by commons-app.

the class MultipleShareActivity method multipleUploadBegins.

private void multipleUploadBegins() {
    Timber.d("Multiple upload begins");
    final ProgressDialog dialog = new ProgressDialog(this);
    dialog.setIndeterminate(false);
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setMax(photosList.size());
    dialog.setTitle(getResources().getQuantityString(R.plurals.starting_multiple_uploads, photosList.size(), photosList.size()));
    dialog.show();
    for (int i = 0; i < photosList.size(); i++) {
        Contribution up = photosList.get(i);
        // Goddamn Java
        final int uploadCount = i + 1;
        uploadController.startUpload(up, new UploadController.ContributionUploadProgress() {

            @Override
            public void onUploadStarted(Contribution contribution) {
                dialog.setProgress(uploadCount);
                if (uploadCount == photosList.size()) {
                    dialog.dismiss();
                    Toast startingToast = Toast.makeText(CommonsApplication.getInstance(), R.string.uploading_started, Toast.LENGTH_LONG);
                    startingToast.show();
                }
            }
        });
    }
    uploadsList.setImageOnlyMode(true);
    categorizationFragment = (CategorizationFragment) getSupportFragmentManager().findFragmentByTag("categorization");
    if (categorizationFragment == null) {
        categorizationFragment = new CategorizationFragment();
    }
    // FIXME: Stops the keyboard from being shown 'stale' while moving out of this fragment into the next
    View target = getCurrentFocus();
    if (target != null) {
        InputMethodManager imm = (InputMethodManager) target.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
    }
    getSupportFragmentManager().beginTransaction().add(R.id.uploadsFragmentContainer, categorizationFragment, "categorization").commitAllowingStateLoss();
//See http://stackoverflow.com/questions/7469082/getting-exception-illegalstateexception-can-not-perform-this-action-after-onsa
}
Also used : Toast(android.widget.Toast) CategorizationFragment(fr.free.nrw.commons.category.CategorizationFragment) InputMethodManager(android.view.inputmethod.InputMethodManager) ProgressDialog(android.app.ProgressDialog) View(android.view.View) AdapterView(android.widget.AdapterView) Contribution(fr.free.nrw.commons.contributions.Contribution)

Example 7 with Contribution

use of fr.free.nrw.commons.contributions.Contribution in project apps-android-commons by commons-app.

the class MultipleShareActivity method onAuthCookieAcquired.

@Override
protected void onAuthCookieAcquired(String authCookie) {
    app.getMWApi().setAuthCookie(authCookie);
    Intent intent = getIntent();
    if (intent.getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
        if (photosList == null) {
            photosList = new ArrayList<>();
            ArrayList<Uri> urisList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
            for (int i = 0; i < urisList.size(); i++) {
                Contribution up = new Contribution();
                Uri uri = urisList.get(i);
                up.setLocalUri(uri);
                up.setTag("mimeType", intent.getType());
                up.setTag("sequence", i);
                up.setSource(Contribution.SOURCE_EXTERNAL);
                up.setMultiple(true);
                photosList.add(up);
            }
        }
        uploadsList = (MultipleUploadListFragment) getSupportFragmentManager().findFragmentByTag("uploadsList");
        if (uploadsList == null) {
            uploadsList = new MultipleUploadListFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.uploadsFragmentContainer, uploadsList, "uploadsList").commit();
        }
        setTitle(getResources().getQuantityString(R.plurals.multiple_uploads_title, photosList.size(), photosList.size()));
        uploadController.prepareService();
    }
}
Also used : Intent(android.content.Intent) Uri(android.net.Uri) Contribution(fr.free.nrw.commons.contributions.Contribution)

Example 8 with Contribution

use of fr.free.nrw.commons.contributions.Contribution in project apps-android-commons by commons-app.

the class ModificationsSyncAdapter method onPerformSync.

@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!
    Cursor allModifications;
    try {
        allModifications = contentProviderClient.query(ModificationsContentProvider.BASE_URI, null, null, null, null);
    } catch (RemoteException e) {
        throw new RuntimeException(e);
    }
    // Exit early if nothing to do
    if (allModifications == null || allModifications.getCount() == 0) {
        Timber.d("No modifications to perform");
        return;
    }
    String authCookie;
    try {
        authCookie = AccountManager.get(getContext()).blockingGetAuthToken(account, "", false);
    } catch (OperationCanceledException | AuthenticatorException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        Timber.d("Could not authenticate :(");
        return;
    }
    if (Utils.isNullOrWhiteSpace(authCookie)) {
        Timber.d("Could not authenticate :(");
        return;
    }
    MWApi api = CommonsApplication.getInstance().getMWApi();
    api.setAuthCookie(authCookie);
    String editToken;
    ApiResult requestResult, responseResult;
    try {
        editToken = api.getEditToken();
    } catch (IOException e) {
        Timber.d("Can not retreive edit token!");
        return;
    }
    allModifications.moveToFirst();
    Timber.d("Found %d modifications to execute", allModifications.getCount());
    ContentProviderClient contributionsClient = null;
    try {
        contributionsClient = getContext().getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
        while (!allModifications.isAfterLast()) {
            ModifierSequence sequence = ModifierSequence.fromCursor(allModifications);
            sequence.setContentProviderClient(contentProviderClient);
            Contribution contrib;
            Cursor contributionCursor;
            try {
                contributionCursor = contributionsClient.query(sequence.getMediaUri(), null, null, null, null);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
            contributionCursor.moveToFirst();
            contrib = Contribution.fromCursor(contributionCursor);
            if (contrib.getState() == Contribution.STATE_COMPLETED) {
                try {
                    requestResult = api.action("query").param("prop", "revisions").param("rvprop", "timestamp|content").param("titles", contrib.getFilename()).get();
                } catch (IOException e) {
                    Timber.d("Network fuckup on modifications sync!");
                    continue;
                }
                Timber.d("Page content is %s", Utils.getStringFromDOM(requestResult.getDocument()));
                String pageContent = requestResult.getString("/api/query/pages/page/revisions/rev");
                String processedPageContent = sequence.executeModifications(contrib.getFilename(), pageContent);
                try {
                    responseResult = api.action("edit").param("title", contrib.getFilename()).param("token", editToken).param("text", processedPageContent).param("summary", sequence.getEditSummary()).post();
                } catch (IOException e) {
                    Timber.d("Network fuckup on modifications sync!");
                    continue;
                }
                Timber.d("Response is %s", Utils.getStringFromDOM(responseResult.getDocument()));
                String result = responseResult.getString("/api/edit/@result");
                if (!result.equals("Success")) {
                    // FIXME: Log this somewhere else
                    Timber.d("Non success result! %s", result);
                } else {
                    sequence.delete();
                }
            }
            allModifications.moveToNext();
        }
    } finally {
        if (contributionsClient != null) {
            contributionsClient.release();
        }
    }
}
Also used : OperationCanceledException(android.accounts.OperationCanceledException) AuthenticatorException(android.accounts.AuthenticatorException) IOException(java.io.IOException) Cursor(android.database.Cursor) ApiResult(org.mediawiki.api.ApiResult) MWApi(fr.free.nrw.commons.MWApi) RemoteException(android.os.RemoteException) ContentProviderClient(android.content.ContentProviderClient) Contribution(fr.free.nrw.commons.contributions.Contribution)

Aggregations

Contribution (fr.free.nrw.commons.contributions.Contribution)8 ContentProviderClient (android.content.ContentProviderClient)2 Intent (android.content.Intent)2 Cursor (android.database.Cursor)2 Toast (android.widget.Toast)2 IOException (java.io.IOException)2 AuthenticatorException (android.accounts.AuthenticatorException)1 OperationCanceledException (android.accounts.OperationCanceledException)1 ProgressDialog (android.app.ProgressDialog)1 SharedPreferences (android.content.SharedPreferences)1 Uri (android.net.Uri)1 RemoteException (android.os.RemoteException)1 ShareActionProvider (android.support.v7.widget.ShareActionProvider)1 View (android.view.View)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 AdapterView (android.widget.AdapterView)1 MWApi (fr.free.nrw.commons.MWApi)1 Media (fr.free.nrw.commons.Media)1 CategorizationFragment (fr.free.nrw.commons.category.CategorizationFragment)1 CategoryModifier (fr.free.nrw.commons.modifications.CategoryModifier)1