|
1 | 1 | package vi.filepicker; |
2 | 2 |
|
3 | | - |
4 | 3 | import android.Manifest; |
5 | 4 | import android.app.Activity; |
6 | 5 | import android.content.Intent; |
|
17 | 16 | import android.view.ViewGroup; |
18 | 17 | import android.widget.Button; |
19 | 18 | import android.widget.Toast; |
20 | | - |
21 | 19 | import butterknife.BindView; |
| 20 | + |
22 | 21 | import java.util.ArrayList; |
23 | 22 |
|
24 | 23 | import butterknife.ButterKnife; |
25 | 24 | import butterknife.OnClick; |
26 | 25 | import droidninja.filepicker.FilePickerBuilder; |
27 | 26 | import droidninja.filepicker.FilePickerConst; |
28 | 27 | import droidninja.filepicker.fragments.BaseFragment; |
29 | | - |
30 | | - |
| 28 | +import java.util.ArrayList; |
31 | 29 | /** |
32 | 30 | * A simple {@link Fragment} subclass. |
33 | 31 | */ |
34 | 32 | public class CallerFragment extends BaseFragment { |
35 | | - private int MAX_ATTACHMENT_COUNT = 10; |
36 | | - private ArrayList<String> photoPaths = new ArrayList<>(); |
37 | | - private ArrayList<String> docPaths = new ArrayList<>(); |
38 | | - |
39 | | - public CallerFragment() { |
40 | | - // Required empty public constructor |
41 | | - } |
| 33 | + private int MAX_ATTACHMENT_COUNT = 10; |
| 34 | + private ArrayList<String> photoPaths = new ArrayList<>(); |
| 35 | + private ArrayList<String> docPaths = new ArrayList<>(); |
42 | 36 |
|
43 | | - @BindView(R.id.open_fragment) |
44 | | - Button openFragmentBtn; |
| 37 | + public CallerFragment() { |
| 38 | + // Required empty public constructor |
| 39 | + } |
45 | 40 |
|
| 41 | + @BindView(R.id.open_fragment) Button openFragmentBtn; |
46 | 42 |
|
47 | | - @Override |
48 | | - public View onCreateView(LayoutInflater inflater, ViewGroup container, |
49 | | - Bundle savedInstanceState) { |
50 | | - // Inflate the layout for this fragment |
51 | | - View view = inflater.inflate(R.layout.activity_main, container, false); |
52 | | - ButterKnife.bind(this, view); |
| 43 | + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 44 | + Bundle savedInstanceState) { |
| 45 | + // Inflate the layout for this fragment |
| 46 | + View view = inflater.inflate(R.layout.activity_main, container, false); |
| 47 | + ButterKnife.bind(this, view); |
53 | 48 |
|
54 | | - return view; |
55 | | - } |
| 49 | + return view; |
| 50 | + } |
56 | 51 |
|
57 | | - @Override |
58 | | - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { |
59 | | - super.onViewCreated(view, savedInstanceState); |
| 52 | + @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { |
| 53 | + super.onViewCreated(view, savedInstanceState); |
60 | 54 |
|
61 | | - initView(); |
62 | | - } |
| 55 | + initView(); |
| 56 | + } |
63 | 57 |
|
64 | | - private void initView() { |
65 | | - openFragmentBtn.setVisibility(View.GONE); |
66 | | - } |
| 58 | + private void initView() { |
| 59 | + openFragmentBtn.setVisibility(View.GONE); |
| 60 | + } |
67 | 61 |
|
68 | 62 | @OnClick(R.id.pick_photo) |
69 | 63 | public void pickPhotoClicked(View view) { |
@@ -75,54 +69,50 @@ public void pickDocClicked(View view) { |
75 | 69 | onPickDoc(); |
76 | 70 | } |
77 | 71 |
|
78 | | - @Override |
79 | | - public void onActivityResult(int requestCode, int resultCode, Intent data) { |
80 | | - switch (requestCode) |
81 | | - { |
82 | | - case FilePickerConst.REQUEST_CODE_PHOTO: |
83 | | - if(resultCode== Activity.RESULT_OK && data!=null) |
84 | | - { |
85 | | - photoPaths = new ArrayList<>(); |
86 | | - photoPaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_MEDIA)); |
87 | | - |
88 | | - } |
89 | | - break; |
90 | | - |
91 | | - case FilePickerConst.REQUEST_CODE_DOC: |
92 | | - if(resultCode== Activity.RESULT_OK && data!=null) |
93 | | - { |
94 | | - docPaths = new ArrayList<>(); |
95 | | - docPaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS)); |
96 | | - } |
97 | | - break; |
| 72 | + @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 73 | + switch (requestCode) { |
| 74 | + case FilePickerConst.REQUEST_CODE_PHOTO: |
| 75 | + if (resultCode == Activity.RESULT_OK && data != null) { |
| 76 | + photoPaths = new ArrayList<>(); |
| 77 | + photoPaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_MEDIA)); |
98 | 78 | } |
| 79 | + break; |
99 | 80 |
|
100 | | - addThemToView(photoPaths,docPaths); |
| 81 | + case FilePickerConst.REQUEST_CODE_DOC: |
| 82 | + if (resultCode == Activity.RESULT_OK && data != null) { |
| 83 | + docPaths = new ArrayList<>(); |
| 84 | + docPaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS)); |
| 85 | + } |
| 86 | + break; |
101 | 87 | } |
102 | 88 |
|
103 | | - private void addThemToView(ArrayList<String> imagePaths, ArrayList<String> docPaths) { |
104 | | - ArrayList<String> filePaths = new ArrayList<>(); |
105 | | - if(imagePaths!=null) |
106 | | - filePaths.addAll(imagePaths); |
| 89 | + addThemToView(photoPaths, docPaths); |
| 90 | + } |
107 | 91 |
|
108 | | - if(docPaths!=null) |
109 | | - filePaths.addAll(docPaths); |
| 92 | + private void addThemToView(ArrayList<String> imagePaths, ArrayList<String> docPaths) { |
| 93 | + ArrayList<String> filePaths = new ArrayList<>(); |
| 94 | + if (imagePaths != null) filePaths.addAll(imagePaths); |
110 | 95 |
|
111 | | - RecyclerView recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerview); |
112 | | - if(recyclerView!=null) { |
113 | | - StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL); |
114 | | - layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); |
115 | | - recyclerView.setLayoutManager(layoutManager); |
| 96 | + if (docPaths != null) filePaths.addAll(docPaths); |
116 | 97 |
|
117 | | - ImageAdapter imageAdapter = new ImageAdapter(getActivity(), filePaths); |
| 98 | + RecyclerView recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerview); |
| 99 | + if (recyclerView != null) { |
| 100 | + StaggeredGridLayoutManager layoutManager = |
| 101 | + new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL); |
| 102 | + layoutManager.setGapStrategy( |
| 103 | + StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); |
| 104 | + recyclerView.setLayoutManager(layoutManager); |
118 | 105 |
|
119 | | - recyclerView.setAdapter(imageAdapter); |
120 | | - recyclerView.setItemAnimator(new DefaultItemAnimator()); |
121 | | - } |
| 106 | + ImageAdapter imageAdapter = new ImageAdapter(getActivity(), filePaths); |
122 | 107 |
|
123 | | - Toast.makeText(getActivity(), "Num of files selected: "+ filePaths.size(), Toast.LENGTH_SHORT).show(); |
| 108 | + recyclerView.setAdapter(imageAdapter); |
| 109 | + recyclerView.setItemAnimator(new DefaultItemAnimator()); |
124 | 110 | } |
125 | 111 |
|
| 112 | + Toast.makeText(getActivity(), "Num of files selected: " + filePaths.size(), Toast.LENGTH_SHORT) |
| 113 | + .show(); |
| 114 | + } |
| 115 | + |
126 | 116 | public void onPickPhoto() { |
127 | 117 | int maxCount = MAX_ATTACHMENT_COUNT-docPaths.size(); |
128 | 118 | if((docPaths.size()+photoPaths.size())==MAX_ATTACHMENT_COUNT) |
@@ -145,6 +135,7 @@ public void onPickDoc() { |
145 | 135 | .pickFile(this); |
146 | 136 | } |
147 | 137 |
|
| 138 | + |
148 | 139 | @Override |
149 | 140 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
150 | 141 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
|