Change Upload way
- Add FloatingActionButton for Upload - Delete Upload Tab
This commit is contained in:
		@ -1,6 +1,7 @@
 | 
			
		||||
package com.localtransfer;
 | 
			
		||||
 | 
			
		||||
import android.Manifest;
 | 
			
		||||
import android.app.Activity;
 | 
			
		||||
import android.content.ActivityNotFoundException;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.content.SharedPreferences;
 | 
			
		||||
@ -31,6 +32,8 @@ import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
public class MainActivity extends AppCompatActivity {
 | 
			
		||||
 | 
			
		||||
    public static final int REQUEST_ID_CHOOSE_FILES = 2002;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void onCreate(Bundle savedInstanceState) {
 | 
			
		||||
        super.onCreate(savedInstanceState);
 | 
			
		||||
@ -42,6 +45,19 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        viewPager.setAdapter(sectionsPagerAdapter);
 | 
			
		||||
        TabLayout tabs = findViewById(R.id.tabs);
 | 
			
		||||
        tabs.setupWithViewPager(viewPager);
 | 
			
		||||
        FloatingActionButton fab = findViewById(R.id.flotUpload);
 | 
			
		||||
        fab.setOnClickListener(new View.OnClickListener() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onClick(View view) {
 | 
			
		||||
                Intent intent = new Intent()
 | 
			
		||||
                        .setType("*/*")
 | 
			
		||||
                        .setAction(Intent.ACTION_GET_CONTENT)
 | 
			
		||||
                        .putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
 | 
			
		||||
 | 
			
		||||
                startActivityForResult(Intent.createChooser(intent, "Select a file"), REQUEST_ID_CHOOSE_FILES);
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        checkAndRequestPermissions();
 | 
			
		||||
 | 
			
		||||
@ -84,6 +100,42 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
 | 
			
		||||
        super.onActivityResult(requestCode, resultCode, data);
 | 
			
		||||
        Log.d("RequestCode", String.valueOf(requestCode));
 | 
			
		||||
        if (requestCode == REQUEST_ID_CHOOSE_FILES && resultCode == Activity.RESULT_OK) {
 | 
			
		||||
            String type = data.getType();
 | 
			
		||||
 | 
			
		||||
            ViewPager viewPager = this.findViewById(R.id.view_pager);
 | 
			
		||||
            viewPager.setCurrentItem(2);
 | 
			
		||||
 | 
			
		||||
            Transfer tr = new Transfer();
 | 
			
		||||
 | 
			
		||||
            if (type != null) {
 | 
			
		||||
                String sharedText = data.getStringExtra(Intent.EXTRA_TEXT);
 | 
			
		||||
                tr.handleSendText(sharedText);
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                ArrayList<Uri> fileUris = new ArrayList<>();
 | 
			
		||||
                if (data.getClipData() != null) { // Checking for selection multiple files
 | 
			
		||||
                    int nbItem = data.getClipData().getItemCount();
 | 
			
		||||
                    Toast.makeText(this, "You select " + nbItem + " files", Toast.LENGTH_SHORT).show();
 | 
			
		||||
                    for (int i = 0; i < nbItem; i++) {
 | 
			
		||||
                        Uri uri = data.getClipData().getItemAt(i).getUri();
 | 
			
		||||
                        fileUris.add(uri);
 | 
			
		||||
                    }
 | 
			
		||||
                } else {
 | 
			
		||||
                    Uri uri = data.getData(); //The uri with the location of the file
 | 
			
		||||
                    fileUris.add(uri);
 | 
			
		||||
                }
 | 
			
		||||
                for (Uri uri : fileUris) {
 | 
			
		||||
                    tr.handleSendFile(uri);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onResume(){
 | 
			
		||||
        super.onResume();
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,6 @@ import androidx.fragment.app.FragmentPagerAdapter;
 | 
			
		||||
 | 
			
		||||
import com.localtransfer.fragment.DownloadFragment;
 | 
			
		||||
import com.localtransfer.fragment.ProgressFragment;
 | 
			
		||||
import com.localtransfer.fragment.UploadFragment;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A [FragmentPagerAdapter] that returns a fragment corresponding to
 | 
			
		||||
@ -19,7 +18,7 @@ import com.localtransfer.fragment.UploadFragment;
 | 
			
		||||
public class SectionsPagerAdapter extends FragmentPagerAdapter {
 | 
			
		||||
 | 
			
		||||
    @StringRes
 | 
			
		||||
    private static final int[] TAB_TITLES = new int[]{R.string.download, R.string.upload, R.string.progress};
 | 
			
		||||
    private static final int[] TAB_TITLES = new int[]{R.string.download, R.string.progress};
 | 
			
		||||
    private final Context mContext;
 | 
			
		||||
 | 
			
		||||
    public SectionsPagerAdapter(Context context, FragmentManager fm) {
 | 
			
		||||
@ -35,8 +34,6 @@ public class SectionsPagerAdapter extends FragmentPagerAdapter {
 | 
			
		||||
            case 0: //Page number 1
 | 
			
		||||
                return DownloadFragment.newInstance();
 | 
			
		||||
            case 1: //Page number 2
 | 
			
		||||
                return UploadFragment.newInstance();
 | 
			
		||||
            case 2: //Page number 3
 | 
			
		||||
                return ProgressFragment.newInstance();
 | 
			
		||||
            default:
 | 
			
		||||
                return null;
 | 
			
		||||
@ -51,6 +48,6 @@ public class SectionsPagerAdapter extends FragmentPagerAdapter {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getCount() {
 | 
			
		||||
        return 3;
 | 
			
		||||
        return 2;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,97 +0,0 @@
 | 
			
		||||
package com.localtransfer.fragment;
 | 
			
		||||
 | 
			
		||||
import android.app.Activity;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.net.Uri;
 | 
			
		||||
import android.os.Bundle;
 | 
			
		||||
 | 
			
		||||
import androidx.fragment.app.Fragment;
 | 
			
		||||
import androidx.viewpager.widget.ViewPager;
 | 
			
		||||
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
import android.view.LayoutInflater;
 | 
			
		||||
import android.view.View;
 | 
			
		||||
import android.view.ViewGroup;
 | 
			
		||||
import android.widget.Button;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
 | 
			
		||||
import com.localtransfer.R;
 | 
			
		||||
import com.localtransfer.Transfer;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
public class UploadFragment extends Fragment {
 | 
			
		||||
 | 
			
		||||
    public UploadFragment() {
 | 
			
		||||
        // Required empty public constructor
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static UploadFragment newInstance() {
 | 
			
		||||
        UploadFragment fragment = new UploadFragment();
 | 
			
		||||
        return fragment;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onCreate(Bundle savedInstanceState) {
 | 
			
		||||
        super.onCreate(savedInstanceState);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
 | 
			
		||||
                             Bundle savedInstanceState) {
 | 
			
		||||
        // Inflate the layout for this fragment
 | 
			
		||||
        final View root =  inflater.inflate(R.layout.fragment_upload, container, false);
 | 
			
		||||
 | 
			
		||||
        Button upload = (Button) root.findViewById(R.id.upload);
 | 
			
		||||
        upload.setOnClickListener(v -> selectFile());
 | 
			
		||||
 | 
			
		||||
        return root;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static final int REQUEST_ID_CHOOSE_FILES = 2002;
 | 
			
		||||
 | 
			
		||||
    private void selectFile() {
 | 
			
		||||
        Intent intent = new Intent()
 | 
			
		||||
                .setType("*/*")
 | 
			
		||||
                .setAction(Intent.ACTION_GET_CONTENT)
 | 
			
		||||
                .putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
 | 
			
		||||
 | 
			
		||||
        startActivityForResult(Intent.createChooser(intent, "Select a file"), REQUEST_ID_CHOOSE_FILES);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
 | 
			
		||||
        super.onActivityResult(requestCode, resultCode, data);
 | 
			
		||||
        Log.d("RequestCode", String.valueOf(requestCode));
 | 
			
		||||
        if (requestCode == REQUEST_ID_CHOOSE_FILES && resultCode == Activity.RESULT_OK) {
 | 
			
		||||
            String type = data.getType();
 | 
			
		||||
 | 
			
		||||
            ViewPager viewPager = getActivity().findViewById(R.id.view_pager);
 | 
			
		||||
            viewPager.setCurrentItem(2);
 | 
			
		||||
 | 
			
		||||
            Transfer tr = new Transfer();
 | 
			
		||||
 | 
			
		||||
            if (type != null) {
 | 
			
		||||
                String sharedText = data.getStringExtra(Intent.EXTRA_TEXT);
 | 
			
		||||
                tr.handleSendText(sharedText);
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                ArrayList<Uri> fileUris = new ArrayList<>();
 | 
			
		||||
                if (data.getClipData() != null) { // Checking for selection multiple files
 | 
			
		||||
                    int nbItem = data.getClipData().getItemCount();
 | 
			
		||||
                    Toast.makeText(getContext(), "You select " + nbItem + " files", Toast.LENGTH_SHORT).show();
 | 
			
		||||
                    for (int i = 0; i < nbItem; i++) {
 | 
			
		||||
                        Uri uri = data.getClipData().getItemAt(i).getUri();
 | 
			
		||||
                        fileUris.add(uri);
 | 
			
		||||
                    }
 | 
			
		||||
                } else {
 | 
			
		||||
                    Uri uri = data.getData(); //The uri with the location of the file
 | 
			
		||||
                    fileUris.add(uri);
 | 
			
		||||
                }
 | 
			
		||||
                for (Uri uri : fileUris) {
 | 
			
		||||
                    tr.handleSendFile(uri);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user