Skip to content

Commit

Permalink
Merge pull request #704 from nextcloud/taaem-fix
Browse files Browse the repository at this point in the history
Taaem fix
  • Loading branch information
mario authored Mar 3, 2017
2 parents f7a6430 + c25b928 commit acf1caa
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
import com.owncloud.android.lib.resources.shares.OCShare;
import com.owncloud.android.syncadapter.FileSyncAdapter;
import com.owncloud.android.utils.DataHolderUtil;
import com.owncloud.android.utils.FileStorageUtils;
import com.owncloud.android.utils.MimeTypeUtil;

Expand Down Expand Up @@ -519,10 +520,16 @@ private void sendLocalBroadcast(
Log_OC.d(TAG, "Send broadcast " + event);
Intent intent = new Intent(event);
intent.putExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME, mAccount.name);

if (dirRemotePath != null) {
intent.putExtra(FileSyncAdapter.EXTRA_FOLDER_PATH, dirRemotePath);
}
intent.putExtra(FileSyncAdapter.EXTRA_RESULT, result);

DataHolderUtil dataHolderUtil = DataHolderUtil.getInstance();
String dataHolderItemId = dataHolderUtil.nextItemId();
dataHolderUtil.save(dataHolderItemId, result);
intent.putExtra(FileSyncAdapter.EXTRA_RESULT, dataHolderItemId);

intent.setPackage(mContext.getPackageName());
mContext.sendStickyBroadcast(intent);
//LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
Expand Down
11 changes: 8 additions & 3 deletions src/com/owncloud/android/syncadapter/FileSyncAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.owncloud.android.operations.RefreshFolderOperation;
import com.owncloud.android.operations.UpdateOCVersionOperation;
import com.owncloud.android.ui.activity.ErrorsWhileCopyingHandlerActivity;
import com.owncloud.android.utils.DataHolderUtil;

import org.apache.jackrabbit.webdav.DavException;

Expand Down Expand Up @@ -82,8 +83,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
public static final String EXTRA_FOLDER_PATH = FileSyncAdapter.class.getName() +
".EXTRA_FOLDER_PATH";
public static final String EXTRA_RESULT = FileSyncAdapter.class.getName() + ".EXTRA_RESULT";



/** Time stamp for the current synchronization process, used to distinguish fresh data */
private long mCurrentSyncTime;

