Search in sources :

Example 1 with Contribution

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

the class ShareActivity method uploadBegins.

private void uploadBegins() {
    if (locationPermission) {
        getFileMetadata(true);
    } else {
        getFileMetadata(false);
    }
    Toast startingToast = Toast.makeText(CommonsApplication.getInstance(), R.string.uploading_started, Toast.LENGTH_LONG);
    startingToast.show();
    if (!cacheFound) {
        //Has to be called after apiCall.request()
        app.getCacheData().cacheCategory();
        Timber.d("Cache the categories found");
    }
    uploadController.startUpload(title, mediaUri, description, mimeType, source, decimalCoords, new UploadController.ContributionUploadProgress() {

        @Override
        public void onUploadStarted(Contribution contribution) {
            ShareActivity.this.contribution = contribution;
            showPostUpload();
        }
    });
}
Also used : Toast(android.widget.Toast) Contribution(fr.free.nrw.commons.contributions.Contribution)

Example 2 with Contribution

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

the class UploadController method startUpload.

public void startUpload(final Contribution contribution, final ContributionUploadProgress onComplete) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app);
    //Set creator, desc, and license
    if (TextUtils.isEmpty(contribution.getCreator())) {
        contribution.setCreator(app.getCurrentAccount().name);
    }
    if (contribution.getDescription() == null) {
        contribution.setDescription("");
    }
    String license = prefs.getString(Prefs.DEFAULT_LICENSE, Prefs.Licenses.CC_BY_SA_3);
    contribution.setLicense(license);
    //FIXME: Add permission request here. Only executeAsyncTask if permission has been granted
    new AsyncTask<Void, Void, Contribution>() {

        // Fills up missing information about Contributions
        // Only does things that involve some form of IO
        // Runs in background thread
        @Override
        protected Contribution doInBackground(Void... voids) /* stare into you */
        {
            long length;
            try {
                if (contribution.getDataLength() <= 0) {
                    length = app.getContentResolver().openAssetFileDescriptor(contribution.getLocalUri(), "r").getLength();
                    if (length == -1) {
                        // Let us find out the long way!
                        length = Utils.countBytes(app.getContentResolver().openInputStream(contribution.getLocalUri()));
                    }
                    contribution.setDataLength(length);
                }
            } catch (IOException e) {
                Timber.e(e, "IO Exception: ");
            } catch (NullPointerException e) {
                Timber.e(e, "Null Pointer Exception: ");
            } catch (SecurityException e) {
                Timber.e(e, "Security Exception: ");
            }
            String mimeType = (String) contribution.getTag("mimeType");
            Boolean imagePrefix = false;
            if (mimeType == null || TextUtils.isEmpty(mimeType) || mimeType.endsWith("*")) {
                mimeType = app.getContentResolver().getType(contribution.getLocalUri());
            }
            if (mimeType != null) {
                contribution.setTag("mimeType", mimeType);
                imagePrefix = mimeType.startsWith("image/");
                Timber.d("MimeType is: %s", mimeType);
            }
            if (imagePrefix && contribution.getDateCreated() == null) {
                Cursor cursor = app.getContentResolver().query(contribution.getLocalUri(), new String[] { MediaStore.Images.ImageColumns.DATE_TAKEN }, null, null, null);
                if (cursor != null && cursor.getCount() != 0) {
                    cursor.moveToFirst();
                    Date dateCreated = new Date(cursor.getLong(0));
                    Date epochStart = new Date(0);
                    if (dateCreated.equals(epochStart) || dateCreated.before(epochStart)) {
                        // If date is incorrect (1st second of unix time) then set it to the current date
                        dateCreated = new Date();
                    }
                    contribution.setDateCreated(dateCreated);
                    cursor.close();
                } else {
                    contribution.setDateCreated(new Date());
                }
            }
            return contribution;
        }

        @Override
        protected void onPostExecute(Contribution contribution) {
            super.onPostExecute(contribution);
            //Starts the upload. If commented out, user can proceed to next Fragment but upload doesn't happen
            uploadService.queue(UploadService.ACTION_UPLOAD_FILE, contribution);
            onComplete.onUploadStarted(contribution);
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : SharedPreferences(android.content.SharedPreferences) IOException(java.io.IOException) Cursor(android.database.Cursor) Contribution(fr.free.nrw.commons.contributions.Contribution) Date(java.util.Date)

Example 3 with Contribution

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

the class MultipleShareActivity method onCategoriesSave.

@Override
public void onCategoriesSave(ArrayList<String> categories) {
    if (categories.size() > 0) {
        ContentProviderClient client = getContentResolver().acquireContentProviderClient(ModificationsContentProvider.AUTHORITY);
        for (Contribution contribution : photosList) {
            ModifierSequence categoriesSequence = new ModifierSequence(contribution.getContentUri());
            categoriesSequence.queueModifier(new CategoryModifier(categories.toArray(new String[] {})));
            categoriesSequence.queueModifier(new TemplateRemoveModifier("Uncategorized"));
            categoriesSequence.setContentProviderClient(client);
            categoriesSequence.save();
        }
    }
    // FIXME: Make sure that the content provider is up
    // This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
    // Enable sync by default!
    ContentResolver.setSyncAutomatically(app.getCurrentAccount(), ModificationsContentProvider.AUTHORITY, true);
    EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT).param("username", app.getCurrentAccount().name).param("categories-count", categories.size()).param("files-count", photosList.size()).param("source", Contribution.SOURCE_EXTERNAL).param("result", "queued").log();
    finish();
}
Also used : TemplateRemoveModifier(fr.free.nrw.commons.modifications.TemplateRemoveModifier) ContentProviderClient(android.content.ContentProviderClient) Contribution(fr.free.nrw.commons.contributions.Contribution) ModifierSequence(fr.free.nrw.commons.modifications.ModifierSequence) CategoryModifier(fr.free.nrw.commons.modifications.CategoryModifier)

Example 4 with Contribution

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

the class UploadController method startUpload.

public void startUpload(String title, Uri mediaUri, String description, String mimeType, String source, String decimalCoords, ContributionUploadProgress onComplete) {
    Contribution contribution;
    //TODO: Modify this to include coords
    contribution = new Contribution(mediaUri, null, title, description, -1, null, null, app.getCurrentAccount().name, CommonsApplication.DEFAULT_EDIT_SUMMARY, decimalCoords);
    contribution.setTag("mimeType", mimeType);
    contribution.setSource(source);
    //Calls the next overloaded method
    startUpload(contribution, onComplete);
}
Also used : Contribution(fr.free.nrw.commons.contributions.Contribution)

Example 5 with Contribution

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

the class MediaDetailPagerFragment method onCreateOptionsMenu.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (!editable) {
        // Disable menu options for editable views
        // see http://stackoverflow.com/a/8495697/17865
        menu.clear();
        inflater.inflate(R.menu.fragment_image_detail, menu);
        if (pager != null) {
            MediaDetailProvider provider = (MediaDetailProvider) getActivity();
            Media m = provider.getMediaAtPosition(pager.getCurrentItem());
            if (m != null) {
                // Enable default set of actions, then re-enable different set of actions only if it is a failed contrib
                menu.findItem(R.id.menu_retry_current_image).setEnabled(false).setVisible(false);
                menu.findItem(R.id.menu_cancel_current_image).setEnabled(false).setVisible(false);
                menu.findItem(R.id.menu_browser_current_image).setEnabled(true).setVisible(true);
                menu.findItem(R.id.menu_share_current_image).setEnabled(true).setVisible(true);
                menu.findItem(R.id.menu_download_current_image).setEnabled(true).setVisible(true);
                // Set ShareActionProvider Intent
                ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menu.findItem(R.id.menu_share_current_image));
                // https://github.com/commons-app/apps-android-commons/issues/413
                if (mShareActionProvider != null) {
                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                    shareIntent.setType("text/plain");
                    shareIntent.putExtra(Intent.EXTRA_TEXT, m.getDisplayTitle() + " \n" + m.getDescriptionUrl());
                    mShareActionProvider.setShareIntent(shareIntent);
                }
                if (m instanceof Contribution) {
                    Contribution c = (Contribution) m;
                    switch(c.getState()) {
                        case Contribution.STATE_FAILED:
                            menu.findItem(R.id.menu_retry_current_image).setEnabled(true).setVisible(true);
                            menu.findItem(R.id.menu_cancel_current_image).setEnabled(true).setVisible(true);
                            menu.findItem(R.id.menu_browser_current_image).setEnabled(false).setVisible(false);
                            menu.findItem(R.id.menu_share_current_image).setEnabled(false).setVisible(false);
                            menu.findItem(R.id.menu_download_current_image).setEnabled(false).setVisible(false);
                            break;
                        case Contribution.STATE_IN_PROGRESS:
                        case Contribution.STATE_QUEUED:
                            menu.findItem(R.id.menu_retry_current_image).setEnabled(false).setVisible(false);
                            menu.findItem(R.id.menu_cancel_current_image).setEnabled(false).setVisible(false);
                            menu.findItem(R.id.menu_browser_current_image).setEnabled(false).setVisible(false);
                            menu.findItem(R.id.menu_share_current_image).setEnabled(false).setVisible(false);
                            menu.findItem(R.id.menu_download_current_image).setEnabled(false).setVisible(false);
                            break;
                        case Contribution.STATE_COMPLETED:
                            // Default set of menu items works fine. Treat same as regular media object
                            break;
                    }
                }
            }
        }
    }
}
Also used : Media(fr.free.nrw.commons.Media) Intent(android.content.Intent) ShareActionProvider(android.support.v7.widget.ShareActionProvider) 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