mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-22 03:54:18 -04:00
Increment: Transfer progress is now displayed in the list of files
This commit is contained in:
parent
98184e82cd
commit
4115ca3bfb
@ -85,6 +85,7 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
private int totalFiles = -1;
|
private int totalFiles = -1;
|
||||||
private int totalFilesSent = 0;
|
private int totalFilesSent = 0;
|
||||||
private ArrayList<FileItem> fileItems = new ArrayList<>();
|
private ArrayList<FileItem> fileItems = new ArrayList<>();
|
||||||
|
private TransferProgressFragment transferProgressFragment;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||||
@ -124,6 +125,8 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
|
|
||||||
for(int i = 0; i < fileUriList.size(); i++)
|
for(int i = 0; i < fileUriList.size(); i++)
|
||||||
fileItems.add(new FileItem(getFileName(fileUriList.get(i)), TO_BE_SENT));
|
fileItems.add(new FileItem(getFileName(fileUriList.get(i)), TO_BE_SENT));
|
||||||
|
|
||||||
|
displayTransferProgressFragment();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,10 +247,15 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
return fileUriList;
|
return fileUriList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void incrementTotalFilesSent() {
|
public void incrementTotalFilesSent() {
|
||||||
this.totalFilesSent++;
|
this.totalFilesSent++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getTotalFilesSent() {
|
||||||
|
return totalFilesSent;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean allFilesSent() {
|
public boolean allFilesSent() {
|
||||||
return (totalFilesSent == totalFiles);
|
return (totalFilesSent == totalFiles);
|
||||||
}
|
}
|
||||||
@ -261,19 +269,19 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void displayTransferProgressFragment() {
|
private void displayTransferProgressFragment() {
|
||||||
TransferProgressFragment fragment = new TransferProgressFragment(fileItems);
|
transferProgressFragment = new TransferProgressFragment(fileItems);
|
||||||
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
|
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
|
||||||
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
||||||
fragmentTransaction.add(R.id.container_fragment_transfer_progress, fragment)
|
fragmentTransaction.add(R.id.container_fragment_transfer_progress, transferProgressFragment)
|
||||||
.commit();
|
.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startFileTransfer() {
|
private void startFileTransfer() {
|
||||||
|
|
||||||
displayTransferProgressFragment();
|
|
||||||
|
|
||||||
if(groupInfo.groupFormed && !fileSender) {
|
if(groupInfo.groupFormed && !fileSender) {
|
||||||
new FileServerAsyncTask(getActivity(), this).execute();
|
displayTransferProgressFragment();
|
||||||
|
|
||||||
|
new FileServerAsyncTask(getActivity(), this, transferProgressFragment).execute();
|
||||||
Toast.makeText(getActivity(), "File receiving device", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getActivity(), "File receiving device", Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
} else if(groupInfo.groupFormed) {
|
} else if(groupInfo.groupFormed) {
|
||||||
@ -309,7 +317,7 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
for(int i = 0; i < 10000000; i++);
|
for(int i = 0; i < 10000000; i++);
|
||||||
|
|
||||||
for(int i = 0; i < totalFiles; i++) {
|
for(int i = 0; i < totalFiles; i++) {
|
||||||
new FileSenderAsyncTask(getContext(), this, groupInfo).execute(fileUriList.get(i));
|
new FileSenderAsyncTask(getContext(), this, groupInfo, transferProgressFragment).execute(fileUriList.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -541,11 +549,29 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
private Context context;
|
private Context context;
|
||||||
private DeviceListFragment deviceListFragment;
|
private DeviceListFragment deviceListFragment;
|
||||||
private WifiP2pInfo groupInfo;
|
private WifiP2pInfo groupInfo;
|
||||||
|
private TransferProgressFragment transferProgressFragment;
|
||||||
|
private ArrayList<FileItem> fileItems;
|
||||||
|
private int fileItemIndex;
|
||||||
|
|
||||||
public FileSenderAsyncTask(Context context, DeviceListFragment deviceListFragment, WifiP2pInfo groupInfo) {
|
public FileSenderAsyncTask(Context context, DeviceListFragment deviceListFragment, WifiP2pInfo groupInfo, TransferProgressFragment transferProgressFragment) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.deviceListFragment = deviceListFragment;
|
this.deviceListFragment = deviceListFragment;
|
||||||
this.groupInfo = groupInfo;
|
this.groupInfo = groupInfo;
|
||||||
|
this.transferProgressFragment = transferProgressFragment;
|
||||||
|
this.fileItems = deviceListFragment.getFileItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPreExecute() {
|
||||||
|
fileItemIndex = deviceListFragment.getTotalFilesSent();
|
||||||
|
|
||||||
|
deviceListFragment.getActivity().runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
transferProgressFragment.changeStatus(fileItemIndex, FileItem.SENDING);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -598,6 +624,13 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
//
|
//
|
||||||
deviceListFragment.incrementTotalFilesSent();
|
deviceListFragment.incrementTotalFilesSent();
|
||||||
|
|
||||||
|
deviceListFragment.getActivity().runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
transferProgressFragment.changeStatus(fileItemIndex, FileItem.SENT);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if(deviceListFragment.allFilesSent()) {
|
if(deviceListFragment.allFilesSent()) {
|
||||||
deviceListFragment.getActivity().finish();
|
deviceListFragment.getActivity().finish();
|
||||||
}
|
}
|
||||||
@ -608,11 +641,14 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
private DeviceListFragment deviceListFragment;
|
private DeviceListFragment deviceListFragment;
|
||||||
|
private TransferProgressFragment transferProgressFragment;
|
||||||
|
private int fileItemIndex;
|
||||||
//private View statusView
|
//private View statusView
|
||||||
|
|
||||||
public FileServerAsyncTask(Context context, DeviceListFragment deviceListFragment) {
|
public FileServerAsyncTask(Context context, DeviceListFragment deviceListFragment, TransferProgressFragment transferProgressFragment) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.deviceListFragment = deviceListFragment;
|
this.deviceListFragment = deviceListFragment;
|
||||||
|
this.transferProgressFragment = transferProgressFragment;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -627,6 +663,13 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
for(int currentFile = 1; currentFile <= totalFileCount; currentFile++) {
|
for(int currentFile = 1; currentFile <= totalFileCount; currentFile++) {
|
||||||
Socket client = serverSocket.accept();
|
Socket client = serverSocket.accept();
|
||||||
Log.d(LocalFileTransferActivity.TAG, "Server: Client connected");
|
Log.d(LocalFileTransferActivity.TAG, "Server: Client connected");
|
||||||
|
fileItemIndex = currentFile-1;
|
||||||
|
deviceListFragment.getActivity().runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
transferProgressFragment.changeStatus(fileItemIndex, FileItem.SENDING);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
ArrayList<FileItem> fileItems = deviceListFragment.getFileItems();
|
ArrayList<FileItem> fileItems = deviceListFragment.getFileItems();
|
||||||
String incomingFileName = fileItems.get(currentFile-1).getFileName();
|
String incomingFileName = fileItems.get(currentFile-1).getFileName();
|
||||||
@ -645,6 +688,15 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
Log.d(LocalFileTransferActivity.TAG, "Copying files");
|
Log.d(LocalFileTransferActivity.TAG, "Copying files");
|
||||||
InputStream inputStream = client.getInputStream();
|
InputStream inputStream = client.getInputStream();
|
||||||
copyFile(inputStream, new FileOutputStream(clientNoteFileLocation));
|
copyFile(inputStream, new FileOutputStream(clientNoteFileLocation));
|
||||||
|
|
||||||
|
deviceListFragment.getActivity().runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
transferProgressFragment.changeStatus(fileItemIndex, FileItem.SENT);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
deviceListFragment.incrementTotalFilesSent();
|
||||||
}
|
}
|
||||||
serverSocket.close();
|
serverSocket.close();
|
||||||
|
|
||||||
@ -691,4 +743,8 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*public interface UpdateProgressCallback {
|
||||||
|
public void changeStatus(int itemIndex, short status);
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import android.content.Context;
|
|||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
@ -31,8 +33,21 @@ public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.FileVi
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull FileListAdapter.FileViewHolder holder, int position) {
|
public void onBindViewHolder(@NonNull FileListAdapter.FileViewHolder holder, int position) {
|
||||||
String name = fileItems.get(position).getFileName();
|
FileItem fileItem = fileItems.get(position);
|
||||||
holder.fileItemView.setText(name);
|
|
||||||
|
String name = fileItem.getFileName();
|
||||||
|
holder.fileName.setText(name);
|
||||||
|
|
||||||
|
if(fileItem.getFileStatus() == FileItem.SENDING) {
|
||||||
|
holder.statusImage.setVisibility(View.GONE);
|
||||||
|
holder.progressBar.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
|
} else if(fileItem.getFileStatus() == FileItem.SENT) {
|
||||||
|
holder.progressBar.setVisibility(View.GONE);
|
||||||
|
|
||||||
|
holder.statusImage.setImageResource(R.drawable.ic_baseline_check_24px);
|
||||||
|
holder.statusImage.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -41,12 +56,16 @@ public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.FileVi
|
|||||||
}
|
}
|
||||||
|
|
||||||
class FileViewHolder extends RecyclerView.ViewHolder {
|
class FileViewHolder extends RecyclerView.ViewHolder {
|
||||||
public final TextView fileItemView;
|
public final TextView fileName;
|
||||||
|
public final ImageView statusImage;
|
||||||
|
public final ProgressBar progressBar;
|
||||||
final FileListAdapter fileListAdapter;
|
final FileListAdapter fileListAdapter;
|
||||||
|
|
||||||
public FileViewHolder(View itemView, FileListAdapter fileListAdapter) {
|
public FileViewHolder(View itemView, FileListAdapter fileListAdapter) {
|
||||||
super(itemView);
|
super(itemView);
|
||||||
this.fileItemView = itemView.findViewById(R.id.text_view_file_item_name);
|
this.fileName = itemView.findViewById(R.id.text_view_file_item_name);
|
||||||
|
this.statusImage = itemView.findViewById(R.id.image_view_file_transferred);
|
||||||
|
this.progressBar = itemView.findViewById(R.id.progress_bar_transferring_file);
|
||||||
this.fileListAdapter = fileListAdapter;
|
this.fileListAdapter = fileListAdapter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
|
|
||||||
/*TODO
|
/*TODO
|
||||||
* - Fix activity closure upon file transfer (successful or otherwise)
|
* - Fix activity closure upon file transfer (successful or otherwise)
|
||||||
* - Handle multiple selected files
|
* - Handle multiple selected files - DONE
|
||||||
* */
|
* */
|
||||||
|
|
||||||
public static final String TAG = "LocalFileTransferActvty"; // Not a typo, Tags have a length upper limit of 25 characters
|
public static final String TAG = "LocalFileTransferActvty"; // Not a typo, Tags have a length upper limit of 25 characters
|
||||||
|
@ -116,6 +116,11 @@ public class TransferProgressFragment extends Fragment {
|
|||||||
//mListener = null;
|
//mListener = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void changeStatus(int itemIndex, short status) {
|
||||||
|
fileItems.get(itemIndex).setFileStatus(status);
|
||||||
|
fileListAdapter.notifyItemChanged(itemIndex);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This interface must be implemented by activities that contain this
|
* This interface must be implemented by activities that contain this
|
||||||
* fragment to allow an interaction in this fragment to be communicated
|
* fragment to allow an interaction in this fragment to be communicated
|
||||||
|
Loading…
x
Reference in New Issue
Block a user