Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.ContextWrapper; |
| 21 | import android.content.Intent; |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 22 | import android.content.IntentSender; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 23 | import android.graphics.SurfaceTexture; |
| 24 | import android.os.IBinder; |
| 25 | import android.os.RemoteException; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.util.DisplayMetrics; |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 28 | import android.util.Log; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 29 | import android.view.Surface; |
| 30 | import android.view.TextureView; |
| 31 | import android.view.TextureView.SurfaceTextureListener; |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 32 | import android.view.View; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 33 | import android.view.ViewGroup; |
| 34 | import android.view.WindowManager; |
| 35 | |
| 36 | public class ActivityView extends ViewGroup { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 37 | private final String TAG = "ActivityView"; |
| 38 | |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 39 | private final TextureView mTextureView; |
| 40 | private IActivityContainer mActivityContainer; |
| 41 | private Activity mActivity; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 42 | private int mWidth; |
| 43 | private int mHeight; |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 44 | private Surface mSurface; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 45 | |
| 46 | public ActivityView(Context context) { |
| 47 | this(context, null); |
| 48 | } |
| 49 | |
| 50 | public ActivityView(Context context, AttributeSet attrs) { |
| 51 | this(context, attrs, 0); |
| 52 | } |
| 53 | |
| 54 | public ActivityView(Context context, AttributeSet attrs, int defStyle) { |
| 55 | super(context, attrs, defStyle); |
| 56 | |
| 57 | while (context instanceof ContextWrapper) { |
| 58 | if (context instanceof Activity) { |
| 59 | mActivity = (Activity)context; |
| 60 | break; |
| 61 | } |
| 62 | context = ((ContextWrapper)context).getBaseContext(); |
| 63 | } |
| 64 | if (mActivity == null) { |
| 65 | throw new IllegalStateException("The ActivityView's Context is not an Activity."); |
| 66 | } |
| 67 | |
| 68 | mTextureView = new TextureView(context); |
| 69 | mTextureView.setSurfaceTextureListener(new ActivityViewSurfaceTextureListener()); |
| 70 | addView(mTextureView); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 75 | mTextureView.layout(l, t, r, b); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected void onAttachedToWindow() { |
| 80 | try { |
| 81 | final IBinder token = mActivity.getActivityToken(); |
| 82 | mActivityContainer = |
| 83 | ActivityManagerNative.getDefault().createActivityContainer(token, null); |
| 84 | } catch (RemoteException e) { |
| 85 | throw new IllegalStateException("ActivityView: Unable to create ActivityContainer. " |
| 86 | + e); |
| 87 | } |
| 88 | |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 89 | attachToSurfaceWhenReady(); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | protected void onDetachedFromWindow() { |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 94 | if (mActivityContainer != null) { |
| 95 | detach(); |
| 96 | mActivityContainer = null; |
| 97 | } |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 101 | protected void onWindowVisibilityChanged(int visibility) { |
| 102 | super.onWindowVisibilityChanged(visibility); |
| 103 | if (visibility == View.VISIBLE) { |
| 104 | attachToSurfaceWhenReady(); |
| 105 | } else { |
| 106 | detach(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public boolean isAttachedToDisplay() { |
| 111 | return mSurface != null; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | public void startActivity(Intent intent) { |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 115 | if (mSurface != null) { |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 116 | try { |
| 117 | mActivityContainer.startActivity(intent); |
| 118 | } catch (RemoteException e) { |
| 119 | throw new IllegalStateException("ActivityView: Unable to startActivity. " + e); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 124 | public void startActivity(IntentSender intentSender) { |
| 125 | if (mSurface != null) { |
| 126 | try { |
| 127 | mActivityContainer.startActivityIntentSender(intentSender.getTarget()); |
| 128 | } catch (RemoteException e) { |
| 129 | throw new IllegalStateException( |
| 130 | "ActivityView: Unable to startActivity from IntentSender. " + e); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public void startActivity(PendingIntent pendingIntent) { |
| 136 | if (mSurface != null) { |
| 137 | try { |
| 138 | mActivityContainer.startActivityIntentSender(pendingIntent.getTarget()); |
| 139 | } catch (RemoteException e) { |
| 140 | throw new IllegalStateException( |
| 141 | "ActivityView: Unable to startActivity from PendingIntent. " + e); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private void attachToSurfaceWhenReady() { |
| 147 | final SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture(); |
| 148 | if (mActivityContainer == null || surfaceTexture == null || mSurface != null) { |
| 149 | // Either not ready to attach, or already attached. |
| 150 | return; |
| 151 | } |
| 152 | |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 153 | WindowManager wm = (WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE); |
| 154 | DisplayMetrics metrics = new DisplayMetrics(); |
| 155 | wm.getDefaultDisplay().getMetrics(metrics); |
| 156 | |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 157 | mSurface = new Surface(surfaceTexture); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 158 | try { |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 159 | mActivityContainer.attachToSurface(mSurface, mWidth, mHeight, metrics.densityDpi); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 160 | } catch (RemoteException e) { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 161 | mSurface.release(); |
| 162 | mSurface = null; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 163 | throw new IllegalStateException( |
| 164 | "ActivityView: Unable to create ActivityContainer. " + e); |
| 165 | } |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 168 | private void detach() { |
| 169 | if (mSurface != null) { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 170 | try { |
| 171 | mActivityContainer.detachFromDisplay(); |
| 172 | } catch (RemoteException e) { |
| 173 | } |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 174 | mSurface.release(); |
| 175 | mSurface = null; |
| 176 | } |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 179 | private class ActivityViewSurfaceTextureListener implements SurfaceTextureListener { |
| 180 | @Override |
| 181 | public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, |
| 182 | int height) { |
| 183 | mWidth = width; |
| 184 | mHeight = height; |
| 185 | if (mActivityContainer != null) { |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 186 | attachToSurfaceWhenReady(); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, |
| 192 | int height) { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 193 | Log.d(TAG, "onSurfaceTextureSizeChanged: w=" + width + " h=" + height); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 198 | Log.d(TAG, "onSurfaceTextureDestroyed"); |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame^] | 199 | detach(); |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 200 | return true; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame] | 205 | // Log.d(TAG, "onSurfaceTextureUpdated"); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | } |
| 209 | } |