diff --git a/app/src/main/java/com/hossainkhan/android/demo/data/ResourceInfo.kt b/app/src/main/java/com/hossainkhan/android/demo/data/ResourceInfo.kt index eaf14b3..f4fc747 100644 --- a/app/src/main/java/com/hossainkhan/android/demo/data/ResourceInfo.kt +++ b/app/src/main/java/com/hossainkhan/android/demo/data/ResourceInfo.kt @@ -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 /** @@ -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.") } \ No newline at end of file diff --git a/app/src/main/java/com/hossainkhan/android/demo/ui/resource/LearningResourceActivity.kt b/app/src/main/java/com/hossainkhan/android/demo/ui/resource/LearningResourceActivity.kt index f29f616..c3e6dba 100644 --- a/app/src/main/java/com/hossainkhan/android/demo/ui/resource/LearningResourceActivity.kt +++ b/app/src/main/java/com/hossainkhan/android/demo/ui/resource/LearningResourceActivity.kt @@ -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)) } } } diff --git a/app/src/test/java/com/hossainkhan/android/demo/data/ResourceInfoTest.kt b/app/src/test/java/com/hossainkhan/android/demo/data/ResourceInfoTest.kt new file mode 100644 index 0000000..52767c5 --- /dev/null +++ b/app/src/test/java/com/hossainkhan/android/demo/data/ResourceInfoTest.kt @@ -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 + } +} \ No newline at end of file