Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert AsyncTask to IntentService #969

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions AndroidManifest.xml
Expand Up @@ -150,6 +150,9 @@
<service android:name=".files.services.FileDownloader" />
<service android:name=".files.services.FileUploader" />
<service android:name=".media.MediaService" />
<service android:name=".services.LoadingLogService" android:exported="false"/>
<service android:name=".services.MoveFilesService" android:exported="false"/>
<service android:name=".services.CheckAvailableSpaceService" android:exported="false"/>

<activity android:name=".ui.activity.PinCodeActivity" />
<activity android:name=".ui.activity.ConflictsResolveActivity"/>
Expand Down
39 changes: 39 additions & 0 deletions src/com/owncloud/android/services/CheckAvailableSpaceService.java
@@ -0,0 +1,39 @@
package com.owncloud.android.services;

import java.io.File;

import android.accounts.Account;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.owncloud.android.ui.activity.UploadFilesActivity;
import com.owncloud.android.utils.FileStorageUtils;

public class CheckAvailableSpaceService extends IntentService {
Account mAccountOnCreation;

public CheckAvailableSpaceService() {
super("CheckAvailableSpaceService");
}

private void init(Intent intent) {
this.mAccountOnCreation = (Account) intent.getParcelableExtra("mAccountOnCreation");
}

public void onHandleIntent(Intent intent) {
init(intent);
String[] checkedFilePaths = intent.getStringArrayExtra("filePaths");
long total = 0;
for (int i = 0; checkedFilePaths != null && i < checkedFilePaths.length; i++) {
String localPath = checkedFilePaths[i];
File localFile = new File(localPath);
total += localFile.length();
}
Intent resultIntent = new Intent(UploadFilesActivity.CHECK_SPACE_RECEIVER_FILTER);
resultIntent.putExtra("result", Boolean.valueOf(
FileStorageUtils.getUsableSpace(mAccountOnCreation.name) >= total));
LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent);
return;
}
}
79 changes: 79 additions & 0 deletions src/com/owncloud/android/services/LoadingLogService.java
@@ -0,0 +1,79 @@
package com.owncloud.android.services;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.ui.activity.LogHistoryActivity;

public class LoadingLogService extends IntentService {
private String mLogPath;
private String TAG;

public LoadingLogService() {
super("LoadingLogService");
}

private void init(Intent intent) {
this.mLogPath = intent.getStringExtra("mLogPath");
this.TAG = intent.getStringExtra("TAG");
}

public void onHandleIntent(Intent intent) {
init(intent);
Intent resultIntent = new Intent(LogHistoryActivity.LOG_RECEIVER_FILTER);
resultIntent.putExtra("result", readLogFile());
LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent);
}

/**
* Read and show log file info
*/
private String readLogFile() {

String[] logFileName = Log_OC.getLogFileNames();

//Read text from files
StringBuilder text = new StringBuilder();

BufferedReader br = null;
try {
String line;

for (int i = logFileName.length-1; i >= 0; i--) {
File file = new File(mLogPath,logFileName[i]);
if (file.exists()) {
// Check if FileReader is ready
if (new FileReader(file).ready()) {
br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
// Append the log info
text.append(line);
text.append('\n');
}
}
}
}
}
catch (IOException e) {
Log_OC.d(TAG, e.getMessage().toString());

} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// ignore
}
}
}

return text.toString();
}
}
66 changes: 66 additions & 0 deletions src/com/owncloud/android/services/MoveFilesService.java
@@ -0,0 +1,66 @@
package com.owncloud.android.services;

import java.io.File;
import java.util.ArrayList;

import android.accounts.Account;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.ui.activity.ErrorsWhileCopyingHandlerActivity;
import com.owncloud.android.utils.FileStorageUtils;

