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; |
| 22 | import android.graphics.SurfaceTexture; |
| 23 | import android.os.IBinder; |
| 24 | import android.os.RemoteException; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.util.DisplayMetrics; |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 27 | import android.util.Log; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 28 | import android.view.Surface; |
| 29 | import android.view.TextureView; |
| 30 | import android.view.TextureView.SurfaceTextureListener; |
| 31 | import android.view.ViewGroup; |
| 32 | import android.view.WindowManager; |
| 33 | |
| 34 | public class ActivityView extends ViewGroup { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 35 | private final String TAG = "ActivityView"; |
| 36 | |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 37 | private final TextureView mTextureView; |
| 38 | private IActivityContainer mActivityContainer; |
| 39 | private Activity mActivity; |
| 40 | private boolean mAttached; |
| 41 | private int mWidth; |
| 42 | private int mHeight; |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 43 | private Surface mSurface; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 44 | |
| 45 | public ActivityView(Context context) { |
| 46 | this(context, null); |
| 47 | } |
| 48 | |
| 49 | public ActivityView(Context context, AttributeSet attrs) { |
| 50 | this(context, attrs, 0); |
| 51 | } |
| 52 | |
| 53 | public ActivityView(Context context, AttributeSet attrs, int defStyle) { |
| 54 | super(context, attrs, defStyle); |
| 55 | |
| 56 | while (context instanceof ContextWrapper) { |
| 57 | if (context instanceof Activity) { |
| 58 | mActivity = (Activity)context; |
| 59 | break; |
| 60 | } |
| 61 | context = ((ContextWrapper)context).getBaseContext(); |
| 62 | } |
| 63 | if (mActivity == null) { |
| 64 | throw new IllegalStateException("The ActivityView's Context is not an Activity."); |
| 65 | } |
| 66 | |
| 67 | mTextureView = new TextureView(context); |
| 68 | mTextureView.setSurfaceTextureListener(new ActivityViewSurfaceTextureListener()); |
| 69 | addView(mTextureView); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 74 | mTextureView.layout(l, t, r, b); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void onAttachedToWindow() { |
| 79 | try { |
| 80 | final IBinder token = mActivity.getActivityToken(); |
| 81 | mActivityContainer = |
| 82 | ActivityManagerNative.getDefault().createActivityContainer(token, null); |
| 83 | } catch (RemoteException e) { |
| 84 | throw new IllegalStateException("ActivityView: Unable to create ActivityContainer. " |
| 85 | + e); |
| 86 | } |
| 87 | |
| 88 | final SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture(); |
| 89 | if (surfaceTexture != null) { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 90 | attachToSurface(surfaceTexture); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void onDetachedFromWindow() { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 96 | detachFromSurface(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean isAttachedToWindow() { |
| 101 | return mAttached; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | public void startActivity(Intent intent) { |
| 105 | if (mActivityContainer != null && mAttached) { |
| 106 | try { |
| 107 | mActivityContainer.startActivity(intent); |
| 108 | } catch (RemoteException e) { |
| 109 | throw new IllegalStateException("ActivityView: Unable to startActivity. " + e); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** Call when both mActivityContainer and mTextureView's SurfaceTexture are not null */ |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 115 | private void attachToSurface(SurfaceTexture surfaceTexture) { |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 116 | WindowManager wm = (WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE); |
| 117 | DisplayMetrics metrics = new DisplayMetrics(); |
| 118 | wm.getDefaultDisplay().getMetrics(metrics); |
| 119 | |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 120 | mSurface = new Surface(surfaceTexture); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 121 | try { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 122 | mActivityContainer.attachToSurface(mSurface, mWidth, mHeight, |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 123 | metrics.densityDpi); |
| 124 | } catch (RemoteException e) { |
| 125 | mActivityContainer = null; |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 126 | mSurface.release(); |
| 127 | mSurface = null; |
| 128 | mAttached = false; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 129 | throw new IllegalStateException( |
| 130 | "ActivityView: Unable to create ActivityContainer. " + e); |
| 131 | } |
| 132 | mAttached = true; |
| 133 | } |
| 134 | |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 135 | private void detachFromSurface() { |
| 136 | if (mActivityContainer != null) { |
| 137 | try { |
| 138 | mActivityContainer.detachFromDisplay(); |
| 139 | } catch (RemoteException e) { |
| 140 | } |
| 141 | mActivityContainer = null; |
| 142 | } |
| 143 | if (mSurface != null) { |
| 144 | mSurface.release(); |
| 145 | mSurface = null; |
| 146 | } |
| 147 | mAttached = false; |
| 148 | } |
| 149 | |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 150 | private class ActivityViewSurfaceTextureListener implements SurfaceTextureListener { |
| 151 | @Override |
| 152 | public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, |
| 153 | int height) { |
| 154 | mWidth = width; |
| 155 | mHeight = height; |
| 156 | if (mActivityContainer != null) { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 157 | attachToSurface(surfaceTexture); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, |
| 163 | int height) { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 164 | Log.d(TAG, "onSurfaceTextureSizeChanged: w=" + width + " h=" + height); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 169 | Log.d(TAG, "onSurfaceTextureDestroyed"); |
| 170 | detachFromSurface(); |
| 171 | return true; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { |
Craig Mautner | 34b73df | 2014-01-12 21:11:08 -0800 | [diff] [blame^] | 176 | // Log.d(TAG, "onSurfaceTextureUpdated"); |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | } |
| 180 | } |