|
27 | 27 |
|
28 | 28 | public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks { |
29 | 29 |
|
30 | | - public static final int RC_PHOTO_PICKER_PERM = 123; |
31 | | - public static final int RC_FILE_PICKER_PERM = 321; |
32 | | - private int MAX_ATTACHMENT_COUNT = 10; |
33 | | - private ArrayList<String> photoPaths = new ArrayList<>(); |
34 | | - private ArrayList<String> docPaths = new ArrayList<>(); |
35 | | - |
36 | | - @Override |
37 | | - protected void onCreate(Bundle savedInstanceState) { |
38 | | - super.onCreate(savedInstanceState); |
39 | | - setContentView(R.layout.activity_main); |
40 | | - ButterKnife.bind(this); |
| 30 | + public static final int RC_PHOTO_PICKER_PERM = 123; |
| 31 | + public static final int RC_FILE_PICKER_PERM = 321; |
| 32 | + private int MAX_ATTACHMENT_COUNT = 10; |
| 33 | + private ArrayList<String> photoPaths = new ArrayList<>(); |
| 34 | + private ArrayList<String> docPaths = new ArrayList<>(); |
| 35 | + |
| 36 | + @Override protected void onCreate(Bundle savedInstanceState) { |
| 37 | + super.onCreate(savedInstanceState); |
| 38 | + setContentView(R.layout.activity_main); |
| 39 | + ButterKnife.bind(this); |
| 40 | + } |
| 41 | + |
| 42 | + @AfterPermissionGranted(RC_PHOTO_PICKER_PERM) @OnClick(R.id.pick_photo) |
| 43 | + public void pickPhotoClicked() { |
| 44 | + if (EasyPermissions.hasPermissions(this, FilePickerConst.PERMISSIONS_FILE_PICKER)) { |
| 45 | + onPickPhoto(); |
| 46 | + } else { |
| 47 | + // Ask for one permission |
| 48 | + EasyPermissions.requestPermissions(this, getString(R.string.rationale_photo_picker), |
| 49 | + RC_PHOTO_PICKER_PERM, FilePickerConst.PERMISSIONS_FILE_PICKER); |
41 | 50 | } |
42 | | - |
43 | | - @AfterPermissionGranted(RC_PHOTO_PICKER_PERM) |
44 | | - @OnClick(R.id.pick_photo) |
45 | | - public void pickPhotoClicked() { |
46 | | - if (EasyPermissions.hasPermissions(this,FilePickerConst.PERMISSIONS_FILE_PICKER)) { |
47 | | - onPickPhoto(); |
48 | | - } else { |
49 | | - // Ask for one permission |
50 | | - EasyPermissions.requestPermissions( |
51 | | - this, |
52 | | - getString(R.string.rationale_photo_picker), |
53 | | - RC_PHOTO_PICKER_PERM, |
54 | | - FilePickerConst.PERMISSIONS_FILE_PICKER); |
55 | | - } |
| 51 | + } |
| 52 | + |
| 53 | + @AfterPermissionGranted(RC_FILE_PICKER_PERM) @OnClick(R.id.pick_doc) |
| 54 | + public void pickDocClicked() { |
| 55 | + if (EasyPermissions.hasPermissions(this, FilePickerConst.PERMISSIONS_FILE_PICKER)) { |
| 56 | + onPickDoc(); |
| 57 | + } else { |
| 58 | + // Ask for one permission |
| 59 | + EasyPermissions.requestPermissions(this, getString(R.string.rationale_doc_picker), |
| 60 | + RC_FILE_PICKER_PERM, FilePickerConst.PERMISSIONS_FILE_PICKER); |
56 | 61 | } |
57 | | - |
58 | | - @AfterPermissionGranted(RC_FILE_PICKER_PERM) |
59 | | - @OnClick(R.id.pick_doc) |
60 | | - public void pickDocClicked() { |
61 | | - if (EasyPermissions.hasPermissions(this,FilePickerConst.PERMISSIONS_FILE_PICKER)) { |
62 | | - onPickDoc(); |
63 | | - } else { |
64 | | - // Ask for one permission |
65 | | - EasyPermissions.requestPermissions( |
66 | | - this, |
67 | | - getString(R.string.rationale_doc_picker), |
68 | | - RC_FILE_PICKER_PERM, |
69 | | - FilePickerConst.PERMISSIONS_FILE_PICKER); |
| 62 | + } |
| 63 | + |
| 64 | + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 65 | + super.onActivityResult(requestCode, resultCode, data); |
| 66 | + switch (requestCode) { |
| 67 | + case FilePickerConst.REQUEST_CODE_PHOTO: |
| 68 | + if (resultCode == Activity.RESULT_OK && data != null) { |
| 69 | + photoPaths = new ArrayList<>(); |
| 70 | + photoPaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_MEDIA)); |
70 | 71 | } |
71 | | - } |
| 72 | + break; |
72 | 73 |
|
73 | | - @Override |
74 | | - protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
75 | | - super.onActivityResult(requestCode, resultCode, data); |
76 | | - switch (requestCode) |
77 | | - { |
78 | | - case FilePickerConst.REQUEST_CODE_PHOTO: |
79 | | - if(resultCode== Activity.RESULT_OK && data!=null) |
80 | | - { |
81 | | - photoPaths = new ArrayList<>(); |
82 | | - photoPaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_MEDIA)); |
83 | | - |
84 | | - } |
85 | | - break; |
86 | | - |
87 | | - case FilePickerConst.REQUEST_CODE_DOC: |
88 | | - if(resultCode== Activity.RESULT_OK && data!=null) |
89 | | - { |
90 | | - docPaths = new ArrayList<>(); |
91 | | - docPaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS)); |
92 | | - } |
93 | | - break; |
| 74 | + case FilePickerConst.REQUEST_CODE_DOC: |
| 75 | + if (resultCode == Activity.RESULT_OK && data != null) { |
| 76 | + docPaths = new ArrayList<>(); |
| 77 | + docPaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS)); |
94 | 78 | } |
95 | | - |
96 | | - addThemToView(photoPaths,docPaths); |
| 79 | + break; |
97 | 80 | } |
98 | 81 |
|
99 | | - private void addThemToView(ArrayList<String> imagePaths, ArrayList<String> docPaths) { |
100 | | - ArrayList<String> filePaths = new ArrayList<>(); |
101 | | - if(imagePaths!=null) |
102 | | - filePaths.addAll(imagePaths); |
| 82 | + addThemToView(photoPaths, docPaths); |
| 83 | + } |
103 | 84 |
|
104 | | - if(docPaths!=null) |
105 | | - filePaths.addAll(docPaths); |
| 85 | + private void addThemToView(ArrayList<String> imagePaths, ArrayList<String> docPaths) { |
| 86 | + ArrayList<String> filePaths = new ArrayList<>(); |
| 87 | + if (imagePaths != null) filePaths.addAll(imagePaths); |
106 | 88 |
|
107 | | - RecyclerView recyclerView = findViewById(R.id.recyclerview); |
108 | | - if(recyclerView!=null) { |
109 | | - StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL); |
110 | | - layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); |
111 | | - recyclerView.setLayoutManager(layoutManager); |
| 89 | + if (docPaths != null) filePaths.addAll(docPaths); |
112 | 90 |
|
113 | | - ImageAdapter imageAdapter = new ImageAdapter(this, filePaths); |
| 91 | + RecyclerView recyclerView = findViewById(R.id.recyclerview); |
| 92 | + if (recyclerView != null) { |
| 93 | + StaggeredGridLayoutManager layoutManager = |
| 94 | + new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL); |
| 95 | + layoutManager.setGapStrategy( |
| 96 | + StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); |
| 97 | + recyclerView.setLayoutManager(layoutManager); |
114 | 98 |
|
115 | | - recyclerView.setAdapter(imageAdapter); |
116 | | - recyclerView.setItemAnimator(new DefaultItemAnimator()); |
117 | | - } |
| 99 | + ImageAdapter imageAdapter = new ImageAdapter(this, filePaths); |
118 | 100 |
|
119 | | - Toast.makeText(this, "Num of files selected: "+ filePaths.size(), Toast.LENGTH_SHORT).show(); |
| 101 | + recyclerView.setAdapter(imageAdapter); |
| 102 | + recyclerView.setItemAnimator(new DefaultItemAnimator()); |
120 | 103 | } |
121 | 104 |
|
122 | | - public void onPickPhoto() { |
123 | | - int maxCount = MAX_ATTACHMENT_COUNT-docPaths.size(); |
124 | | - if((docPaths.size()+photoPaths.size())==MAX_ATTACHMENT_COUNT) |
125 | | - Toast.makeText(this, "Cannot select more than " + MAX_ATTACHMENT_COUNT + " items", Toast.LENGTH_SHORT).show(); |
126 | | - else |
127 | | - FilePickerBuilder.getInstance() |
128 | | - .setMaxCount(maxCount) |
129 | | - .setSelectedFiles(photoPaths) |
130 | | - .setActivityTheme(R.style.FilePickerTheme) |
131 | | - .setActivityTitle("Please select media") |
132 | | - .enableVideoPicker(true) |
133 | | - .enableCameraSupport(true) |
134 | | - .showGifs(false) |
135 | | - .showFolderView(true) |
136 | | - .enableSelectAll(true) |
137 | | - .enableImagePicker(true) |
138 | | - .setCameraPlaceholder(R.drawable.custom_camera) |
139 | | - .withOrientation(Orientation.UNSPECIFIED) |
140 | | - .pickPhoto(this); |
141 | | - |
| 105 | + Toast.makeText(this, "Num of files selected: " + filePaths.size(), Toast.LENGTH_SHORT).show(); |
| 106 | + } |
| 107 | + |
| 108 | + public void onPickPhoto() { |
| 109 | + int maxCount = MAX_ATTACHMENT_COUNT - docPaths.size(); |
| 110 | + if ((docPaths.size() + photoPaths.size()) == MAX_ATTACHMENT_COUNT) { |
| 111 | + Toast.makeText(this, "Cannot select more than " + MAX_ATTACHMENT_COUNT + " items", |
| 112 | + Toast.LENGTH_SHORT).show(); |
| 113 | + } else { |
| 114 | + FilePickerBuilder.getInstance() |
| 115 | + .setMaxCount(maxCount) |
| 116 | + .setSelectedFiles(photoPaths) |
| 117 | + .setActivityTheme(R.style.FilePickerTheme) |
| 118 | + .setActivityTitle("Please select media") |
| 119 | + .enableVideoPicker(true) |
| 120 | + .enableCameraSupport(true) |
| 121 | + .showGifs(false) |
| 122 | + .showFolderView(false) |
| 123 | + .enableSelectAll(true) |
| 124 | + .enableImagePicker(true) |
| 125 | + .setCameraPlaceholder(R.drawable.custom_camera) |
| 126 | + .withOrientation(Orientation.UNSPECIFIED) |
| 127 | + .pickPhoto(this); |
142 | 128 | } |
143 | | - |
144 | | - public void onPickDoc() { |
145 | | - String[] zips = {".zip",".rar"}; |
146 | | - String[] pdfs = {".pdf"}; |
147 | | - int maxCount = MAX_ATTACHMENT_COUNT-photoPaths.size(); |
148 | | - if((docPaths.size()+photoPaths.size())==MAX_ATTACHMENT_COUNT) |
149 | | - Toast.makeText(this, "Cannot select more than " + MAX_ATTACHMENT_COUNT + " items", Toast.LENGTH_SHORT).show(); |
150 | | - else |
151 | | - FilePickerBuilder.getInstance() |
152 | | - .setMaxCount(maxCount) |
153 | | - .setSelectedFiles(docPaths) |
154 | | - .setActivityTheme(R.style.FilePickerTheme) |
155 | | - .setActivityTitle("Please select doc") |
156 | | - .addFileSupport("ZIP",zips) |
157 | | - .addFileSupport("PDF",pdfs,R.drawable.pdf_blue) |
158 | | - .enableDocSupport(false) |
159 | | - .sortDocumentsBy(SortingTypes.name) |
160 | | - .withOrientation(Orientation.UNSPECIFIED) |
161 | | - .pickFile(this); |
| 129 | + } |
| 130 | + |
| 131 | + public void onPickDoc() { |
| 132 | + String[] zips = { ".zip", ".rar" }; |
| 133 | + String[] pdfs = { ".pdf" }; |
| 134 | + int maxCount = MAX_ATTACHMENT_COUNT - photoPaths.size(); |
| 135 | + if ((docPaths.size() + photoPaths.size()) == MAX_ATTACHMENT_COUNT) { |
| 136 | + Toast.makeText(this, "Cannot select more than " + MAX_ATTACHMENT_COUNT + " items", |
| 137 | + Toast.LENGTH_SHORT).show(); |
| 138 | + } else { |
| 139 | + FilePickerBuilder.getInstance() |
| 140 | + .setMaxCount(maxCount) |
| 141 | + .setSelectedFiles(docPaths) |
| 142 | + .setActivityTheme(R.style.FilePickerTheme) |
| 143 | + .setActivityTitle("Please select doc") |
| 144 | + .addFileSupport("ZIP", zips) |
| 145 | + .addFileSupport("PDF", pdfs, R.drawable.pdf_blue) |
| 146 | + .enableDocSupport(false) |
| 147 | + .sortDocumentsBy(SortingTypes.name) |
| 148 | + .withOrientation(Orientation.UNSPECIFIED) |
| 149 | + .pickFile(this); |
162 | 150 | } |
| 151 | + } |
163 | 152 |
|
164 | | - @Override |
165 | | - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
166 | | - super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 153 | + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
| 154 | + @NonNull int[] grantResults) { |
| 155 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
167 | 156 |
|
168 | | - EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); |
169 | | - } |
| 157 | + EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); |
| 158 | + } |
170 | 159 |
|
171 | | - public void onOpenFragmentClicked(View view) { |
172 | | - Intent intent = new Intent(this, FragmentActivity.class); |
173 | | - startActivity(intent); |
174 | | - } |
| 160 | + public void onOpenFragmentClicked(View view) { |
| 161 | + Intent intent = new Intent(this, FragmentActivity.class); |
| 162 | + startActivity(intent); |
| 163 | + } |
175 | 164 |
|
176 | | - @Override public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) { |
177 | | - if(requestCode == RC_PHOTO_PICKER_PERM){ |
178 | | - onPickPhoto(); |
179 | | - } |
180 | | - else if(requestCode == RC_FILE_PICKER_PERM){ |
181 | | - onPickDoc(); |
182 | | - } |
183 | | - } |
| 165 | + @Override public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) { |
| 166 | + } |
184 | 167 |
|
185 | | - @Override public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) { |
| 168 | + @Override public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) { |
186 | 169 |
|
187 | | - if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { |
188 | | - new AppSettingsDialog.Builder(this).build().show(); |
189 | | - } |
| 170 | + if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { |
| 171 | + new AppSettingsDialog.Builder(this).build().show(); |
190 | 172 | } |
| 173 | + } |
191 | 174 | } |