Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      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 package com.android.messaging.util;
     17 
     18 import android.net.Uri;
     19 import android.text.TextUtils;
     20 
     21 public class YouTubeUtil {
     22     private static final String YOUTUBE_HOST_1 = "www.youtube.com";
     23     private static final String YOUTUBE_HOST_2 = "youtube.com";
     24     private static final String YOUTUBE_HOST_3 = "m.youtube.com";
     25     private static final String YOUTUBE_HOST_4 = "youtube.googleapis.com";
     26     private static final String YOUTUBE_HOST_5 = "youtu.be";
     27 
     28     private static final String YOUTUBE_PATH_1 = "/watch";
     29     private static final String YOUTUBE_PATH_2 = "/embed/";
     30     private static final String YOUTUBE_PATH_3 = "/v/";
     31     private static final String YOUTUBE_PATH_4 = "/apiplayer";
     32 
     33     public static final String YOUTUBE_STATIC_THUMBNAIL_PREFIX = "https://img.youtube.com/vi/";
     34     public static final String YOUTUBE_STATIC_THUMBNAIL_END = "/hqdefault.jpg";
     35 
     36     public static String getYoutubePreviewImageLink(String urlString) {
     37         // Types of youtube urls:
     38         // 1.) http://www.youtube.com/watch?v=VIDEOID
     39         // 2.) http://www.youtube.com/embed/VIDEOID
     40         // 3.) http://www.youtube.com/v/VIDEOID
     41         // 3a.) https://youtube.googleapis.com/v/VIDEOID
     42         // 4.) http://www.youtube.com/apiplayer?video_id=VIDEO_ID
     43         // 5.) http://youtu.be/VIDEOID
     44         if (!urlString.startsWith("http")) {
     45             // Apparently the url is not an RFC 2396 compliant uri without the port
     46             urlString = "http://" + urlString;
     47         }
     48         final Uri uri = Uri.parse(urlString);
     49         final String host = uri.getHost();
     50         if (YOUTUBE_HOST_1.equalsIgnoreCase(host)
     51                 || YOUTUBE_HOST_2.equalsIgnoreCase(host)
     52                 || YOUTUBE_HOST_3.equalsIgnoreCase(host)
     53                 || YOUTUBE_HOST_4.equalsIgnoreCase(host)
     54                 || YOUTUBE_HOST_5.equalsIgnoreCase(host)) {
     55             final String videoId = getYouTubeVideoId(uri);
     56             if (!TextUtils.isEmpty(videoId)) {
     57                 return YOUTUBE_STATIC_THUMBNAIL_PREFIX + videoId + YOUTUBE_STATIC_THUMBNAIL_END;
     58             }
     59             return null;
     60         }
     61         return null;
     62     }
     63 
     64     private static String getYouTubeVideoId(Uri uri) {
     65         final String urlPath = uri.getPath();
     66 
     67         if (TextUtils.isEmpty(urlPath)) {
     68             // There is no path so no need to continue.
     69             return null;
     70         }
     71         // Case 1
     72         if (urlPath.startsWith(YOUTUBE_PATH_1)) {
     73             return uri.getQueryParameter("v");
     74         }
     75         // Case 2
     76         if (urlPath.startsWith(YOUTUBE_PATH_2)) {
     77             return getVideoIdFromPath(YOUTUBE_PATH_2, urlPath);
     78         }
     79         // Case 3
     80         if (urlPath.startsWith(YOUTUBE_PATH_3)) {
     81             return getVideoIdFromPath(YOUTUBE_PATH_3, urlPath);
     82         }
     83         // Case 4
     84         if (urlPath.startsWith(YOUTUBE_PATH_4)) {
     85             return uri.getQueryParameter("video_id");
     86         }
     87         // Case 5
     88         if (YOUTUBE_HOST_5.equalsIgnoreCase(uri.getHost())) {
     89             return getVideoIdFromPath("/", urlPath);
     90         }
     91         return null;
     92     }
     93 
     94     private static String getVideoIdFromPath(String prefixSubstring, String urlPath) {
     95         return urlPath.substring(prefixSubstring.length());
     96     }
     97 
     98 }
     99