public class MoveFilesService extends IntentService {
ArrayList<String> mLocalPaths;
Account mAccount;
ArrayList<String> mRemotePaths;

public MoveFilesService() {
super("MoveFilesService");
}

private void init(Intent intent) {
this.mLocalPaths = intent.getStringArrayListExtra("mLocalPaths");
this.mAccount = (Account) intent.getParcelableExtra("mAccount");
this.mRemotePaths = intent.getStringArrayListExtra("mRemotePaths");
}

/**
* Performs the movement
*
* return 'False' when the movement of any file fails.
*/
public void onHandleIntent(Intent intent) {
init(intent);
FileDataStorageManager mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
while (!mLocalPaths.isEmpty()) {
String currentPath = mLocalPaths.get(0);
File currentFile = new File(currentPath);
String expectedPath = FileStorageUtils.getSavePath(mAccount.name) + mRemotePaths.get(0);
File expectedFile = new File(expectedPath);
if (expectedFile.equals(currentFile) || currentFile.renameTo(expectedFile)) {
OCFile file = mStorageManager.getFileByPath(mRemotePaths.get(0));
file.setStoragePath(expectedPath);
mStorageManager.saveFile(file);
mRemotePaths.remove(0);
mLocalPaths.remove(0);
} else {
Intent resultIntent = new Intent(ErrorsWhileCopyingHandlerActivity.MOVE_FILES_RECEIVER_FILTER);
resultIntent.putExtra("result", false);
resultIntent.putExtra("mRemotePaths", mRemotePaths);
resultIntent.putExtra("mLocalPaths", mLocalPaths);
LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent);
return;
}
}
Intent resultIntent = new Intent(ErrorsWhileCopyingHandlerActivity.MOVE_FILES_RECEIVER_FILTER);
resultIntent.putExtra("result", true);
resultIntent.putExtra("mRemotePaths", mRemotePaths);
resultIntent.putExtra("mLocalPaths", mLocalPaths);
LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent);
return;
}
}
Expand Up @@ -20,16 +20,17 @@

package com.owncloud.android.ui.activity;

import java.io.File;
import java.util.ArrayList;

import android.accounts.Account;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.LocalBroadcastManager;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -43,12 +44,9 @@

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.utils.Log_OC;

import com.owncloud.android.services.MoveFilesService;
import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
import com.owncloud.android.utils.FileStorageUtils;



Expand All @@ -65,19 +63,20 @@ public class ErrorsWhileCopyingHandlerActivity extends SherlockFragmentActivity

private static final String TAG = ErrorsWhileCopyingHandlerActivity.class.getSimpleName();

public static final String MOVE_FILES_RECEIVER_FILTER = "ErrorsWhileCopyingHandlerActivity_MoveFilesReceiver";
public static final String EXTRA_ACCOUNT = ErrorsWhileCopyingHandlerActivity.class.getCanonicalName() + ".EXTRA_ACCOUNT";
public static final String EXTRA_LOCAL_PATHS = ErrorsWhileCopyingHandlerActivity.class.getCanonicalName() + ".EXTRA_LOCAL_PATHS";
public static final String EXTRA_REMOTE_PATHS = ErrorsWhileCopyingHandlerActivity.class.getCanonicalName() + ".EXTRA_REMOTE_PATHS";

private static final String WAIT_DIALOG_TAG = "WAIT_DIALOG";

protected Account mAccount;
protected FileDataStorageManager mStorageManager;
protected ArrayList<String> mLocalPaths;
protected ArrayList<String> mRemotePaths;
protected ArrayAdapter<String> mAdapter;
protected Handler mHandler;
private DialogFragment mCurrentDialog;
private MoveFilesReceiver moveFilesReceiver;