Expand Down Expand Up @@ -373,9 +373,14 @@ private void sendLocalBroadcast(String event, String dirRemotePath,
if (dirRemotePath != null) {
intent.putExtra(FileSyncAdapter.EXTRA_FOLDER_PATH, dirRemotePath);
}

if (result != null) {
intent.putExtra(FileSyncAdapter.EXTRA_RESULT, result);
DataHolderUtil dataHolderUtil = DataHolderUtil.getInstance();
String dataHolderItemId = dataHolderUtil.nextItemId();
dataHolderUtil.save(dataHolderUtil.nextItemId(), result);
intent.putExtra(FileSyncAdapter.EXTRA_RESULT, dataHolderItemId);
}

intent.setPackage(getContext().getPackageName());
getContext().sendStickyBroadcast(intent);
//LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
Expand Down
9 changes: 6 additions & 3 deletions src/com/owncloud/android/ui/activity/FileDisplayActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import com.owncloud.android.ui.preview.PreviewMediaFragment;
import com.owncloud.android.ui.preview.PreviewTextFragment;
import com.owncloud.android.ui.preview.PreviewVideoActivity;
import com.owncloud.android.utils.DataHolderUtil;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.ErrorMessageAdapter;
import com.owncloud.android.utils.PermissionUtil;
Expand Down Expand Up @@ -1088,9 +1089,8 @@ public void onReceive(Context context, Intent intent) {

String synchFolderRemotePath =
intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
RemoteOperationResult synchResult =
(RemoteOperationResult) intent.getSerializableExtra(
FileSyncAdapter.EXTRA_RESULT);
RemoteOperationResult synchResult = (RemoteOperationResult)
DataHolderUtil.getInstance().retrieve(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
boolean sameAccount = (getAccount() != null &&
accountName.equals(getAccount().name) && getStorageManager() != null);

Expand Down Expand Up @@ -1159,6 +1159,8 @@ public void onReceive(Context context, Intent intent) {

}
removeStickyBroadcast(intent);
DataHolderUtil.getInstance().delete(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));

Log_OC.d(TAG, "Setting progress visibility to " + mSyncInProgress);
setIndeterminate(mSyncInProgress);

Expand All @@ -1174,6 +1176,7 @@ public void onReceive(Context context, Intent intent) {
// avoid app crashes after changing the serial id of RemoteOperationResult
// in owncloud library with broadcast notifications pending to process
removeStickyBroadcast(intent);
DataHolderUtil.getInstance().delete(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
import com.owncloud.android.ui.fragment.FileFragment;
import com.owncloud.android.ui.fragment.OCFileListFragment;
import com.owncloud.android.utils.DataHolderUtil;
import com.owncloud.android.utils.ErrorMessageAdapter;

import java.util.ArrayList;
Expand Down Expand Up @@ -431,9 +432,9 @@ public void onReceive(Context context, Intent intent) {
String event = intent.getAction();
Log_OC.d(TAG, "Received broadcast " + event);
String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME);
String synchFolderRemotePath = intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
RemoteOperationResult synchResult = (RemoteOperationResult)intent.
getSerializableExtra(FileSyncAdapter.EXTRA_RESULT);
String synchFolderRemotePath = intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
RemoteOperationResult synchResult = (RemoteOperationResult)
DataHolderUtil.getInstance().retrieve(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
boolean sameAccount = (getAccount() != null &&
accountName.equals(getAccount().name) && getStorageManager() != null);

Expand Down Expand Up @@ -495,6 +496,7 @@ public void onReceive(Context context, Intent intent) {
}
}
removeStickyBroadcast(intent);
DataHolderUtil.getInstance().delete(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
Log_OC.d(TAG, "Setting progress visibility to " + mSyncInProgress);

setIndeterminate(mSyncInProgress);
Expand All @@ -506,6 +508,7 @@ public void onReceive(Context context, Intent intent) {
// avoid app crashes after changing the serial id of RemoteOperationResult
// in owncloud library with broadcast notifications pending to process
removeStickyBroadcast(intent);
DataHolderUtil.getInstance().delete(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
import com.owncloud.android.ui.fragment.TaskRetainerFragment;
import com.owncloud.android.ui.helpers.UriUploader;
import com.owncloud.android.utils.DataHolderUtil;
import com.owncloud.android.utils.ErrorMessageAdapter;
import com.owncloud.android.utils.FileStorageUtils;

Expand Down Expand Up @@ -1032,9 +1033,8 @@ public void onReceive(Context context, Intent intent) {
String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME);
String synchFolderRemotePath =
intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
RemoteOperationResult synchResult =
(RemoteOperationResult) intent.getSerializableExtra(
FileSyncAdapter.EXTRA_RESULT);
RemoteOperationResult synchResult = (RemoteOperationResult)
DataHolderUtil.getInstance().retrieve(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
boolean sameAccount = (getAccount() != null &&
accountName.equals(getAccount().name) && getStorageManager() != null);

Expand Down Expand Up @@ -1099,6 +1099,7 @@ public void onReceive(Context context, Intent intent) {
// avoid app crashes after changing the serial id of RemoteOperationResult
// in owncloud library with broadcast notifications pending to process
removeStickyBroadcast(intent);
DataHolderUtil.getInstance().delete(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions src/com/owncloud/android/utils/DataHolderUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Nextcloud Android client application
*
* @author Mario Danic
* Copyright (C) 2017 Mario Danic
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
* <p>
* 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 Affero General Public License for more details.
* <p>
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.utils;

import java.lang.ref.WeakReference;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

/**
* Data holder utility to store & retrieve stuff
*/

public class DataHolderUtil {
private Map<String, WeakReference<Object>> data = new HashMap<String, WeakReference<Object>>();

private static DataHolderUtil instance;

private SecureRandom random = new SecureRandom();

public static synchronized DataHolderUtil getInstance() {
if (instance == null) {
instance = new DataHolderUtil();
}
return instance;
}

public void save(String id, Object object) {
data.put(id, new WeakReference<Object>(object));
}

public Object retrieve(String id) {
WeakReference<Object> objectWeakReference = data.get(id);
return objectWeakReference.get();
}

public void delete(String id) {
if (id != null) {
data.remove(id);
}
}

public String nextItemId() {
String nextItemId = new BigInteger(130, random).toString(32);
while (data.containsKey(nextItemId)) {
nextItemId = new BigInteger(130, random).toString(32);
}
return nextItemId;
}
}

0 comments on commit acf1caa

Please sign in to comment.