apps-android-commons/app/src/main/java/fr/free/nrw/commons/Media.java
Ujjwal Agrawal 86878fb62d Add feature to Browse commons via app (#1716)
* Search activity, image search fragment added

* Removed explore icon xml

* Updated Javadocs for search Activity and Navigation Base Activity

* SearchImageItem class updated

* Javadocs added for search activity

* removed redundant code and added javadocs for search image modules

* Javadocs added for updateImageList  method

* Rename featuredImage to explore

* Fixed null query issue

* changed cisibility to gone in case of successful Fetch

* Consolidate the networking libraries - drop volley in favor of OkHttp

* Extracted a few networking related items into a new Dagger module and finished the process of mocking the main component for tests.

* Refactoring to extract GpsCategoryModel and ensure single-responsibility-principle is maintained in CategoryApi.

* fixed featured image back bug

* Localisation updates from https://translatewiki.net.

* Javadocs added

* Add option to set image as wallpaper (#1535)

* Add option to set image as wallpaper

* Added java docs

* Toast message on setting the wallpaper successfully

* Localisation updates from https://translatewiki.net.

* SearchHistory Fragment added

* Search History Item added

* Content Provider, RecentSearchesDao added

* Database version changed to 7 and added methods for find, save ,..

* Delete icon deleted

* Reverted changes in gradle files

* Reverted changes in gradle files 2

* Optimized Improts

* reverted refractor for container name

* Refactored packagename, changed name to query in POJO class.

* Updated lastUsed to lastSearched

* Javadocs updated

* Check if user has been in search page for 5 seconds if yes then save it to history

* If else indentation updated

* added import in test

* edittext replaced with Searchview

* RxSearchview added

* Added support for API 21+

* Snackbar removed on success

* Improved code

* Pagination added

* Removed unnecessary toast

* Comment added in method

* Support for landscape mode added

* Fixed screen rotation issue on Explore and Search activity

* Clear focus added

* Delete all function added in Content Provider and called from fragment

* Scrollbar Recyclerview added

* Share Icon changed to 32 dp and back button added in explore, search activity.

* Removed unnecessary code

* Wrote and run tests for Recent Searches
 (creating db, migrating from versions, deletion, finding,..

* Category Search Fragment added

* Adapter factory added

* Renderer added

* Improvements

* Viewpager adapter added

* Updated XML

* Improvements in category card design

* tabs colors changed

* renamed images to media

* Java docs improved

* Javadoc added for setTabs

* JavaDoc for ViewPagerAdapter added

* Refreshed listview after delete

* Added mediaContainer

* Fixed ghost issue in image search fragment

* Ghost issue for categories fixed

* Removed Calling API call onback press

* Category Details activity added

* Menu added in category details activity

* back button added

* back button bugs

* Improvements in category images fragment

* JavaDoc added for some methods

* trimming added, Tab layout hided, recent searches refreshed

* SubCategory list fragment added, API added to extract subCategory Details

* API params updated to get more precise results

* Javadocs added for MWAPI method

* Pagination removed

* Fix API for fetching images inside category

* Parent category API added

* Fix #1704

* Fix #1704 corrected

* Fix #1702

* Fix #1702 and #1704

* added try catch statements

* Optimimzed imports

* loops replaced with Functions

* Javadocs for various methods added

* Fix java docs for Dao

* Javadocs for various methods added

* Fix java docs for Dao

* More javadocs added for explore Feature

* Javadocs added

* Javadocs added

* Improvements in indentation (#1739)
2018-07-24 10:17:24 +03:00

428 lines
11 KiB
Java

package fr.free.nrw.commons;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.free.nrw.commons.location.LatLng;
public class Media implements Parcelable {
public static Creator<Media> CREATOR = new Creator<Media>() {
@Override
public Media createFromParcel(Parcel parcel) {
return new Media(parcel);
}
@Override
public Media[] newArray(int i) {
return new Media[0];
}
};
private static Pattern displayTitlePattern = Pattern.compile("(.*)(\\.\\w+)", Pattern.CASE_INSENSITIVE);
// Primary metadata fields
protected Uri localUri;
protected String imageUrl;
protected String filename;
protected String description; // monolingual description on input...
protected long dataLength;
protected Date dateCreated;
protected @Nullable Date dateUploaded;
protected int width;
protected int height;
protected String license;
protected String creator;
protected ArrayList<String> categories; // as loaded at runtime?
protected boolean requestedDeletion;
private Map<String, String> descriptions; // multilingual descriptions as loaded
private HashMap<String, Object> tags = new HashMap<>();
private @Nullable LatLng coordinates;
/**
* Provides local constructor
*/
protected Media() {
this.categories = new ArrayList<>();
this.descriptions = new HashMap<>();
}
/**
* Provides a minimal constructor
*
* @param filename Media filename
*/
public Media(String filename) {
this();
this.filename = filename;
}
/**
* Provide Media constructor
* @param localUri Media URI
* @param imageUrl Media image URL
* @param filename Media filename
* @param description Media description
* @param dataLength Media date length
* @param dateCreated Media creation date
* @param dateUploaded Media date uploaded
* @param creator Media creator
*/
public Media(Uri localUri, String imageUrl, String filename, String description,
long dataLength, Date dateCreated, @Nullable Date dateUploaded, String creator) {
this();
this.localUri = localUri;
this.imageUrl = imageUrl;
this.filename = filename;
this.description = description;
this.dataLength = dataLength;
this.dateCreated = dateCreated;
this.dateUploaded = dateUploaded;
this.creator = creator;
}
@SuppressWarnings("unchecked")
public Media(Parcel in) {
localUri = in.readParcelable(Uri.class.getClassLoader());
imageUrl = in.readString();
filename = in.readString();
description = in.readString();
dataLength = in.readLong();
dateCreated = (Date) in.readSerializable();
dateUploaded = (Date) in.readSerializable();
creator = in.readString();
tags = (HashMap<String, Object>) in.readSerializable();
width = in.readInt();
height = in.readInt();
license = in.readString();
if (categories != null) {
in.readStringList(categories);
}
descriptions = in.readHashMap(ClassLoader.getSystemClassLoader());
}
/**
* Gets tag of media
* @param key Media key
* @return Media tag
*/
public Object getTag(String key) {
return tags.get(key);
}
/**
* Modifies( or creates a) tag of media
* @param key Media key
* @param value Media value
*/
public void setTag(String key, Object value) {
tags.put(key, value);
}
/**
* Gets media display title
* @return Media title
*/
public String getDisplayTitle() {
if (filename == null) {
return "";
}
// FIXME: Gross hack because my regex skills suck maybe or I am too lazy who knows
String title = getFilePageTitle().getDisplayText().replaceFirst("^File:", "");
Matcher matcher = displayTitlePattern.matcher(title);
if (matcher.matches()) {
return matcher.group(1);
} else {
return title;
}
}
/**
* Gets file page title
* @return New media page title
*/
public PageTitle getFilePageTitle() {
return new PageTitle("File:" + getFilename().replaceFirst("^File:", ""));
}
/**
* Gets local URI
* @return Media local URI
*/
public Uri getLocalUri() {
return localUri;
}
/**
* Gets image URL
* can be null.
* @return Image URL
*/
@Nullable
public String getImageUrl() {
if (imageUrl == null && this.getFilename() != null) {
imageUrl = Utils.makeThumbBaseUrl(this.getFilename());
}
return imageUrl;
}
/**
* Gets the name of the file.
* @return file name as a string
*/
public String getFilename() {
return filename;
}
/**
* Sets the name of the file.
* @param filename the new name of the file
*/
public void setFilename(String filename) {
this.filename = filename;
}
/**
* Gets the file description.
* @return file description as a string
*/
public String getDescription() {
return description;
}
/**
* Sets the file description.
* @param description the new description of the file
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Gets the datalength of the file.
* @return file datalength as a long
*/
public long getDataLength() {
return dataLength;
}
/**
* Sets the datalength of the file.
* @param dataLength as a long
*/
public void setDataLength(long dataLength) {
this.dataLength = dataLength;
}
/**
* Gets the creation date of the file.
* @return creation date as a Date
*/
public Date getDateCreated() {
return dateCreated;
}
/**
* Sets the creation date of the file.
* @param date creation date as a Date
*/
public void setDateCreated(Date date) {
this.dateCreated = date;
}
/**
* Gets the upload date of the file.
* Can be null.
* @return upload date as a Date
*/
public @Nullable
Date getDateUploaded() {
return dateUploaded;
}
/**
* Gets the name of the creator of the file.
* @return creator name as a String
*/
public String getCreator() {
return creator;
}
/**
* Sets the creator name of the file.
* @param creator creator name as a string
*/
public void setCreator(String creator) {
this.creator = creator;
}
/**
* Gets the width of the media.
* @return file width as an int
*/
public int getWidth() {
return width;
}
/**
* Sets the width of the media.
* @param width file width as an int
*/
public void setWidth(int width) {
this.width = width;
}
/**
* Gets the height of the media.
* @return file height as an int
*/
public int getHeight() {
return height;
}
/**
* Sets the height of the media.
* @param height file height as an int
*/
public void setHeight(int height) {
this.height = height;
}
/**
* Gets the license name of the file.
* @return license as a String
*/
public String getLicense() {
return license;
}
/**
* Sets the license name of the file.
* @param license license name as a String
*/
public void setLicense(String license) {
this.license = license;
}
/**
* Gets the coordinates of where the file was created.
* @return file coordinates as a LatLng
*/
public @Nullable
LatLng getCoordinates() {
return coordinates;
}
/**
* Sets the coordinates of where the file was created.
* @param coordinates file coordinates as a LatLng
*/
public void setCoordinates(@Nullable LatLng coordinates) {
this.coordinates = coordinates;
}
/**
* Gets the categories the file falls under.
* @return file categories as an ArrayList of Strings
*/
@SuppressWarnings("unchecked")
public ArrayList<String> getCategories() {
return (ArrayList<String>) categories.clone(); // feels dirty
}
/**
* Sets the categories the file falls under.
* </p>
* Does not append: i.e. will clear the current categories
* and then add the specified ones.
* @param categories file categories as a list of Strings
*/
public void setCategories(List<String> categories) {
this.categories.clear();
this.categories.addAll(categories);
}
/**
* Modifies (or sets) media descriptions
* @param descriptions Media descriptions
*/
void setDescriptions(Map<String, String> descriptions) {
this.descriptions.clear();
this.descriptions.putAll(descriptions);
}
/**
* Gets media description in preferred language
* @param preferredLanguage Language preferred
* @return Description in preferred language
*/
public String getDescription(String preferredLanguage) {
if (descriptions.containsKey(preferredLanguage)) {
// See if the requested language is there.
return descriptions.get(preferredLanguage);
} else if (descriptions.containsKey("en")) {
// Ah, English. Language of the world, until the Chinese crush us.
return descriptions.get("en");
} else if (descriptions.containsKey("default")) {
// No languages marked...
return descriptions.get("default");
} else {
// FIXME: return the first available non-English description?
return "";
}
}
/**
* Method of Parcelable interface
* @return zero
*/
@Override
public int describeContents() {
return 0;
}
/**
* Creates a way to transfer information between two or more
* activities.
* @param parcel Instance of Parcel
* @param flags Parcel flag
*/
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeParcelable(localUri, flags);
parcel.writeString(imageUrl);
parcel.writeString(filename);
parcel.writeString(description);
parcel.writeLong(dataLength);
parcel.writeSerializable(dateCreated);
parcel.writeSerializable(dateUploaded);
parcel.writeString(creator);
parcel.writeSerializable(tags);
parcel.writeInt(width);
parcel.writeInt(height);
parcel.writeString(license);
parcel.writeStringList(categories);
parcel.writeMap(descriptions);
}
public void setRequestedDeletion(){
requestedDeletion = true;
}
public boolean getRequestedDeletion(){
return requestedDeletion;
}
}