1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.activity
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 import android
.accounts
.Account
;
24 import android
.app
.Activity
;
25 import android
.content
.Intent
;
26 import android
.database
.Cursor
;
27 import android
.graphics
.Bitmap
;
28 import android
.graphics
.BitmapFactory
;
29 import android
.os
.Bundle
;
30 import android
.util
.Log
;
31 import android
.util
.SparseArray
;
32 import android
.view
.Gravity
;
33 import android
.view
.View
;
34 import android
.view
.View
.OnClickListener
;
35 import android
.view
.ViewGroup
;
36 import android
.widget
.Button
;
37 import android
.widget
.CheckBox
;
38 import android
.widget
.CompoundButton
;
39 import android
.widget
.CompoundButton
.OnCheckedChangeListener
;
40 import android
.widget
.ImageButton
;
41 import android
.widget
.LinearLayout
;
42 import android
.widget
.Toast
;
44 import com
.owncloud
.android
.AccountUtils
;
45 import com
.owncloud
.android
.R
;
46 import com
.owncloud
.android
.db
.DbHandler
;
47 import com
.owncloud
.android
.files
.InstantUploadBroadcastReceiver
;
48 import com
.owncloud
.android
.files
.services
.FileUploader
;
49 import com
.owncloud
.android
.files
.services
.InstantUploadService
;
52 * This Activity is used to display a list with images they could not be
53 * uploaded instantly. The images can be selected for delete or for a try again
56 * The entrypoint for this activity is the 'Failed upload Notification" and a
57 * submenue underneath the 'Upload' menuentry
59 * @author andomaex / Matthias Baumann
61 * This program is free software: you can redistribute it and/or modify
62 * it under the terms of the GNU General Public License as published by
63 * the Free Software Foundation, either version 3 of the License, or (at
64 * your option) any later version.
66 * This program is distributed in the hope that it will be useful, but
67 * WITHOUT ANY WARRANTY; without even the implied warranty of
68 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
69 * General Public License for more de/
71 public class InstantUploadActivity
extends Activity
{
73 private static final String LOG_TAG
= InstantUploadActivity
.class.getSimpleName();
74 private LinearLayout listView
;
75 private static final String retry_chexbox_tag
= "retry_chexbox_tag";
76 private static int MAX_LOAD_IMAGES
= 5;
77 private int lastLoadImageIdx
= 0;
79 private SparseArray
<String
> fileList
= null
;
82 protected void onCreate(Bundle savedInstanceState
) {
83 super.onCreate(savedInstanceState
);
84 setContentView(R
.layout
.failed_upload_files
);
86 Button delete_all_btn
= (Button
) findViewById(R
.id
.failed_upload_delete_all_btn
);
87 delete_all_btn
.setOnClickListener(getDeleteListner());
88 Button retry_all_btn
= (Button
) findViewById(R
.id
.failed_upload_retry_all_btn
);
89 retry_all_btn
.setOnClickListener(getRetryListner());
90 CheckBox failed_upload_all_cb
= (CheckBox
) findViewById(R
.id
.failed_upload_headline_cb
);
91 failed_upload_all_cb
.setOnCheckedChangeListener(getCheckAllListener());
92 listView
= (LinearLayout
) findViewById(R
.id
.failed_upload_scrollviewlayout
);
99 * init the listview with ImageButtons, checkboxes and filename for every
100 * Image that was not successfully uploaded
102 * this method is call at Activity creation and on delete one ore more
103 * list-entry an on retry the upload by clicking the ImageButton or by click
104 * to the 'retry all' button
107 private void loadListView(boolean reset
) {
108 DbHandler db
= new DbHandler(getApplicationContext());
109 Cursor c
= db
.getFailedFiles();
112 fileList
= new SparseArray
<String
>();
113 listView
.removeAllViews();
114 lastLoadImageIdx
= 0;
118 c
.moveToPosition(lastLoadImageIdx
);
120 while (c
.moveToNext()) {
123 String imp_path
= c
.getString(1);
124 fileList
.put(lastLoadImageIdx
, imp_path
);
125 LinearLayout rowLayout
= getLinearLayout(lastLoadImageIdx
);
126 rowLayout
.addView(getFileCheckbox(lastLoadImageIdx
));
127 rowLayout
.addView(getImageButton(imp_path
, lastLoadImageIdx
));
128 rowLayout
.addView(getFileButton(imp_path
, lastLoadImageIdx
));
129 listView
.addView(rowLayout
);
130 Log
.d(LOG_TAG
, imp_path
+ " on idx: " + lastLoadImageIdx
);
131 if (lastLoadImageIdx
% MAX_LOAD_IMAGES
== 0) {
135 if (lastLoadImageIdx
> 0) {
136 addLoadMoreButton(listView
);
144 private void addLoadMoreButton(LinearLayout listView
) {
145 if (listView
!= null
) {
146 Button loadmoreBtn
= null
;
147 View oldButton
= listView
.findViewById(42);
148 if (oldButton
!= null
) {
149 // remove existing button
150 listView
.removeView(oldButton
);
151 // to add the button at the end
152 loadmoreBtn
= (Button
) oldButton
;
154 // create a new button to add to the scoll view
155 loadmoreBtn
= new Button(this);
156 loadmoreBtn
.setId(42);
157 loadmoreBtn
.setText(getString(R
.string
.failed_upload_load_more_images
));
158 loadmoreBtn
.setBackgroundResource(R
.color
.owncloud_white
);
159 loadmoreBtn
.setTextSize(12);
160 loadmoreBtn
.setOnClickListener(new OnClickListener() {
162 public void onClick(View v
) {
168 listView
.addView(loadmoreBtn
);
173 * provide a list of CheckBox instances, looked up from parent listview this
174 * list ist used to select/deselect all checkboxes at the list
176 * @return List<CheckBox>
178 private List
<CheckBox
> getCheckboxList() {
179 List
<CheckBox
> list
= new ArrayList
<CheckBox
>();
180 for (int i
= 0; i
< listView
.getChildCount(); i
++) {
181 Log
.d(LOG_TAG
, "ListView has Childs: " + listView
.getChildCount());
182 View childView
= listView
.getChildAt(i
);
183 if (childView
!= null
&& childView
instanceof ViewGroup
) {
184 View checkboxView
= getChildViews((ViewGroup
) childView
);
185 if (checkboxView
!= null
&& checkboxView
instanceof CheckBox
) {
186 Log
.d(LOG_TAG
, "found Child: " + checkboxView
.getId() + " " + checkboxView
.getClass());
187 list
.add((CheckBox
) checkboxView
);
195 * recursive called method, used from getCheckboxList method
200 private View
getChildViews(ViewGroup view
) {
202 for (int i
= 0; i
< view
.getChildCount(); i
++) {
203 View cb
= view
.getChildAt(i
);
204 if (cb
!= null
&& cb
instanceof ViewGroup
) {
205 return getChildViews((ViewGroup
) cb
);
206 } else if (cb
instanceof CheckBox
) {
215 * create a new OnCheckedChangeListener for the 'check all' checkbox *
217 * @return OnCheckedChangeListener to select all checkboxes at the list
219 private OnCheckedChangeListener
getCheckAllListener() {
220 return new OnCheckedChangeListener() {
222 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
223 List
<CheckBox
> list
= getCheckboxList();
224 for (CheckBox checkbox
: list
) {
225 ((CheckBox
) checkbox
).setChecked(isChecked
);
233 * Button click Listener for the retry button at the headline
235 * @return a Listener to perform a retry for all selected images
237 private OnClickListener
getRetryListner() {
238 return new OnClickListener() {
241 public void onClick(View v
) {
244 List
<CheckBox
> list
= getCheckboxList();
245 for (CheckBox checkbox
: list
) {
246 boolean to_retry
= checkbox
.isChecked();
248 Log
.d(LOG_TAG
, "Checkbox for " + checkbox
.getId() + " was checked: " + to_retry
);
249 String img_path
= fileList
.get(checkbox
.getId());
252 final String msg
= "Image-Path " + checkbox
.getId() + " was checked: " + img_path
;
254 startUpload(img_path
);
260 listView
.removeAllViews();
269 * Button click Listener for the delete button at the headline
271 * @return a Listener to perform a delete for all selected images
273 private OnClickListener
getDeleteListner() {
275 return new OnClickListener() {
278 public void onClick(View v
) {
279 final DbHandler dbh
= new DbHandler(getApplicationContext());
281 List
<CheckBox
> list
= getCheckboxList();
282 for (CheckBox checkbox
: list
) {
283 boolean to_be_delete
= checkbox
.isChecked();
285 Log
.d(LOG_TAG
, "Checkbox for " + checkbox
.getId() + " was checked: " + to_be_delete
);
286 String img_path
= fileList
.get(checkbox
.getId());
287 Log
.d(LOG_TAG
, "Image-Path " + checkbox
.getId() + " was checked: " + img_path
);
289 boolean deleted
= dbh
.removeIUPendingFile(img_path
);
290 Log
.d(LOG_TAG
, "removing " + checkbox
.getId() + " was : " + deleted
);
298 listView
.removeAllViews();
306 private LinearLayout
getLinearLayout(int id
) {
307 LinearLayout linearLayout
= new LinearLayout(getApplicationContext());
308 linearLayout
.setId(id
);
309 linearLayout
.setLayoutParams(new LinearLayout
.LayoutParams(LinearLayout
.LayoutParams
.WRAP_CONTENT
,
310 LinearLayout
.LayoutParams
.MATCH_PARENT
));
311 linearLayout
.setGravity(Gravity
.RIGHT
);
312 linearLayout
.setOrientation(LinearLayout
.HORIZONTAL
);
316 private Button
getFileButton(final String img_path
, int id
) {
317 Button retryButton
= new Button(this);
318 retryButton
.setId(id
);
319 retryButton
.setText(img_path
);
320 retryButton
.setBackgroundResource(R
.color
.owncloud_white
);
321 retryButton
.setTextSize(8);
322 retryButton
.setOnClickListener(getImageButtonOnClickListener(img_path
));
326 private CheckBox
getFileCheckbox(int id
) {
327 CheckBox retryCB
= new CheckBox(this);
329 retryCB
.setBackgroundResource(R
.color
.owncloud_white
);
330 retryCB
.setTextSize(8);
331 retryCB
.setTag(retry_chexbox_tag
);
335 private ImageButton
getImageButton(String img_path
, int id
) {
336 ImageButton imageButton
= new ImageButton(this);
337 imageButton
.setId(id
);
338 imageButton
.setClickable(true
);
339 imageButton
.setOnClickListener(getImageButtonOnClickListener(img_path
));
341 // scale and add a thumbnail to the imagebutton
342 int base_scale_size
= 32;
343 if (img_path
!= null
) {
344 Log
.d(LOG_TAG
, "add " + img_path
+ " to Image Button");
345 BitmapFactory
.Options options
= new BitmapFactory
.Options();
346 options
.inJustDecodeBounds
= true
;
347 Bitmap bitmap
= BitmapFactory
.decodeFile(img_path
, options
);
348 int width_tpm
= options
.outWidth
, height_tmp
= options
.outHeight
;
351 if (width_tpm
/ 2 < base_scale_size
|| height_tmp
/ 2 < base_scale_size
) {
359 Log
.d(LOG_TAG
, "scale Imgae with: " + scale
);
360 BitmapFactory
.Options options2
= new BitmapFactory
.Options();
361 options2
.inSampleSize
= scale
;
362 bitmap
= BitmapFactory
.decodeFile(img_path
, options2
);
364 if (bitmap
!= null
) {
365 Log
.d(LOG_TAG
, "loaded Bitmap Bytes: " + bitmap
.getRowBytes());
366 imageButton
.setImageBitmap(bitmap
);
368 Log
.d(LOG_TAG
, "could not load imgage: " + img_path
);
374 private OnClickListener
getImageButtonOnClickListener(final String img_path
) {
375 return new OnClickListener() {
378 public void onClick(View v
) {
379 startUpload(img_path
);
387 * start uploading a file to the INSTANT_UPLOD_DIR
391 private void startUpload(String img_path
) {
393 String filename
= img_path
.substring(img_path
.lastIndexOf('/'), img_path
.length());
394 if (canInstantUpload()) {
395 Account account
= AccountUtils
.getCurrentOwnCloudAccount(InstantUploadActivity
.this);
396 // add file again to upload queue
397 DbHandler db
= new DbHandler(InstantUploadActivity
.this);
399 db
.updateFileState(img_path
, DbHandler
.UPLOAD_STATUS_UPLOAD_LATER
);
404 Intent i
= new Intent(InstantUploadActivity
.this, FileUploader
.class);
405 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
406 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, img_path
);
407 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, InstantUploadService
.INSTANT_UPLOAD_DIR
+ "/" + filename
);
408 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
409 i
.putExtra(com
.owncloud
.android
.files
.services
.FileUploader
.KEY_INSTANT_UPLOAD
, true
);
411 final String msg
= "try to upload file with name :" + filename
;
413 Toast toast
= Toast
.makeText(InstantUploadActivity
.this, getString(R
.string
.failed_upload_retry_text
)
414 + filename
, Toast
.LENGTH_LONG
);
419 Toast toast
= Toast
.makeText(InstantUploadActivity
.this,
420 getString(R
.string
.failed_upload_retry_do_nothing_text
) + filename
, Toast
.LENGTH_LONG
);
425 private boolean canInstantUpload() {
427 if (!InstantUploadBroadcastReceiver
.isOnline(this)
428 || (InstantUploadBroadcastReceiver
.instantUploadViaWiFiOnly(this) && !InstantUploadBroadcastReceiver
429 .isConnectedViaWiFi(this))) {