mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Refactor the multiple uploads list view into a fragment
This commit is contained in:
parent
ad89f2564f
commit
154a559b86
5 changed files with 224 additions and 164 deletions
13
commons/res/layout/activity_multiple_uploads.xml
Normal file
13
commons/res/layout/activity_multiple_uploads.xml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<fragment
|
||||
android:name="org.wikimedia.commons.MultipleUploadListFragment"
|
||||
android:id="@+id/uploadsListFragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
/>
|
||||
</FrameLayout>
|
||||
|
|
@ -12,17 +12,10 @@ import android.os.IBinder;
|
|||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.*;
|
||||
import com.actionbarsherlock.view.Menu;
|
||||
import com.actionbarsherlock.view.MenuItem;
|
||||
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
|
||||
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
|
||||
import de.akquinet.android.androlog.Log;
|
||||
import org.wikimedia.commons.auth.AuthenticatedActivity;
|
||||
import org.wikimedia.commons.auth.WikiAccountAuthenticator;
|
||||
import org.wikimedia.commons.contributions.Contribution;
|
||||
|
|
@ -32,88 +25,14 @@ import java.util.concurrent.ExecutionException;
|
|||
|
||||
public class MultipleShareActivity extends AuthenticatedActivity {
|
||||
private CommonsApplication app;
|
||||
private GridView photosGrid;
|
||||
private PhotoDisplayAdapter photosAdapter;
|
||||
private ArrayList<PreparedUpload> photosList = new ArrayList<PreparedUpload>();
|
||||
private EditText baseTitle;
|
||||
private int picHeight;
|
||||
private DisplayImageOptions uploadDisplayOptions;
|
||||
private ArrayList<Contribution> photosList = new ArrayList<Contribution>();
|
||||
|
||||
private MultipleUploadListFragment uploadsList;
|
||||
|
||||
public MultipleShareActivity() {
|
||||
super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
|
||||
}
|
||||
|
||||
private static class UploadHolderView {
|
||||
Uri imageUri;
|
||||
|
||||
ImageView image;
|
||||
TextView title;
|
||||
}
|
||||
|
||||
private static class PreparedUpload {
|
||||
Uri uri;
|
||||
String title;
|
||||
boolean isDirty;
|
||||
int sequence;
|
||||
String mimeType;
|
||||
|
||||
private PreparedUpload(Uri uri, int seq) {
|
||||
this.uri = uri;
|
||||
this.sequence = seq;
|
||||
}
|
||||
}
|
||||
|
||||
private class PhotoDisplayAdapter extends BaseAdapter {
|
||||
|
||||
private ArrayList<PreparedUpload> urisList;
|
||||
|
||||
private PhotoDisplayAdapter(ArrayList<PreparedUpload> urisList) {
|
||||
this.urisList = urisList;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return urisList.size();
|
||||
}
|
||||
|
||||
public Object getItem(int i) {
|
||||
return urisList.get(i);
|
||||
}
|
||||
|
||||
public long getItemId(int i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
public View getView(int i, View view, ViewGroup viewGroup) {
|
||||
UploadHolderView holder;
|
||||
|
||||
if(view == null) {
|
||||
view = getLayoutInflater().inflate(R.layout.layout_upload_item, null);
|
||||
holder = new UploadHolderView();
|
||||
holder.image = (ImageView) view.findViewById(R.id.uploadImage);
|
||||
holder.title = (TextView) view.findViewById(R.id.uploadTitle);
|
||||
|
||||
holder.image.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, picHeight));
|
||||
|
||||
view.setTag(holder);
|
||||
} else {
|
||||
holder = (UploadHolderView)view.getTag();
|
||||
}
|
||||
|
||||
|
||||
PreparedUpload up = (PreparedUpload)this.getItem(i);
|
||||
|
||||
if(holder.imageUri == null || !holder.imageUri.equals(up.uri)) {
|
||||
ImageLoader.getInstance().displayImage(up.uri.toString(), holder.image, uploadDisplayOptions);
|
||||
holder.imageUri = up.uri;
|
||||
}
|
||||
|
||||
holder.title.setText(up.title);
|
||||
|
||||
return view;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private class StartMultipleUploadTask extends AsyncTask<Void, Integer, Void> {
|
||||
|
||||
ProgressDialog dialog;
|
||||
|
|
@ -121,15 +40,16 @@ public class MultipleShareActivity extends AuthenticatedActivity {
|
|||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
for(int i = 0; i < photosList.size(); i++) {
|
||||
PreparedUpload up = photosList.get(i);
|
||||
if(up.mimeType == null || TextUtils.isEmpty(up.mimeType) || up.mimeType.endsWith("*")) {
|
||||
String mimeType = getContentResolver().getType(up.uri);
|
||||
Contribution up = photosList.get(i);
|
||||
String curMimetype = (String)up.getTag("mimeType");
|
||||
if(curMimetype == null || TextUtils.isEmpty(curMimetype) || curMimetype.endsWith("*")) {
|
||||
String mimeType = getContentResolver().getType(up.getLocalUri());
|
||||
if(mimeType != null) {
|
||||
up.mimeType = mimeType;
|
||||
up.setTag("mimeType", mimeType);
|
||||
}
|
||||
}
|
||||
|
||||
StartUploadTask startUploadTask = new StartUploadTask(MultipleShareActivity.this, uploadService, up.title, up.uri, "", up.mimeType, Contribution.SOURCE_EXTERNAL);
|
||||
StartUploadTask startUploadTask = new StartUploadTask(MultipleShareActivity.this, uploadService, up.getFilename(), up.getLocalUri(), up.getDescription(), (String)up.getTag("mimeType"), Contribution.SOURCE_EXTERNAL);
|
||||
try {
|
||||
Utils.executeAsyncTask(startUploadTask);
|
||||
startUploadTask.get();
|
||||
|
|
@ -205,18 +125,9 @@ public class MultipleShareActivity extends AuthenticatedActivity {
|
|||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setContentView(R.layout.activity_multiple_share);
|
||||
|
||||
setContentView(R.layout.activity_multiple_uploads);
|
||||
uploadsList = (MultipleUploadListFragment)getSupportFragmentManager().findFragmentById(R.id.uploadsListFragment);
|
||||
app = (CommonsApplication)this.getApplicationContext();
|
||||
photosGrid = (GridView)findViewById(R.id.multipleShareBackground);
|
||||
baseTitle = (EditText)findViewById(R.id.multipleBaseTitle);
|
||||
|
||||
uploadDisplayOptions = new DisplayImageOptions.Builder().cacheInMemory()
|
||||
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
|
||||
.displayer(new FadeInBitmapDisplayer(300))
|
||||
.cacheInMemory()
|
||||
.resetViewBeforeLoading().build();
|
||||
|
||||
requestAuthToken();
|
||||
|
||||
}
|
||||
|
|
@ -237,56 +148,23 @@ public class MultipleShareActivity extends AuthenticatedActivity {
|
|||
|
||||
if(intent.getAction() == Intent.ACTION_SEND_MULTIPLE) {
|
||||
ArrayList<Uri> urisList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
|
||||
for(int i=0; i < urisList.size(); i++) {
|
||||
PreparedUpload up = new PreparedUpload(urisList.get(i), i + 1);
|
||||
up.mimeType = intent.getType();
|
||||
for(Uri uri: urisList) {
|
||||
Contribution up = new Contribution();
|
||||
up.setLocalUri(uri);
|
||||
up.setTag("mimeType", intent.getType());
|
||||
photosList.add(up);
|
||||
}
|
||||
|
||||
uploadsList.setData(photosList);
|
||||
|
||||
setTitle(getResources().getQuantityString(R.plurals.multiple_uploads_title, urisList.size(), urisList.size()));
|
||||
|
||||
DisplayMetrics screenMetrics = getResources().getDisplayMetrics();
|
||||
int screenWidth = screenMetrics.widthPixels;
|
||||
int screenHeight = screenMetrics.heightPixels;
|
||||
|
||||
int picWidth = Math.min((int) Math.sqrt(screenWidth * screenHeight / urisList.size()), screenWidth);
|
||||
picWidth = Math.min((int)(192 * screenMetrics.density), Math.max((int) (120 * screenMetrics.density), picWidth / 48 * 48));
|
||||
picHeight = Math.min(picWidth, (int)(192 * screenMetrics.density)); // Max Height is same as Contributions list
|
||||
|
||||
photosGrid.setColumnWidth(picWidth);
|
||||
photosGrid.invalidateViews();
|
||||
|
||||
photosAdapter = new PhotoDisplayAdapter(photosList);
|
||||
photosGrid.setAdapter(photosAdapter);
|
||||
|
||||
Intent uploadServiceIntent = new Intent(getApplicationContext(), UploadService.class);
|
||||
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
|
||||
startService(uploadServiceIntent);
|
||||
bindService(uploadServiceIntent, uploadServiceConnection, Context.BIND_AUTO_CREATE);
|
||||
|
||||
baseTitle.addTextChangedListener(new TextWatcher() {
|
||||
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
||||
|
||||
}
|
||||
|
||||
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
||||
for(PreparedUpload up: photosList) {
|
||||
if(!up.isDirty) {
|
||||
if(!TextUtils.isEmpty(charSequence)) {
|
||||
up.title = charSequence.toString() + " - " + up.sequence;
|
||||
} else {
|
||||
up.title = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
photosAdapter.notifyDataSetChanged();
|
||||
|
||||
}
|
||||
|
||||
public void afterTextChanged(Editable editable) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,164 @@
|
|||
package org.wikimedia.commons;
|
||||
|
||||
import android.graphics.Point;
|
||||
import android.graphics.Rect;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.*;
|
||||
import com.actionbarsherlock.app.SherlockFragment;
|
||||
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
|
||||
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
|
||||
import org.wikimedia.commons.contributions.Contribution;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MultipleUploadListFragment extends SherlockFragment {
|
||||
|
||||
private GridView photosGrid;
|
||||
private PhotoDisplayAdapter photosAdapter;
|
||||
private EditText baseTitle;
|
||||
|
||||
private Point photoSize;
|
||||
|
||||
private ArrayList<Contribution> photosList;
|
||||
|
||||
private DisplayImageOptions uploadDisplayOptions;
|
||||
|
||||
private static class UploadHolderView {
|
||||
Uri imageUri;
|
||||
|
||||
ImageView image;
|
||||
TextView title;
|
||||
}
|
||||
|
||||
private class PhotoDisplayAdapter extends BaseAdapter {
|
||||
|
||||
private ArrayList<Contribution> urisList;
|
||||
|
||||
private PhotoDisplayAdapter(ArrayList<Contribution> urisList) {
|
||||
this.urisList = urisList;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return urisList.size();
|
||||
}
|
||||
|
||||
public Object getItem(int i) {
|
||||
return urisList.get(i);
|
||||
}
|
||||
|
||||
public long getItemId(int i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
public View getView(int i, View view, ViewGroup viewGroup) {
|
||||
UploadHolderView holder;
|
||||
|
||||
if(view == null) {
|
||||
view = getLayoutInflater(null).inflate(R.layout.layout_upload_item, null);
|
||||
holder = new UploadHolderView();
|
||||
holder.image = (ImageView) view.findViewById(R.id.uploadImage);
|
||||
holder.title = (TextView) view.findViewById(R.id.uploadTitle);
|
||||
|
||||
holder.image.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, photoSize.y));
|
||||
|
||||
view.setTag(holder);
|
||||
} else {
|
||||
holder = (UploadHolderView)view.getTag();
|
||||
}
|
||||
|
||||
|
||||
Contribution up = (Contribution)this.getItem(i);
|
||||
|
||||
if(holder.imageUri == null || !holder.imageUri.equals(up.getLocalUri())) {
|
||||
ImageLoader.getInstance().displayImage(up.getLocalUri().toString(), holder.image, uploadDisplayOptions);
|
||||
holder.imageUri = up.getLocalUri();
|
||||
}
|
||||
|
||||
holder.title.setText(up.getFilename());
|
||||
|
||||
return view;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Wrong result type
|
||||
private Point calculatePicDimension(int count) {
|
||||
DisplayMetrics screenMetrics = getResources().getDisplayMetrics();
|
||||
int screenWidth = screenMetrics.widthPixels;
|
||||
int screenHeight = screenMetrics.heightPixels;
|
||||
|
||||
int picWidth = Math.min((int) Math.sqrt(screenWidth * screenHeight / count), screenWidth);
|
||||
picWidth = Math.min((int)(192 * screenMetrics.density), Math.max((int) (120 * screenMetrics.density), picWidth / 48 * 48));
|
||||
int picHeight = Math.min(picWidth, (int)(192 * screenMetrics.density)); // Max Height is same as Contributions list
|
||||
return new Point(picWidth, picHeight);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_multiple_uploads_list, container);
|
||||
photosGrid = (GridView)view.findViewById(R.id.multipleShareBackground);
|
||||
baseTitle = (EditText)view.findViewById(R.id.multipleBaseTitle);
|
||||
|
||||
baseTitle.addTextChangedListener(new TextWatcher() {
|
||||
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
||||
|
||||
}
|
||||
|
||||
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
||||
for(Contribution up: photosList) {
|
||||
Boolean isDirty = (Boolean)up.getTag("isDirty");
|
||||
if(isDirty == null || !isDirty) {
|
||||
if(!TextUtils.isEmpty(charSequence)) {
|
||||
up.setFilename(charSequence.toString() + " - " + i);
|
||||
} else {
|
||||
up.setFilename("");
|
||||
}
|
||||
}
|
||||
}
|
||||
photosAdapter.notifyDataSetChanged();
|
||||
|
||||
}
|
||||
|
||||
public void afterTextChanged(Editable editable) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
uploadDisplayOptions = new DisplayImageOptions.Builder().cacheInMemory()
|
||||
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
|
||||
.displayer(new FadeInBitmapDisplayer(300))
|
||||
.cacheInMemory()
|
||||
.resetViewBeforeLoading().build();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void setData(ArrayList<Contribution> photosList) {
|
||||
if(this.photosList == null) {
|
||||
photosAdapter = new PhotoDisplayAdapter(photosList);
|
||||
photosGrid.setAdapter(photosAdapter);
|
||||
}
|
||||
this.photosList = photosList;
|
||||
photoSize = calculatePicDimension(photosList.size());
|
||||
photosAdapter.notifyDataSetChanged();
|
||||
photosGrid.setColumnWidth(photoSize.x);
|
||||
}
|
||||
}
|
||||
|
|
@ -20,61 +20,66 @@ import java.util.Date;
|
|||
public class StartUploadTask extends AsyncTask<Void, Void, Contribution> {
|
||||
|
||||
private Activity context;
|
||||
private String rawTitle;
|
||||
private Uri mediaUri;
|
||||
private String description;
|
||||
private String mimeType;
|
||||
private String source;
|
||||
private UploadService uploadService;
|
||||
|
||||
private Contribution contribution;
|
||||
|
||||
private CommonsApplication app;
|
||||
|
||||
public StartUploadTask(Activity context, UploadService uploadService, String rawTitle, Uri mediaUri, String description, String mimeType, String source) {
|
||||
|
||||
this.context = context;
|
||||
this.uploadService = uploadService;
|
||||
|
||||
app = (CommonsApplication)context.getApplicationContext();
|
||||
|
||||
contribution = new Contribution(mediaUri, null, rawTitle, description, -1, null, null, app.getCurrentAccount().name, CommonsApplication.DEFAULT_EDIT_SUMMARY);
|
||||
contribution.setTag("mimeType", mimeType);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public StartUploadTask(Activity context, UploadService uploadService, Contribution contribution) {
|
||||
this.context = context;
|
||||
this.rawTitle = rawTitle;
|
||||
this.mediaUri = mediaUri;
|
||||
this.description = description;
|
||||
this.mimeType = mimeType;
|
||||
this.source = source;
|
||||
this.uploadService = uploadService;
|
||||
|
||||
app = (CommonsApplication)context.getApplicationContext();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Contribution doInBackground(Void... voids) {
|
||||
String title = rawTitle;
|
||||
String title = contribution.getFilename();
|
||||
|
||||
Date dateCreated = null;
|
||||
|
||||
Long length = null;
|
||||
long length;
|
||||
try {
|
||||
length = context.getContentResolver().openAssetFileDescriptor(mediaUri, "r").getLength();
|
||||
if(length == -1) {
|
||||
// Let us find out the long way!
|
||||
length = Utils.countBytes(context.getContentResolver().openInputStream(mediaUri));
|
||||
if(contribution.getDataLength() == -1) {
|
||||
length = context.getContentResolver().openAssetFileDescriptor(contribution.getLocalUri(), "r").getLength();
|
||||
if(length == -1) {
|
||||
// Let us find out the long way!
|
||||
length = Utils.countBytes(context.getContentResolver().openInputStream(contribution.getLocalUri()));
|
||||
}
|
||||
contribution.setDataLength(length);
|
||||
}
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
String mimeType = (String)contribution.getTag("mimeType");
|
||||
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
|
||||
|
||||
if(extension != null && !title.toLowerCase().endsWith(extension.toLowerCase())) {
|
||||
title += "." + extension;
|
||||
}
|
||||
|
||||
Log.d("Commons", "Title is " + title + " mimetype is " + mimeType);
|
||||
|
||||
if(mimeType.startsWith("image/")) {
|
||||
Cursor cursor = context.getContentResolver().query(mediaUri,
|
||||
if(mimeType.startsWith("image/") && contribution.getDateCreated() == null) {
|
||||
Cursor cursor = context.getContentResolver().query(contribution.getLocalUri(),
|
||||
new String[]{MediaStore.Images.ImageColumns.DATE_TAKEN}, null, null, null);
|
||||
if(cursor != null && cursor.getCount() != 0) {
|
||||
cursor.moveToFirst();
|
||||
dateCreated = new Date(cursor.getLong(0));
|
||||
contribution.setDateCreated(new Date(cursor.getLong(0)));
|
||||
} // FIXME: Alternate way of setting dateCreated if this data is not found
|
||||
}
|
||||
Contribution contribution = new Contribution(mediaUri, null, title, description, length, dateCreated, null, app.getCurrentAccount().name, CommonsApplication.DEFAULT_EDIT_SUMMARY);
|
||||
contribution.setSource(source);
|
||||
return contribution;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue