My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* g2android
* Copyright (c) 2009-2010 Anthony Dahanne
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

package net.dahanne.android.g2android.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import net.dahanne.android.g2android.G2AndroidApplication;
import net.dahanne.android.g2android.R;
import net.dahanne.android.g2android.model.Album;
import net.dahanne.android.g2android.tasks.AddPhotoTask;
import net.dahanne.android.g2android.tasks.AddPhotosTask;
import net.dahanne.android.g2android.tasks.FetchAlbumForUploadTask;
import net.dahanne.android.g2android.tasks.LoginTask;
import net.dahanne.android.g2android.utils.G2DataUtils;
import net.dahanne.android.g2android.utils.ShowUtils;
import net.dahanne.android.g2android.utils.UriUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class UploadPhoto extends Activity implements OnClickListener {

private static final String SENT_WITH_G2_ANDROID = "SentWithG2Android";
private ProgressDialog progressDialog;
private Button sendButton;
private Button cancelButton;
private Button goToG2AndroidButton;
private TextView galleryUrlText;
private TextView connectedAsUserText;
private Uri mImageUri;
private Spinner spinner;
private ArrayAdapter<Album> albumAdapter;
private EditText filenameEditText;
private File imageFromCamera;
private ArrayList<Uri> mImageUris;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
albumAdapter = new ArrayAdapter<Album>(this, 0);
// albumAdapter = new AlbumAdapterForUpload(this,
// R.layout.show_albums_for_upload_row, items);
// this will enable the progress bar
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.upload_photo);
setTitle(R.string.upload_photo_title);
filenameEditText = (EditText) findViewById(R.id.filename);

Intent intent = getIntent();
Bundle extras = intent.getExtras();

String fileName = SENT_WITH_G2_ANDROID;

if (Intent.ACTION_SEND.equals(intent.getAction())) {
if ((extras != null) || (intent.getData() != null)) {
// depending on the source of the intent, the uri can be in
// extra or data, or can also be a bmp if coming from the camera
if (extras != null) {
mImageUri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
if (mImageUri == null) {
Bitmap bm = null;
Object o = extras.get("data");
if ((o != null) && (o instanceof Bitmap)) {
bm = (Bitmap) o;
try {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(Settings
.getG2AndroidPath(this));
stringBuilder.append("/");
StringBuilder stringBuilderFileName = new StringBuilder();
stringBuilderFileName.append(fileName);
stringBuilderFileName.append(System
.currentTimeMillis());
stringBuilderFileName.append(".jpg");
stringBuilder.append(stringBuilderFileName);
imageFromCamera = new File(
stringBuilder.toString());
FileOutputStream fos = new FileOutputStream(
imageFromCamera);
bm.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();

mImageUri = Uri.fromFile(imageFromCamera);
fileName = stringBuilderFileName.toString();

} catch (FileNotFoundException e) {
ShowUtils.getInstance().alertFileProblem(
e.getMessage(), this);
} catch (IOException e) {
ShowUtils.getInstance().alertFileProblem(
e.getMessage(), this);
}

}
}

} else {
mImageUri = intent.getData();
}
}
if (fileName.equals(SENT_WITH_G2_ANDROID)) {
fileName = UriUtils.extractFilenameFromUri(mImageUri, this);
}
filenameEditText.setText(fileName);
}

if ("android.intent.action.SEND_MULTIPLE".equals(intent.getAction())) {
mImageUris = extras.getParcelableArrayList(Intent.EXTRA_STREAM);
filenameEditText.setEnabled(false);
filenameEditText.setText(R.string.multiple_files_upload);

}

sendButton = (Button) findViewById(R.id.send_button);
cancelButton = (Button) findViewById(R.id.cancel_button);
goToG2AndroidButton = (Button) findViewById(R.id.g2android_button);
sendButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
goToG2AndroidButton.setOnClickListener(this);

galleryUrlText = (TextView) findViewById(R.id.gallery_url);
connectedAsUserText = (TextView) findViewById(R.id.connected_as_user);

}

@SuppressWarnings("unchecked")
@Override
protected void onResume() {
super.onResume();

spinner = (Spinner) findViewById(R.id.album_list);
spinner.setAdapter(albumAdapter);

progressDialog = ProgressDialog.show(this,
getString(R.string.please_wait),
getString(R.string.connecting_to_the_gallery), true);
new LoginTask(this, progressDialog, connectedAsUserText,
galleryUrlText, sendButton).execute(
Settings.getGalleryUrl(this), Settings.getUsername(this),
Settings.getPassword(this));

}

/**
* This method is called back from LoginTask
*/
@SuppressWarnings("unchecked")
public void showAlbumList() {
// we recover the context from the database
ShowUtils.getInstance().recoverContextFromDatabase(this);
Album currentAlbum = null;
if (((G2AndroidApplication) getApplication()).getRootAlbum() != null) {
currentAlbum = G2DataUtils.getInstance().findAlbumFromAlbumName(
((G2AndroidApplication) getApplication()).getRootAlbum(),
((G2AndroidApplication) getApplication()).getAlbumName());
}
progressDialog = ProgressDialog.show(this,
getString(R.string.please_wait),
getString(R.string.fetching_gallery_albums), true);
new FetchAlbumForUploadTask(this, progressDialog, spinner, currentAlbum)
.execute(Settings.getGalleryUrl(this));

}

@SuppressWarnings("unchecked")
public void onClick(View v) {

switch (v.getId()) {
case R.id.send_button:

Album selectAlbum = (Album) spinner.getSelectedItem();

if (mImageUri == null && mImageUris == null) {
// there is no image to send
Toast.makeText(this, R.string.upload_photo_no_photo,
Toast.LENGTH_LONG);
} else {

boolean mustLogIn = ShowUtils.getInstance()
.recoverContextFromDatabase(this);
progressDialog = ProgressDialog.show(this,
getString(R.string.please_wait),
getString(R.string.adding_photo), true);

// one picture to upload : can be from camera or from sdcard
if (mImageUri != null) {
new AddPhotoTask(this, progressDialog).execute(
Settings.getGalleryUrl(this),
Integer.valueOf(selectAlbum.getName()), mImageUri,
mustLogIn, filenameEditText.getText().toString(),
imageFromCamera);
}
// multiple file upload
else if (mImageUris != null) {
new AddPhotosTask(this, progressDialog).execute(
Settings.getGalleryUrl(this),
Integer.valueOf(selectAlbum.getName()), mImageUris,
mustLogIn);
}
}

break;
case R.id.cancel_button:
finish();
break;
case R.id.g2android_button:
startActivity(new Intent(this, Settings.class));
break;

}

}
}

Change log

r90 by anthony.dahanne on Aug 25, 2010   Diff
g2android  release 1.6.0 : fixed issues
#19, #45, #46, #48, #49, #57
Go to: 
Project members, sign in to write a code review

Older revisions

r85 by anthony.dahanne on Jul 19, 2010   Diff
g2android  release 1.5 :
All revisions of this file

File info

Size: 8327 bytes, 247 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting