|
25 | 25 | import android.net.Uri; |
26 | 26 | import android.os.Build; |
27 | 27 | import android.os.Bundle; |
28 | | -import androidx.annotation.NonNull; |
29 | | -import androidx.core.content.ContextCompat; |
30 | | -import androidx.appcompat.app.AppCompatActivity; |
31 | | -import androidx.recyclerview.widget.GridLayoutManager; |
32 | | -import androidx.recyclerview.widget.RecyclerView; |
33 | | -import androidx.appcompat.widget.Toolbar; |
34 | 28 | import android.view.Menu; |
35 | 29 | import android.view.MenuItem; |
36 | 30 | import android.widget.Toast; |
37 | 31 |
|
38 | 32 | import com.jaiselrahman.filepicker.R; |
39 | 33 | import com.jaiselrahman.filepicker.adapter.FileGalleryAdapter; |
40 | | -import com.jaiselrahman.filepicker.adapter.MultiSelectionAdapter; |
| 34 | +import com.jaiselrahman.filepicker.adapter.FileGalleryAdapter.OnCameraClickListener; |
| 35 | +import com.jaiselrahman.filepicker.adapter.MultiSelectionAdapter.OnSelectionListener; |
41 | 36 | import com.jaiselrahman.filepicker.config.Configurations; |
42 | 37 | import com.jaiselrahman.filepicker.loader.FileLoader; |
43 | 38 | import com.jaiselrahman.filepicker.loader.FileResultCallback; |
|
47 | 42 | import java.io.File; |
48 | 43 | import java.util.ArrayList; |
49 | 44 |
|
| 45 | +import androidx.annotation.NonNull; |
| 46 | +import androidx.appcompat.app.AppCompatActivity; |
| 47 | +import androidx.appcompat.widget.Toolbar; |
| 48 | +import androidx.core.content.ContextCompat; |
| 49 | +import androidx.recyclerview.widget.GridLayoutManager; |
| 50 | +import androidx.recyclerview.widget.RecyclerView; |
| 51 | + |
50 | 52 | public class FilePickerActivity extends AppCompatActivity |
51 | | - implements MultiSelectionAdapter.OnSelectionListener<FileGalleryAdapter.ViewHolder> { |
| 53 | + implements OnSelectionListener<FileGalleryAdapter.ViewHolder>, OnCameraClickListener { |
52 | 54 | public static final String MEDIA_FILES = "MEDIA_FILES"; |
53 | 55 | public static final String SELECTED_MEDIA_FILES = "SELECTED_MEDIA_FILES"; |
54 | 56 | public static final String CONFIGS = "CONFIGS"; |
55 | 57 | public static final String TAG = "FilePicker"; |
56 | 58 | private static final String PATH = "PATH"; |
57 | 59 | private static final String URI = "URI"; |
58 | | - private static final int REQUEST_PERMISSION = 1; |
59 | | - public final String[] permissions = new String[]{ |
60 | | - Manifest.permission.READ_EXTERNAL_STORAGE, |
61 | | - Manifest.permission.WRITE_EXTERNAL_STORAGE, |
62 | | - Manifest.permission.CAMERA |
63 | | - }; |
| 60 | + private static final int REQUEST_WRITE_PERMISSION = 1; |
| 61 | + private static final int REQUEST_CAMERA_PERMISSION_FOR_CAMERA = 2; |
| 62 | + private static final int REQUEST_CAMERA_PERMISSION_FOR_VIDEO = 3; |
64 | 63 | private Configurations configs; |
65 | 64 | private ArrayList<MediaFile> mediaFiles = new ArrayList<>(); |
66 | 65 | private FileGalleryAdapter fileGalleryAdapter; |
@@ -104,27 +103,16 @@ protected void onCreate(Bundle savedInstanceState) { |
104 | 103 | fileGalleryAdapter.setSingleChoiceMode(isSingleChoice); |
105 | 104 | fileGalleryAdapter.setMaxSelection(isSingleChoice ? 1 : configs.getMaxSelection()); |
106 | 105 | fileGalleryAdapter.setSelectedItems(configs.getSelectedMediaFiles()); |
| 106 | + fileGalleryAdapter.setOnCameraClickListener(this); |
107 | 107 | RecyclerView recyclerView = findViewById(R.id.file_gallery); |
108 | 108 | recyclerView.setLayoutManager(new GridLayoutManager(this, spanCount)); |
109 | 109 | recyclerView.setAdapter(fileGalleryAdapter); |
110 | 110 | recyclerView.addItemDecoration(new DividerItemDecoration(this)); |
111 | 111 | recyclerView.setItemAnimator(null); |
112 | 112 |
|
113 | 113 | if (savedInstanceState == null) { |
114 | | - boolean success = false; |
115 | | - for (String permission : permissions) { |
116 | | - success = ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED; |
117 | | - if (!success) break; |
118 | | - } |
119 | | - if (success) |
| 114 | + if (requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE, REQUEST_WRITE_PERMISSION)) { |
120 | 115 | loadFiles(false); |
121 | | - else if (configs.isCheckPermission()) { |
122 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
123 | | - requestPermissions(permissions, REQUEST_PERMISSION); |
124 | | - } |
125 | | - } else { |
126 | | - Toast.makeText(this, R.string.permission_not_given, Toast.LENGTH_SHORT).show(); |
127 | | - finish(); |
128 | 116 | } |
129 | 117 | } else { |
130 | 118 | loadFiles(false); |
@@ -152,10 +140,18 @@ public void onResult(ArrayList<MediaFile> filesResults) { |
152 | 140 | @Override |
153 | 141 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
154 | 142 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
155 | | - if (requestCode == REQUEST_PERMISSION) { |
156 | | - if (grantResults[0] == PackageManager.PERMISSION_GRANTED |
157 | | - && grantResults[1] == PackageManager.PERMISSION_GRANTED) { |
| 143 | + if (requestCode == REQUEST_WRITE_PERMISSION) { |
| 144 | + if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
158 | 145 | loadFiles(false); |
| 146 | + } else { |
| 147 | + Toast.makeText(this, R.string.permission_not_given, Toast.LENGTH_SHORT).show(); |
| 148 | + finish(); |
| 149 | + } |
| 150 | + } else if (requestCode == REQUEST_CAMERA_PERMISSION_FOR_CAMERA || requestCode == REQUEST_CAMERA_PERMISSION_FOR_VIDEO) { |
| 151 | + if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 152 | + fileGalleryAdapter.openCamera(requestCode == REQUEST_CAMERA_PERMISSION_FOR_VIDEO); |
| 153 | + } else { |
| 154 | + Toast.makeText(this, R.string.permission_not_given, Toast.LENGTH_SHORT).show(); |
159 | 155 | } |
160 | 156 | } |
161 | 157 | } |
@@ -233,6 +229,29 @@ protected void onRestoreInstanceState(Bundle savedInstanceState) { |
233 | 229 | } |
234 | 230 | } |
235 | 231 |
|
| 232 | + @Override |
| 233 | + public boolean onCameraClick(boolean forVideo) { |
| 234 | + return requestPermission( |
| 235 | + Manifest.permission.CAMERA, |
| 236 | + forVideo ? REQUEST_CAMERA_PERMISSION_FOR_VIDEO : REQUEST_CAMERA_PERMISSION_FOR_CAMERA |
| 237 | + ); |
| 238 | + } |
| 239 | + |
| 240 | + public boolean requestPermission(String permission, int requestCode) { |
| 241 | + if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { |
| 242 | + if (configs.isCheckPermission()) { |
| 243 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 244 | + requestPermissions(new String[]{permission}, requestCode); |
| 245 | + } |
| 246 | + } else { |
| 247 | + Toast.makeText(this, R.string.permission_not_given, Toast.LENGTH_SHORT).show(); |
| 248 | + finish(); |
| 249 | + } |
| 250 | + return false; |
| 251 | + } |
| 252 | + return true; |
| 253 | + } |
| 254 | + |
236 | 255 | @Override |
237 | 256 | public void onSelectionBegin() { |
238 | 257 |
|
|
0 commit comments