|
| 1 | +package vi.filepicker; |
| 2 | + |
| 3 | + |
| 4 | +import android.Manifest; |
| 5 | +import android.app.Activity; |
| 6 | +import android.content.Intent; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.support.annotation.NonNull; |
| 9 | +import android.support.annotation.Nullable; |
| 10 | +import android.support.v4.app.Fragment; |
| 11 | +import android.support.v7.widget.DefaultItemAnimator; |
| 12 | +import android.support.v7.widget.OrientationHelper; |
| 13 | +import android.support.v7.widget.RecyclerView; |
| 14 | +import android.support.v7.widget.StaggeredGridLayoutManager; |
| 15 | +import android.view.LayoutInflater; |
| 16 | +import android.view.View; |
| 17 | +import android.view.ViewGroup; |
| 18 | +import android.widget.Button; |
| 19 | +import android.widget.Toast; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | + |
| 23 | +import butterknife.Bind; |
| 24 | +import butterknife.ButterKnife; |
| 25 | +import butterknife.OnClick; |
| 26 | +import droidninja.filepicker.FilePickerBuilder; |
| 27 | +import droidninja.filepicker.FilePickerConst; |
| 28 | +import droidninja.filepicker.fragments.BaseFragment; |
| 29 | +import permissions.dispatcher.NeedsPermission; |
| 30 | +import permissions.dispatcher.RuntimePermissions; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * A simple {@link Fragment} subclass. |
| 35 | + */ |
| 36 | +@RuntimePermissions |
| 37 | +public class CallerFragment extends BaseFragment { |
| 38 | + private ArrayList<String> filePaths; |
| 39 | + |
| 40 | + public CallerFragment() { |
| 41 | + // Required empty public constructor |
| 42 | + } |
| 43 | + |
| 44 | + @Bind(R.id.open_fragment) |
| 45 | + Button openFragmentBtn; |
| 46 | + |
| 47 | + |
| 48 | + @Override |
| 49 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 50 | + Bundle savedInstanceState) { |
| 51 | + // Inflate the layout for this fragment |
| 52 | + View view = inflater.inflate(R.layout.activity_main, container, false); |
| 53 | + ButterKnife.bind(this, view); |
| 54 | + |
| 55 | + return view; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { |
| 60 | + super.onViewCreated(view, savedInstanceState); |
| 61 | + |
| 62 | + initView(); |
| 63 | + } |
| 64 | + |
| 65 | + private void initView() { |
| 66 | + openFragmentBtn.setVisibility(View.GONE); |
| 67 | + } |
| 68 | + |
| 69 | + @OnClick(R.id.pick_photo) |
| 70 | + public void pickPhotoClicked(View view) { |
| 71 | + CallerFragmentPermissionsDispatcher.onPickPhotoWithCheck(this); |
| 72 | + } |
| 73 | + |
| 74 | + @OnClick(R.id.pick_doc) |
| 75 | + public void pickDocClicked(View view) { |
| 76 | + CallerFragmentPermissionsDispatcher.onPickDocWithCheck(this); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 82 | + super.onActivityResult(requestCode, resultCode, data); |
| 83 | + switch (requestCode) |
| 84 | + { |
| 85 | + case FilePickerConst.REQUEST_CODE: |
| 86 | + if(resultCode== Activity.RESULT_OK && data!=null) |
| 87 | + { |
| 88 | + filePaths = data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_PHOTOS); |
| 89 | + addThemToView(filePaths); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void addThemToView(ArrayList<String> filePaths) { |
| 95 | + RecyclerView recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerview); |
| 96 | + if(recyclerView!=null) { |
| 97 | + StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL); |
| 98 | + layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); |
| 99 | + recyclerView.setLayoutManager(layoutManager); |
| 100 | + |
| 101 | + ImageAdapter imageAdapter = new ImageAdapter(getActivity(), filePaths); |
| 102 | + |
| 103 | + recyclerView.setAdapter(imageAdapter); |
| 104 | + recyclerView.setItemAnimator(new DefaultItemAnimator()); |
| 105 | + } |
| 106 | + |
| 107 | + Toast.makeText(getActivity(), "Num of files selected: "+ filePaths.size(), Toast.LENGTH_SHORT).show(); |
| 108 | + } |
| 109 | + |
| 110 | + @NeedsPermission({Manifest.permission.WRITE_EXTERNAL_STORAGE}) |
| 111 | + public void onPickPhoto() { |
| 112 | + FilePickerBuilder.getInstance().setMaxCount(5) |
| 113 | + .setSelectedFiles(filePaths) |
| 114 | + //make your own Dark action bar theme according to your app design and set it here |
| 115 | + .setActivityTheme(R.style.FilePickerTheme) |
| 116 | + .pickPhoto(this); |
| 117 | + } |
| 118 | + |
| 119 | + @NeedsPermission({Manifest.permission.WRITE_EXTERNAL_STORAGE}) |
| 120 | + public void onPickDoc() { |
| 121 | + FilePickerBuilder.getInstance().setMaxCount(10) |
| 122 | + .setSelectedFiles(filePaths) |
| 123 | + //make your own Dark action bar theme according to your app design and set it here |
| 124 | + .setActivityTheme(R.style.FilePickerTheme) |
| 125 | + .pickDocument(this); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 130 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 131 | + CallerFragmentPermissionsDispatcher.onRequestPermissionsResult(this,requestCode,grantResults); |
| 132 | + } |
| 133 | + |
| 134 | +} |