mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Merge remote-tracking branch 'refs/remotes/commons-app/master'
This commit is contained in:
commit
a83099f0c7
5 changed files with 161 additions and 16 deletions
|
|
@ -33,6 +33,10 @@ import android.widget.ProgressBar;
|
|||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.mediawiki.api.ApiResult;
|
||||
import org.mediawiki.api.MWApi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
|
@ -41,8 +45,10 @@ import java.util.LinkedHashSet;
|
|||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.Utils;
|
||||
import fr.free.nrw.commons.upload.MwVolleyApi;
|
||||
|
|
@ -73,6 +79,9 @@ public class CategorizationFragment extends Fragment {
|
|||
PrefixUpdater prefixUpdaterSub;
|
||||
MethodAUpdater methodAUpdaterSub;
|
||||
|
||||
private final ArrayList<String> titleCatItems = new ArrayList<String>();
|
||||
final CountDownLatch mergeLatch = new CountDownLatch(1);
|
||||
|
||||
private ContentProviderClient client;
|
||||
|
||||
protected final static int SEARCH_CATS_LIMIT = 25;
|
||||
|
|
@ -113,12 +122,48 @@ public class CategorizationFragment extends Fragment {
|
|||
}
|
||||
|
||||
/**
|
||||
* Retrieves recently-used categories and nearby categories, and merges them without duplicates.
|
||||
* @return a list containing these categories
|
||||
* Retrieves category suggestions from title input
|
||||
* @return a list containing title-related categories
|
||||
*/
|
||||
protected ArrayList<String> titleCatQuery() {
|
||||
|
||||
TitleCategories titleCategoriesSub;
|
||||
|
||||
//Retrieve the title that was saved when user tapped submit icon
|
||||
SharedPreferences titleDesc = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||
String title = titleDesc.getString("Title", "");
|
||||
Log.d(TAG, "Title: " + title);
|
||||
|
||||
//Override onPostExecute to access the results of async API call
|
||||
titleCategoriesSub = new TitleCategories(title) {
|
||||
@Override
|
||||
protected void onPostExecute(ArrayList<String> result) {
|
||||
super.onPostExecute(result);
|
||||
Log.d(TAG, "Results in onPostExecute: " + result);
|
||||
titleCatItems.addAll(result);
|
||||
Log.d(TAG, "TitleCatItems in onPostExecute: " + titleCatItems);
|
||||
mergeLatch.countDown();
|
||||
}
|
||||
};
|
||||
|
||||
Utils.executeAsyncTask(titleCategoriesSub);
|
||||
Log.d(TAG, "TitleCatItems in titleCatQuery: " + titleCatItems);
|
||||
|
||||
//Only return titleCatItems after API call has finished
|
||||
try {
|
||||
mergeLatch.await();
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(TAG, "Interrupted exception: ", e);
|
||||
}
|
||||
return titleCatItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves recently-used categories
|
||||
* @return a list containing recent categories
|
||||
*/
|
||||
protected ArrayList<String> recentCatQuery() {
|
||||
ArrayList<String> items = new ArrayList<String>();
|
||||
Set<String> mergedItems = new LinkedHashSet<String>();
|
||||
|
||||
try {
|
||||
Cursor cursor = client.query(
|
||||
|
|
@ -133,23 +178,49 @@ public class CategorizationFragment extends Fragment {
|
|||
items.add(cat.getName());
|
||||
}
|
||||
cursor.close();
|
||||
|
||||
if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true) {
|
||||
//Log.d(TAG, "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString());
|
||||
List<String> gpsItems = new ArrayList<String>(MwVolleyApi.getGpsCat());
|
||||
//Log.d(TAG, "GPS items: " + gpsItems.toString());
|
||||
|
||||
mergedItems.addAll(gpsItems);
|
||||
}
|
||||
|
||||
mergedItems.addAll(items);
|
||||
}
|
||||
catch (RemoteException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges nearby categories, categories suggested based on title, and recent categories... without duplicates.
|
||||
* @return a list containing merged categories
|
||||
*/
|
||||
protected ArrayList<String> mergeItems() {
|
||||
|
||||
Set<String> mergedItems = new LinkedHashSet<String>();
|
||||
|
||||
Log.d(TAG, "Calling APIs for GPS cats, title cats and recent cats...");
|
||||
|
||||
List<String> gpsItems = new ArrayList<String>();
|
||||
if (MwVolleyApi.GpsCatExists.getGpsCatExists()) {
|
||||
gpsItems.addAll(MwVolleyApi.getGpsCat());
|
||||
}
|
||||
List<String> titleItems = new ArrayList<String>(titleCatQuery());
|
||||
List<String> recentItems = new ArrayList<String>(recentCatQuery());
|
||||
|
||||
//Await results of titleItems, which is likely to come in last
|
||||
try {
|
||||
mergeLatch.await();
|
||||
Log.d(TAG, "Waited for merge");
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(TAG, "Interrupted Exception: ", e);
|
||||
}
|
||||
|
||||
mergedItems.addAll(gpsItems);
|
||||
Log.d(TAG, "Adding GPS items: " + gpsItems);
|
||||
mergedItems.addAll(titleItems);
|
||||
Log.d(TAG, "Adding title items: " + titleItems);
|
||||
mergedItems.addAll(recentItems);
|
||||
Log.d(TAG, "Adding recent items: " + recentItems);
|
||||
|
||||
//Needs to be an ArrayList and not a List unless we want to modify a big portion of preexisting code
|
||||
ArrayList<String> mergedItemsList = new ArrayList<String>(mergedItems);
|
||||
Log.d(TAG, "Merged item list: " + mergedItemsList);
|
||||
|
||||
return mergedItemsList;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ public class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
|
|||
protected ArrayList<String> doInBackground(Void... voids) {
|
||||
//If user hasn't typed anything in yet, get GPS and recent items
|
||||
if(TextUtils.isEmpty(filter)) {
|
||||
return catFragment.recentCatQuery();
|
||||
return catFragment.mergeItems();
|
||||
}
|
||||
|
||||
//if user types in something that is in cache, return cached category
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public class PrefixUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
|
|||
protected ArrayList<String> doInBackground(Void... voids) {
|
||||
//If user hasn't typed anything in yet, get GPS and recent items
|
||||
if(TextUtils.isEmpty(filter)) {
|
||||
return catFragment.recentCatQuery();
|
||||
return catFragment.mergeItems();
|
||||
}
|
||||
|
||||
//if user types in something that is in cache, return cached category
|
||||
|
|
|
|||
|
|
@ -0,0 +1,73 @@
|
|||
package fr.free.nrw.commons.category;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import org.mediawiki.api.ApiResult;
|
||||
import org.mediawiki.api.MWApi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
|
||||
/**
|
||||
* Sends asynchronous queries to the Commons MediaWiki API to retrieve categories that are related to
|
||||
* the title entered in previous screen. The 'srsearch' action-specific parameter is used for this
|
||||
* purpose. This class should be subclassed in CategorizationFragment.java to add the results to recent and GPS cats.
|
||||
*/
|
||||
public class TitleCategories extends AsyncTask<Void, Void, ArrayList<String>> {
|
||||
|
||||
private final static int SEARCH_CATS_LIMIT = 25;
|
||||
private static final String TAG = TitleCategories.class.getName();
|
||||
private String title;
|
||||
|
||||
public TitleCategories(String title) {
|
||||
super();
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ArrayList<String> doInBackground(Void... voids) {
|
||||
|
||||
MWApi api = CommonsApplication.createMWApi();
|
||||
ApiResult result;
|
||||
ArrayList<String> items = new ArrayList<>();
|
||||
|
||||
//URL https://commons.wikimedia.org/w/api.php?action=query&format=xml&list=search&srwhat=text&srenablerewrites=1&srnamespace=14&srlimit=10&srsearch=
|
||||
try {
|
||||
result = api.action("query")
|
||||
.param("format", "xml")
|
||||
.param("list", "search")
|
||||
.param("srwhat", "text")
|
||||
.param("srnamespace", "14")
|
||||
.param("srlimit", SEARCH_CATS_LIMIT)
|
||||
.param("srsearch", title)
|
||||
.get();
|
||||
Log.d(TAG, "Searching for cats for title: " + result.toString());
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "IO Exception: ", e);
|
||||
//Return empty arraylist
|
||||
return items;
|
||||
}
|
||||
|
||||
ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/search/p/@title");
|
||||
for(ApiResult categoryNode: categoryNodes) {
|
||||
String cat = categoryNode.getDocument().getTextContent();
|
||||
String catString = cat.replace("Category:", "");
|
||||
items.add(catString);
|
||||
}
|
||||
|
||||
Log.d(TAG, "Title cat query results: " + items);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -52,12 +52,13 @@ public class SingleUploadFragment extends Fragment {
|
|||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
//What happens when the 'submit' icon is tapped
|
||||
case R.id.menu_upload_single:
|
||||
|
||||
String title = titleEdit.getText().toString();
|
||||
String desc = descEdit.getText().toString();
|
||||
|
||||
//Save the values of these fields in short-lived cache so next time this fragment is loaded, we can access these
|
||||
//Save the title/desc in short-lived cache so next time this fragment is loaded, we can access these
|
||||
SharedPreferences titleDesc = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||
SharedPreferences.Editor editor = titleDesc.edit();
|
||||
editor.putString("Title", title);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue