visageSDK
MediaFragment.java
1 package com.visagetechnologies.showcasedemo;
2 
3 import android.Manifest;
4 import android.app.Activity;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.res.Configuration;
8 import android.database.Cursor;
9 import android.net.Uri;
10 import android.os.Bundle;
11 import android.provider.MediaStore;
12 
13 import androidx.activity.result.ActivityResultLauncher;
14 import androidx.activity.result.contract.ActivityResultContracts;
15 import androidx.core.app.ActivityCompat;
16 import androidx.fragment.app.Fragment;
17 import androidx.fragment.app.FragmentActivity;
18 
19 import android.util.Log;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 
24 
25 import java.io.IOException;
26 
27 public class MediaFragment extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback {
28 
29  private static MediaFragment instance = null;
30 
31  private static final String TAG = "MediaFragment";
32 
33  public static int RESULT_LOAD_MEDIA = 1;
34 
35  private static final int REQUEST_READ_PERMISSIONS = 1;
36  private static final String FRAGMENT_DIALOG = "dialog";
37 
38  public static final String[] READ_PERMISSIONS = {
39  Manifest.permission.READ_EXTERNAL_STORAGE,
40  Manifest.permission.READ_MEDIA_IMAGES,
41  Manifest.permission.READ_MEDIA_VIDEO,
42  };
43 
44  private String mediaPath = null;
45 
46  private VisageWrapper visageWrapper;
47  private MediaLoader.VideoLoaderThread videoLoaderThread = null;
48 
49  private Uri selectedMediaUri;
50 
51  public static MediaFragment newInstance() {
52  return new MediaFragment();
53  }
54 
55  public static MediaFragment get(Context context) {
56  instance = MediaFragment.newInstance();
57 
58  return instance;
59  }
60 
61  private FragmentActivity activity;
62 
63  @Override
64  public void onAttach(Context context) {
65  super.onAttach(context);
66  activity = (FragmentActivity) context;
67  }
68 
69  @Override
70  public void onCreate(Bundle savedInstanceState) {
71  super.onCreate(savedInstanceState);
72  }
73 
74  @Override
75  public View onCreateView(LayoutInflater inflater, ViewGroup container,
76  Bundle savedInstanceState) {
77  mediaPath = null;
78  return inflater.inflate(R.layout.fragment, container, false);
79  }
80 
81  // This event is triggered soon after onCreateView().
82  // Any view setup should occur here. E.g., view lookups and attaching view listeners.
83  @Override
84  public void onViewCreated(View view, Bundle savedInstanceState) {
85  // Setup any handles to view objects here
86  // EditText etFoo = (EditText) view.findViewById(R.id.etFoo);
87  Context context = getActivity();
88  assert(context != null);
89  visageWrapper = VisageWrapper.get(context);
90  visageWrapper.switchToMedia();
91  visageWrapper.onCreate();
92  }
93 
94  @Override
95  public void onResume() {
96  super.onResume();
97  visageWrapper.onResume();
98 
99  if (mediaPath != null) {
100  try {
101  handleMedia();
102  } catch (IOException e) {
103  e.printStackTrace();
104  }
105 
106  } else {
107  MainActivity a = (MainActivity) activity;
108 
109  if (a.mediaFragmentPermissionDenied)
110  return;
111 
112  if (!a.mediaFragmentPermissionGranted) {
113  a.mediaFragmentPermissionLouncher.launch(READ_PERMISSIONS);
114  return;
115  }
116 
117  Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
118  photoPickerIntent.setType("*/*");
119  photoPickerIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"});
120 
121  galleryActivity.launch(photoPickerIntent);
122 
123  }
124  }
125 
126  @Override
127  public void onPause() {
128  super.onPause();
129  visageWrapper.onPause();
130  if (videoLoaderThread != null) {
131  try {
132  videoLoaderThread.stop();
133  videoLoaderThread = null;
134  } catch (InterruptedException e) {
135  e.printStackTrace();
136  }
137  }
138  }
139 
140  @Override
141  public void onConfigurationChanged(Configuration newConfiguration) {
142  super.onConfigurationChanged(newConfiguration);
143  visageWrapper.onPause();
144 
145  try {
146  handleMedia();
147  } catch (IOException e) {
148  e.printStackTrace();
149  }
150  }
151 
152  public ActivityResultLauncher<Intent> galleryActivity = registerForActivityResult(
153  new ActivityResultContracts.StartActivityForResult(),
154  result -> {
155  if (result.getResultCode() == Activity.RESULT_OK) {
156  // PERMISSION GRANTED
157  Intent data = result.getData();
158  selectedMediaUri = data.getData();
159  String[] filePathColumn = {MediaStore.Images.Media.DATA};
160 
161  Cursor cursor = getActivity().getContentResolver().query(selectedMediaUri,
162  filePathColumn, null, null, null);
163  assert cursor != null;
164  cursor.moveToFirst();
165 
166  int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
167  mediaPath = cursor.getString(columnIndex);
168  cursor.close();
169  } else {
170  // PERMISSION NOT GRANTED
171  Log.e(TAG, "Failed to get permission to open gallery");
172  }
173  }
174  );;
175 
176  public void handleMedia() throws IOException {
177  if (selectedMediaUri.toString().contains("image")) {
178  if (videoLoaderThread != null) {
179  try {
180  videoLoaderThread.stop();
181  videoLoaderThread = null;
182  } catch (InterruptedException e) {
183  e.printStackTrace();
184  }
185  }
186  MediaLoader.ImageLoader.LoadPictureSendToVW(getActivity().getContentResolver(), selectedMediaUri);
187 
188 
189  } else if (selectedMediaUri.toString().contains("video")) {
190 
191  if (videoLoaderThread == null) {
192  videoLoaderThread = new MediaLoader.VideoLoaderThread(selectedMediaUri, getActivity());
193  videoLoaderThread.start();
194  } else {
195  visageWrapper.ResumeTracker();
196  }
197  }
198  }
199 }
200