mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Removed campaigns folder and modified ContributionsActivity
This commit is contained in:
parent
02dfa2117b
commit
fff3cf05a9
8 changed files with 12 additions and 694 deletions
|
|
@ -1,190 +0,0 @@
|
||||||
package fr.free.nrw.commons.campaigns;
|
|
||||||
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
// FIXME: Implement Parcelable
|
|
||||||
public class Campaign implements Serializable {
|
|
||||||
private boolean enabled;
|
|
||||||
|
|
||||||
private String autoAddWikitext;
|
|
||||||
private ArrayList<String> autoAddCategories;
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
private String ownWorkLicenseDefault;
|
|
||||||
|
|
||||||
private String defaultDescription;
|
|
||||||
|
|
||||||
private JSONObject config;
|
|
||||||
private String body;
|
|
||||||
private boolean isParsed;
|
|
||||||
private String trackingCategory;
|
|
||||||
private String description;
|
|
||||||
private String title;
|
|
||||||
|
|
||||||
public boolean isEnabled() {
|
|
||||||
return enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAutoAddWikitext() {
|
|
||||||
if(!this.isParsed) {
|
|
||||||
this.parseConfig();
|
|
||||||
}
|
|
||||||
return autoAddWikitext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<String> getAutoAddCategories() {
|
|
||||||
if(!this.isParsed) {
|
|
||||||
this.parseConfig();
|
|
||||||
}
|
|
||||||
return autoAddCategories;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getOwnWorkLicenseDefault() {
|
|
||||||
if(!this.isParsed) {
|
|
||||||
this.parseConfig();
|
|
||||||
}
|
|
||||||
return ownWorkLicenseDefault;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDefaultDescription() {
|
|
||||||
if(!this.isParsed) {
|
|
||||||
this.parseConfig();
|
|
||||||
}
|
|
||||||
return defaultDescription;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JSONObject getConfig() {
|
|
||||||
if(!this.isParsed) {
|
|
||||||
this.parseConfig();
|
|
||||||
}
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void parseConfig() {
|
|
||||||
try {
|
|
||||||
this.config = new JSONObject(body);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
throw new RuntimeException(e); // because what else are you gonna do?
|
|
||||||
}
|
|
||||||
if(config.has("autoAdd")) {
|
|
||||||
this.autoAddWikitext = config.optJSONObject("autoAdd").optString("wikitext", null);
|
|
||||||
if(config.optJSONObject("autoAdd").has("categories")) {
|
|
||||||
this.autoAddCategories = new ArrayList<String>();
|
|
||||||
JSONArray catsArray = config.optJSONObject("autoAdd").optJSONArray("categories");
|
|
||||||
for(int i=0; i < catsArray.length(); i++) {
|
|
||||||
autoAddCategories.add(catsArray.optString(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.title = config.optString("title", name);
|
|
||||||
this.description = config.optString("description", "");
|
|
||||||
this.isParsed = true;
|
|
||||||
}
|
|
||||||
private Campaign(String name, String body, String trackingCategory) {
|
|
||||||
this.name = name;
|
|
||||||
this.body = body;
|
|
||||||
this.trackingCategory = trackingCategory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ContentValues toContentValues() {
|
|
||||||
ContentValues cv = new ContentValues();
|
|
||||||
cv.put(Table.COLUMN_NAME, this.getName());
|
|
||||||
cv.put(Table.COLUMN_ENABLED, this.isEnabled() ? 1 : 0);
|
|
||||||
cv.put(Table.COLUMN_TITLE, this.getTitle());
|
|
||||||
cv.put(Table.COLUMN_DESCRIPTION, this.getDescription());
|
|
||||||
cv.put(Table.COLUMN_TRACKING_CATEGORY, this.getTrackingCategory());
|
|
||||||
cv.put(Table.COLUMN_BODY, this.body);
|
|
||||||
return cv;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Campaign parse(String name, String body, String trackingCategory) {
|
|
||||||
Campaign c = new Campaign(name, body, trackingCategory);
|
|
||||||
c.parseConfig();
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Campaign fromCursor(Cursor cursor) {
|
|
||||||
String name = cursor.getString(1);
|
|
||||||
Boolean enabled = cursor.getInt(2) == 1;
|
|
||||||
String title = cursor.getString(3);
|
|
||||||
String description = cursor.getString(4);
|
|
||||||
String trackingCategory = cursor.getString(5);
|
|
||||||
String body = cursor.getString(6);
|
|
||||||
Campaign c = new Campaign(name, body, trackingCategory);
|
|
||||||
c.title = title;
|
|
||||||
c.description = description;
|
|
||||||
c.enabled = enabled;
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTrackingCategory() {
|
|
||||||
return trackingCategory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() {
|
|
||||||
return description;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Table {
|
|
||||||
public static final String TABLE_NAME = "campaigns";
|
|
||||||
|
|
||||||
public static final String COLUMN_ID = "_id";
|
|
||||||
public static final String COLUMN_NAME = "name";
|
|
||||||
public static final String COLUMN_ENABLED = "enabled";
|
|
||||||
public static final String COLUMN_TITLE = "title";
|
|
||||||
public static final String COLUMN_DESCRIPTION = "description";
|
|
||||||
public static final String COLUMN_TRACKING_CATEGORY = "tracking_category";
|
|
||||||
public static final String COLUMN_BODY = "body";
|
|
||||||
|
|
||||||
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
|
|
||||||
public static final String[] ALL_FIELDS = {
|
|
||||||
COLUMN_ID,
|
|
||||||
COLUMN_NAME,
|
|
||||||
COLUMN_ENABLED,
|
|
||||||
COLUMN_TITLE,
|
|
||||||
COLUMN_DESCRIPTION,
|
|
||||||
COLUMN_TRACKING_CATEGORY,
|
|
||||||
COLUMN_BODY
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
private static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " ("
|
|
||||||
+ "_id INTEGER PRIMARY KEY,"
|
|
||||||
+ "name STRING,"
|
|
||||||
+ "enabled INTEGER,"
|
|
||||||
+ "title STRING,"
|
|
||||||
+ "description STRING,"
|
|
||||||
+ "tracking_category STRING,"
|
|
||||||
+ "body STRING"
|
|
||||||
+ ");";
|
|
||||||
|
|
||||||
|
|
||||||
public static void onCreate(SQLiteDatabase db) {
|
|
||||||
db.execSQL(CREATE_TABLE_STATEMENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void onUpdate(SQLiteDatabase db, int from, int to) {
|
|
||||||
if(to <= 6) {
|
|
||||||
onCreate(db);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,59 +0,0 @@
|
||||||
package fr.free.nrw.commons.campaigns;
|
|
||||||
|
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.support.v4.app.LoaderManager;
|
|
||||||
import android.support.v4.content.CursorLoader;
|
|
||||||
import android.support.v4.content.Loader;
|
|
||||||
import android.view.View;
|
|
||||||
import android.widget.AdapterView;
|
|
||||||
import android.widget.ListView;
|
|
||||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
|
||||||
import fr.free.nrw.commons.contributions.ContributionsActivity;
|
|
||||||
import fr.free.nrw.commons.CommonsApplication;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
|
|
||||||
public class CampaignActivity
|
|
||||||
extends SherlockFragmentActivity
|
|
||||||
implements LoaderManager.LoaderCallbacks<Cursor> {
|
|
||||||
|
|
||||||
private ListView campaignsListView;
|
|
||||||
private CampaignsListAdapter campaignsListAdapter;
|
|
||||||
|
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
setContentView(R.layout.activity_campaigns);
|
|
||||||
|
|
||||||
ContentResolver.setSyncAutomatically(((CommonsApplication)getApplicationContext()).getCurrentAccount(), CampaignsContentProvider.AUTHORITY, true); // Enable sync by default!
|
|
||||||
campaignsListView = (ListView) findViewById(R.id.campaignsList);
|
|
||||||
|
|
||||||
campaignsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
||||||
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
|
||||||
Campaign c = Campaign.fromCursor((Cursor) adapterView.getItemAtPosition(i));
|
|
||||||
Intent intent = new Intent(CampaignActivity.this, ContributionsActivity.class);
|
|
||||||
intent.putExtra("campaign", c);
|
|
||||||
startActivity(intent);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
getSupportLoaderManager().initLoader(0, null, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
|
|
||||||
return new CursorLoader(this, CampaignsContentProvider.BASE_URI, Campaign.Table.ALL_FIELDS, "", null, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
|
|
||||||
if(campaignsListAdapter == null) {
|
|
||||||
campaignsListAdapter = new CampaignsListAdapter(this, cursor, 0);
|
|
||||||
campaignsListView.setAdapter(campaignsListAdapter);
|
|
||||||
} else {
|
|
||||||
campaignsListAdapter.swapCursor(cursor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onLoaderReset(Loader<Cursor> cursorLoader) {
|
|
||||||
campaignsListAdapter.swapCursor(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
package fr.free.nrw.commons.campaigns;
|
|
||||||
|
|
||||||
import android.net.Uri;
|
|
||||||
import fr.free.nrw.commons.contributions.Contribution;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
public class CampaignContribution extends Contribution {
|
|
||||||
private Campaign campaign;
|
|
||||||
|
|
||||||
private ArrayList<String> fieldValues;
|
|
||||||
|
|
||||||
|
|
||||||
public CampaignContribution(Uri localUri, String remoteUri, String filename, String description, long dataLength, Date dateCreated, Date dateUploaded, String creator, String editSummary, Campaign campaign) {
|
|
||||||
super(localUri, remoteUri, filename, description, dataLength, dateCreated, dateUploaded, creator, editSummary);
|
|
||||||
this.campaign = campaign;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Campaign getCampaign() {
|
|
||||||
return campaign;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCampaign(Campaign campaign) {
|
|
||||||
this.campaign = campaign;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getTrackingTemplates() {
|
|
||||||
StringBuffer buffer = new StringBuffer();
|
|
||||||
if(campaign.getAutoAddWikitext() != null) {
|
|
||||||
buffer.append(campaign.getAutoAddWikitext()).append("\n");
|
|
||||||
}
|
|
||||||
if(campaign.getAutoAddCategories() != null && campaign.getAutoAddCategories().size() != 0) {
|
|
||||||
for(String cat : campaign.getAutoAddCategories()) {
|
|
||||||
buffer.append("[[Category:").append(cat).append("]]").append("\n");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
buffer.append("{{subst:unc}}\n");
|
|
||||||
}
|
|
||||||
buffer.append("[[Category:").append(campaign.getTrackingCategory()).append("]]").append("\n");
|
|
||||||
return buffer.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getDescription() {
|
|
||||||
return super.getDescription();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,206 +0,0 @@
|
||||||
package fr.free.nrw.commons.campaigns;
|
|
||||||
|
|
||||||
import android.content.ContentProvider;
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.content.UriMatcher;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.database.sqlite.SQLiteQueryBuilder;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.util.Log;
|
|
||||||
import fr.free.nrw.commons.CommonsApplication;
|
|
||||||
import fr.free.nrw.commons.data.DBOpenHelper;
|
|
||||||
|
|
||||||
public class CampaignsContentProvider extends ContentProvider{
|
|
||||||
|
|
||||||
private static final int CAMPAIGNS = 1;
|
|
||||||
private static final int CAMPAIGNS_ID = 2;
|
|
||||||
|
|
||||||
public static final String AUTHORITY = "fr.free.nrw.commons.campaigns.contentprovider";
|
|
||||||
private static final String BASE_PATH = "campiagns";
|
|
||||||
|
|
||||||
public static final Uri BASE_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
|
|
||||||
|
|
||||||
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
||||||
static {
|
|
||||||
uriMatcher.addURI(AUTHORITY, BASE_PATH, CAMPAIGNS);
|
|
||||||
uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", CAMPAIGNS_ID);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static Uri uriForId(int id) {
|
|
||||||
return Uri.parse(BASE_URI.toString() + "/" + id);
|
|
||||||
}
|
|
||||||
|
|
||||||
private DBOpenHelper dbOpenHelper;
|
|
||||||
@Override
|
|
||||||
public boolean onCreate() {
|
|
||||||
dbOpenHelper = ((CommonsApplication)this.getContext().getApplicationContext()).getDbOpenHelper();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
|
|
||||||
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
|
|
||||||
queryBuilder.setTables(Campaign.Table.TABLE_NAME);
|
|
||||||
|
|
||||||
int uriType = uriMatcher.match(uri);
|
|
||||||
|
|
||||||
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
|
|
||||||
Cursor cursor;
|
|
||||||
|
|
||||||
switch(uriType) {
|
|
||||||
case CAMPAIGNS:
|
|
||||||
cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
|
|
||||||
break;
|
|
||||||
case CAMPAIGNS_ID:
|
|
||||||
cursor = queryBuilder.query(db,
|
|
||||||
Campaign.Table.ALL_FIELDS,
|
|
||||||
"_id = ?",
|
|
||||||
new String[] { uri.getLastPathSegment() },
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
sortOrder
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI" + uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor.setNotificationUri(getContext().getContentResolver(), uri);
|
|
||||||
|
|
||||||
return cursor;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getType(Uri uri) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Uri insert(Uri uri, ContentValues contentValues) {
|
|
||||||
int uriType = uriMatcher.match(uri);
|
|
||||||
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
|
|
||||||
long id = 0;
|
|
||||||
switch (uriType) {
|
|
||||||
case CAMPAIGNS:
|
|
||||||
sqlDB.beginTransaction();
|
|
||||||
// if the campaign already exists, rip it out and then re-insert
|
|
||||||
if(campaignExists(sqlDB, contentValues)) {
|
|
||||||
sqlDB.delete(
|
|
||||||
Campaign.Table.TABLE_NAME,
|
|
||||||
Campaign.Table.COLUMN_NAME + " = ?",
|
|
||||||
new String[]{contentValues.getAsString(Campaign.Table.COLUMN_NAME)}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
id = sqlDB.insert(Campaign.Table.TABLE_NAME, null, contentValues);
|
|
||||||
sqlDB.endTransaction();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI: " + uri);
|
|
||||||
}
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return Uri.parse(BASE_URI + "/" + id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int delete(Uri uri, String s, String[] strings) {
|
|
||||||
int rows = 0;
|
|
||||||
int uriType = uriMatcher.match(uri);
|
|
||||||
|
|
||||||
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
|
|
||||||
|
|
||||||
switch(uriType) {
|
|
||||||
case CAMPAIGNS_ID:
|
|
||||||
rows = db.delete(Campaign.Table.TABLE_NAME,
|
|
||||||
"_id = ?",
|
|
||||||
new String[] { uri.getLastPathSegment() }
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI" + uri);
|
|
||||||
}
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean campaignExists(SQLiteDatabase db, ContentValues campaign) {
|
|
||||||
Cursor cr = db.query(
|
|
||||||
Campaign.Table.TABLE_NAME,
|
|
||||||
new String[]{Campaign.Table.COLUMN_NAME},
|
|
||||||
Campaign.Table.COLUMN_NAME + " = ?",
|
|
||||||
new String[]{campaign.getAsString(Campaign.Table.COLUMN_NAME)},
|
|
||||||
"", "", ""
|
|
||||||
);
|
|
||||||
return cr != null && cr.getCount() != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int bulkInsert(Uri uri, ContentValues[] values) {
|
|
||||||
Log.d("Commons", "Hello, bulk insert!");
|
|
||||||
int uriType = uriMatcher.match(uri);
|
|
||||||
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
|
|
||||||
sqlDB.beginTransaction();
|
|
||||||
switch (uriType) {
|
|
||||||
case CAMPAIGNS:
|
|
||||||
for(ContentValues value: values) {
|
|
||||||
Log.d("Commons", "Inserting! " + value.toString());
|
|
||||||
// if the campaign already exists, rip it out and then re-insert
|
|
||||||
if(campaignExists(sqlDB, value)) {
|
|
||||||
sqlDB.delete(
|
|
||||||
Campaign.Table.TABLE_NAME,
|
|
||||||
Campaign.Table.COLUMN_NAME + " = ?",
|
|
||||||
new String[]{value.getAsString(Campaign.Table.COLUMN_NAME)}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
sqlDB.insert(Campaign.Table.TABLE_NAME, null, value);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI: " + uri);
|
|
||||||
}
|
|
||||||
sqlDB.setTransactionSuccessful();
|
|
||||||
sqlDB.endTransaction();
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return values.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
|
|
||||||
/*
|
|
||||||
SQL Injection warnings: First, note that we're not exposing this to the outside world (exported="false")
|
|
||||||
Even then, we should make sure to sanitize all user input appropriately. Input that passes through ContentValues
|
|
||||||
should be fine. So only issues are those that pass in via concating.
|
|
||||||
|
|
||||||
In here, the only concat created argument is for id. It is cast to an int, and will error out otherwise.
|
|
||||||
*/
|
|
||||||
int uriType = uriMatcher.match(uri);
|
|
||||||
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
|
|
||||||
int rowsUpdated = 0;
|
|
||||||
switch (uriType) {
|
|
||||||
case CAMPAIGNS:
|
|
||||||
rowsUpdated = sqlDB.update(Campaign.Table.TABLE_NAME,
|
|
||||||
contentValues,
|
|
||||||
selection,
|
|
||||||
selectionArgs);
|
|
||||||
break;
|
|
||||||
case CAMPAIGNS_ID:
|
|
||||||
int id = Integer.valueOf(uri.getLastPathSegment());
|
|
||||||
|
|
||||||
if (TextUtils.isEmpty(selection)) {
|
|
||||||
rowsUpdated = sqlDB.update(Campaign.Table.TABLE_NAME,
|
|
||||||
contentValues,
|
|
||||||
Campaign.Table.COLUMN_ID + " = ?",
|
|
||||||
new String[] { String.valueOf(id) } );
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("Parameter `selection` should be empty when updating an ID");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI: " + uri + " with type " + uriType);
|
|
||||||
}
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return rowsUpdated;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,38 +0,0 @@
|
||||||
package fr.free.nrw.commons.campaigns;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.support.v4.widget.CursorAdapter;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.TextView;
|
|
||||||
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
|
||||||
import fr.free.nrw.commons.Utils;
|
|
||||||
|
|
||||||
class CampaignsListAdapter extends CursorAdapter {
|
|
||||||
|
|
||||||
private DisplayImageOptions contributionDisplayOptions = Utils.getGenericDisplayOptions().build();;
|
|
||||||
private Activity activity;
|
|
||||||
|
|
||||||
public CampaignsListAdapter(Activity activity, Cursor c, int flags) {
|
|
||||||
super(activity, c, flags);
|
|
||||||
this.activity = activity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
|
|
||||||
View parent = activity.getLayoutInflater().inflate(android.R.layout.simple_list_item_1, viewGroup, false);
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void bindView(View view, Context context, Cursor cursor) {
|
|
||||||
TextView campaignName = (TextView)view.findViewById(android.R.id.text1);
|
|
||||||
|
|
||||||
Campaign campaign = Campaign.fromCursor(cursor);
|
|
||||||
|
|
||||||
campaignName.setText(campaign.getTitle());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,90 +0,0 @@
|
||||||
package fr.free.nrw.commons.campaigns;
|
|
||||||
|
|
||||||
import android.accounts.Account;
|
|
||||||
import android.content.*;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.os.RemoteException;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.util.Log;
|
|
||||||
import org.mediawiki.api.ApiResult;
|
|
||||||
import org.mediawiki.api.MWApi;
|
|
||||||
import fr.free.nrw.commons.CommonsApplication;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
|
|
||||||
public class CampaignsSyncAdapter extends AbstractThreadedSyncAdapter {
|
|
||||||
private static int COMMIT_THRESHOLD = 10;
|
|
||||||
public CampaignsSyncAdapter(Context context, boolean autoInitialize) {
|
|
||||||
super(context, autoInitialize);
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getLimit() {
|
|
||||||
return 500; // FIXME: Parameterize!
|
|
||||||
}
|
|
||||||
|
|
||||||
@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();
|
|
||||||
ApiResult result;
|
|
||||||
Boolean done = false;
|
|
||||||
String queryContinue = null;
|
|
||||||
while(!done) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
MWApi.RequestBuilder builder = api.action("query")
|
|
||||||
.param("list", "allcampaigns")
|
|
||||||
// Disabled, since we want to modify local state if the campaign was disabled
|
|
||||||
// FIXME: To be more effecient, delete the disabled campaigns locally
|
|
||||||
//.param("ucenabledonly", "true")
|
|
||||||
.param("uclimit", getLimit());
|
|
||||||
if(!TextUtils.isEmpty(queryContinue)) {
|
|
||||||
builder.param("uccontinue", 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayList<ApiResult> campaigns = result.getNodes("/api/query/allcampaigns/campaign");
|
|
||||||
Log.d("Commons", campaigns.size() + " results!");
|
|
||||||
ArrayList<ContentValues> campaignValues = new ArrayList<ContentValues>();
|
|
||||||
for(ApiResult campaignItem: campaigns) {
|
|
||||||
String name = campaignItem.getString("@name");
|
|
||||||
String body = campaignItem.getString(".");
|
|
||||||
Log.d("Commons", "Campaign body is " + body);
|
|
||||||
String trackingCat = campaignItem.getString("@trackingCategory");
|
|
||||||
Campaign campaign = Campaign.parse(name, body, trackingCat);
|
|
||||||
campaignValues.add(campaign.toContentValues());
|
|
||||||
|
|
||||||
if(campaignValues.size() % COMMIT_THRESHOLD == 0) {
|
|
||||||
try {
|
|
||||||
contentProviderClient.bulkInsert(CampaignsContentProvider.BASE_URI, campaignValues.toArray(new ContentValues[]{}));
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
campaignValues.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(campaignValues.size() != 0) {
|
|
||||||
try {
|
|
||||||
contentProviderClient.bulkInsert(CampaignsContentProvider.BASE_URI, campaignValues.toArray(new ContentValues[]{}));
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
queryContinue = result.getString("/api/query-continue/allcampaigns/@uccontinue");
|
|
||||||
if(TextUtils.isEmpty(queryContinue)) {
|
|
||||||
done = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
package fr.free.nrw.commons.campaigns;
|
|
||||||
|
|
||||||
import android.app.Service;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.IBinder;
|
|
||||||
|
|
||||||
public class CampaignsSyncService extends Service {
|
|
||||||
|
|
||||||
private static final Object sSyncAdapterLock = new Object();
|
|
||||||
|
|
||||||
private static CampaignsSyncAdapter sSyncAdapter = null;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate() {
|
|
||||||
synchronized (sSyncAdapterLock) {
|
|
||||||
if (sSyncAdapter == null) {
|
|
||||||
sSyncAdapter = new CampaignsSyncAdapter(getApplicationContext(), true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public IBinder onBind(Intent intent) {
|
|
||||||
return sSyncAdapter.getSyncAdapterBinder();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -22,7 +22,6 @@ import fr.free.nrw.commons.auth.*;
|
||||||
import fr.free.nrw.commons.CommonsApplication;
|
import fr.free.nrw.commons.CommonsApplication;
|
||||||
import fr.free.nrw.commons.HandlerService;
|
import fr.free.nrw.commons.HandlerService;
|
||||||
import fr.free.nrw.commons.Media;
|
import fr.free.nrw.commons.Media;
|
||||||
import fr.free.nrw.commons.campaigns.Campaign;
|
|
||||||
import fr.free.nrw.commons.media.*;
|
import fr.free.nrw.commons.media.*;
|
||||||
import fr.free.nrw.commons.upload.UploadService;
|
import fr.free.nrw.commons.upload.UploadService;
|
||||||
|
|
||||||
|
|
@ -33,7 +32,6 @@ public class ContributionsActivity
|
||||||
implements LoaderManager.LoaderCallbacks<Object>,
|
implements LoaderManager.LoaderCallbacks<Object>,
|
||||||
AdapterView.OnItemClickListener,
|
AdapterView.OnItemClickListener,
|
||||||
MediaDetailPagerFragment.MediaDetailProvider,
|
MediaDetailPagerFragment.MediaDetailProvider,
|
||||||
ContributionsListFragment.CurrentCampaignProvider,
|
|
||||||
FragmentManager.OnBackStackChangedListener,
|
FragmentManager.OnBackStackChangedListener,
|
||||||
ContributionsListFragment.SourceRefresher {
|
ContributionsListFragment.SourceRefresher {
|
||||||
|
|
||||||
|
|
@ -43,7 +41,6 @@ public class ContributionsActivity
|
||||||
private MediaDetailPagerFragment mediaDetails;
|
private MediaDetailPagerFragment mediaDetails;
|
||||||
private ArrayList<DataSetObserver> observersWaitingForLoad = new ArrayList<DataSetObserver>();
|
private ArrayList<DataSetObserver> observersWaitingForLoad = new ArrayList<DataSetObserver>();
|
||||||
|
|
||||||
private Campaign campaign;
|
|
||||||
|
|
||||||
public ContributionsActivity() {
|
public ContributionsActivity() {
|
||||||
super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
|
super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
|
||||||
|
|
@ -115,10 +112,6 @@ public class ContributionsActivity
|
||||||
setTitle(R.string.title_activity_contributions);
|
setTitle(R.string.title_activity_contributions);
|
||||||
setContentView(R.layout.activity_contributions);
|
setContentView(R.layout.activity_contributions);
|
||||||
|
|
||||||
if(getIntent().hasExtra("campaign")) {
|
|
||||||
this.campaign = (Campaign) getIntent().getSerializableExtra("campaign");
|
|
||||||
this.setTitle(campaign.getTitle());
|
|
||||||
}
|
|
||||||
|
|
||||||
contributionsList = (ContributionsListFragment)getSupportFragmentManager().findFragmentById(R.id.contributionsListFragment);
|
contributionsList = (ContributionsListFragment)getSupportFragmentManager().findFragmentById(R.id.contributionsListFragment);
|
||||||
|
|
||||||
|
|
@ -208,49 +201,35 @@ public class ContributionsActivity
|
||||||
}
|
}
|
||||||
|
|
||||||
public Loader onCreateLoader(int i, Bundle bundle) {
|
public Loader onCreateLoader(int i, Bundle bundle) {
|
||||||
if(campaign == null) {
|
return new CursorLoader(this, ContributionsContentProvider.BASE_URI, Contribution.Table.ALL_FIELDS, CONTRIBUTION_SELECTION, null, CONTRIBUTION_SORT);
|
||||||
return new CursorLoader(this, ContributionsContentProvider.BASE_URI, Contribution.Table.ALL_FIELDS, CONTRIBUTION_SELECTION, null, CONTRIBUTION_SORT);
|
|
||||||
} else {
|
|
||||||
return new CategoryImagesLoader(this, campaign.getTrackingCategory());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onLoadFinished(Loader cursorLoader, Object result) {
|
public void onLoadFinished(Loader cursorLoader, Object result) {
|
||||||
if(campaign == null) {
|
|
||||||
Cursor cursor = (Cursor) result;
|
|
||||||
if(contributionsList.getAdapter() == null) {
|
|
||||||
contributionsList.setAdapter(new ContributionsListAdapter(this, cursor, 0));
|
|
||||||
} else {
|
|
||||||
((CursorAdapter)contributionsList.getAdapter()).swapCursor(cursor);
|
|
||||||
}
|
|
||||||
|
|
||||||
getSupportActionBar().setSubtitle(getResources().getQuantityString(R.plurals.contributions_subtitle, cursor.getCount(), cursor.getCount()));
|
Cursor cursor = (Cursor) result;
|
||||||
|
if(contributionsList.getAdapter() == null) {
|
||||||
|
contributionsList.setAdapter(new ContributionsListAdapter(this, cursor, 0));
|
||||||
} else {
|
} else {
|
||||||
if(contributionsList.getAdapter() == null) {
|
((CursorAdapter)contributionsList.getAdapter()).swapCursor(cursor);
|
||||||
contributionsList.setAdapter(new MediaListAdapter(this, (ArrayList<Media>) result));
|
|
||||||
} else {
|
|
||||||
((MediaListAdapter)contributionsList.getAdapter()).updateMediaList((ArrayList<Media>) result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSupportActionBar().setSubtitle(getResources().getQuantityString(R.plurals.contributions_subtitle, cursor.getCount(), cursor.getCount()));
|
||||||
|
|
||||||
notifyAndMigrateDataSetObservers();
|
notifyAndMigrateDataSetObservers();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onLoaderReset(Loader cursorLoader) {
|
public void onLoaderReset(Loader cursorLoader) {
|
||||||
if(campaign == null) {
|
|
||||||
((CursorAdapter) contributionsList.getAdapter()).swapCursor(null);
|
((CursorAdapter) contributionsList.getAdapter()).swapCursor(null);
|
||||||
} else {
|
|
||||||
contributionsList.setAdapter(null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Media getMediaAtPosition(int i) {
|
public Media getMediaAtPosition(int i) {
|
||||||
if (contributionsList.getAdapter() == null) {
|
if (contributionsList.getAdapter() == null) {
|
||||||
// not yet ready to return data
|
// not yet ready to return data
|
||||||
return null;
|
return null;
|
||||||
} else if(campaign == null) {
|
} else {
|
||||||
return Contribution.fromCursor((Cursor) contributionsList.getAdapter().getItem(i));
|
return Contribution.fromCursor((Cursor) contributionsList.getAdapter().getItem(i));
|
||||||
} else {
|
|
||||||
return (Media) contributionsList.getAdapter().getItem(i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -306,9 +285,6 @@ public class ContributionsActivity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Campaign getCurrentCampaign() {
|
|
||||||
return campaign;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void refreshSource() {
|
public void refreshSource() {
|
||||||
getSupportLoaderManager().restartLoader(0, null, this);
|
getSupportLoaderManager().restartLoader(0, null, this);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue