Repository: square/leakcanary
File: leakcanary-android-core/src/main/java/leakcanary/internal/SquigglySpan.kt (Kotlin)

Code before:
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package leakcanary.internal

import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.text.SpannableStringBuilder
import android.text.style.ReplacementSpan
import android.text.style.UnderlineSpan
import com.squareup.leakcanary.core.R

/**
 * Inspired from https://github.com/flavienlaurent/spans and
 * https://github.com/andyxialm/WavyLineView
 */
internal class SquigglySpan(resources: Resources) : ReplacementSpan() {

  private val squigglyPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
  private val path: Path
  private val referenceColor: Int
  private val halfStrokeWidth: Float
  private val amplitude: Float
  private val halfWaveHeight: Float
  private val periodDegrees: Float

  private var width: Int = 0

  init {
    squigglyPaint.style = Paint.Style.STROKE
    squigglyPaint.color = resources.getColor(R.color.leak_canary_leak)
    val strokeWidth =
      resources.getDimensionPixelSize(R.dimen.leak_canary_squiggly_span_stroke_width)
          .toFloat()
    squigglyPaint.strokeWidth = strokeWidth

    halfStrokeWidth = strokeWidth / 2
    amplitude = resources.getDimensionPixelSize(R.dimen.leak_canary_squiggly_span_amplitude)
        .toFloat()
    periodDegrees =
      resources.getDimensionPixelSize(R.dimen.leak_canary_squiggly_span_period_degrees)
          .toFloat()
    path = Path()
    val waveHeight = 2 * amplitude + strokeWidth

Review comment (suggestion):
Same as elsewhere. Also if we go that route  we should remove Resources. Not a big fan of having context everywhere though

Code after:
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package leakcanary.internal

import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.text.SpannableStringBuilder
import android.text.style.ReplacementSpan
import android.text.style.UnderlineSpan
import androidx.core.content.ContextCompat
import com.squareup.leakcanary.core.R

/**
 * Inspired from https://github.com/flavienlaurent/spans and
 * https://github.com/andyxialm/WavyLineView
 */
internal class SquigglySpan(context: Context, resources: Resources) : ReplacementSpan() {

  private val squigglyPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
  private val path: Path
  private val referenceColor: Int
  private val halfStrokeWidth: Float
  private val amplitude: Float
  private val halfWaveHeight: Float
  private val periodDegrees: Float

  private var width: Int = 0

  init {
    squigglyPaint.style = Paint.Style.STROKE
    squigglyPaint.color = ContextCompat.getColor(context, R.color.leak_canary_leak)
    val strokeWidth =
      resources.getDimensionPixelSize(R.dimen.leak_canary_squiggly_span_stroke_width)
          .toFloat()
    squigglyPaint.strokeWidth = strokeWidth

    halfStrokeWidth = strokeWidth / 2
    amplitude = resources.getDimensionPixelSize(R.dimen.leak_canary_squiggly_span_amplitude)
        .toFloat()
    periodDegrees =
      resources.getDimensionPixelSize(R.dimen.leak_canary_squiggly_span_period_degrees)
          .toFloat()