-package eu.alefzero.owncloud.files.services;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-\r
-import android.accounts.Account;\r
-import android.accounts.AccountManager;\r
-import android.app.Notification;\r
-import android.app.NotificationManager;\r
-import android.app.PendingIntent;\r
-import android.app.Service;\r
-import android.content.ContentValues;\r
-import android.content.Intent;\r
-import android.os.Environment;\r
-import android.os.Handler;\r
-import android.os.HandlerThread;\r
-import android.os.IBinder;\r
-import android.os.Looper;\r
-import android.os.Message;\r
-import android.os.Process;\r
-import android.util.Log;\r
-import android.widget.RemoteViews;\r
-import eu.alefzero.owncloud.R;\r
-import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
-import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;\r
-import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener;\r
-import eu.alefzero.webdav.WebdavClient;\r
-\r
-public class FileDownloader extends Service implements OnDatatransferProgressListener {\r
- public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH";\r
- public static final String BAD_DOWNLOAD_MESSAGE = "BAD_DOWNLOAD"; \r
- public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
- public static final String EXTRA_FILE_PATH = "FILE_PATH";\r
- public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";\r
- public static final String EXTRA_FILE_SIZE = "FILE_SIZE";\r
- private static final String TAG = "FileDownloader";\r
-\r
- private NotificationManager mNotificationMngr;\r
- private Looper mServiceLooper;\r
- private ServiceHandler mServiceHandler;\r
- private Account mAccount;\r
- private String mFilePath;\r
- private String mRemotePath;\r
- private int mLastPercent;\r
- private long mTotalDownloadSize;\r
- private long mCurrentDownlodSize;\r
- private Notification mNotification;\r
-\r
- private final class ServiceHandler extends Handler {\r
- public ServiceHandler(Looper looper) {\r
- super(looper);\r
- }\r
-\r
- @Override\r
- public void handleMessage(Message msg) {\r
- downloadFile();\r
- stopSelf(msg.arg1);\r
- }\r
- }\r
-\r
- @Override\r
- public void onCreate() {\r
- super.onCreate();\r
- mNotificationMngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);\r
- HandlerThread thread = new HandlerThread("FileDownladerThread",\r
- Process.THREAD_PRIORITY_BACKGROUND);\r
- thread.start();\r
- mServiceLooper = thread.getLooper();\r
- mServiceHandler = new ServiceHandler(mServiceLooper);\r
- }\r
-\r
- @Override\r
- public IBinder onBind(Intent arg0) {\r
- return null;\r
- }\r
-\r
- @Override\r
- public int onStartCommand(Intent intent, int flags, int startId) {\r
- if (!intent.hasExtra(EXTRA_ACCOUNT)\r
- && !intent.hasExtra(EXTRA_FILE_PATH)) {\r
- Log.e(TAG, "Not enough information provided in intent");\r
- return START_STICKY;\r
- }\r
- mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT);\r
- mFilePath = intent.getStringExtra(EXTRA_FILE_PATH);\r
- mRemotePath = intent.getStringExtra(EXTRA_REMOTE_PATH);\r
- Message msg = mServiceHandler.obtainMessage();\r
- msg.arg1 = startId;\r
- mServiceHandler.sendMessage(msg);\r
- mCurrentDownlodSize = mLastPercent = 0;\r
- mTotalDownloadSize = intent.getLongExtra(EXTRA_FILE_SIZE, -1);\r
-\r
- return START_NOT_STICKY;\r
- }\r
-\r
- void downloadFile() {\r
- AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);\r
-\r
-\r
- WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext());\r
- \r
- String username = mAccount.name.split("@")[0];\r
- String password = "";\r
- try {\r
- password = am.blockingGetAuthToken(mAccount,\r
- AccountAuthenticator.AUTH_TOKEN_TYPE, true);\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- return;\r
- }\r
-\r
- wdc.setCredentials(username, password);\r
- wdc.allowSelfsignedCertificates();\r
- wdc.setDataTransferProgressListener(this);\r
-\r
- mNotification = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis());\r
-\r
- mNotification.flags |= Notification.FLAG_ONGOING_EVENT;\r
- mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout);\r
- mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, mTotalDownloadSize == -1);\r
- mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);\r
- // dvelasco ; contentIntent MUST be assigned to avoid app crashes in versions previous to Android 4.x ;\r
- // BUT an empty Intent is not a very elegant solution; something smart should happen when a user 'clicks' on a download in the notification bar\r
- mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);\r
- \r
- mNotificationMngr.notify(1, mNotification);\r
-\r
- File sdCard = Environment.getExternalStorageDirectory();\r
- File file = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + mFilePath);\r
- try {\r
- file.getParentFile().mkdirs();\r
- file.createNewFile();\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
- }\r
-\r
- String message;\r
- if (wdc.downloadFile(mRemotePath, file)) {\r
- ContentValues cv = new ContentValues();\r
- cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath());\r
- getContentResolver().update(\r
- ProviderTableMeta.CONTENT_URI,\r
- cv,\r
- ProviderTableMeta.FILE_NAME + "=? AND "\r
- + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",\r
- new String[] {\r
- mFilePath.substring(mFilePath.lastIndexOf('/') + 1),\r
- mAccount.name }); \r
- message = DOWNLOAD_FINISH_MESSAGE;\r
- } else {\r
- message = BAD_DOWNLOAD_MESSAGE;\r
- }\r
- \r
- mNotificationMngr.cancel(1);\r
- Intent end = new Intent(message);\r
- end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath());\r
- sendBroadcast(end);\r
- }\r
-\r
- @Override\r
- public void transferProgress(long progressRate) {\r
- mCurrentDownlodSize += progressRate;\r
- int percent = (int)(100.0*((double)mCurrentDownlodSize)/((double)mTotalDownloadSize));\r
- if (percent != mLastPercent) {\r
- mNotification.contentView.setProgressBar(R.id.status_progress, 100, (int)(100*mCurrentDownlodSize/mTotalDownloadSize), mTotalDownloadSize == -1);\r
- mNotification.contentView.setTextViewText(R.id.status_text, percent+"%");\r
- mNotificationMngr.notify(1, mNotification);\r
- }\r
- \r
- mLastPercent = percent;\r
- }\r
-\r
-}\r