17661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal/*
27661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal * Copyright (C) 2014 The Android Open Source Project
37661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal *
47661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
57661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal * you may not use this file except in compliance with the License.
67661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal * You may obtain a copy of the License at
77661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal *
87661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
97661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal *
107661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal * Unless required by applicable law or agreed to in writing, software
117661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
127661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal * See the License for the specific language governing permissions and
147661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal * limitations under the License.
157661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal */
167661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
177661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyalpackage com.android.launcher3;
187661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
197661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyalimport android.graphics.Canvas;
207661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyalimport android.graphics.ColorFilter;
217661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyalimport android.graphics.Rect;
227661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyalimport android.graphics.drawable.Drawable;
237661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
247661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyalpublic class BorderCropDrawable extends Drawable {
257661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
267661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    private final Drawable mChild;
277661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    private final Rect mBoundsShift;
287661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    private final Rect mPadding;
297661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
307661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    BorderCropDrawable(Drawable child, boolean cropLeft,
317661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal            boolean cropTop, boolean cropRight, boolean cropBottom) {
327661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        mChild = child;
337661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
347661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        mBoundsShift = new Rect();
357661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        mPadding = new Rect();
367661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        mChild.getPadding(mPadding);
377661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
387661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        if (cropLeft) {
397661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal            mBoundsShift.left = -mPadding.left;
407661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal            mPadding.left = 0;
417661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        }
427661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        if (cropTop) {
437661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal            mBoundsShift.top = -mPadding.top;
447661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal            mPadding.top = 0;
457661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        }
467661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        if (cropRight) {
477661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal            mBoundsShift.right = mPadding.right;
487661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal            mPadding.right = 0;
497661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        }
507661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        if (cropBottom) {
517661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal            mBoundsShift.bottom = mPadding.bottom;
527661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal            mPadding.bottom = 0;
537661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        }
547661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    }
557661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
567661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    @Override
577661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    protected void onBoundsChange(Rect bounds) {
587661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        mChild.setBounds(
597661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal                bounds.left + mBoundsShift.left,
607661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal                bounds.top + mBoundsShift.top,
617661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal                bounds.right + mBoundsShift.right,
627661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal                bounds.bottom + mBoundsShift.bottom);
637661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    }
647661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
657661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    @Override
667661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    public boolean getPadding(Rect padding) {
677661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        padding.set(mPadding);
687661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        return (padding.left | padding.top | padding.right | padding.bottom) != 0;
697661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    }
707661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
717661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    @Override
727661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    public void draw(Canvas canvas) {
737661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        mChild.draw(canvas);
747661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    }
757661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
767661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    @Override
777661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    public int getOpacity() {
787661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        return mChild.getOpacity();
797661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    }
807661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
817661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    @Override
827661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    public void setAlpha(int alpha) {
837661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        mChild.setAlpha(alpha);
847661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    }
857661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal
867661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    @Override
877661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    public void setColorFilter(ColorFilter cf) {
887661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal        mChild.setColorFilter(cf);
897661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal    }
907661614383dc17d69a9b1fd819e4d610896d5205Sunny Goyal}
91