├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── drawable
│ │ │ │ └── side_nav_bar.xml
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── strings.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── drawables.xml
│ │ │ │ └── styles.xml
│ │ │ ├── menu
│ │ │ │ ├── main.xml
│ │ │ │ └── activity_main_drawer.xml
│ │ │ ├── values-v21
│ │ │ │ └── styles.xml
│ │ │ ├── drawable-v21
│ │ │ │ ├── ic_menu_send.xml
│ │ │ │ ├── ic_menu_slideshow.xml
│ │ │ │ ├── ic_menu_gallery.xml
│ │ │ │ ├── ic_menu_manage.xml
│ │ │ │ ├── ic_menu_camera.xml
│ │ │ │ └── ic_menu_share.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ ├── content_main.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── tube_loading.xml
│ │ │ │ ├── app_bar_main.xml
│ │ │ │ └── nav_header_main.xml
│ │ ├── java
│ │ │ └── dodola
│ │ │ │ └── downtube
│ │ │ │ ├── core
│ │ │ │ ├── ResolutionNote.java
│ │ │ │ ├── entity
│ │ │ │ │ ├── VideoPlayBean.java
│ │ │ │ │ ├── Resolution.java
│ │ │ │ │ ├── MediaFileInfo.java
│ │ │ │ │ ├── FmtStreamMap.java
│ │ │ │ │ └── RingtoneEntity.java
│ │ │ │ ├── RxYoutube.java
│ │ │ │ └── YoutubeUtils.java
│ │ │ │ ├── view
│ │ │ │ └── YouTuBeWebView.java
│ │ │ │ ├── utils
│ │ │ │ └── LogUtil.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── dodola
│ │ │ └── downtube
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── dodola
│ │ └── downtube
│ │ └── ApplicationTest.java
├── proguard-rules.pro
├── build.gradle
└── app.iml
├── settings.gradle
├── imgs
├── img1.png
└── img2.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── README.md
├── .gitignore
├── gradle.properties
├── DownTube.iml
├── LICENSE
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/imgs/img1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/YoutubeDown/HEAD/imgs/img1.png
--------------------------------------------------------------------------------
/imgs/img2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/YoutubeDown/HEAD/imgs/img2.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/YoutubeDown/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/YoutubeDown/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/YoutubeDown/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/YoutubeDown/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/YoutubeDown/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/YoutubeDown/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/core/ResolutionNote.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube.core;
5 |
6 | public enum ResolutionNote {
7 | HD, MHD, LHD, XLHD
8 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Oct 31 15:20:04 CST 2016
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/side_nav_bar.xml:
--------------------------------------------------------------------------------
1 |
4 |
6 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | #3F51B5
7 | #303F9F
8 | #FF4081
9 |
10 |
--------------------------------------------------------------------------------
/app/src/test/java/dodola/downtube/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package dodola.downtube;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DownTube
2 | A YouTube Downloader, can download YouTube video on Android ,include encrypted video
3 | YouTube视频下载应用,可下载非加密和加密视频。
4 |
5 |
6 | 
7 |
8 | 
9 |
10 |
11 | ##待完成功能
12 |
13 | 1. 解决崩溃
14 | 2. 视频转Mp3功能
15 | 3. 直接下载音频功能
16 | 4. 下载管理
17 | 5. 直接音频播放
18 |
19 | ##Thanks
20 |
21 | * [RxJava](https://github.com/ReactiveX/RxJava)
22 | * [duktape](https://github.com/svaarala/duktape)
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 | DownTube
6 |
7 | Open navigation drawer
8 | Close navigation drawer
9 |
10 | Settings
11 |
12 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 | /*/build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 | .idea/
29 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/dodola/downtube/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package dodola.downtube;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/core/entity/VideoPlayBean.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube.core.entity;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | public class VideoPlayBean {
10 | /**
11 | * 视频id
12 | */
13 | public String vid;
14 |
15 | /**
16 | * 视频的播放流
17 | */
18 | public List currentMaps = new ArrayList();
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values-v21/styles.xml:
--------------------------------------------------------------------------------
1 |
4 | >
5 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v21/ic_menu_send.xml:
--------------------------------------------------------------------------------
1 |
4 |
9 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
8 | 64dp
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 | 16dp
7 | 160dp
8 |
9 | 16dp
10 | 16dp
11 | 16dp
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v21/ic_menu_slideshow.xml:
--------------------------------------------------------------------------------
1 |
4 |
9 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v21/ic_menu_gallery.xml:
--------------------------------------------------------------------------------
1 |
4 |
9 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v21/ic_menu_manage.xml:
--------------------------------------------------------------------------------
1 |
4 |
9 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/core/entity/Resolution.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube.core.entity;
5 |
6 | import dodola.downtube.core.ResolutionNote;
7 |
8 | public class Resolution {
9 | public Resolution(String _id, String _resolution, String _format, String _type, ResolutionNote _notes) {
10 | id = _id;
11 | resolution = _resolution;
12 | format = _format;
13 | type = _type;
14 | notes = _notes;
15 | }
16 |
17 | public String id;
18 | public String resolution;
19 | public String format;
20 | public String type;
21 | public ResolutionNote notes;
22 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/baidu/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v21/ic_menu_camera.xml:
--------------------------------------------------------------------------------
1 |
4 |
9 |
12 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v21/ic_menu_share.xml:
--------------------------------------------------------------------------------
1 |
4 |
9 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values/drawables.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 | - @android:drawable/ic_menu_camera
7 |
8 | - @android:drawable/ic_menu_gallery
10 |
11 | - @android:drawable/ic_menu_slideshow
13 |
14 | - @android:drawable/ic_menu_manage
16 |
17 | - @android:drawable/ic_menu_share
19 |
20 | - @android:drawable/ic_menu_send
22 |
23 |
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
7 |
14 |
18 |
20 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/DownTube.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | applicationId "dodola.downtube"
9 | minSdkVersion 15
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | }
18 | }
19 | useLibrary 'org.apache.http.legacy'
20 |
21 | }
22 |
23 | dependencies {
24 | compile fileTree(dir: 'libs', include: ['*.jar'])
25 | testCompile 'junit:junit:4.12'
26 | compile 'com.android.support:appcompat-v7:25.0.0'
27 | compile 'com.android.support:design:25.0.0'
28 | compile 'io.reactivex:rxandroid:1.0.1'
29 | // Because RxAndroid releases are few and far between, it is recommended you also
30 | // explicitly depend on RxJava's latest version for bug fixes and new features.
31 | compile 'io.reactivex:rxjava:1.0.15'
32 | compile 'com.squareup.duktape:duktape-android:0.9.4'
33 |
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/core/entity/MediaFileInfo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube.core.entity;
5 |
6 | import java.io.Serializable;
7 |
8 | import android.os.Parcel;
9 | import android.os.Parcelable;
10 |
11 | public class MediaFileInfo implements Serializable {
12 |
13 | private static final long serialVersionUID = 401314654472890540L;
14 |
15 | public enum FileCategory implements Serializable {
16 | Music, Video, Picture, Doc, Apk, Other, Albums, PicBack
17 | }
18 |
19 | public String folderPath = "";
20 |
21 | public String filePath = "";
22 |
23 | public String fileName = "";
24 |
25 | public long fileSize;
26 |
27 | public int Count;
28 |
29 | public long ModifiedDate;
30 |
31 | public boolean Selected;
32 |
33 | public long duration;
34 |
35 | public boolean canRead;
36 |
37 | public boolean canWrite;
38 |
39 | public boolean isHidden;
40 |
41 | public FileCategory fileType;// 1 app 2 video 3 picture 4 music 5 document
42 |
43 | public long dbId;
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 dodola
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
14 |
15 |
19 |
27 |
28 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
14 |
15 |
19 |
20 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/tube_loading.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
16 |
17 |
23 |
24 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/activity_main_drawer.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
40 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
7 |
8 |
9 |
10 |
11 |
12 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/app_bar_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
13 |
14 |
18 |
19 |
25 |
26 |
27 |
28 |
29 |
30 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/nav_header_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
16 |
17 |
23 |
24 |
30 |
31 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/core/entity/FmtStreamMap.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube.core.entity;
5 |
6 |
7 | public class FmtStreamMap {
8 | public String fallbackHost;
9 | /**
10 | * 视频签名
11 | */
12 | public String s;
13 | public String itag;
14 | public String type;
15 | /**
16 | *
17 | */
18 | public String quality;
19 | /**
20 | * 原始地址[如无需解密则该链接为真实下载地址]
21 | */
22 | public String url;
23 | /**
24 | * 加密签名
25 | */
26 | public String sig;
27 | /**
28 | * 视频标题
29 | */
30 | public String title;
31 |
32 | public String mediatype;
33 |
34 | public boolean encrypted;
35 | /**
36 | * 视频类型[mp4,3gp]
37 | */
38 | public String extension;
39 | /**
40 | * 分辨率相关信息
41 | */
42 | public Resolution resolution;
43 | /**
44 | * 该视频对应的JS解析代码
45 | */
46 | public String html5playerJS;
47 | /**
48 | * 视频ID
49 | */
50 | public CharSequence videoid;
51 | /**
52 | * 视频下载地址
53 | */
54 | public String realUrl;
55 |
56 | @Override
57 | public String toString() {
58 | return "FmtStreamMap [fallbackHost=" + fallbackHost + ", s=" + s + ", itag=" + itag + ", type=" + type + ", quality=" + quality
59 | + ", url=" + url + ", sig=" + sig + ", title=" + title + ", mediatype=" + mediatype + ", encrypted=" + encrypted
60 | + ", extension=" + extension + ", resolution=" + resolution + ", html5playerJS=" + html5playerJS + ", videoid=" + videoid
61 | + "]";
62 | }
63 |
64 | public String getStreamString() {
65 | if (resolution != null) {
66 | return String.format("%s (%s)", extension, resolution.resolution);
67 | } else {
68 | return null;
69 | }
70 | }
71 | }
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/core/entity/RingtoneEntity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube.core.entity;
5 |
6 | public class RingtoneEntity {
7 | public long currentPosition;
8 | public boolean Selected;
9 | public long ModifiedDate;
10 | public String donwloadUrl;
11 | public int mDuration;
12 | public String mName;
13 | private String mOriginUrl;
14 | private String videoId;
15 |
16 | /** 播放状态 0:初始状态 1:播放状态 2:暂停状态 3: loading */
17 | public enum PLAYSTATE {
18 | INIT_STATE, PLAY_STATE, PAUSE_STATE, LOADING_STATE
19 | };
20 |
21 | private PLAYSTATE playState = PLAYSTATE.INIT_STATE; // updatePlayState playInterrupt
22 |
23 | // 两个地方修改了playState
24 |
25 | public boolean mPlaying = false;
26 |
27 | private String desc;
28 |
29 | public RingtoneEntity() {
30 | initParams();
31 | }
32 |
33 | private void initParams() {
34 | playState = PLAYSTATE.INIT_STATE;
35 | mPlaying = false;
36 | currentPosition = 0;
37 | }
38 |
39 | public void setId(int id) {
40 | }
41 |
42 | public String getDesc() {
43 | return desc;
44 | }
45 |
46 | public void setDesc(String desc) {
47 | this.desc = desc;
48 | }
49 |
50 | public void setPlayState(PLAYSTATE initState) {
51 | playState = initState;
52 | }
53 |
54 | public String getDownloadUrl() {
55 | return donwloadUrl;
56 | }
57 |
58 | public void setDownloadUrl(String url) {
59 | donwloadUrl = url;
60 | }
61 |
62 | public void setDuration(int duration) {
63 | mDuration = duration;
64 | }
65 |
66 | public int getDuration() {
67 | return mDuration;
68 | }
69 |
70 | public String getName() {
71 | return mName;
72 | }
73 |
74 | public void setName(String name) {
75 | mName = name;
76 | }
77 |
78 | public PLAYSTATE getPlayState() {
79 | return playState;
80 | }
81 |
82 | public String getOriginUrl() {
83 | return mOriginUrl;
84 | }
85 |
86 | public void setOriginUrl(String mOriginUrl) {
87 | this.mOriginUrl = mOriginUrl;
88 | }
89 |
90 | public String getVideoId() {
91 | return videoId;
92 | }
93 |
94 | public void setVideoId(String videoId) {
95 | this.videoId = videoId;
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/view/YouTuBeWebView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube.view;
5 |
6 | import android.content.Context;
7 | import android.graphics.Canvas;
8 | import android.util.AttributeSet;
9 | import android.view.View;
10 | import android.webkit.WebView;
11 |
12 | public class YouTuBeWebView extends WebView {
13 |
14 | DisplayFinish df;
15 |
16 | public void setDf(DisplayFinish df) {
17 | this.df = df;
18 | }
19 |
20 | public YouTuBeWebView(Context context, AttributeSet attrs) {
21 | super(context, attrs);
22 | }
23 |
24 | public YouTuBeWebView(Context context) {
25 | super(context);
26 | }
27 |
28 | @Override
29 | protected void onDraw(Canvas canvas) {
30 | super.onDraw(canvas);
31 | // try {
32 | // df.After();
33 | // } catch (Exception ex) {
34 | //
35 | // }
36 | }
37 |
38 | public interface DisplayFinish {
39 | public void After();
40 | }
41 |
42 | private boolean is_gone = false;
43 |
44 | public void visibleChange(int visibility) {
45 | if (visibility == View.GONE) {
46 | try {
47 | WebView.class.getMethod("onPause").invoke(this);//stop flash
48 | } catch (Exception e) {
49 | }
50 | this.pauseTimers();
51 | this.is_gone = true;
52 | } else if (visibility == View.VISIBLE) {
53 | try {
54 | WebView.class.getMethod("onResume").invoke(this);//resume flash
55 | } catch (Exception e) {
56 | }
57 | this.resumeTimers();
58 | this.is_gone = false;
59 | }
60 | }
61 |
62 | @Override
63 | protected void onScrollChanged(int l, int t, int oldl, int oldt) {
64 | super.onScrollChanged(l, t, oldl, oldt);
65 | if (onScrollListener != null) {
66 | onScrollListener.onScrollChanged(l, t, oldl, oldt);
67 | }
68 | }
69 |
70 | public void onDetachedFromWindow() {//this will be trigger when back key pressed, not when home key pressed
71 | super.onDetachedFromWindow();
72 | if (this.is_gone) {
73 | try {
74 | this.destroy();
75 | } catch (Exception e) {
76 | }
77 | }
78 | }
79 |
80 | WebViewScrollListener onScrollListener;
81 |
82 | public void setOnScrollListener(WebViewScrollListener s) {
83 | onScrollListener = s;
84 | }
85 |
86 | public interface WebViewScrollListener {
87 | public void onScrollChanged(int l, int t, int oldl, int oldt);
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/utils/LogUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube.utils;
5 |
6 | import java.lang.reflect.Method;
7 | import java.util.ArrayList;
8 |
9 | import android.util.Log;
10 | import dodola.downtube.BuildConfig;
11 |
12 | /**
13 | * 用于输出日志。
14 | * 可以输入日志级别V/I/D/W/E,每个级别都有三种种重载方法:
15 | * 两个String参数的方法可以自定义TAG和message,一个String参数的方法可以自动获取当前类名作为TAG。
16 | * 另外还加入了传入Throwable参数的方法,便于直接输出异常信息。
17 | * 输出日志的message部分都会包含输出日志所在位置的方法名和行号,方便查找。
18 | *
19 | * @since 2014-7-10
20 | */
21 | public class LogUtil {
22 |
23 | private static String classname;
24 |
25 | private static ArrayList methods;
26 |
27 | static {
28 | classname = LogUtil.class.getName();
29 | methods = new ArrayList();
30 |
31 | Method[] ms = LogUtil.class.getDeclaredMethods();
32 | for (Method m : ms) {
33 | methods.add(m.getName());
34 | }
35 | }
36 |
37 | /**
38 | * 输出Debug级别的日志,自动获取类名作为TAG。
39 | *
40 | * @param msg 输出内容,带有方法名和行号。
41 | *
42 | * @since 2014-7-10
43 | */
44 | public static void d(String msg) {
45 | if (BuildConfig.DEBUG) {
46 | String[] content = getMsgAndTagWithLineNumber(msg);
47 | Log.d(content[0], content[1]);
48 | }
49 | }
50 |
51 | /**
52 | * 输出Debug级别的日志。
53 | *
54 | * @param tag TAG.
55 | * @param msg 输出内容,带有方法名和行号。
56 | *
57 | * @since 2014-7-10
58 | */
59 | public static void d(String tag, String msg) {
60 | if (BuildConfig.DEBUG) {
61 |
62 | Log.d(tag, getMsgWithLineNumber(msg));
63 | }
64 | }
65 |
66 | /**
67 | * 输出Debug级别的异常日志。
68 | *
69 | * @param t 异常对象。
70 | *
71 | * @since 2014-7-10
72 | */
73 | public static void d(Throwable t) {
74 | e(t.getMessage());
75 | }
76 |
77 | /**
78 | * 输出Error级别的日志。
79 | *
80 | * @param msg 输出内容,带有方法名和行号。
81 | *
82 | * @since 2014年3月4日
83 | */
84 | public static void e(String msg) {
85 | if (BuildConfig.DEBUG) {
86 | String[] content = getMsgAndTagWithLineNumber(msg);
87 | Log.e(content[0], content[1]);
88 | }
89 | }
90 |
91 | /**
92 | * 输出Error级别的日志。
93 | *
94 | * @param tag TAG.
95 | * @param msg 输出内容,带有方法名和行号。
96 | *
97 | * @since 2014年3月4日
98 | */
99 | public static void e(String tag, String msg) {
100 | if (BuildConfig.DEBUG) {
101 | Log.e(tag, getMsgWithLineNumber(msg));
102 | }
103 | }
104 |
105 | /**
106 | * 输出Error级别的异常日志。
107 | *
108 | * @param t 异常对象。
109 | *
110 | * @since 2014-7-10
111 | */
112 | public static void e(Throwable t) {
113 | if (BuildConfig.DEBUG) {
114 | if (t != null) {
115 | t.printStackTrace();
116 | }
117 | }
118 | }
119 |
120 | /**
121 | * 输出Info级别的日志。
122 | *
123 | * @param msg 日志内容,带有方法名和行号。
124 | *
125 | * @since 2014年3月4日
126 | */
127 | public static void i(String msg) {
128 | if (BuildConfig.DEBUG) {
129 | String[] content = getMsgAndTagWithLineNumber(msg);
130 | Log.i(content[0], content[1]);
131 | }
132 | }
133 |
134 | /**
135 | * 输出Info级别的日志。
136 | *
137 | * @param tag TAG.
138 | * @param msg 日志内容,带有方法名和行号。
139 | *
140 | * @since 2014年3月4日
141 | */
142 | public static void i(String tag, String msg) {
143 | if (BuildConfig.DEBUG) {
144 | Log.i(tag, getMsgWithLineNumber(msg));
145 | }
146 | }
147 |
148 | /**
149 | * 输出Info级别的异常日志。
150 | *
151 | * @param t 异常对象。
152 | *
153 | * @since 2014-7-10
154 | */
155 | public static void i(Throwable t) {
156 | e(t.getMessage());
157 | }
158 |
159 | /**
160 | * 输出Vorbose级别的日志。
161 | *
162 | * @param msg 日志内容,带有方法名和行号。
163 | *
164 | * @since 2014年3月4日
165 | */
166 | public static void v(String msg) {
167 | if (BuildConfig.DEBUG) {
168 | String[] content = getMsgAndTagWithLineNumber(msg);
169 | Log.v(content[0], content[1]);
170 | }
171 | }
172 |
173 | /**
174 | * 输出Vorbose级别的日志。
175 | *
176 | * @param tag TAG.
177 | * @param msg 日志内容,带有方法名和行号。
178 | *
179 | * @since 2014年3月4日
180 | */
181 | public static void v(String tag, String msg) {
182 | if (BuildConfig.DEBUG) {
183 | Log.v(tag, getMsgWithLineNumber(msg));
184 | }
185 | }
186 |
187 | /**
188 | * 输出Vorbose级别的异常日志。
189 | *
190 | * @param t 异常对象。
191 | *
192 | * @since 2014-7-10
193 | */
194 | public static void v(Throwable t) {
195 | e(t.getMessage());
196 | }
197 |
198 | /**
199 | * 输出Warn级别的日志。
200 | *
201 | * @param msg 日志内容,带有方法名和行号。
202 | *
203 | * @since 2014年3月4日
204 | */
205 | public static void w(String msg) {
206 | if (BuildConfig.DEBUG) {
207 | String[] content = getMsgAndTagWithLineNumber(msg);
208 | Log.w(content[0], content[1]);
209 | }
210 | }
211 |
212 | /**
213 | * 输出Warn级别的日志。
214 | *
215 | * @param tag TAG.
216 | * @param msg 日志内容,带有方法名和行号。
217 | *
218 | * @since 2014年3月4日
219 | */
220 | public static void w(String tag, String msg) {
221 | if (BuildConfig.DEBUG) {
222 | Log.w(tag, getMsgWithLineNumber(msg));
223 | }
224 | }
225 |
226 | /**
227 | * 输出Warn级别的异常日志。
228 | *
229 | * @param t 异常对象。
230 | *
231 | * @since 2014-7-10
232 | */
233 | public static void w(Throwable t) {
234 | e(t.getMessage());
235 | }
236 |
237 | /**
238 | * 获取日志信息的TAG、带行号的内容。
239 | *
240 | * @param msg 日志内容。
241 | *
242 | * @return TAG、带行号的内容组成的字符串数组。
243 | *
244 | * @since 2014年3月4日
245 | */
246 | private static String[] getMsgAndTagWithLineNumber(String msg) {
247 | try {
248 | for (StackTraceElement st : (new Throwable()).getStackTrace()) {
249 | if (classname.equals(st.getClassName()) || methods.contains(st.getMethodName())) {
250 | continue;
251 | } else {
252 | int b = st.getClassName().lastIndexOf(".") + 1;
253 | String tag = st.getClassName().substring(b);
254 | String message = st.getMethodName() + "():" + st.getLineNumber() + "->" + msg;
255 | String[] content = new String[] {tag, message};
256 | return content;
257 | }
258 |
259 | }
260 | } catch (Exception e) {
261 | LogUtil.e(e);
262 | }
263 | return new String[] {"MoboGenie", msg};
264 | }
265 |
266 | /**
267 | * 获取带行号的日志信息内容。
268 | *
269 | * @param msg 日志内容。
270 | *
271 | * @return 带行号的日志信息内容。
272 | *
273 | * @since 2014年3月4日
274 | */
275 | private static String getMsgWithLineNumber(String msg) {
276 | try {
277 | for (StackTraceElement st : (new Throwable()).getStackTrace()) {
278 | if (classname.equals(st.getClassName()) || methods.contains(st.getMethodName())) {
279 | continue;
280 | } else {
281 | int b = st.getClassName().lastIndexOf(".") + 1;
282 | String tag = st.getClassName().substring(b);
283 | String message = tag + "->" + st.getMethodName() + "():" + st.getLineNumber() + "->" + msg;
284 | return message;
285 | }
286 |
287 | }
288 | } catch (Exception e) {
289 | LogUtil.e(e);
290 | }
291 | return msg;
292 | }
293 |
294 | /**
295 | * Log.d "mobopush"
296 | *
297 | * @param value Message
298 | *
299 | * @since 2014-7-1
300 | */
301 | public static void p(String value) {
302 | d("mobopush", value);
303 | }
304 |
305 | }
306 |
--------------------------------------------------------------------------------
/app/app.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | generateDebugSources
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/core/RxYoutube.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube.core;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 | import java.util.Scanner;
9 | import java.util.regex.Matcher;
10 | import java.util.regex.Pattern;
11 |
12 | import org.apache.http.HttpResponse;
13 | import org.apache.http.client.methods.HttpGet;
14 | import org.apache.http.impl.client.DefaultHttpClient;
15 | import org.apache.http.util.EntityUtils;
16 | import org.json.JSONObject;
17 |
18 | import com.squareup.duktape.Duktape;
19 |
20 | import android.text.TextUtils;
21 | import android.util.Log;
22 | import dodola.downtube.core.entity.FmtStreamMap;
23 | import rx.Observable;
24 | import rx.Subscriber;
25 | import rx.android.schedulers.AndroidSchedulers;
26 | import rx.functions.Func1;
27 | import rx.schedulers.Schedulers;
28 |
29 | /**
30 | * Created by sunpengfei on 15/11/9.
31 | */
32 | public class RxYoutube {
33 | public static final String BASEURL = "http://www.youtube.com/";
34 | public static final String WATCHV = "http://www.youtube.com/watch?v=%s";
35 | private static final String USERAGENT = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
36 | private static final String JSPLAYER = "ytplayer\\.config\\s*=\\s*([^\\n]+);";
37 | private static final String FUNCCALL = "([$\\w]+)=([$\\w]+)\\(((?:\\w+,?)+)\\)$";
38 | private static final String OBJCALL = "([$\\w]+).([$\\w]+)\\(((?:\\w+,?)+)\\)$";
39 | private static final String[] REGEX_PRE =
40 | {"*", ".", "?", "+", "$", "^", "[", "]", "(", ")", "{", "}", "|", "\\", "/"};
41 |
42 | public static void fetchYoutube(final String vid, Subscriber> resultSubscriber) {
43 |
44 | Observable.create(new Observable.OnSubscribe() {
45 | @Override
46 | public void call(Subscriber super String> subscriber) {
47 | //下载youtube播放页面
48 |
49 | String watchUrl = String.format(WATCHV, vid);
50 | DefaultHttpClient client = new DefaultHttpClient();
51 | HttpGet getData = new HttpGet(watchUrl);
52 | getData.setHeader("User-Agent", USERAGENT);
53 | HttpResponse execute;
54 | try {
55 | execute = client.execute(getData);
56 | String pageContent = EntityUtils.toString(execute.getEntity(), "utf-8");
57 | subscriber.onNext(pageContent);
58 | subscriber.onCompleted();
59 | } catch (Exception ex) {
60 | subscriber.onError(ex);
61 | }
62 |
63 | }
64 | }).map(new Func1>() {
65 | @Override
66 | public List call(String pageContent) {
67 | //转换成StreamMap
68 |
69 | try {
70 | Pattern jsPattern = Pattern.compile(JSPLAYER, Pattern.MULTILINE);
71 | Matcher matcher = jsPattern.matcher(pageContent);
72 | if (matcher.find()) {
73 | JSONObject ytplayerConfig = new JSONObject(matcher.group(1));
74 | JSONObject args = ytplayerConfig.getJSONObject("args");
75 |
76 | String html5playerJS = ytplayerConfig.getJSONObject("assets").getString("js");
77 | if (html5playerJS.startsWith("//")) {
78 | html5playerJS = "http:" + html5playerJS;
79 | } else if (html5playerJS.startsWith("/")) {
80 | html5playerJS = BASEURL + html5playerJS;
81 | }
82 |
83 | Log.d("Html5PlayerJS", "htmljs:" + html5playerJS);
84 | String fmtStream = args.getString("url_encoded_fmt_stream_map");
85 |
86 | String[] fmtArray = fmtStream.split(",");
87 | // 数据格式如下
88 |
89 | List streamMaps = new ArrayList();
90 | for (String fmt : fmtArray) {
91 | FmtStreamMap parseFmtStreamMap = YoutubeUtils.parseFmtStreamMap(new Scanner(fmt), "utf-8");
92 | parseFmtStreamMap.html5playerJS = html5playerJS;
93 | parseFmtStreamMap.videoid = args.optString("video_id");
94 | parseFmtStreamMap.title = args.optString("title");
95 | if (parseFmtStreamMap.resolution != null) {
96 | streamMaps.add(parseFmtStreamMap);
97 | }
98 | }
99 |
100 | String adaptiveStream = args.getString("adaptive_fmts");
101 |
102 | String[] adaptiveStreamArray = adaptiveStream.split(",");
103 | // 数据格式如下
104 |
105 | for (String fmt : adaptiveStreamArray) {
106 | FmtStreamMap parseFmtStreamMap = YoutubeUtils.parseFmtStreamMap(new Scanner(fmt), "utf-8");
107 | parseFmtStreamMap.html5playerJS = html5playerJS;
108 | parseFmtStreamMap.videoid = args.optString("video_id");
109 | parseFmtStreamMap.title = args.optString("title");
110 | if (parseFmtStreamMap.resolution != null) {
111 | streamMaps.add(parseFmtStreamMap);
112 | }
113 | }
114 |
115 | return streamMaps;
116 | }
117 | } catch (Exception ex) {
118 | Observable.error(ex);
119 | }
120 | return null;
121 | }
122 | }).subscribeOn(Schedulers.io())
123 | .observeOn(AndroidSchedulers.mainThread())
124 | .subscribe(resultSubscriber);
125 | }
126 |
127 | public static void parseDownloadUrl(final FmtStreamMap fmtStreamMap, Subscriber resultSubscriber) {
128 | Observable.create(new Observable.OnSubscribe() {
129 | @Override
130 | public void call(Subscriber super String> subscriber) {
131 | String downloadUrl = null;
132 | if (!TextUtils.isEmpty(fmtStreamMap.sig)) {
133 | String sig = fmtStreamMap.sig;
134 | downloadUrl = String.format("%s&signature=%s", fmtStreamMap.url, sig);
135 | } else {
136 | String jsContent = YoutubeUtils.getContent(fmtStreamMap.html5playerJS);
137 | downloadUrl = (decipher(jsContent, fmtStreamMap));
138 | }
139 | subscriber.onNext(downloadUrl);
140 | subscriber.onCompleted();
141 | }
142 | }).subscribeOn(Schedulers.io())
143 | .observeOn(AndroidSchedulers.mainThread())
144 | .subscribe(resultSubscriber);
145 | }
146 |
147 | private static String decipher(String jsContent, FmtStreamMap fmtStreamMap) {
148 | String f1 =
149 | YoutubeUtils.getRegexString(jsContent, "\\w+\\.sig\\|\\|([$a-zA-Z]+)\\([$a-zA-Z]+\\ .[$a-zA-Z]+\\)");
150 | if (TextUtils.isEmpty(f1)) {
151 | f1 = YoutubeUtils
152 | .getRegexString(jsContent,
153 | "\\w+\\.sig.*?\\?.*&&\\w+\\.set\\(\\\"signature\\\",([$a-zA-Z]+)\\([$a-zA-Z]+\\"
154 | + ".[$a-zA-Z]+\\)\\)");
155 | }
156 | String finalF1 = f1;
157 |
158 | for (String aREGEX_PRE : REGEX_PRE) {
159 |
160 | if (f1.contains(aREGEX_PRE)) {
161 | finalF1 = "\\" + f1;
162 | break;
163 | }
164 | }
165 | String f1def =
166 | YoutubeUtils.getRegexString(jsContent, String.format(
167 | "((function\\s+%s|[{;,]%s\\s*=\\s*function|var\\s+%s\\s*=\\s*function\\s*)\\([^)]*\\)"
168 | + "\\s*\\{[^\\{]+\\})",
169 | finalF1, finalF1, finalF1));
170 |
171 | if (f1def.startsWith(",")) {
172 | f1def = f1def.replaceFirst(",", "");
173 | }
174 |
175 | StringBuilder functionSb = new StringBuilder();
176 | trJs(f1def, jsContent, functionSb);
177 |
178 | if (functionSb.length() > 0) {
179 | String jsStr = functionSb.toString() + "\n" + String.format("%s('%s')", f1, fmtStreamMap.s);
180 |
181 | Duktape duktape = Duktape.create();
182 | try {
183 | String sig = duktape.evaluate(jsStr);
184 | return String.format("%s&signature=%s", fmtStreamMap.url, sig);
185 | } finally {
186 | duktape.close();
187 | }
188 | }
189 | return null;
190 | }
191 |
192 | private static void trJs(String jsfunction, String jsContent, StringBuilder functionSb) {
193 | // 将js切成几部分
194 | String[] split = jsfunction.split(";");
195 | Pattern funcPattern = Pattern.compile(FUNCCALL);
196 | Pattern objPattern = Pattern.compile(OBJCALL);
197 | Matcher matcher = null;
198 | for (String code : split) {
199 | String innerFuncCall = null;
200 | // 判断是否为obj调用
201 | matcher = objPattern.matcher(code);
202 | if (matcher.matches()) {// obj调用
203 | String strObj, strFuncName, strArgs;
204 | strObj = matcher.group(1);
205 | strFuncName = matcher.group(2);
206 | strArgs = matcher.group(3);
207 | if (!TextUtils.isEmpty(strObj)) {
208 | jsfunction = jsfunction.replace(strObj + ".", "");
209 | }
210 | String objFunction = "(" + strFuncName + "\\s*:\\s*function\\(.*?\\)\\{[^\\{]+\\})";
211 | String f1def = YoutubeUtils.getRegexString(jsContent, objFunction);
212 |
213 | if (!TextUtils.isEmpty(f1def)) {
214 | String objFuncMain = "function ";
215 | f1def = f1def.replace(":function", "");
216 | f1def = f1def.replace("}}", "}");
217 | objFuncMain += f1def;
218 | functionSb.append(objFuncMain);
219 | functionSb.append("\n");
220 | }
221 | }
222 |
223 | matcher = funcPattern.matcher(code);
224 | if (matcher.matches()) {
225 | String strFunName, strArgs;
226 | strFunName = matcher.group(2);
227 | if (!TextUtils.isEmpty(strFunName)) {
228 | strFunName = Pattern.quote(strFunName);
229 | }
230 | strArgs = matcher.group(3);
231 | if (!TextUtils.isEmpty(strArgs)) {
232 | String[] args = strArgs.split(",");
233 | if (args.length == 1) {
234 | innerFuncCall = String.format("(function %s\\(\\w+\\)\\{[^\\{]+\\})", strFunName);
235 | } else {
236 | innerFuncCall = String.format("(function %s\\(", strFunName);
237 | for (int i = 0; i < args.length - 1; ++i) {
238 | innerFuncCall += "\\w+,";
239 | }
240 | innerFuncCall += "\\w+\\)\\{[^\\{]+\\})";
241 | }
242 | }
243 | if (!TextUtils.isEmpty(innerFuncCall)) {
244 |
245 | String f1def = YoutubeUtils.getRegexString(jsContent, innerFuncCall);
246 | functionSb.append(f1def);
247 | functionSb.append("\n");
248 | }
249 | }
250 |
251 | }
252 | functionSb.append(jsfunction);
253 | }
254 |
255 | }
256 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/core/YoutubeUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube.core;
5 |
6 | import java.io.BufferedReader;
7 | import java.io.IOException;
8 | import java.io.InputStream;
9 | import java.io.InputStreamReader;
10 | import java.io.UnsupportedEncodingException;
11 | import java.net.HttpURLConnection;
12 | import java.net.URL;
13 | import java.net.URLConnection;
14 | import java.net.URLDecoder;
15 | import java.util.ArrayList;
16 | import java.util.HashMap;
17 | import java.util.List;
18 | import java.util.Scanner;
19 | import java.util.regex.Matcher;
20 | import java.util.regex.Pattern;
21 |
22 | import org.apache.http.protocol.HTTP;
23 |
24 | import android.text.TextUtils;
25 | import android.util.Log;
26 | import dodola.downtube.core.entity.FmtStreamMap;
27 | import dodola.downtube.core.entity.Resolution;
28 | import dodola.downtube.utils.LogUtil;
29 |
30 | /**
31 | * @Description://youtube工具类
32 | * @Version
33 | */
34 | public class YoutubeUtils {
35 | private static final String PARAMETER_SEPARATOR = "&";
36 | private static final String NAME_VALUE_SEPARATOR = "=";
37 | /**
38 | * 解析地址使用的Useragent
39 | */
40 | public static final String USERAGENT = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
41 |
42 | public static List playResolutions = new ArrayList();
43 |
44 | static {
45 | playResolutions.add(new Resolution("17", "176x144", "3gp", "normal", ResolutionNote.LHD));
46 | playResolutions.add(new Resolution("36", "320x240", "3gp", "normal", ResolutionNote.LHD));
47 | playResolutions.add(new Resolution("18", "640x360", "mp4", "normal", ResolutionNote.MHD));
48 | playResolutions.add(new Resolution("242", "360x240", "webm", "normal", ResolutionNote.LHD));
49 | playResolutions.add(new Resolution("242", "360x240", "webm", "normal", ResolutionNote.LHD));
50 | playResolutions.add(new Resolution("243", "480x360", "webm", "normal", ResolutionNote.MHD));
51 | playResolutions.add(new Resolution("243", "480x360", "webm", "normal", ResolutionNote.MHD));
52 | playResolutions.add(new Resolution("43", "640x360", "webm", "normal", ResolutionNote.MHD));
53 | playResolutions.add(new Resolution("244", "640x480", "webm", "normal", ResolutionNote.MHD));
54 | playResolutions.add(new Resolution("245", "640x480", "webm", "normal", ResolutionNote.MHD));
55 | playResolutions.add(new Resolution("167", "640x480", "webm", "video", ResolutionNote.MHD));
56 | playResolutions.add(new Resolution("246", "640x480", "webm", "normal", ResolutionNote.MHD));
57 | playResolutions.add(new Resolution("247", "720x480", "webm", "normal", ResolutionNote.MHD));
58 | playResolutions.add(new Resolution("44", "854x480", "webm", "normal", ResolutionNote.MHD));
59 | playResolutions.add(new Resolution("168", "854x480", "webm", "video", ResolutionNote.MHD));
60 | }
61 |
62 | /**
63 | * 下载用的分辨率和格式
64 | */
65 | public static HashMap Resolutions = new HashMap();
66 |
67 | static {
68 | Resolutions.put("5", new Resolution("5", "400x240", "flv", "normal", ResolutionNote.LHD));//
69 | Resolutions.put("6", new Resolution("6", "450x270", "flv", "normal", ResolutionNote.MHD));
70 | Resolutions.put("17", new Resolution("17", "176x144", "3gp", "normal", ResolutionNote.LHD));//
71 | Resolutions.put("18", new Resolution("18", "640x360", "mp4", "normal", ResolutionNote.MHD));
72 | Resolutions.put("22", new Resolution("22", "1280x720", "mp4", "normal", ResolutionNote.HD));
73 | Resolutions.put("34", new Resolution("34", "640x360", "flv", "normal", ResolutionNote.MHD));
74 | Resolutions.put("35", new Resolution("35", "854x480", "flv", "normal", ResolutionNote.MHD));
75 | Resolutions.put("36", new Resolution("36", "320x240", "3gp", "normal", ResolutionNote.LHD));//
76 | Resolutions.put("37", new Resolution("37", "1920x1080", "mp4", "normal", ResolutionNote.XLHD));
77 | Resolutions.put("38", new Resolution("38", "4096x3072", "mp4", "normal", ResolutionNote.XLHD));
78 | Resolutions.put("43", new Resolution("43", "640x360", "webm", "normal", ResolutionNote.MHD));
79 | Resolutions.put("44", new Resolution("44", "854x480", "webm", "normal", ResolutionNote.MHD));
80 | Resolutions.put("45", new Resolution("45", "1280x720", "webm", "normal", ResolutionNote.HD));
81 | Resolutions.put("46", new Resolution("46", "1920x1080", "webm", "normal", ResolutionNote.XLHD));
82 | Resolutions.put("167", new Resolution("167", "640x480", "webm", "video", ResolutionNote.MHD));
83 | Resolutions.put("168", new Resolution("168", "854x480", "webm", "video", ResolutionNote.MHD));
84 | Resolutions.put("169", new Resolution("169", "1280x720", "webm", "video", ResolutionNote.HD));
85 | Resolutions.put("170", new Resolution("170", "1920x1080", "webm", "video", ResolutionNote.XLHD));
86 | Resolutions.put("242", new Resolution("242", "360x240", "webm", "normal", ResolutionNote.LHD));//
87 | Resolutions.put("243", new Resolution("243", "480x360", "webm", "normal", ResolutionNote.MHD));
88 | Resolutions.put("244", new Resolution("244", "640x480", "webm", "normal", ResolutionNote.MHD));
89 | Resolutions.put("245", new Resolution("245", "640x480", "webm", "normal", ResolutionNote.MHD));
90 | Resolutions.put("246", new Resolution("246", "640x480", "webm", "normal", ResolutionNote.MHD));
91 | Resolutions.put("247", new Resolution("247", "720x480", "webm", "normal", ResolutionNote.MHD));
92 |
93 | Resolutions.put("82", new Resolution("82", "360p", "mp4", "normal", ResolutionNote.MHD));
94 | Resolutions.put("83", new Resolution("83", "480p", "mp4", "normal", ResolutionNote.MHD));
95 | Resolutions.put("84", new Resolution("84", "720p", "mp4", "normal", ResolutionNote.MHD));
96 | Resolutions.put("85", new Resolution("85", "1080p", "mp4", "normal", ResolutionNote.MHD));
97 | Resolutions.put("100", new Resolution("100", "360p", "webm", "normal", ResolutionNote.MHD));
98 | Resolutions.put("101", new Resolution("101", "480p", "webm", "normal", ResolutionNote.MHD));
99 | Resolutions.put("102", new Resolution("102", "720p", "webm", "normal", ResolutionNote.MHD));
100 |
101 | // 直播流 ,暂不增加
102 | // '92': {'ext': 'mp4', 'height': 240, 'format_note': 'HLS',
103 | // 'preference': -10},
104 | // '93': {'ext': 'mp4', 'height': 360, 'format_note': 'HLS',
105 | // 'preference': -10},
106 | // '94': {'ext': 'mp4', 'height': 480, 'format_note': 'HLS',
107 | // 'preference': -10},
108 | // '95': {'ext': 'mp4', 'height': 720, 'format_note': 'HLS',
109 | // 'preference': -10},
110 | // '96': {'ext': 'mp4', 'height': 1080, 'format_note': 'HLS',
111 | // 'preference': -10},
112 | // '132': {'ext': 'mp4', 'height': 240, 'format_note': 'HLS',
113 | // 'preference': -10},
114 | // '151': {'ext': 'mp4', 'height': 72, 'format_note': 'HLS',
115 | // 'preference': -10},
116 |
117 | // Resolutions.put("133", new Resolution("133", "240p", "mp4", "normal", ResolutionNote.MHD));
118 | // Resolutions.put("134", new Resolution("134", "360p", "mp4", "normal", ResolutionNote.MHD));
119 | // Resolutions.put("135", new Resolution("135", "480p", "mp4", "normal", ResolutionNote.MHD));
120 | // Resolutions.put("136", new Resolution("136", "720p", "mp4", "normal", ResolutionNote.MHD));
121 | // Resolutions.put("137", new Resolution("137", "1080p", "mp4", "normal", ResolutionNote.MHD));
122 | // Resolutions.put("138", new Resolution("138", "2160p", "mp4", "normal", ResolutionNote.MHD));
123 | // Resolutions.put("160", new Resolution("160", "144p", "mp4", "normal", ResolutionNote.MHD));
124 | // Resolutions.put("264", new Resolution("264", "1440p", "mp4", "normal", ResolutionNote.MHD));
125 | // Resolutions.put("298", new Resolution("298", "720p", "mp4", "normal", ResolutionNote.MHD));
126 | // Resolutions.put("299", new Resolution("299", "1080p", "mp4", "normal", ResolutionNote.MHD));
127 | // Resolutions.put("266", new Resolution("266", "2160p", "mp4", "normal", ResolutionNote.MHD));
128 | // m4a的音频
129 | Resolutions.put("139", new Resolution("139", "Audio only", "m4a", "normal", ResolutionNote.MHD));
130 | Resolutions.put("140", new Resolution("140", "Audio only", "m4a", "normal", ResolutionNote.MHD));
131 | Resolutions.put("141", new Resolution("141", "Audio only", "m4a", "normal", ResolutionNote.MHD));
132 |
133 | // Resolutions.put("167", new Resolution("167", "360p", "webm", "normal", ResolutionNote.MHD));
134 | // Resolutions.put("168", new Resolution("168", "480p", "webm", "normal", ResolutionNote.MHD));
135 | // Resolutions.put("169", new Resolution("169", "720p", "webm", "normal", ResolutionNote.MHD));
136 | // Resolutions.put("170", new Resolution("170", "1080p", "webm", "normal", ResolutionNote.MHD));
137 | // Resolutions.put("218", new Resolution("218", "480p", "webm", "normal", ResolutionNote.MHD));
138 | // Resolutions.put("219", new Resolution("219", "480p", "webm", "normal", ResolutionNote.MHD));
139 | // Resolutions.put("278", new Resolution("278", "144p", "webm", "normal", ResolutionNote.MHD));
140 | // Resolutions.put("242", new Resolution("242", "240p", "webm", "normal", ResolutionNote.MHD));
141 | // Resolutions.put("243", new Resolution("243", "360p", "webm", "normal", ResolutionNote.MHD));
142 | // Resolutions.put("244", new Resolution("244", "480p", "webm", "normal", ResolutionNote.MHD));
143 | // Resolutions.put("245", new Resolution("245", "480p", "webm", "normal", ResolutionNote.MHD));
144 | // Resolutions.put("246", new Resolution("246", "480p", "webm", "normal", ResolutionNote.MHD));
145 | // Resolutions.put("247", new Resolution("247", "720p", "webm", "normal", ResolutionNote.MHD));
146 | // Resolutions.put("248", new Resolution("248", "1080p", "webm", "normal", ResolutionNote.MHD));
147 | // Resolutions.put("271", new Resolution("271", "1440p", "webm", "normal", ResolutionNote.MHD));
148 | // Resolutions.put("272", new Resolution("272", "2160p", "webm", "normal", ResolutionNote.MHD));
149 | // Resolutions.put("302", new Resolution("302", "720 p", "webm", "normal", ResolutionNote.MHD));
150 | // Resolutions.put("303", new Resolution("303", "1080p", "webm", "normal", ResolutionNote.MHD));
151 | // Resolutions.put("313", new Resolution("313", "2160p", "webm", "normal", ResolutionNote.MHD));
152 |
153 | Resolutions.put("171", new Resolution("313", "Audio only", "webm", "normal", ResolutionNote.MHD));
154 | Resolutions.put("172", new Resolution("313", "Audio only", "webm", "normal", ResolutionNote.MHD));
155 | }
156 |
157 | /**
158 | * 构造正则表达式对象
159 | *
160 | * @param content
161 | * @param pattern
162 | *
163 | * @return
164 | */
165 | public static String getRegexString(String content, String pattern) {
166 | Pattern p = Pattern.compile(pattern);
167 | Matcher matcher = p.matcher(content);
168 | String group = null;
169 | if (matcher.find()) {
170 | LogUtil.d("group:" + matcher.group(0));
171 | group = matcher.group(1);
172 | }
173 | return group;
174 | }
175 |
176 | /**
177 | * 从youtube url中解析vid
178 | *
179 | * @param url
180 | *
181 | * @return
182 | */
183 | public static String extractVideoId(String url) {
184 | try {
185 | Pattern p = Pattern.compile("(?:^|[^\\w-]+)([\\w-]{11})(?:[^\\w-]+|$)");
186 | Matcher matcher = p.matcher(url);
187 | if (matcher.find()) {
188 | return matcher.group(1);
189 | }
190 | } catch (Exception ex) {
191 | return null;
192 | }
193 | return null;
194 | }
195 |
196 | public static void parse(final HashMap parameters, final Scanner scanner, final String encoding) {
197 | scanner.useDelimiter(PARAMETER_SEPARATOR);
198 | while (scanner.hasNext()) {
199 | final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR);
200 | if (nameValue.length == 0 || nameValue.length > 2) {
201 | throw new IllegalArgumentException("bad parameter");
202 | }
203 |
204 | final String name = decode(nameValue[0], encoding);
205 | String value = null;
206 | if (nameValue.length == 2) {
207 | value = decode(nameValue[1], encoding);
208 | }
209 | parameters.put(name, value);
210 | }
211 | }
212 |
213 | public static FmtStreamMap parseFmtStreamMap(final Scanner scanner, final String encoding) {
214 | FmtStreamMap streamMap = new FmtStreamMap();
215 | scanner.useDelimiter(PARAMETER_SEPARATOR);
216 | while (scanner.hasNext()) {
217 | final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR);
218 | if (nameValue.length == 0 || nameValue.length > 2) {
219 | throw new IllegalArgumentException("bad parameter");
220 | }
221 |
222 | final String name = decode(nameValue[0], encoding);
223 | String value = null;
224 | if (nameValue.length == 2) {
225 | value = decode(nameValue[1], encoding);
226 | }
227 | Log.d("YoutubeUtils", "name:" + name + ",values:" + value);
228 |
229 | if (TextUtils.equals("fallback_host", name)) {
230 | streamMap.fallbackHost = value;
231 | }
232 | if (TextUtils.equals("s", name)) {
233 | streamMap.s = value;
234 | }
235 | if (TextUtils.equals("itag", name)) {
236 | streamMap.itag = value;
237 | }
238 | if (TextUtils.equals("type", name)) {
239 | streamMap.type = value;
240 | }
241 | if (TextUtils.equals("quality", name)) {
242 | streamMap.quality = value;
243 | }
244 | if (TextUtils.equals("url", name)) {
245 | streamMap.url = value;
246 | }
247 | if (TextUtils.equals("sig", name)) {
248 | streamMap.sig = value;
249 | }
250 | if (TextUtils.equals("signature", name)) {
251 |
252 | }
253 | if (!TextUtils.isEmpty(streamMap.itag)) {
254 | streamMap.resolution = Resolutions.get(streamMap.itag);// 下载的格式要比播放的格式多
255 | }
256 | if (streamMap.resolution != null) {
257 | streamMap.extension = streamMap.resolution.format;
258 | }
259 |
260 | }
261 | return streamMap;
262 | }
263 |
264 | /**
265 | * url解码
266 | *
267 | * @param content
268 | * @param encoding
269 | *
270 | * @return
271 | */
272 | public static String decode(final String content, final String encoding) {
273 | try {
274 | return URLDecoder.decode(content, encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
275 | } catch (UnsupportedEncodingException problem) {
276 | throw new IllegalArgumentException(problem);
277 | }
278 | }
279 |
280 | /**
281 | * 获取url里的内容
282 | *
283 | * @param url
284 | *
285 | * @return 解析失败返回 Null
286 | */
287 | // public static String getContent(String url) {
288 | // DefaultHttpClient client = new DefaultHttpClient();
289 | // HttpGet getData = new HttpGet(url);
290 | // getData.setHeader("User-Agent", USERAGENT);
291 | // HttpResponse execute;
292 | // String data = null;
293 | // try {
294 | // execute = client.execute(getData);
295 | // HttpEntity entity = execute.getEntity();
296 | // data = EntityUtils.toString(entity, "utf-8");
297 | // } catch (IOException e) {
298 | // LogUtil.e(e);
299 | // }
300 | //
301 | // return data;
302 | // }
303 | public static String getContent(String url) {
304 | InputStream inputStream = null;
305 | InputStreamReader inputStreamReader = null;
306 | BufferedReader reader = null;
307 | StringBuilder resultBuffer = new StringBuilder();
308 | String tempLine = null;
309 | try {
310 | URL localURL = new URL(url);
311 |
312 | URLConnection connection = openConnection(localURL);
313 | HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
314 |
315 | if (httpURLConnection.getResponseCode() >= 300) {
316 | throw new Exception(
317 | "HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
318 | }
319 |
320 | inputStream = httpURLConnection.getInputStream();
321 | inputStreamReader = new InputStreamReader(inputStream);
322 | reader = new BufferedReader(inputStreamReader);
323 |
324 | while ((tempLine = reader.readLine()) != null) {
325 | resultBuffer.append(tempLine);
326 | }
327 |
328 | } catch (Exception ex) {
329 | } finally {
330 |
331 | if (reader != null) {
332 | try {
333 | reader.close();
334 | } catch (IOException e) {
335 | e.printStackTrace();
336 | }
337 | }
338 |
339 | if (inputStreamReader != null) {
340 | try {
341 | inputStreamReader.close();
342 | } catch (IOException e) {
343 | e.printStackTrace();
344 | }
345 | }
346 |
347 | if (inputStream != null) {
348 | try {
349 | inputStream.close();
350 | } catch (IOException e) {
351 | e.printStackTrace();
352 | }
353 | }
354 |
355 | }
356 |
357 | return resultBuffer.toString();
358 | }
359 |
360 | private static URLConnection openConnection(URL localURL) throws IOException {
361 | URLConnection connection;
362 | connection = localURL.openConnection();
363 | return connection;
364 | }
365 |
366 | }
367 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/downtube/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.downtube;
5 |
6 | import java.lang.reflect.Field;
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | import android.app.Dialog;
11 | import android.app.DownloadManager;
12 | import android.app.ProgressDialog;
13 | import android.content.DialogInterface;
14 | import android.content.Intent;
15 | import android.content.pm.ActivityInfo;
16 | import android.graphics.Bitmap;
17 | import android.net.Uri;
18 | import android.os.Build;
19 | import android.os.Bundle;
20 | import android.os.Environment;
21 | import android.support.design.widget.FloatingActionButton;
22 | import android.support.design.widget.NavigationView;
23 | import android.support.v4.view.GravityCompat;
24 | import android.support.v4.widget.DrawerLayout;
25 | import android.support.v7.app.ActionBarDrawerToggle;
26 | import android.support.v7.app.AlertDialog;
27 | import android.support.v7.app.AppCompatActivity;
28 | import android.support.v7.widget.Toolbar;
29 | import android.text.TextUtils;
30 | import android.view.LayoutInflater;
31 | import android.view.Menu;
32 | import android.view.MenuItem;
33 | import android.view.View;
34 | import android.view.ViewGroup;
35 | import android.webkit.ConsoleMessage;
36 | import android.webkit.WebChromeClient;
37 | import android.webkit.WebSettings;
38 | import android.webkit.WebView;
39 | import android.webkit.WebViewClient;
40 | import android.widget.FrameLayout;
41 | import android.widget.ProgressBar;
42 | import android.widget.Toast;
43 | import android.widget.VideoView;
44 | import dodola.downtube.core.RxYoutube;
45 | import dodola.downtube.core.YoutubeUtils;
46 | import dodola.downtube.core.entity.FmtStreamMap;
47 | import dodola.downtube.utils.LogUtil;
48 | import dodola.downtube.view.YouTuBeWebView;
49 | import rx.Subscriber;
50 |
51 | public class MainActivity extends AppCompatActivity
52 | implements NavigationView.OnNavigationItemSelectedListener {
53 |
54 | private ProgressDialog mProgressDialog;
55 | private DownloadManager downloadManager;
56 | private YouTuBeWebView myWebView;
57 | private WebChromeClient mWebChromeClient;
58 | private VideoView mVideoView = null;
59 | private WebChromeClient.CustomViewCallback mCustomViewCallback = null;
60 | private String mVideoId;
61 | private String mCurrentUrl;
62 | private LayoutInflater layoutInflater;
63 | private View videoView;
64 | public static final String YOUTUBE = "https://m.youtube.com/";
65 | private String loadUrl = YOUTUBE;
66 | private FloatingActionButton fab;
67 | private ProgressBar mLoadingProgressBar;
68 |
69 | @Override
70 | protected void onCreate(Bundle savedInstanceState) {
71 | super.onCreate(savedInstanceState);
72 | setContentView(R.layout.activity_main);
73 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
74 | setSupportActionBar(toolbar);
75 | myWebView = (YouTuBeWebView) findViewById(R.id.webview);
76 | fab = (FloatingActionButton) findViewById(R.id.fab);
77 | mLoadingProgressBar = (ProgressBar) findViewById(R.id.progress);
78 |
79 | fab.setOnClickListener(new View.OnClickListener() {
80 | @Override
81 | public void onClick(View view) {
82 | showWaitDialog();
83 | //调用解析
84 | RxYoutube.fetchYoutube(mVideoId, new Subscriber>() {
85 | @Override
86 | public void onCompleted() {
87 | dismissWaitDialog();
88 | }
89 |
90 | @Override
91 | public void onError(Throwable e) {
92 | dismissWaitDialog();
93 | }
94 |
95 | @Override
96 | public void onNext(List fmtStreamMaps) {
97 | dismissWaitDialog();
98 | showDialog(fmtStreamMaps);
99 | }
100 | });
101 | }
102 | });
103 | downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
104 | DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
105 | ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
106 | this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
107 | drawer.setDrawerListener(toggle);
108 | toggle.syncState();
109 |
110 | NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
111 | navigationView.setNavigationItemSelectedListener(this);
112 |
113 | initWebView();
114 | }
115 |
116 | @Override
117 | public void onBackPressed() {
118 | DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
119 | if (drawer.isDrawerOpen(GravityCompat.START)) {
120 | drawer.closeDrawer(GravityCompat.START);
121 | } else {
122 | // super.onBackPressed();
123 | if (myWebView.canGoBack()) {
124 | myWebView.goBack();
125 | } else {
126 | super.onBackPressed();
127 | }
128 | }
129 | }
130 |
131 | private void showDialog(final List result) {
132 | if (result != null && result.size() > 0) {
133 | List streamArrays = new ArrayList();
134 | for (int i = 0; i < result.size(); i++) {
135 | final String streamType = result.get(i).getStreamString();
136 | streamArrays.add(streamType);
137 | }
138 | String[] item1 = new String[streamArrays.size()];
139 | streamArrays.toArray(item1);
140 |
141 | Dialog alertDialog = new AlertDialog.Builder(this).
142 | setTitle("选择下载类型").
143 | setIcon(R.mipmap.ic_launcher)
144 | .setItems(item1, new DialogInterface.OnClickListener() {
145 | @Override
146 | public void onClick(DialogInterface dialog, int which) {
147 | final FmtStreamMap fmtStreamMap = result.get(which);
148 | RxYoutube.parseDownloadUrl(fmtStreamMap, new Subscriber() {
149 | @Override
150 | public void onCompleted() {
151 | dismissWaitDialog();
152 | }
153 |
154 | @Override
155 | public void onError(Throwable e) {
156 | dismissWaitDialog();
157 | e.printStackTrace();
158 | Toast.makeText(MainActivity.this, "Download Error", Toast.LENGTH_SHORT).show();
159 | }
160 |
161 | @Override
162 | public void onNext(String downloadUrl) {
163 | dismissWaitDialog();
164 |
165 | //调用系统下载
166 | String fileName = fmtStreamMap.title + "." + fmtStreamMap.extension;
167 | Uri uri = Uri.parse(downloadUrl);
168 | DownloadManager.Request request = new DownloadManager.Request(uri);
169 | request.setDestinationInExternalFilesDir(MainActivity.this,
170 | Environment.DIRECTORY_MOVIES, fileName);
171 | downloadManager.enqueue(request);
172 | }
173 | });
174 |
175 | }
176 | }).
177 | setNegativeButton("取消", new DialogInterface.OnClickListener() {
178 |
179 | @Override
180 | public void onClick(DialogInterface dialog, int which) {
181 | }
182 | }).
183 | create();
184 | alertDialog.show();
185 | }
186 | }
187 |
188 | protected void showWaitDialog() {
189 | if (mProgressDialog == null) {
190 | mProgressDialog = ProgressDialog.show(this, "Loading...", "Please wait...", true, true);
191 | mProgressDialog.setCanceledOnTouchOutside(false);
192 | mProgressDialog.setOnCancelListener(new ProgressDialog.OnCancelListener() {
193 | @Override
194 | public void onCancel(DialogInterface dialog) {
195 | }
196 | });
197 | } else {
198 | mProgressDialog.show();
199 | }
200 | }
201 |
202 | private void dismissWaitDialog() {
203 | if (mProgressDialog != null) {
204 | mProgressDialog.dismiss();
205 | }
206 | }
207 |
208 | @Override
209 | public boolean onCreateOptionsMenu(Menu menu) {
210 | // Inflate the menu; this adds items to the action bar if it is present.
211 | getMenuInflater().inflate(R.menu.main, menu);
212 | return true;
213 | }
214 |
215 | @Override
216 | public boolean onOptionsItemSelected(MenuItem item) {
217 | // Handle action bar item clicks here. The action bar will
218 | // automatically handle clicks on the Home/Up button, so long
219 | // as you specify a parent activity in AndroidManifest.xml.
220 | int id = item.getItemId();
221 |
222 | //noinspection SimplifiableIfStatement
223 | if (id == R.id.action_settings) {
224 | return true;
225 | }
226 |
227 | return super.onOptionsItemSelected(item);
228 | }
229 |
230 | @SuppressWarnings("StatementWithEmptyBody")
231 | @Override
232 | public boolean onNavigationItemSelected(MenuItem item) {
233 | // Handle navigation view item clicks here.
234 | int id = item.getItemId();
235 |
236 | if (id == R.id.nav_camera) {
237 | // Handle the camera action
238 | } else if (id == R.id.nav_gallery) {
239 |
240 | } else if (id == R.id.nav_slideshow) {
241 |
242 | } else if (id == R.id.nav_manage) {
243 |
244 | } else if (id == R.id.nav_share) {
245 |
246 | } else if (id == R.id.nav_send) {
247 |
248 | }
249 |
250 | DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
251 | drawer.closeDrawer(GravityCompat.START);
252 | return true;
253 | }
254 |
255 | /**
256 | * 初始化WebView
257 | */
258 | private void initWebView() {
259 | WebSettings webSettings = myWebView.getSettings();
260 | webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
261 | webSettings.setAppCacheEnabled(false);
262 | final String USER_AGENT_STRING = myWebView.getSettings().getUserAgentString() + " Rong/2.0";
263 | webSettings.setUserAgentString(USER_AGENT_STRING);
264 | webSettings.setSupportZoom(false);
265 | webSettings.setPluginState(WebSettings.PluginState.ON);
266 | webSettings.setLoadWithOverviewMode(true);
267 | webSettings.setJavaScriptEnabled(true);
268 | mWebChromeClient = new WebChromeClient() {
269 |
270 | @Override
271 | public void onProgressChanged(WebView view, int newProgress) {
272 | super.onProgressChanged(view, newProgress);
273 | if (newProgress >= 90) {
274 | mLoadingProgressBar.setVisibility(View.GONE);
275 | } else {
276 | if (mLoadingProgressBar.getVisibility() == View.GONE) {
277 | mLoadingProgressBar.setVisibility(View.VISIBLE);
278 | }
279 | mLoadingProgressBar.setProgress(newProgress);
280 | }
281 | }
282 |
283 | @Override
284 | public View getVideoLoadingProgressView() {
285 | try {
286 | myWebView.requestFocus();
287 | } catch (Exception ex) {
288 | LogUtil.e(ex);
289 | }
290 | if (layoutInflater == null) {
291 | layoutInflater = LayoutInflater.from(MainActivity.this);
292 | }
293 | View loadingView = layoutInflater.inflate(R.layout.tube_loading, null);
294 | return loadingView;
295 | }
296 |
297 | @Override
298 | public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
299 | return super.onConsoleMessage(consoleMessage);
300 | }
301 |
302 | @Override
303 | public void onHideCustomView() {
304 | hideFullscreen();
305 | }
306 |
307 | @Override
308 | public void onShowCustomView(View view, CustomViewCallback callback) {
309 | // mRootView.addView(view);
310 | LogUtil.d("=====onShowCustomView=====");
311 | mCustomViewCallback = callback;
312 |
313 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
314 | FrameLayout frame = (FrameLayout) view;
315 |
316 | if (view instanceof FrameLayout) {
317 |
318 | if (frame.getFocusedChild() instanceof VideoView) {
319 |
320 | mVideoView = (VideoView) frame.getFocusedChild();
321 |
322 | try {
323 | Field field = VideoView.class.getDeclaredField("mUri");
324 | field.setAccessible(true);
325 | Uri videouri = (Uri) field.get(mVideoView);
326 |
327 | Intent intentv = new Intent(Intent.ACTION_VIEW);
328 | intentv.setDataAndType(videouri, "video/*");
329 | startActivity(intentv);
330 | } catch (Exception e) {
331 | LogUtil.e(e);
332 | }
333 | }
334 | }
335 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
336 | changeFullscreen();
337 | videoView = view;
338 | ((ViewGroup) getWindow().getDecorView()).addView(videoView);
339 |
340 | } else {
341 | try {
342 | Field localField2 = Class.forName("android.webkit.HTML5VideoFullScreen$VideoSurfaceView")
343 | .getDeclaredField("this$0");
344 | localField2.setAccessible(true);
345 | Object localObject = localField2.get(((FrameLayout) view).getFocusedChild());
346 | Field localField3 = localField2.getType().getSuperclass().getDeclaredField("mUri");
347 | localField3.setAccessible(true);
348 | Uri localUri2 = (Uri) localField3.get(localObject);
349 | Intent intentv = new Intent(Intent.ACTION_VIEW);
350 | intentv.setDataAndType(localUri2, "video/*");
351 | startActivity(intentv);
352 | } catch (Exception localException1) {
353 | LogUtil.e(localException1);
354 | }
355 |
356 | }
357 | }
358 | };
359 | myWebView.setWebChromeClient(mWebChromeClient);
360 | myWebView.loadUrl(loadUrl);
361 | myWebView.setWebViewClient(new WebViewClient() {
362 | @Override
363 | public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
364 | if (myWebView == null || TextUtils.isEmpty(url)) {
365 | return;
366 | }
367 |
368 | try {
369 | LogUtil.d("=======doUpdateVisitedHistory=======");
370 | updateButtonUI();
371 | } catch (Exception ex) {
372 | LogUtil.e(ex);
373 | }
374 | super.doUpdateVisitedHistory(view, url, isReload);
375 | }
376 |
377 | @Override
378 | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
379 | super.onReceivedError(view, errorCode, description, failingUrl);
380 | LogUtil.d("========onReceivedError========");
381 | if ((errorCode == WebViewClient.ERROR_HOST_LOOKUP) || (errorCode == WebViewClient.ERROR_TIMEOUT)
382 | || (errorCode == WebViewClient.ERROR_CONNECT)) {
383 | myWebView.loadData("", "text/html", "utf-8");
384 | }
385 | }
386 |
387 | @Override
388 | public void onPageFinished(WebView view, String url) {
389 | super.onPageFinished(view, url);
390 |
391 | }
392 |
393 | @Override
394 | public void onPageStarted(WebView view, String url, Bitmap favicon) {
395 | super.onPageStarted(view, url, favicon);
396 |
397 | }
398 |
399 | @Override
400 | public boolean shouldOverrideUrlLoading(WebView view, String url) {
401 | LogUtil.d("======shouldOverrideUrlLoading=====" + url);
402 | view.loadUrl(url);
403 | return super.shouldOverrideUrlLoading(view, url);
404 | }
405 | });
406 |
407 | myWebView.setDf(new YouTuBeWebView.DisplayFinish() {
408 |
409 | @Override
410 | public void After() {
411 |
412 | if (myWebView != null) {
413 | String urlx = myWebView.getUrl();
414 | if (urlx != null) {
415 | if (!TextUtils.isEmpty(urlx)) {
416 | mVideoId = YoutubeUtils.extractVideoId(urlx);
417 | }
418 | if (!TextUtils.isEmpty(mVideoId)) {
419 | mCurrentUrl = urlx;
420 | }
421 | updateButtonUI();
422 | }
423 | }
424 | }
425 |
426 | });
427 | }
428 |
429 | public void hideFullscreen() {
430 | if (mCustomViewCallback != null) {
431 | mCustomViewCallback.onCustomViewHidden();
432 | mCustomViewCallback = null;
433 | }
434 |
435 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
436 | if (mVideoView != null) {
437 | mVideoView = null;
438 | }
439 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
440 | if (videoView != null) {
441 | ((ViewGroup) getWindow().getDecorView()).removeView(videoView);
442 | }
443 | videoView = null;
444 | exitFullscreen();
445 | }
446 | }
447 |
448 | private boolean mIsFullscreen;
449 |
450 | public boolean isFullScreen() {
451 | return mIsFullscreen;
452 | }
453 |
454 | public void changeFullscreen() {
455 | mIsFullscreen = true;
456 | if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
457 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
458 | }
459 | }
460 |
461 | private void exitFullscreen() {
462 | mIsFullscreen = false;
463 | if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
464 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
465 | }
466 | }
467 |
468 | private void updateButtonUI() {
469 | if (myWebView == null) {
470 | return;
471 | }
472 | String urlx = myWebView.getUrl();
473 | if (!TextUtils.isEmpty(urlx)) {
474 | mVideoId = YoutubeUtils.extractVideoId(urlx);
475 | LogUtil.d("mVideoId:" + mVideoId);
476 | }
477 | if (!TextUtils.isEmpty(mVideoId)) {
478 | mCurrentUrl = urlx;
479 | }
480 | fab.setEnabled(!TextUtils.isEmpty(mVideoId));
481 | }
482 | }
483 |
--------------------------------------------------------------------------------