OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. 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.chromium.chromecast.shell; |
| 6 |
| 7 import android.app.Service; |
| 8 import android.content.Intent; |
| 9 import android.media.AudioManager; |
| 10 import android.os.IBinder; |
| 11 import android.widget.Toast; |
| 12 |
| 13 import org.chromium.base.Log; |
| 14 import org.chromium.base.annotations.JNINamespace; |
| 15 import org.chromium.content.browser.ContentView; |
| 16 import org.chromium.content.browser.ContentViewCore; |
| 17 import org.chromium.content_public.browser.WebContents; |
| 18 import org.chromium.ui.base.ViewAndroidDelegate; |
| 19 import org.chromium.ui.base.WindowAndroid; |
| 20 |
| 21 /** |
| 22 * Service for "displaying" a WebContents in CastShell. |
| 23 * <p> |
| 24 * Typically, this class is controlled by CastContentWindowAndroid, which will |
| 25 * bind to this service. |
| 26 */ |
| 27 @JNINamespace("chromecast::shell") |
| 28 public class CastWebContentsService extends Service { |
| 29 private static final String TAG = "cr_CastWebService"; |
| 30 private static final boolean DEBUG = true; |
| 31 |
| 32 private String mInstanceId; |
| 33 private AudioManager mAudioManager; |
| 34 private WindowAndroid mWindow; |
| 35 private ContentViewCore mContentViewCore; |
| 36 private ContentView mContentView; |
| 37 |
| 38 protected void handleIntent(Intent intent) { |
| 39 intent.setExtrasClassLoader(WebContents.class.getClassLoader()); |
| 40 mInstanceId = intent.getData().getPath(); |
| 41 |
| 42 WebContents webContents = (WebContents) intent.getParcelableExtra( |
| 43 CastWebContentsComponent.ACTION_EXTRA_WEB_CONTENTS); |
| 44 if (webContents == null) { |
| 45 Log.e(TAG, "Received null WebContents in intent."); |
| 46 return; |
| 47 } |
| 48 |
| 49 detachWebContentsIfAny(); |
| 50 showWebContents(webContents); |
| 51 } |
| 52 |
| 53 @Override |
| 54 public void onDestroy() { |
| 55 if (DEBUG) Log.d(TAG, "onDestroy"); |
| 56 |
| 57 if (mAudioManager.abandonAudioFocus(null) != AudioManager.AUDIOFOCUS_REQ
UEST_GRANTED) { |
| 58 Log.e(TAG, "Failed to abandon audio focus"); |
| 59 } |
| 60 |
| 61 super.onDestroy(); |
| 62 } |
| 63 |
| 64 @Override |
| 65 public void onCreate() { |
| 66 if (DEBUG) Log.d(TAG, "onCreate"); |
| 67 |
| 68 if (!CastBrowserHelper.initializeBrowser(getApplicationContext())) { |
| 69 Toast.makeText(this, R.string.browser_process_initialization_failed,
Toast.LENGTH_SHORT) |
| 70 .show(); |
| 71 stopSelf(); |
| 72 } |
| 73 |
| 74 mWindow = new WindowAndroid(this); |
| 75 mAudioManager = CastAudioManager.getAudioManager(this); |
| 76 |
| 77 if (mAudioManager.requestAudioFocus( |
| 78 null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAI
N) |
| 79 != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { |
| 80 Log.e(TAG, "Failed to obtain audio focus"); |
| 81 } |
| 82 } |
| 83 |
| 84 @Override |
| 85 public IBinder onBind(Intent intent) { |
| 86 if (DEBUG) Log.d(TAG, "onBind"); |
| 87 |
| 88 handleIntent(intent); |
| 89 |
| 90 return null; |
| 91 } |
| 92 |
| 93 // Sets webContents to be the currently displayed webContents. |
| 94 private void showWebContents(WebContents webContents) { |
| 95 if (DEBUG) Log.d(TAG, "showWebContents"); |
| 96 |
| 97 // TODO(derekjchow): productVersion |
| 98 mContentViewCore = new ContentViewCore(this, ""); |
| 99 mContentView = ContentView.createContentView(this, mContentViewCore); |
| 100 mContentViewCore.initialize(ViewAndroidDelegate.createBasicDelegate(mCon
tentView), |
| 101 mContentView, webContents, mWindow); |
| 102 // Enable display of current webContents. |
| 103 mContentViewCore.onShow(); |
| 104 } |
| 105 |
| 106 // Remove the currently displayed webContents. no-op if nothing is being dis
played. |
| 107 private void detachWebContentsIfAny() { |
| 108 if (DEBUG) Log.d(TAG, "detachWebContentsIfAny"); |
| 109 if (mContentView != null) { |
| 110 mContentView = null; |
| 111 mContentViewCore = null; |
| 112 |
| 113 // Inform CastContentWindowAndroid we're detaching. |
| 114 CastWebContentsComponent.onComponentClosed(this, mInstanceId); |
| 115 } |
| 116 } |
| 117 } |
OLD | NEW |