InstantUploadService.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.files.services;
  20. import java.util.HashMap;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import com.owncloud.android.Log_OC;
  24. import com.owncloud.android.network.OwnCloudClientUtils;
  25. import eu.alefzero.webdav.WebdavClient;
  26. import android.accounts.Account;
  27. import android.app.Service;
  28. import android.content.Intent;
  29. import android.os.IBinder;
  30. import android.util.Log;
  31. public class InstantUploadService extends Service {
  32. public static String KEY_FILE_PATH = "KEY_FILEPATH";
  33. public static String KEY_FILE_SIZE = "KEY_FILESIZE";
  34. public static String KEY_MIME_TYPE = "KEY_MIMETYPE";
  35. public static String KEY_DISPLAY_NAME = "KEY_FILENAME";
  36. public static String KEY_ACCOUNT = "KEY_ACCOUNT";
  37. private static String TAG = "InstantUploadService";
  38. // TODO make it configurable over the settings dialog
  39. public static String INSTANT_UPLOAD_DIR = "/InstantUpload";
  40. private UploaderRunnable mUploaderRunnable;
  41. @Override
  42. public IBinder onBind(Intent arg0) {
  43. return null;
  44. }
  45. @Override
  46. public int onStartCommand(Intent intent, int flags, int startId) {
  47. if (intent == null || !intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_DISPLAY_NAME)
  48. || !intent.hasExtra(KEY_FILE_PATH) || !intent.hasExtra(KEY_FILE_SIZE)
  49. || !intent.hasExtra(KEY_MIME_TYPE)) {
  50. Log_OC.w(TAG, "Not all required information was provided, abording");
  51. return Service.START_NOT_STICKY;
  52. }
  53. if (mUploaderRunnable == null) {
  54. mUploaderRunnable = new UploaderRunnable();
  55. }
  56. String filename = intent.getStringExtra(KEY_DISPLAY_NAME);
  57. String filepath = intent.getStringExtra(KEY_FILE_PATH);
  58. String mimetype = intent.getStringExtra(KEY_MIME_TYPE);
  59. Account account = intent.getParcelableExtra(KEY_ACCOUNT);
  60. long filesize = intent.getLongExtra(KEY_FILE_SIZE, -1);
  61. mUploaderRunnable.addElementToQueue(filename, filepath, mimetype, filesize, account);
  62. // starting new thread for new download doesnt seems like a good idea
  63. // maybe some thread pool or single background thread would be better
  64. Log_OC.d(TAG, "Starting instant upload thread");
  65. new Thread(mUploaderRunnable).start();
  66. return Service.START_STICKY;
  67. }
  68. private class UploaderRunnable implements Runnable {
  69. Object mLock;
  70. List<HashMap<String, Object>> mHashMapList;
  71. public UploaderRunnable() {
  72. mHashMapList = new LinkedList<HashMap<String, Object>>();
  73. mLock = new Object();
  74. }
  75. public void addElementToQueue(String filename,
  76. String filepath,
  77. String mimetype,
  78. long length,
  79. Account account) {
  80. HashMap<String, Object> new_map = new HashMap<String, Object>();
  81. new_map.put(KEY_ACCOUNT, account);
  82. new_map.put(KEY_DISPLAY_NAME, filename);
  83. new_map.put(KEY_FILE_PATH, filepath);
  84. new_map.put(KEY_MIME_TYPE, mimetype);
  85. new_map.put(KEY_FILE_SIZE, length);
  86. synchronized (mLock) {
  87. mHashMapList.add(new_map);
  88. }
  89. }
  90. private HashMap<String, Object> getFirstObject() {
  91. synchronized (mLock) {
  92. if (mHashMapList.size() == 0)
  93. return null;
  94. HashMap<String, Object> ret = mHashMapList.get(0);
  95. mHashMapList.remove(0);
  96. return ret;
  97. }
  98. }
  99. public void run() {
  100. HashMap<String, Object> working_map;
  101. while ((working_map = getFirstObject()) != null) {
  102. Account account = (Account) working_map.get(KEY_ACCOUNT);
  103. String filename = (String) working_map.get(KEY_DISPLAY_NAME);
  104. String filepath = (String) working_map.get(KEY_FILE_PATH);
  105. String mimetype = (String) working_map.get(KEY_MIME_TYPE);
  106. WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(account, getApplicationContext());
  107. wdc.createDirectory(INSTANT_UPLOAD_DIR); // fail could just mean that it already exists; put will be tried anyway
  108. try {
  109. wdc.putFile(filepath, INSTANT_UPLOAD_DIR + "/" + filename, mimetype);
  110. } catch (Exception e) {
  111. // nothing to do; this service is deprecated, indeed
  112. }
  113. }
  114. }
  115. }
  116. }