OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 private static final String ACTION_EXTRA_WEB_CONTENTS = |
| 39 "com.google.android.apps.castshell.intent.extra.WEB_CONTENTS"; |
| 40 |
| 41 protected void handleIntent(Intent intent) { |
| 42 intent.setExtrasClassLoader(WebContents.class.getClassLoader()); |
| 43 mInstanceId = intent.getData().getPath(); |
| 44 |
| 45 WebContents webContents = |
| 46 (WebContents) intent.getParcelableExtra(ACTION_EXTRA_WEB_CONTENT
S); |
| 47 if (webContents == null) { |
| 48 Log.e(TAG, "Received null WebContents in intent."); |
| 49 return; |
| 50 } |
| 51 |
| 52 detachWebContentsIfAny(); |
| 53 showWebContents(webContents); |
| 54 } |
| 55 |
| 56 @Override |
| 57 public void onDestroy() { |
| 58 if (DEBUG) Log.d(TAG, "onDestroy"); |
| 59 |
| 60 if (mAudioManager.abandonAudioFocus(null) != AudioManager.AUDIOFOCUS_REQ
UEST_GRANTED) { |
| 61 Log.e(TAG, "Failed to abandon audio focus"); |
| 62 } |
| 63 |
| 64 super.onDestroy(); |
| 65 } |
| 66 |
| 67 @Override |
| 68 public void onCreate() { |
| 69 if (DEBUG) Log.d(TAG, "onCreate"); |
| 70 |
| 71 // TODO(derekjchow): Remove this call. |
| 72 if (!CastBrowserHelper.initializeBrowser(getApplicationContext())) { |
| 73 Toast.makeText(this, R.string.browser_process_initialization_failed,
Toast.LENGTH_SHORT) |
| 74 .show(); |
| 75 stopSelf(); |
| 76 } |
| 77 |
| 78 mWindow = new WindowAndroid(this); |
| 79 mAudioManager = CastAudioManager.getAudioManager(this); |
| 80 |
| 81 if (mAudioManager.requestAudioFocus( |
| 82 null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAI
N) |
| 83 != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { |
| 84 Log.e(TAG, "Failed to obtain audio focus"); |
| 85 } |
| 86 } |
| 87 |
| 88 @Override |
| 89 public IBinder onBind(Intent intent) { |
| 90 if (DEBUG) Log.d(TAG, "onBind"); |
| 91 |
| 92 handleIntent(intent); |
| 93 |
| 94 return null; |
| 95 } |
| 96 |
| 97 // Sets webContents to be the currently displayed webContents. |
| 98 private void showWebContents(WebContents webContents) { |
| 99 if (DEBUG) Log.d(TAG, "showWebContents"); |
| 100 |
| 101 // TODO(derekjchow): productVersion |
| 102 mContentViewCore = new ContentViewCore(this, ""); |
| 103 mContentView = ContentView.createContentView(this, mContentViewCore); |
| 104 mContentViewCore.initialize(ViewAndroidDelegate.createBasicDelegate(mCon
tentView), |
| 105 mContentView, webContents, mWindow); |
| 106 // Enable display of current webContents. |
| 107 mContentViewCore.onShow(); |
| 108 } |
| 109 |
| 110 // Remove the currently displayed webContents. no-op if nothing is being dis
played. |
| 111 private void detachWebContentsIfAny() { |
| 112 if (DEBUG) Log.d(TAG, "detachWebContentsIfAny"); |
| 113 if (mContentView != null) { |
| 114 mContentView = null; |
| 115 mContentViewCore = null; |
| 116 |
| 117 // Inform CastContentWindowAndroid we're detaching. |
| 118 CastWebContentsComponent.onComponentClosed(this, mInstanceId); |
| 119 } |
| 120 } |
| 121 } |
OLD | NEW |