11package com .gowtham .library .ui ;
22
3+ import static com .gowtham .library .utils .VideoResKt .fromDisplayName ;
4+ import static com .gowtham .library .utils .VideoResKt .getVideoResNames ;
5+
36import android .Manifest ;
47import android .app .AlertDialog ;
58import android .app .Dialog ;
2023import android .view .View ;
2124import android .view .ViewGroup ;
2225import android .widget .ImageView ;
26+ import android .widget .PopupMenu ;
2327import android .widget .ProgressBar ;
2428import android .widget .TextView ;
2529import android .widget .Toast ;
2630
2731import androidx .annotation .NonNull ;
2832import androidx .annotation .Nullable ;
2933import androidx .appcompat .app .ActionBar ;
34+ import androidx .appcompat .view .ContextThemeWrapper ;
3035import androidx .appcompat .widget .Toolbar ;
3136import androidx .core .app .ActivityCompat ;
3237import androidx .core .content .ContextCompat ;
8287
8388import java .io .File ;
8489import java .util .Calendar ;
90+ import java .util .HashMap ;
91+ import java .util .List ;
8592import java .util .Locale ;
8693import java .util .Objects ;
8794import java .util .concurrent .Executors ;
126133 private TrimVideoOptions trimVideoOptions ;
127134
128135 private long currentDuration , lastClickedTime ;
136+
137+ private VideoRes selectedRes ;
129138 Runnable updateSeekbar = new Runnable () {
130139 @ Override
131140 public void run () {
@@ -151,6 +160,8 @@ public void run() {
151160 private CustomProgressView progressView ;
152161 private String fileName ;
153162
163+ private TextView resChangeSpinner , txtFileSize ;
164+ private boolean isCompressionEnabled ;
154165
155166 @ Override
156167 public void onCreate (Bundle savedInstanceState ) {
@@ -168,6 +179,7 @@ public void onCreate(Bundle savedInstanceState) {
168179 toolbar .setNavigationOnClickListener (v -> finish ());
169180 progressView = new CustomProgressView (this );
170181 View viewThumbnails = findViewById (R .id .view_thumbnails );
182+ View viewTimer = findViewById (R .id .view_timer );
171183
172184 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
173185 getWindow ().setNavigationBarContrastEnforced (false );
@@ -187,16 +199,12 @@ public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowIn
187199 toolbar .getPaddingBottom ());
188200
189201 ViewGroup .MarginLayoutParams params = (ViewGroup .MarginLayoutParams ) viewThumbnails .getLayoutParams ();
190- params .bottomMargin =btmInsets .bottom + ViewUtil .dpToPx (60 );
202+ params .bottomMargin =btmInsets .bottom + ViewUtil .dpToPx (80 );
191203 viewThumbnails .setLayoutParams (params );
192204
193- ViewGroup .MarginLayoutParams endDurationLayoutParams = (ViewGroup .MarginLayoutParams ) txtEndDuration .getLayoutParams ();
194- endDurationLayoutParams .bottomMargin =btmInsets .bottom + ViewUtil .dpToPx (40 );
195- txtEndDuration .setLayoutParams (endDurationLayoutParams );
196-
197- ViewGroup .MarginLayoutParams startDurationLayoutParams = (ViewGroup .MarginLayoutParams ) txtStartDuration .getLayoutParams ();
198- startDurationLayoutParams .bottomMargin =btmInsets .bottom + ViewUtil .dpToPx (40 );
199- txtStartDuration .setLayoutParams (startDurationLayoutParams );
205+ ViewGroup .MarginLayoutParams endDurationLayoutParams = (ViewGroup .MarginLayoutParams ) viewTimer .getLayoutParams ();
206+ endDurationLayoutParams .bottomMargin =btmInsets .bottom + ViewUtil .dpToPx (58 );
207+ viewTimer .setLayoutParams (endDurationLayoutParams );
200208 return insets ;
201209 }
202210 });
@@ -227,18 +235,33 @@ protected void onPostCreate(@Nullable Bundle savedInstanceState) {
227235 ImageView imageEight = findViewById (R .id .image_eight );
228236 ImageView imageNine = findViewById (R .id .image_nine );
229237 ImageView imageTen = findViewById (R .id .image_ten );
230-
238+ resChangeSpinner = findViewById (R .id .txt_change_res );
239+ txtFileSize = findViewById (R .id .txt_file_size );
231240
232241 View viewThumbnails = findViewById (R .id .view_thumbnails );
233242
234-
235243 ViewUtil .systemGestureExclusionRects (findViewById (R .id .root_view ), viewThumbnails );
236244 imageViews = new ImageView []{imageOne , imageTwo , imageThree ,
237245 imageFour , imageFive , imageSix , imageSeven , imageEight , imageNine , imageTen };
238246 seekHandler = new Handler ();
239247 initPlayer ();
248+
249+ fileUri = Uri .parse (bundle .getString (TrimVideo .TRIM_VIDEO_URI ));
250+ isCompressionEnabled = bundle .getBoolean (TrimVideo .IS_COMPRESSION_ENABLED , true );
251+ Pair <Integer , Integer > videoRes = TrimmerUtils .getVideoRes (this , fileUri );
252+ String selectVideoRes = savedInstanceState !=null ? savedInstanceState .getString ("selectedRes" ) : "" ;
240253 if (checkStoragePermission ())
241- setDataInView ();
254+ setDataInView (selectVideoRes );
255+ }
256+
257+ private double bitsToMbs (long bits ) {
258+ return bits / 8.0 / 1024.0 / 1024 ;
259+ }
260+
261+ @ Override
262+ protected void onSaveInstanceState (@ NonNull Bundle outState ) {
263+ super .onSaveInstanceState (outState );
264+ outState .putString ("selectedRes" , selectedRes .getDisplayName ());
242265 }
243266
244267 private void setUpToolBar (ActionBar actionBar , String title ) {
@@ -269,10 +292,11 @@ private void initPlayer() {
269292 }
270293 }
271294
272- private void setDataInView () {
295+ private void setDataInView (String selectVideoRes ) {
273296 try {
297+ setSelectedVideoRes (selectVideoRes );
298+
274299 Runnable fileUriRunnable = () -> {
275- fileUri = Uri .parse (bundle .getString (TrimVideo .TRIM_VIDEO_URI ));
276300 runOnUiThread (() -> {
277301 LogMessage .v ("VideoPath:: fileUri: " + fileUri );
278302 progressBar .setVisibility (View .GONE );
@@ -285,12 +309,55 @@ private void setDataInView() {
285309 buildMediaSource ();
286310 loadThumbnails ();
287311 setUpSeekBar ();
312+ setUpResChanger ();
288313 });
289314 };
290315 Executors .newSingleThreadExecutor ().execute (fileUriRunnable );
291316 } catch (Exception e ) {
292- e .printStackTrace ();
317+ Log .e ("VideoTrimmer" , "" , e );
318+ }
319+ }
320+
321+ private void setSelectedVideoRes (String selectVideoRes ) {
322+ try {
323+ Pair <Integer , Integer > wh = TrimmerUtils .getVideoRes (this , fileUri );
324+ selectedRes = selectVideoRes !=null && !selectVideoRes .isEmpty () ?
325+ fromDisplayName (selectVideoRes ) : TrimmerUtils .classifyResolution (wh .first , wh .second );
326+ resChangeSpinner .setText (selectedRes .getDisplayName ());
327+ } catch (Exception e ) {
328+ Log .e ("VideoTrimmer" , "" , e );
329+ }
330+ }
331+
332+ private void setUpResChanger () {
333+ resChangeSpinner .setOnClickListener (new View .OnClickListener () {
334+ @ Override
335+ public void onClick (View view ) {
336+ showResolutionMenu (resChangeSpinner );
337+ }
338+ });
339+ }
340+
341+ private void showResolutionMenu (TextView anchorView ) {
342+
343+ Context wrapperContext = new ContextThemeWrapper (ActVideoTrimmer .this , R .style .AppTheme_PopupMenu );
344+ PopupMenu popupMenu = new PopupMenu (wrapperContext , anchorView );
345+ Log .e ("TAG" , "showResolutionMenu: " +popupMenu .getGravity ());
346+
347+
348+ List <String > resolutions = getVideoResNames (this , fileUri );
349+ for (String resolution : resolutions ) {
350+ popupMenu .getMenu ().add (resolution );
293351 }
352+
353+ popupMenu .setOnMenuItemClickListener (menuItem -> {
354+ Log .e ("TAG" , "showResolutionMenu: " +menuItem .getTitle ().toString ());
355+ selectedRes = fromDisplayName (menuItem .getTitle ().toString ());
356+
357+ anchorView .setText (selectedRes .getDisplayName ());
358+ return true ;
359+ });
360+ popupMenu .show ();
294361 }
295362
296363 private void initTrimData () {
@@ -506,7 +573,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
506573 super .onRequestPermissionsResult (requestCode , permissions , grantResults );
507574 if (requestCode == PER_REQ_CODE ) {
508575 if (isPermissionOk (grantResults ))
509- setDataInView ();
576+ setDataInView ("" );
510577 else {
511578 Toast .makeText (this , "Storage permission denied" , Toast .LENGTH_SHORT ).show ();
512579 finish ();
@@ -573,11 +640,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
573640 LogMessage .v ("sourcePath::" + fileUri );
574641 videoPlayer .setPlayWhenReady (false );
575642 showProcessingDialog ();
576- String [] complexCommand ;
577-
578- if (compressOption != null ){
579-
580- }
581643
582644 androidx .media3 .common .MediaItem mediaItem =
583645 new androidx .media3 .common .MediaItem .Builder ()
@@ -591,27 +653,30 @@ public boolean onOptionsItemSelected(MenuItem item) {
591653
592654 Pair <Integer , Integer > videoRes = TrimmerUtils .getVideoRes (this , fileUri );
593655
594- Log .e ("TAG" , "trimVideo: width: " +videoRes .first );
595- Log .e ("TAG" , "trimVideo: height: " +videoRes .second );
596-
597-
598- String videoRes1 = VideoRes .FULL_HD ;
599- Log .e ("TAG" , "trimVideo: bitarge:: " +TrimmerUtils .getBitRate (this , fileUri ));
600-
656+ int width = videoRes .first ;
657+ int height = videoRes .second ;
601658 EditedMediaItem editedMediaItem = new EditedMediaItem .Builder (mediaItem )
602659 .setEffects (new Effects (ImmutableList .of (),
603- ImmutableList .of (Presentation .createForWidthAndHeight (videoRes . first , videoRes . second ,
660+ ImmutableList .of (Presentation .createForWidthAndHeight (width , height ,
604661 Presentation .LAYOUT_SCALE_TO_FIT ))))
605662 .build ();
606663
664+
665+ long bitRate = TrimmerUtils .getBitRate (this , fileUri );
666+
667+ if (isCompressionEnabled ){
668+ HashMap <VideoRes , Long > videoResMap = TrimmerUtils .getResBitRate (this , fileUri );
669+ Long compressionBitRate = videoResMap .get (selectedRes );
670+ bitRate = compressionBitRate !=null ? compressionBitRate : bitRate ;
671+ }
607672 transformer =
608673 new Transformer .Builder (this )
609674 .addListener (transformerListener )
610675 .setEncoderFactory (
611676 new DefaultEncoderFactory .Builder (this )
612677 .setRequestedVideoEncoderSettings (
613678 new VideoEncoderSettings .Builder ()
614- .setBitrate (2000 )
679+ .setBitrate (Math . toIntExact ( bitRate ) )
615680 .build ()
616681 ).build ())
617682 .build ();
@@ -718,10 +783,10 @@ private void showProcessingDialog() {
718783 }
719784
720785 private boolean checkStoragePermission () {
721- Uri uri = Uri .parse (bundle .getString (TrimVideo .TRIM_VIDEO_URI ));
722- String fileUri = FileUtilKt .getActualFileUri (this , uri );
786+ String uri = FileUtilKt .getActualFileUri (this , fileUri );
723787
724- if (fileUri !=null && new File (fileUri ).canRead ()){
788+ if (uri !=null && new File (uri ).canRead ()){
789+ Log .e ("VideoTrimmer" , "checkStoragePermission: has no permission" );
725790 // might have used photo picker or file picker. therefore have read access without permission.
726791 return true ;
727792 }
0 commit comments