Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeakCanary investigation to find memory leaks #395

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
11 changes: 6 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.61'
ext.kotlin_version = '1.3.72'
repositories {
google()
jcenter()
Expand Down Expand Up @@ -122,7 +122,7 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}
Expand All @@ -145,10 +145,10 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.preference:preference:1.1.1'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'
implementation 'com.google.android.material:material:1.2.0-alpha06'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.wdullaer:materialdatetimepicker:4.2.3'
implementation 'com.github.guardianproject:signal-cli-android:v0.6.0-android-beta-1'
Expand Down Expand Up @@ -178,6 +178,7 @@ dependencies {
kapt "android.arch.persistence.room:compiler:2.1.0"
implementation "android.arch.lifecycle:runtime:2.1.0"
implementation "android.arch.lifecycle:extensions:2.1.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"

testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0'
Expand All @@ -193,5 +194,5 @@ dependencies {
// new camera view: https://github.com/natario1/CameraView
implementation 'com.otaliastudios:cameraview:2.4.0'


debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
}
24 changes: 5 additions & 19 deletions src/main/java/org/havenapp/main/MonitorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
package org.havenapp.main;

import android.Manifest;
import android.animation.ValueAnimator;
import android.app.PictureInPictureParams;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
Expand All @@ -40,7 +38,11 @@
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;

Expand All @@ -55,11 +57,6 @@
import java.io.FileOutputStream;
import java.io.IOException;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import static org.havenapp.main.Utils.getTimerText;

public class MonitorActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
Expand Down Expand Up @@ -233,8 +230,6 @@ private void initSetupLayout() {
}

private void configCamera() {

mFragmentCamera.stopCamera();
startActivityForResult(new Intent(this, CameraConfigureActivity.class),REQUEST_CAMERA);
}

Expand Down Expand Up @@ -317,15 +312,6 @@ else if (requestCode == REQUEST_CAMERA)
}
}

@Override
protected void onDestroy() {
if (!mIsMonitoring)
{
mFragmentCamera.stopCamera();
}
super.onDestroy();
}

private void initTimer() {
txtTimer.setTextColor(getResources().getColor(R.color.colorAccent));
cTimer = new CountDownTimer((preferences.getTimerDelay()) * 1000, 1000) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void stop (Context context)
{
context.unbindService(mConnection);
if (microphone != null)
microphone.cancel(true);
microphone.cancelTask();
}


Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/havenapp/main/sensors/media/MicSamplerTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
import android.os.AsyncTask;
import android.util.Log;

import androidx.annotation.Nullable;

import java.io.IOException;


public class MicSamplerTask extends AsyncTask<Void,Object,Void> {

@Nullable
private MicListener listener = null;
private AudioCodec volumeMeter = new AudioCodec();
private boolean sampling = true;
Expand All @@ -30,10 +34,6 @@ public interface MicListener {
public void setMicListener(MicListener listener) {
this.listener = listener;
}

protected Void onPreExecute(Void...params) {
return null;
}

@Override
protected Void doInBackground(Void... params) {
Expand Down Expand Up @@ -106,6 +106,13 @@ public void pause() {
@Override
protected void onProgressUpdate(Object... progress) {
short[] data = (short[]) progress[0];
listener.onSignalReceived(data);
}
if (listener != null) {
listener.onSignalReceived(data);
}
}

public void cancelTask() {
cancel(true);
listener = null;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/havenapp/main/service/SignalSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class SignalSender {

private SignalSender(Context context, String username)
{
mContext = context;
mContext = context.getApplicationContext();
mUsername = username;
mAlertCount = 0;
preferences = new PreferenceManager(mContext);
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/havenapp/main/ui/CameraConfigureActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import org.havenapp.main.PreferenceManager;
import org.havenapp.main.R;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import me.angrybyte.numberpicker.listener.OnValueChangeListener;

import org.havenapp.main.PreferenceManager;
import org.havenapp.main.R;

import me.angrybyte.numberpicker.view.ActualNumberPicker;


Expand Down Expand Up @@ -111,7 +110,6 @@ else if (camera.equals(PreferenceManager.BACK))
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mFragment.stopCamera();
finish();
break;
}
Expand All @@ -124,7 +122,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
*/
@Override
public void onBackPressed() {
mFragment.stopCamera();
finish();
}

Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/havenapp/main/ui/CameraFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.controls.Audio;

Expand Down Expand Up @@ -76,13 +79,6 @@ public void updateCamera ()
}
}

public void stopCamera ()
{
if (cameraViewHolder != null) {
cameraViewHolder.stopCamera();
}
}

public void initCamera ()
{

Expand All @@ -94,6 +90,7 @@ public void initCamera ()

CameraView cameraView = getActivity().findViewById(R.id.camera_view);
cameraView.setAudio(Audio.OFF);
cameraView.setLifecycleOwner(this);

if (cameraViewHolder == null) {
cameraViewHolder = new CameraViewHolder(getActivity(), cameraView);
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/org/havenapp/main/ui/CameraViewHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ public List<Size> select(@NonNull List<Size> source) {
return result;
}
});
cameraView.open();

cameraView.addFrameProcessor(new FrameProcessor() {
@Override
Expand Down Expand Up @@ -390,21 +389,12 @@ private synchronized boolean recordVideo() {
}


public synchronized void stopCamera ()
{
if (cameraView != null) {
cameraView.close();
}
}


public void destroy ()
{
if (mConnection != null) {
this.context.unbindService(mConnection);
mConnection = null;
}
stopCamera();
}

public int getCorrectCameraOrientation(Facing facing, int orientation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
protected void onDestroy() {
super.onDestroy();
if (microphone != null)
microphone.cancel(true);
microphone.cancelTask();

}

Expand Down
51 changes: 51 additions & 0 deletions src/main/java/org/havenapp/main/ui/widget/HavenCameraView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.havenapp.main.ui.widget

import android.content.Context
import android.util.AttributeSet
import androidx.annotation.NonNull
import androidx.annotation.Nullable
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.otaliastudios.cameraview.CameraView

/**
* A subclass of [CameraView] which implements [DefaultLifecycleObserver]
*
* According to https://developer.android.com/reference/androidx/lifecycle/DefaultLifecycleObserver
* [DefaultLifecycleObserver] should *always* be preferred over [androidx.lifecycle.LifecycleObserver]
* if we use Java 8. [CameraView] library targets Java 7 hence this implementation aims to ignore
* [androidx.lifecycle.OnLifecycleEvent] annotated methods in the super class and replace them with
* the callbacks implemeted in this sub class
*/
class HavenCameraView : CameraView, DefaultLifecycleObserver {
constructor(context: Context) : super(context)

constructor(@NonNull context: Context, @Nullable attrs: AttributeSet) : super(context, attrs)

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
}

override fun onResume(owner: LifecycleOwner) {
super.onResume(owner)
open()
}

override fun onPause(owner: LifecycleOwner) {
close()
super.onPause(owner)
}

override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)
}

override fun onStop(owner: LifecycleOwner) {
super.onStop(owner)
}

override fun onDestroy(owner: LifecycleOwner) {
destroy()
super.onDestroy(owner)
}
}
3 changes: 1 addition & 2 deletions src/main/res/layout/camera_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
android:foreground="@color/translucentOverlay25">

<!-- Camera -->
<com.otaliastudios.cameraview.CameraView
xmlns:app="http://schemas.android.com/apk/res-auto"
<org.havenapp.main.ui.widget.HavenCameraView
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down