Skip to content

Commit

Permalink
002.04.01-16API: improved memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
warren-bank committed Jun 29, 2022
1 parent 03d71f7 commit 9971765
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
Expand Down Expand Up @@ -52,14 +53,23 @@ protected void onResume() {
protected void onPause() {
super.onPause();

stop();
pause();
}

@Override
protected void onDestroy() {
super.onDestroy();
protected void onStop() {
super.onStop();

view.setPlayer(null);
stop();
release();

exoPlayer = null;
view = null;

setContentView(new View(this));
System.gc();
finish();
}

private void play() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import java.util.ArrayList;
Expand Down Expand Up @@ -43,17 +44,35 @@ public void onCreate(Bundle savedInstanceState) {
}

@Override
public void onResume() {
super.onResume();
public void onPause() {
super.onPause();

recyclerView.setAdapter(recyclerViewAdapter);
// deallocate all resources that consume memory
if (recyclerView != null) {
recyclerView.setAdapter(null);
recyclerViewAdapter.release();
recyclerViewAdapter = null;
recyclerView = null;
videos = null;
setContentView(new View(this));
System.gc();
}
}

@Override
public void onPause() {
super.onPause();
public void onResume() {
super.onResume();

recyclerView.setAdapter(null);
if (recyclerView == null) {
// give the garbage collector a little time to recover unused memory
new Handler().postDelayed(
new Runnable() {
public void run() {
// display a fresh instance of this Activity
recreate();
}
}, 1000);
}
}

public static void open(Context context, String jsonVideos, int columns) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import java.util.ArrayList;

Expand Down Expand Up @@ -39,17 +41,35 @@ public void onCreate(Bundle savedInstanceState) {
}

@Override
public void onResume() {
super.onResume();
public void onPause() {
super.onPause();

recyclerView.setAdapter(recyclerViewAdapter);
// deallocate all resources that consume memory
if (recyclerView != null) {
recyclerView.setAdapter(null);
recyclerViewAdapter.release();
recyclerViewAdapter = null;
recyclerView = null;
videos = null;
setContentView(new View(this));
System.gc();
}
}

@Override
public void onPause() {
super.onPause();
public void onResume() {
super.onResume();

recyclerView.setAdapter(null);
if (recyclerView == null) {
// give the garbage collector a little time to recover unused memory
new Handler().postDelayed(
new Runnable() {
public void run() {
// display a fresh instance of this Activity
recreate();
}
}, 1000);
}
}

public static void open(Context context, String jsonVideos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public final class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
public Context context;
public ArrayList<VideoType> videos;
private int minHeight;
private ArrayList<RecyclerViewHolder> holders;

public RecyclerViewAdapter(Context context, ArrayList<VideoType> videos, int minHeight) {
super();

this.context = context;
this.videos = videos;
this.minHeight = minHeight;
this.holders = new ArrayList<RecyclerViewHolder>();

setHasStableIds(true);
}
Expand All @@ -31,7 +33,9 @@ public RecyclerViewAdapter(Context context, ArrayList<VideoType> videos, int min
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = (View) LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view_recycler_view_holder, parent, false);

return new RecyclerViewHolder(view, minHeight);
RecyclerViewHolder holder = new RecyclerViewHolder(view, minHeight);
holders.add(holder);
return holder;
}

@Override
Expand All @@ -43,9 +47,20 @@ public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.bind(video);
}

@Override
public void onViewDetachedFromWindow(RecyclerViewHolder holder) {
holder.stop();
// deallocate all resources that consume memory
public void release() {
if (holders != null) {
for (RecyclerViewHolder holder : holders) {
holder.release();
}
holders.clear();

context = null;
videos = null;
holders = null;

System.gc();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,60 @@ public void bind(VideoType data) {
play();
}

public void pause() {
if (exoPlayer != null) {
try {
exoPlayer.setPlayWhenReady(false);
}
catch (Exception e){}
}
}

public void play() {
try {
exoPlayer.setPlayWhenReady(true);
if (exoPlayer != null) {
try {
exoPlayer.setPlayWhenReady(true);
}
catch (Exception e){}
}
catch (Exception e){}
}

public void pause() {
try {
exoPlayer.setPlayWhenReady(false);
public void togglePausePlay() {
if (exoPlayer != null) {
try {
exoPlayer.setPlayWhenReady(
!exoPlayer.getPlayWhenReady()
);
}
catch (Exception e){}
}
catch (Exception e){}
}

public void stop() {
try {
exoPlayer.stop(true);
if (exoPlayer != null) {
try {
exoPlayer.stop(true);
}
catch (Exception e){}
}
catch (Exception e){}
}

// deallocate all resources that consume memory
public void release() {
try {
exoPlayer.release();
if (exoPlayer != null) {
try {
view.setPlayer(null);
exoPlayer.stop(true);
exoPlayer.release();

exoPlayer = null;
view = null;
data = null;

System.gc();
}
catch (Exception e){}
}
catch (Exception e){}
}

// open selected video in fullscreen view
Expand All @@ -124,11 +152,6 @@ private void doOnClick() {

// toggle play/pause of selected video
private void doOnLongClick() {
try {
exoPlayer.setPlayWhenReady(
!exoPlayer.getPlayWhenReady()
);
}
catch (Exception e){}
togglePausePlay();
}
}
4 changes: 2 additions & 2 deletions android-studio-project/constants.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project.ext {
releaseVersionCode = Integer.parseInt("002040016", 10) //Integer.MAX_VALUE == 2147483647
releaseVersion = '002.04.00-16API'
releaseVersionCode = Integer.parseInt("002040116", 10) //Integer.MAX_VALUE == 2147483647
releaseVersion = '002.04.01-16API'
javaVersion = JavaVersion.VERSION_1_8
minSdkVersion = 16
targetSdkVersion = 29
Expand Down

0 comments on commit 9971765

Please sign in to comment.