|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 The Matrix.org Foundation C.I.C. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.matrix.android.sdk.internal.session.room.send |
| 18 | + |
| 19 | +import timber.log.Timber |
| 20 | +import javax.inject.Inject |
| 21 | +import kotlin.math.abs |
| 22 | +import kotlin.math.ceil |
| 23 | + |
| 24 | +internal class WaveFormSanitizer @Inject constructor() { |
| 25 | + private companion object { |
| 26 | + const val MIN_NUMBER_OF_VALUES = 30 |
| 27 | + const val MAX_NUMBER_OF_VALUES = 120 |
| 28 | + |
| 29 | + const val MAX_VALUE = 1024 |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * The array should have no less than 30 elements and no more than 120. |
| 34 | + * List of integers between zero and 1024, inclusive. |
| 35 | + */ |
| 36 | + fun sanitize(waveForm: List<Int>?): List<Int>? { |
| 37 | + if (waveForm.isNullOrEmpty()) { |
| 38 | + return null |
| 39 | + } |
| 40 | + |
| 41 | + // Limit the number of items |
| 42 | + val sizeInRangeList = mutableListOf<Int>() |
| 43 | + when { |
| 44 | + waveForm.size < MIN_NUMBER_OF_VALUES -> { |
| 45 | + // Repeat the same value to have at least 30 items |
| 46 | + val repeatTimes = ceil(MIN_NUMBER_OF_VALUES / waveForm.size.toDouble()).toInt() |
| 47 | + waveForm.map { value -> |
| 48 | + repeat(repeatTimes) { |
| 49 | + sizeInRangeList.add(value) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + waveForm.size > MAX_NUMBER_OF_VALUES -> { |
| 54 | + val keepOneOf = ceil(waveForm.size.toDouble() / MAX_NUMBER_OF_VALUES).toInt() |
| 55 | + waveForm.mapIndexed { idx, value -> |
| 56 | + if (idx % keepOneOf == 0) { |
| 57 | + sizeInRangeList.add(value) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + else -> { |
| 62 | + sizeInRangeList.addAll(waveForm) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // OK, ensure all items are positive |
| 67 | + val positiveList = sizeInRangeList.map { |
| 68 | + abs(it) |
| 69 | + } |
| 70 | + |
| 71 | + // Ensure max is not above MAX_VALUE |
| 72 | + val max = positiveList.maxOrNull() ?: MAX_VALUE |
| 73 | + |
| 74 | + val finalList = if (max > MAX_VALUE) { |
| 75 | + // Reduce the values |
| 76 | + positiveList.map { |
| 77 | + it * MAX_VALUE / max |
| 78 | + } |
| 79 | + } else { |
| 80 | + positiveList |
| 81 | + } |
| 82 | + |
| 83 | + Timber.d("Sanitize from ${waveForm.size} items to ${finalList.size} items. Max value was $max") |
| 84 | + return finalList |
| 85 | + } |
| 86 | +} |
0 commit comments