Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

package com.hossainkhan.android.demo.data

import android.net.Uri
import com.google.firebase.Timestamp
import java.util.*
import java.util.Date


/**
Expand Down Expand Up @@ -50,4 +51,13 @@ data class ResourceInfo(
val date = publish_date?.toDate() ?: Date()
return FirestoreDateFormatter.date(date)
}

val isYouTubeUrl: Boolean = url.contains("youtube.com/watch?v=", ignoreCase = true)

/**
* Provides [Uri] for directly invoking youtube app via Android Intent.
* @throws IllegalStateException if the URL is not for youtube. Check [isYouTubeUrl] first.
*/
val youtubeIntentUri: String
get() = if (isYouTubeUrl) ("vnd.youtube:" + url.split("=")[1]) else throw IllegalStateException("Not a YouTube URL.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,18 @@ class LearningResourceActivity : AppCompatActivity() {
}

private fun openContent(resourceInfo: ResourceInfo) {
// TODO: Update logic later when the URL is not only youtube.
val intentApp = Intent(Intent.ACTION_VIEW,
Uri.parse("vnd.youtube:" + resourceInfo.url.split("=")[1]))
val urlPreviewUri = Uri.parse(resourceInfo.url) // Generic URL preview using any supported app.

val previewContentIntent = if (resourceInfo.isYouTubeUrl) {
Intent(Intent.ACTION_VIEW, Uri.parse(resourceInfo.youtubeIntentUri))
} else {
Intent(Intent.ACTION_VIEW, urlPreviewUri)
}

val intentBrowser = Intent(Intent.ACTION_VIEW, Uri.parse(resourceInfo.url))
try {
startActivity(intentApp)
startActivity(previewContentIntent)
} catch (ex: ActivityNotFoundException) {
startActivity(intentBrowser)
startActivity(Intent(Intent.ACTION_VIEW, urlPreviewUri))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2019 Hossain Khan
*
* 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.hossainkhan.android.demo.data

import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

/**
* Tests [ResourceInfo].
*/
class ResourceInfoTest {

@Test
fun isYouTubeUrl_givenValidUrl_returnsTrue() {
val resourceInfoYoutube = ResourceInfo(url = "https://www.youtube.com/watch?v=29gLA90m6Gk")

assertTrue(resourceInfoYoutube.isYouTubeUrl)
}

@Test
fun isYouTubeUrl_givenInvalidUrl_returnsFalse() {
val resourceInfoYoutube = ResourceInfo(url = "https://hossainkhan.com/watch?v=29gLA90m6Gk")

assertFalse(resourceInfoYoutube.isYouTubeUrl)
}

@Test
fun getYoutubeIntentUri_givenValidUrl_providesYoutubeIntentUri() {
val resourceInfoYoutube = ResourceInfo(url = "https://www.youtube.com/watch?v=29gLA90m6Gk")

assertEquals("vnd.youtube:29gLA90m6Gk", resourceInfoYoutube.youtubeIntentUri)
}

@Test(expected = IllegalStateException::class)
fun getYoutubeIntentUri_givenInvalidUrl_throwsException() {
val resourceInfoYoutube = ResourceInfo(url = "https://hossainkhan.com")

resourceInfoYoutube.youtubeIntentUri
}
}