Skip to content

Commit

Permalink
SRV: Fix DataPackages ConcurrentModificationException with Watcher ca…
Browse files Browse the repository at this point in the history
…llback
  • Loading branch information
VishalNehra committed Jan 30, 2017
1 parent f0a409e commit c0980c0
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public void onFinish() {

if (openprocesses) {
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, new ProcessViewer());
transaction.replace(R.id.content_frame, new ProcessViewer(), KEY_INTENT_PROCESS_VIEWER);
// transaction.addToBackStack(null);
select = 102;
openprocesses = false;
Expand Down Expand Up @@ -2419,7 +2419,7 @@ public void onNewIntent(Intent i) {
} else if ((openprocesses = i.getBooleanExtra(KEY_INTENT_PROCESS_VIEWER, false))) {

android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, new ProcessViewer());
transaction.replace(R.id.content_frame, new ProcessViewer(), KEY_INTENT_PROCESS_VIEWER);
// transaction.addToBackStack(null);
select = 102;
openprocesses = false;
Expand Down
72 changes: 15 additions & 57 deletions src/main/java/com/amaze/filemanager/fragments/ProcessViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.concurrent.TimeUnit;

public class ProcessViewer extends Fragment {
Expand Down Expand Up @@ -134,23 +133,9 @@ public void onServiceConnected(ComponentName className,
CopyService.LocalBinder localBinder = (CopyService.LocalBinder) service;
CopyService copyService = localBinder.getService();

ArrayList<DataPackage> dataPackages;
try {
dataPackages = copyService.getDataPackageList();
} catch (ConcurrentModificationException e) {
// array list was being modified while fetching (even after synchronization) :/
// return for now
return;
}

for (final DataPackage dataPackage : dataPackages) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
for (int i=0; i<copyService.getDataPackageSize(); i++) {

processResults(dataPackage, ServiceType.COPY);
}
});
processResults(copyService.getDataPackage(i), ServiceType.COPY);
}

