<commit_before>/**
 * Trail drawing library
 * Copyright (C) 2014 Orange
 * Authors: <EMAIL>, <EMAIL>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.orange.dgil.trail.android.drawingtool.quillpen;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
class QuillBitmap {
  private final View view;
  private final Paint paint = new Paint();

  @Getter
  private Bitmap bitmap;
  @Getter
  private Canvas bitmapCanvas;

  void lazyLoading() {
    if (shouldAllocateNewBitmap()) {
      releaseBitmap();
      bitmap = getNewBitmap();
      bitmapCanvas = new Canvas(bitmap);
    }
  }

  private boolean shouldAllocateNewBitmap() {
    return bitmap == null || bitmap.getWidth() != view.getWidth() || bitmap.getHeight() != view.getHeight();
  }

  void releaseBitmap() {
    if (bitmap != null) {
      bitmap.recycle();
      bitmap = null;
    }
  }

  void drawBitmap(Canvas canvas) {
    canvas.drawBitmap(bitmap, 0, 0, paint);
  }

  boolean isLoaded() {
    return bitmap != null;
  }

  void reset() {
    resetAlpha();<commit_msg>fix(memory): when we trim the memory, we need to release the bitmap canvas as well<commit_after>/**
 * Trail drawing library
 * Copyright (C) 2014 Orange
 * Authors: <EMAIL>, <EMAIL>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.orange.dgil.trail.android.drawingtool.quillpen;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
class QuillBitmap {
  private final View view;
  private final Paint paint = new Paint();

  @Getter
  private Bitmap bitmap;
  @Getter
  private Canvas bitmapCanvas;

  void lazyLoading() {
    if (shouldAllocateNewBitmap()) {
      releaseBitmap();
      bitmap = getNewBitmap();
      bitmapCanvas = new Canvas(bitmap);
    }
  }

  private boolean shouldAllocateNewBitmap() {
    return bitmap == null || bitmap.getWidth() != view.getWidth() || bitmap.getHeight() != view.getHeight();
  }

  /**
   * Release bitmap memory allocation; we are quite paranoiac here but it is worthy:
   *  - make sure the bitmap ref is not kept by the canvas (java/native objects)
   *  - indicates that the bitmap can be recycled rapidly (call to recycle())
   * So we release memory, but also try to let the system understand that it can freed it at once.
   * We are fair with other apps, but it is also a way to keep our app in the background alive.
   */
  void releaseBitmap() {
    if (bitmap != null) {
      bitmapCanvas.setBitmap(null);
      bitmapCanvas = null;
      bitmap.recycle();
      bitmap = null;
    }
  }

  void drawBitmap(Canvas canvas) {
    canvas.drawBitmap(bitmap, 0, 0, paint);
  }

  boolean isLoaded() {
    return bitmap != null;
  }

  void reset() {
    resetAlpha();
{'fraction_non_alphanumeric': 0.10624445102101214, 'fraction_numerical': 0.005918910920390648, 'mean_word_length': 3.412532637075718, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6349568', 'n_tokens_mistral': 1021, 'n_tokens_neox': 997, 'n_words': 342}