/**
* {@link}
Expand All @@ -91,7 +90,6 @@ protected void onCreate(Bundle savedInstanceState) {
mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT);
mRemotePaths = intent.getStringArrayListExtra(EXTRA_REMOTE_PATHS);
mLocalPaths = intent.getStringArrayListExtra(EXTRA_LOCAL_PATHS);
mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
mHandler = new Handler();
if (mCurrentDialog != null) {
mCurrentDialog.dismiss();
Expand Down Expand Up @@ -125,9 +123,20 @@ protected void onCreate(Bundle savedInstanceState) {
okBtn.setText(R.string.foreign_files_move);
cancelBtn.setOnClickListener(this);
okBtn.setOnClickListener(this);

moveFilesReceiver = new MoveFilesReceiver();
LocalBroadcastManager.getInstance(this)
.registerReceiver(moveFilesReceiver, new IntentFilter(MOVE_FILES_RECEIVER_FILTER));
}



@Override
public void onDestroy() {
super.onDestroy();
if (moveFilesReceiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(moveFilesReceiver);
}
}

/**
* Customized adapter, showing the local files as main text in two-lines list item and the remote files
* as the secondary text.
Expand Down Expand Up @@ -184,7 +193,12 @@ public void onClick(View v) {
if (v.getId() == R.id.ok) {
/// perform movement operation in background thread
Log_OC.d(TAG, "Clicked MOVE, start movement");
new MoveFilesTask().execute();
Intent moveFilesService = new Intent(this, MoveFilesService.class);
moveFilesReceiver.updateUI();
moveFilesService.putExtra("mLocalPaths", mLocalPaths);
moveFilesService.putExtra("mAccount", mAccount);
moveFilesService.putExtra("mRemotePaths", mRemotePaths);
this.startService(moveFilesService);

} else if (v.getId() == R.id.cancel) {
/// just finish
Expand All @@ -200,49 +214,19 @@ public void onClick(View v) {
/**
* Asynchronous task performing the move of all the local files to the ownCloud folder.
*/
private class MoveFilesTask extends AsyncTask<Void, Void, Boolean> {
private class MoveFilesReceiver extends BroadcastReceiver {

/**
* Updates the UI before trying the movement
*/
@Override
protected void onPreExecute () {
public void updateUI() {
/// progress dialog and disable 'Move' button
mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
findViewById(R.id.ok).setEnabled(false);
}


/**
* Performs the movement
*
* @return 'False' when the movement of any file fails.
*/
@Override
protected Boolean doInBackground(Void... params) {
while (!mLocalPaths.isEmpty()) {
String currentPath = mLocalPaths.get(0);
File currentFile = new File(currentPath);
String expectedPath = FileStorageUtils.getSavePath(mAccount.name) + mRemotePaths.get(0);
File expectedFile = new File(expectedPath);

if (expectedFile.equals(currentFile) || currentFile.renameTo(expectedFile)) {
// SUCCESS
OCFile file = mStorageManager.getFileByPath(mRemotePaths.get(0));
file.setStoragePath(expectedPath);
mStorageManager.saveFile(file);
mRemotePaths.remove(0);
mLocalPaths.remove(0);

} else {
// FAIL
return false;
}
}
return true;
}

/**
* Updates the activity UI after the movement of local files is tried.
*
Expand All @@ -253,23 +237,29 @@ protected Boolean doInBackground(Void... params) {
* @param result 'True' when the movement was successful.
*/
@Override
protected void onPostExecute(Boolean result) {
public void onReceive(Context receiverContext, Intent receiverIntent) {
boolean result = receiverIntent.getBooleanExtra("result", false);
ErrorsWhileCopyingHandlerActivity.this.mRemotePaths = receiverIntent
.getStringArrayListExtra("mRemotePaths");
ErrorsWhileCopyingHandlerActivity.this.mLocalPaths = receiverIntent.getStringArrayListExtra("mLocalPaths");
mAdapter.notifyDataSetChanged();
mCurrentDialog.dismiss();
mCurrentDialog = null;
if (mCurrentDialog != null) {
mCurrentDialog.dismiss();
mCurrentDialog = null;
}
findViewById(R.id.ok).setEnabled(true);

if (result) {
// nothing else to do in this activity
Toast t = Toast.makeText(ErrorsWhileCopyingHandlerActivity.this, getString(R.string.foreign_files_success), Toast.LENGTH_LONG);
t.show();
finish();
ErrorsWhileCopyingHandlerActivity.this.finish();

} else {
Toast t = Toast.makeText(ErrorsWhileCopyingHandlerActivity.this, getString(R.string.foreign_files_fail), Toast.LENGTH_LONG);
t.show();
}
}
}
}

}