// animate the chart a little after initial values have been applied
Expand All @@ -159,7 +144,8 @@ public void run() {
copyService.setProgressListener(new CopyService.ProgressListener() {
@Override
public void onUpdate(final DataPackage dataPackage) {
if (getActivity()==null) {
if (getActivity() == null || getActivity().getSupportFragmentManager().
findFragmentByTag(MainActivity.KEY_INTENT_PROCESS_VIEWER) == null) {
// callback called when we're not inside the app
return;
}
Expand Down Expand Up @@ -193,23 +179,9 @@ public void onServiceConnected(ComponentName className,
ExtractService.LocalBinder localBinder = (ExtractService.LocalBinder) service;
ExtractService extractService = localBinder.getService();

ArrayList<DataPackage> dataPackages;
try {
dataPackages = extractService.getDataPackageList();
} catch (ConcurrentModificationException e) {
// array list was being modified while fetching (even after synchronization) :/
// return for now
return;
}

for (final DataPackage dataPackage : dataPackages) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
for (int i=0; i<extractService.getDataPackageSize(); i++) {

processResults(dataPackage, ServiceType.EXTRACT);
}
});
processResults(extractService.getDataPackage(i), ServiceType.EXTRACT);
}

// animate the chart a little after initial values have been applied
Expand Down Expand Up @@ -252,23 +224,9 @@ public void onServiceConnected(ComponentName className,
ZipTask.LocalBinder localBinder = (ZipTask.LocalBinder) service;
ZipTask zipTask = localBinder.getService();

ArrayList<DataPackage> dataPackages;
try {
dataPackages = zipTask.getDataPackageList();
} catch (ConcurrentModificationException e) {
// array list was being modified while fetching (even after synchronization) :/
// return for now
return;
}
for (int i=0; i<zipTask.getDataPackageSize(); i++) {

for (final DataPackage dataPackage : dataPackages) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {

processResults(dataPackage, ServiceType.COMPRESS);
}
});
processResults(zipTask.getDataPackage(i), ServiceType.COMPRESS);
}

// animate the chart a little after initial values have been applied
Expand Down Expand Up @@ -361,25 +319,25 @@ public void processResults(final DataPackage dataPackage, ServiceType serviceTyp

mProgressFileNameText.setText(name);

Spanned bytesText = Html.fromHtml(getString(R.string.written)
Spanned bytesText = Html.fromHtml(getResources().getString(R.string.written)
+ " <font color='" + accentColor + "'><i>" + Formatter.formatFileSize(getContext(), doneBytes)
+ " </font></i>" + getString(R.string.out_of) + " <i>"
+ " </font></i>" + getResources().getString(R.string.out_of) + " <i>"
+ Formatter.formatFileSize(getContext(), total) + "</i>");
mProgressBytesText.setText(bytesText);

Spanned fileProcessedSpan = Html.fromHtml(getString(R.string.processing_file)
Spanned fileProcessedSpan = Html.fromHtml(getResources().getString(R.string.processing_file)
+ " <font color='" + accentColor + "'><i>" + (dataPackage.getSourceProgress())
+ " </font></i>" + getString(R.string.of) + " <i>"
+ " </font></i>" + getResources().getString(R.string.of) + " <i>"
+ dataPackage.getSourceFiles() + "</i>");
mProgressFileText.setText(fileProcessedSpan);

Spanned speedSpan = Html.fromHtml(getString(R.string.current_speed)
Spanned speedSpan = Html.fromHtml(getResources().getString(R.string.current_speed)
+ ": <font color='" + accentColor + "'><i>"
+ Formatter.formatFileSize(getContext(), dataPackage.getSpeedRaw())
+ "/s</font></i>");
mProgressSpeedText.setText(speedSpan);

Spanned timerSpan = Html.fromHtml(getString(R.string.service_timer)
Spanned timerSpan = Html.fromHtml(getResources().getString(R.string.service_timer)
+ ": <font color='" + accentColor + "'><i>"
+ formatTimer(++time)
+ "</font></i>");
Expand Down Expand Up @@ -457,7 +415,7 @@ public void onClick(View v) {
Toast.makeText(getActivity(),
getResources().getString(R.string.stopping), Toast.LENGTH_LONG).show();
getActivity().sendBroadcast(intent);
mProgressTypeText.setText(getString(R.string.cancelled));
mProgressTypeText.setText(getResources().getString(R.string.cancelled));
mProgressSpeedText.setText("");
mProgressFileText.setText("");
mProgressBytesText.setText("");
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/amaze/filemanager/services/CopyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ private void publishResults(int id, String fileName, int sourceFiles, int source
intent.setMove(move);
intent.setCompleted(isComplete);
putDataPackage(intent);
if(progressListener!=null){
if(progressListener!=null) {
progressListener.onUpdate(intent);
if(isComplete) progressListener.refresh();
}
Expand Down Expand Up @@ -588,8 +588,12 @@ public void setProgressListener(ProgressListener progressListener) {
* is executing the callbacks in {@link com.amaze.filemanager.fragments.ProcessViewer}
* @return
*/
public synchronized ArrayList<DataPackage> getDataPackageList() {
return this.dataPackages;
public synchronized DataPackage getDataPackage(int index) {
return this.dataPackages.get(index);
}

public synchronized int getDataPackageSize() {
return this.dataPackages.size();
}

/**
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/amaze/filemanager/services/ExtractService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.amaze.filemanager.filesystem.FileUtil;
import com.amaze.filemanager.utils.AppConfig;
import com.amaze.filemanager.utils.DataPackage;
import com.amaze.filemanager.utils.Futils;
import com.amaze.filemanager.utils.GenericCopyUtil;
import com.amaze.filemanager.utils.ProgressHandler;
import com.amaze.filemanager.utils.ServiceWatcherUtil;
Expand Down Expand Up @@ -562,7 +561,9 @@ else if(f.getName().toLowerCase().endsWith(".tar") || f.getName().toLowerCase().
@Override
public void onPostExecute(Integer b) {

watcherUtil.stopWatch();
// check whether watcherutil was initialized. It was not initialized when we got exception
// in extracting the file
if(watcherUtil != null) watcherUtil.stopWatch();
Intent intent = new Intent("loadlist");
sendBroadcast(intent);
stopSelf();
Expand Down Expand Up @@ -621,8 +622,12 @@ public IBinder onBind(Intent arg0) {
* is executing the callbacks in {@link com.amaze.filemanager.fragments.ProcessViewer}
* @return
*/
public synchronized ArrayList<DataPackage> getDataPackageList() {
return this.dataPackages;
public synchronized DataPackage getDataPackage(int index) {
return this.dataPackages.get(index);
}

public synchronized int getDataPackageSize() {
return this.dataPackages.size();
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/amaze/filemanager/services/ZipTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.amaze.filemanager.filesystem.BaseFile;
import com.amaze.filemanager.filesystem.FileUtil;
import com.amaze.filemanager.utils.DataPackage;
import com.amaze.filemanager.utils.Futils;
import com.amaze.filemanager.utils.GenericCopyUtil;
import com.amaze.filemanager.utils.PreferenceUtils;
import com.amaze.filemanager.utils.ProgressHandler;
Expand Down Expand Up @@ -344,8 +343,12 @@ public void onDestroy() {
* is executing the callbacks in {@link com.amaze.filemanager.fragments.ProcessViewer}
* @return
*/
public synchronized ArrayList<DataPackage> getDataPackageList() {
return this.dataPackages;
public synchronized DataPackage getDataPackage(int index) {
return this.dataPackages.get(index);
}

public synchronized int getDataPackageSize() {
return this.dataPackages.size();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ private class Listviewadapter extends RecyclerArrayAdapter<Computer, Listviewada
private static final int VIEW_ELEMENT = 2;

LayoutInflater mInflater;
Context context;

public Listviewadapter(Context context, @LayoutRes int resource, List<Computer> objects) {
this.context = context;
addAll(objects);
mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
Expand All @@ -150,14 +152,14 @@ public Listviewadapter(Context context, @LayoutRes int resource, List<Computer>
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case VIEW_PROGRESSBAR:
ProgressBar progressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyle);
ProgressBar progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyle);
progressBar.setIndeterminate(true);
progressBar.setBackgroundDrawable(null);

return new ViewHolder(progressBar);
default:
case VIEW_ELEMENT:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.smb_computers_row, parent, false);
View view = mInflater.inflate(R.layout.smb_computers_row, parent, false);

return new ElementViewHolder(view);
}
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/amaze/filemanager/utils/Futils.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@

import java.io.File;
import java.net.MalformedURLException;
import java.text.DecimalFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -354,10 +353,8 @@ public void shareFiles(ArrayList<File> a, Activity c,AppTheme appTheme,int fab_s
public static float readableFileSizeFloat(long size) {
if (size <= 0)
return 0;
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
Float newSize = Float.valueOf(new DecimalFormat("#,##0.##").format(size
/ Math.pow(1024, digitGroups)));
return newSize;
float digitGroups = (float) (size / (1024*1024));
return digitGroups;
}

private boolean isSelfDefault(File f, Context c){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public void onFinish() {

if (openprocesses) {
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, new ProcessViewer());
transaction.replace(R.id.content_frame, new ProcessViewer(), KEY_INTENT_PROCESS_VIEWER);
// transaction.addToBackStack(null);
select = 102;
openprocesses = false;
Expand Down Expand Up @@ -2572,7 +2572,7 @@ public void onNewIntent(Intent i) {
} else if ((openprocesses = i.getBooleanExtra(KEY_INTENT_PROCESS_VIEWER, false))) {

android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, new ProcessViewer());
transaction.replace(R.id.content_frame, new ProcessViewer(), KEY_INTENT_PROCESS_VIEWER);
// transaction.addToBackStack(null);
select = 102;
openprocesses = false;
Expand Down

1 comment on commit c0980c0

@VishalNehra
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly fixes #431

Please sign in to comment.