public static Drawable fromDrawable(Drawable drawable) {
    if (drawable != null) {
      if (drawable instanceof RoundedDrawable) {
        // just return if it's already a RoundedDrawable
        return drawable;
      } else if (drawable instanceof ColorDrawable) {
        // FIXME we don't support ColorDrawables yet
        return drawable;
      } else if (drawable instanceof LayerDrawable) {
        LayerDrawable ld = (LayerDrawable) drawable;
        int num = ld.getNumberOfLayers();

        // loop through layers to and change to RoundedDrawables if possible
        for (int i = 0; i < num; i++) {
          Drawable d = ld.getDrawable(i);
          ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d));
        }
        return ld;
      }

      // try to get a bitmap from the drawable and
      Bitmap bm = drawableToBitmap(drawable);
      if (bm != null) {
        return new RoundedDrawable(bm);
      } else {
        Log.w(TAG, "Failed to create bitmap from drawable!");
      }
    }
    return drawable;
  }
  private void applyBackgroundChanges() {
    if (!mAttached || mLayerWrapper == null) {
      return;
    }

    if (DEBUG) Log.v(TAG, "applyBackgroundChanges drawable " + mBackgroundDrawable);

    int dimAlpha = 0;

    if (mImageOutWrapper != null && mImageOutWrapper.isAnimationPending()) {
      if (DEBUG) Log.v(TAG, "mImageOutWrapper animation starting");
      mImageOutWrapper.startAnimation();
      mImageOutWrapper = null;
      dimAlpha = DIM_ALPHA_ON_SOLID;
    }

    if (mImageInWrapper == null && mBackgroundDrawable != null) {
      if (DEBUG) Log.v(TAG, "creating new imagein drawable");
      mImageInWrapper = new DrawableWrapper(mBackgroundDrawable);
      mLayerDrawable.setDrawableByLayerId(R.id.background_imagein, mBackgroundDrawable);
      if (DEBUG) Log.v(TAG, "mImageInWrapper animation starting");
      mImageInWrapper.setAlpha(0);
      mImageInWrapper.fadeIn(FADE_DURATION_SLOW, 0);
      mImageInWrapper.startAnimation();
      dimAlpha = FULL_ALPHA;
    }

    if (mDimWrapper != null && dimAlpha != 0) {
      if (DEBUG) Log.v(TAG, "dimwrapper animation starting to " + dimAlpha);
      mDimWrapper.fade(FADE_DURATION_SLOW, 0, dimAlpha);
      mDimWrapper.startAnimation();
    }
  }
 /**
  * Release references to Drawables. Typically called to reduce memory overhead when not visible.
  *
  * <p>When an Activity is resumed, if the BackgroundManager has not been released, the continuity
  * service is updated from the BackgroundManager state. If the BackgroundManager was released, the
  * BackgroundManager inherits the current state from the continuity service.
  */
 public void release() {
   if (DEBUG) Log.v(TAG, "release");
   if (mLayerDrawable != null) {
     mLayerDrawable.setDrawableByLayerId(R.id.background_imagein, createEmptyDrawable());
     mLayerDrawable.setDrawableByLayerId(R.id.background_imageout, createEmptyDrawable());
     mLayerDrawable = null;
   }
   mLayerWrapper = null;
   mImageInWrapper = null;
   mImageOutWrapper = null;
   mColorWrapper = null;
   mDimWrapper = null;
   mThemeDrawable = null;
   if (mChangeRunnable != null) {
     mChangeRunnable.cancel();
     mChangeRunnable = null;
   }
   releaseBackgroundBitmap();
 }
  private void updateImmediate() {
    lazyInit();

    mColorWrapper.setColor(mBackgroundColor);
    if (mDimWrapper != null) {
      mDimWrapper.setAlpha(mBackgroundColor == Color.TRANSPARENT ? 0 : DIM_ALPHA_ON_SOLID);
    }
    showWallpaper(mBackgroundColor == Color.TRANSPARENT);

    mThemeDrawable = getThemeDrawable();
    mLayerDrawable.setDrawableByLayerId(R.id.background_theme, mThemeDrawable);

    if (mBackgroundDrawable == null) {
      mLayerDrawable.setDrawableByLayerId(R.id.background_imagein, createEmptyDrawable());
    } else {
      if (DEBUG) Log.v(TAG, "Background drawable is available");
      mImageInWrapper = new DrawableWrapper(mBackgroundDrawable);
      mLayerDrawable.setDrawableByLayerId(R.id.background_imagein, mBackgroundDrawable);
      if (mDimWrapper != null) {
        mDimWrapper.setAlpha(FULL_ALPHA);
      }
    }
  }
  private void lazyInit() {
    if (mLayerDrawable != null) {
      return;
    }

    mLayerDrawable = (LayerDrawable) mContext.getResources().getDrawable(R.drawable.lb_background);
    mBgView.setBackground(mLayerDrawable);

    mLayerDrawable.setDrawableByLayerId(R.id.background_imageout, createEmptyDrawable());

    mDimWrapper = new DrawableWrapper(mLayerDrawable.findDrawableByLayerId(R.id.background_dim));

    mLayerWrapper = new DrawableWrapper(mLayerDrawable);

    mColorWrapper =
        new DrawableWrapper(mLayerDrawable.findDrawableByLayerId(R.id.background_color));
  }
 /** Updates a target in the GlowPadView */
 private void setTarget(
     int position, String uri, Drawable draw, String iconType, String iconSource, String pkgName) {
   TargetInfo item = mTargetStore.get(position);
   StateListDrawable state = (StateListDrawable) item.icon;
   LayerDrawable inActiveLayer = (LayerDrawable) state.getStateDrawable(0);
   LayerDrawable activeLayer = (LayerDrawable) state.getStateDrawable(1);
   inActiveLayer.setDrawableByLayerId(1, draw);
   boolean isSystem = iconType != null && iconType.equals(GlowPadView.ICON_RESOURCE);
   if (!isSystem) {
     final Drawable activeBack =
         mResources.getDrawable(com.android.internal.R.drawable.ic_lockscreen_target_activated);
     activeLayer.setDrawableByLayerId(0, new InsetDrawable(activeBack, 0, 0, 0, 0));
     activeLayer.setDrawableByLayerId(1, draw);
   } else {
     InsetDrawable empty =
         new InsetDrawable(mResources.getDrawable(android.R.color.transparent), 0, 0, 0, 0);
     activeLayer.setDrawableByLayerId(1, empty);
     int activeId =
         mResources.getIdentifier(
             iconSource.replaceAll("_normal", "_activated"), "drawable", "android");
     Drawable back = null;
     if (activeId != 0) {
       back = mResources.getDrawable(activeId);
       activeLayer.setDrawableByLayerId(0, back);
     } else {
       final Drawable activeBack =
           mResources.getDrawable(com.android.internal.R.drawable.ic_lockscreen_target_activated);
       activeLayer.setDrawableByLayerId(0, new InsetDrawable(activeBack, 0, 0, 0, 0));
     }
   }
   item.defaultIcon = mDialogIcon.getDrawable().getConstantState().newDrawable().mutate();
   item.uri = uri;
   item.iconType = iconType;
   item.iconSource = iconSource;
   item.pkgName = pkgName;
 }