aboutsummaryrefslogtreecommitdiff
path: root/subsonic-android/src/github/daneren2005/subphonic/activity/PlayVideoActivity.java
blob: a393d3a894a4e4ce9fae73bd6ac3cec014422519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2009 (C) Sindre Mehus
 */

package github.daneren2005.subphonic.activity;

import java.lang.reflect.Method;

import android.app.Activity;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import github.daneren2005.subphonic.R;
import github.daneren2005.subphonic.service.MusicServiceFactory;
import github.daneren2005.subphonic.util.Constants;
import github.daneren2005.subphonic.util.Util;

/**
 * Plays videos in a web page.
 *
 * @author Sindre Mehus
 */
public final class PlayVideoActivity extends Activity {

    private static final String TAG = PlayVideoActivity.class.getSimpleName();
    private WebView webView;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        setContentView(R.layout.play_video);

        webView = (WebView) findViewById(R.id.play_video_contents);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setPluginsEnabled(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);

        webView.setWebViewClient(new Client());
        if (bundle != null) {
            webView.restoreState(bundle);
        } else {
            webView.loadUrl(getVideoUrl());
        }

        // Show warning if Flash plugin is not installed.
        if (isFlashPluginInstalled()) {
            Util.toast(this, R.string.play_video_loading, false);
        } else {
            Util.toast(this, R.string.play_video_noplugin, false);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        callHiddenWebViewMethod("onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        callHiddenWebViewMethod("onResume");
    }

    private String getVideoUrl() {
        String id = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_ID);
        return MusicServiceFactory.getMusicService(this).getVideoUrl(this, id);
    }

    @Override
    protected void onSaveInstanceState(Bundle state) {
        webView.saveState(state);
    }

    private void callHiddenWebViewMethod(String name){
        if( webView != null ){
            try {
                Method method = WebView.class.getMethod(name);
                method.invoke(webView);
            } catch (Throwable x) {
                Log.e(TAG, "Failed to invoke " + name, x);
            }
        }
    }

    private boolean isFlashPluginInstalled() {
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo("com.adobe.flashplayer", 0);
            return packageInfo != null;
        } catch (PackageManager.NameNotFoundException x) {
            return false;
        }
    }

    private final class Client extends WebViewClient {

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Util.toast(PlayVideoActivity.this, description);
            Log.e(TAG, "Error: " + description);
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
            Log.d(TAG, "onLoadResource: " + url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Log.d(TAG, "onPageStarted: " + url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.d(TAG, "onPageFinished: " + url);
        }
    }
}