/**
   * Converts a drawable to a tiled version of itself. It will recursively traverse layer and state
   * list drawables.
   */
  private Drawable tileify(Drawable drawable, boolean clip) {

    if (drawable instanceof LayerDrawable) {
      LayerDrawable background = (LayerDrawable) drawable;
      final int N = background.getNumberOfLayers();
      Drawable[] outDrawables = new Drawable[N];

      for (int i = 0; i < N; i++) {
        int id = background.getId(i);
        outDrawables[i] =
            tileify(
                background.getDrawable(i), (id == R.id.progress || id == R.id.secondaryProgress));
      }

      LayerDrawable newBg = new LayerDrawable(outDrawables);

      for (int i = 0; i < N; i++) {
        newBg.setId(i, background.getId(i));
      }

      return newBg;

    } else if (drawable instanceof StateListDrawable) {
      StateListDrawable in = (StateListDrawable) drawable;
      StateListDrawable out = new StateListDrawable();
      int numStates = in.getStateCount();
      for (int i = 0; i < numStates; i++) {
        out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
      }
      return out;

    } else if (drawable instanceof BitmapDrawable) {
      final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
      if (mSampleTile == null) {
        mSampleTile = tileBitmap;
      }

      final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());

      final BitmapShader bitmapShader =
          new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
      shapeDrawable.getPaint().setShader(bitmapShader);

      return (clip)
          ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL)
          : shapeDrawable;
    }

    return drawable;
  }
 /** 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;
 }