@MediumTest public void testWebView() throws Exception { final RemoteViewsActivity activity = getActivity(); RemoteViews orig = new RemoteViews("com.android.frameworktest", R.layout.remote_view_test_bad_2); Parcel p = Parcel.obtain(); orig.writeToParcel(p, 0); p.setDataPosition(0); RemoteViews r = RemoteViews.CREATOR.createFromParcel(p); ViewGroup parent = (ViewGroup) activity.findViewById(R.id.parent); boolean exceptionThrown = false; View result = null; try { result = r.apply(activity, parent); } catch (InflateException e) { exceptionThrown = true; } p.recycle(); assertTrue("WebView allowed to be inflated", exceptionThrown); assertNull("WebView allowed to be inflated", result); }
@MediumTest public void testGood() throws Exception { final RemoteViewsActivity activity = getActivity(); RemoteViews orig = new RemoteViews("com.android.frameworktest", R.layout.remote_view_test_good); Parcel p = Parcel.obtain(); orig.writeToParcel(p, 0); p.setDataPosition(0); RemoteViews r = RemoteViews.CREATOR.createFromParcel(p); ViewGroup parent = (ViewGroup) activity.findViewById(R.id.parent); View result = r.apply(activity, parent); p.recycle(); assertTrue("LinearLayout not inflated", result.findViewById(R.id.linear) != null); assertTrue("TextView not inflated", result.findViewById(R.id.text) != null); assertTrue("ImageView not inflated", result.findViewById(R.id.image) != null); assertTrue("FrameLayout not inflated", result.findViewById(R.id.frame) != null); assertTrue("RelateiveLayout not inflated", result.findViewById(R.id.relative) != null); assertTrue("AbsoluteLayout not inflated", result.findViewById(R.id.absolute) != null); assertTrue("ProgressBar not inflated", result.findViewById(R.id.progress) != null); assertTrue("ImageButton not inflated", result.findViewById(R.id.image_button) != null); assertTrue("Button not inflated", result.findViewById(R.id.button) != null); }
@Override public void onReceive(Context context, Intent intent) { try { Bundle extras = intent.getExtras(); RemoteViews view = extras.getParcelable(AstridApiConstants.EXTRAS_RESPONSE); // add a separator View separator = new View(TaskEditActivity.this); separator.setPadding(5, 5, 5, 5); separator.setBackgroundResource(android.R.drawable.divider_horizontal_dark); LinearLayout dest = (LinearLayout) findViewById(R.id.tab_addons_addons); dest.addView(separator); view.apply(TaskEditActivity.this, dest); } catch (Exception e) { exceptionService.reportError( "receive-detail-" + //$NON-NLS-1$ intent.getStringExtra(AstridApiConstants.EXTRAS_ADDON), e); } }
/** * Process a set of {@link RemoteViews} coming in as an update from the AppWidget provider. Will * animate into these new views as needed */ public void updateAppWidget(RemoteViews remoteViews) { if (LOGD) Log.d(TAG, "updateAppWidget called mOld=" + mOld); boolean recycled = false; View content = null; Exception exception = null; // Capture the old view into a bitmap so we can do the crossfade. if (CROSSFADE) { if (mFadeStartTime < 0) { if (mView != null) { final int width = mView.getWidth(); final int height = mView.getHeight(); try { mOld = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); } catch (OutOfMemoryError e) { // we just won't do the fade mOld = null; } if (mOld != null) { // mView.drawIntoBitmap(mOld); } } } } if (remoteViews == null) { if (mViewMode == VIEW_MODE_DEFAULT) { // We've already done this -- nothing to do. return; } content = getDefaultView(); mLayoutId = -1; mViewMode = VIEW_MODE_DEFAULT; } else { // Prepare a local reference to the remote Context so we're ready to // inflate any requested LayoutParams. mRemoteContext = getRemoteContext(remoteViews); int layoutId = remoteViews.getLayoutId(); // If our stale view has been prepared to match active, and the new // layout matches, try recycling it if (content == null && layoutId == mLayoutId) { try { remoteViews.reapply(mContext, mView, mOnClickHandler); content = mView; recycled = true; if (LOGD) Log.d(TAG, "was able to recycled existing layout"); } catch (RuntimeException e) { exception = e; } } // Try normal RemoteView inflation if (content == null) { try { content = remoteViews.apply(mContext, this, mOnClickHandler); if (LOGD) Log.d(TAG, "had to inflate new layout"); } catch (RuntimeException e) { exception = e; } } mLayoutId = layoutId; mViewMode = VIEW_MODE_CONTENT; } if (content == null) { if (mViewMode == VIEW_MODE_ERROR) { // We've already done this -- nothing to do. return; } Log.w(TAG, "updateAppWidget couldn't find any view, using error view", exception); content = getErrorView(); mViewMode = VIEW_MODE_ERROR; } if (!recycled) { prepareView(content); addView(content); } if (mView != content) { removeView(mView); mView = content; } if (CROSSFADE) { if (mFadeStartTime < 0) { // if there is already an animation in progress, don't do anything -- // the new view will pop in on top of the old one during the cross fade, // and that looks okay. mFadeStartTime = SystemClock.uptimeMillis(); invalidate(); } } }