Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkMediaPlayerResourceLoadingFilter.java
1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.xwalk.core;
6
7 import android.content.Context;
8 import android.content.res.AssetFileDescriptor;
9 import android.media.MediaPlayer;
10 import android.net.Uri;
11
12 import org.chromium.media.MediaPlayerBridge;
13
14 import java.io.File;
15 import java.util.HashMap;
16 import java.util.List;
17
18 /**
19  * This class inherits from MediaPlayerBridge.ResourceLoadingFilter to
20  * customize the resource loading process in xwalk.
21  */
22
23 public class XWalkMediaPlayerResourceLoadingFilter extends
24         MediaPlayerBridge.ResourceLoadingFilter {
25     @Override
26     public boolean shouldOverrideResourceLoading(MediaPlayer mediaPlayer,
27             Context context, Uri uri) {
28         if (uri.getScheme().equals(AndroidProtocolHandler.APP_SCHEME)) {
29             uri = AndroidProtocolHandler.appUriToFileUri(uri);
30         }
31
32         String scheme = uri.getScheme();
33
34         if (!scheme.equals(AndroidProtocolHandler.FILE_SCHEME)) return false;
35
36         try {
37             AssetFileDescriptor afd =
38                     context.getAssets().openFd(AndroidProtocolHandler.getAssetPath(uri));
39             mediaPlayer.setDataSource(
40                     afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
41
42             return true;
43         } catch (Exception e) {
44             return false;
45         }
46     }
47 }