Skip to content

Commit

Permalink
Do not hardcode paths
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisBarreiro committed Aug 1, 2024
1 parent c024579 commit e36c0de
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.duckduckgo.app.browser.di
import android.content.ClipboardManager
import android.content.Context
import android.content.pm.PackageManager
import android.webkit.MimeTypeMap
import androidx.room.Room
import androidx.work.WorkManager
import com.duckduckgo.adclick.api.AdClickManager
Expand Down Expand Up @@ -340,10 +339,4 @@ class BrowserModule {
fun providesMediaPlaybackDao(mediaPlaybackDatabase: MediaPlaybackDatabase): MediaPlaybackDao {
return mediaPlaybackDatabase.mediaPlaybackDao()
}

@Provides
@SingleInstanceIn(AppScope::class)
fun provideMimeTypeMap(): MimeTypeMap {
return MimeTypeMap.getSingleton()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* 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 com.duckduckgo.duckplayer.impl

import android.content.res.AssetManager
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
import java.io.IOException
import javax.inject.Inject

interface DuckPlayerLocalFilesPath {
val assetsPath: List<String>
}

@ContributesBinding(AppScope::class)
class RealDuckPlayerLocalFilesPath @Inject constructor(private val assetManager: AssetManager) : DuckPlayerLocalFilesPath {

override val assetsPath: List<String> = getAllAssetFilePaths("duckplayer")

private fun getAllAssetFilePaths(directory: String): List<String> {
val filePaths = mutableListOf<String>()
val files = assetManager.list(directory) ?: return emptyList()

files.forEach {
val fullPath = "$directory/$it"
try {
assetManager.open(fullPath)
filePaths.add(fullPath.removePrefix("duckplayer/"))
} catch (e: IOException) {
filePaths.addAll(getAllAssetFilePaths(fullPath))
}
}
return filePaths
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class RealDuckPlayer @Inject constructor(
private val duckPlayerFeatureRepository: DuckPlayerFeatureRepository,
private val duckPlayerFeature: DuckPlayerFeature,
private val pixel: Pixel,
private val duckPlayerLocalFilesPath: DuckPlayerLocalFilesPath,
) : DuckPlayer {

private var shouldForceYTNavigation = false
Expand Down Expand Up @@ -143,12 +144,7 @@ class RealDuckPlayer @Inject constructor(
}

override fun isSimulatedYoutubeNoCookie(uri: Uri): Boolean {
val validPaths = listOf(
"js/index.css",
"js/inline.js",
"js/index.js",
"js/player-bg-KEARYNU4.png",
)
val validPaths = duckPlayerLocalFilesPath.assetsPath
return (
uri.host?.removePrefix("www.") ==
YOUTUBE_NO_COOKIE_HOST && (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* 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 com.duckduckgo.duckplayer.impl.di

import android.content.Context
import android.content.res.AssetManager
import android.webkit.MimeTypeMap
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
import dagger.Provides
import dagger.SingleInstanceIn

@Module
@ContributesTo(AppScope::class)
class DuckPlayerModule {
@Provides
@SingleInstanceIn(AppScope::class)
fun provideMimeTypeMap(): MimeTypeMap {
return MimeTypeMap.getSingleton()
}

@Provides
@SingleInstanceIn(AppScope::class)
fun provideAssetManager(context: Context): AssetManager {
return context.assets
}
}

0 comments on commit e36c0de

Please sign in to comment.