SetupWizardLayout.java revision b73a15f71e63dd46bf4dab94ca13caa861ac948f
1d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam/*
2d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam * Copyright (C) 2015 The Android Open Source Project
3d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam *
4d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
5d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam * you may not use this file except in compliance with the License.
6d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam * You may obtain a copy of the License at
7d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam *
8d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
9d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam *
10d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam * Unless required by applicable law or agreed to in writing, software
11d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
12d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam * See the License for the specific language governing permissions and
14d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam * limitations under the License.
15d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam */
16d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam
17d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lampackage com.android.setupwizardlib;
18d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam
1995af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.annotation.SuppressLint;
2095af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.annotation.TargetApi;
2195af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.content.Context;
2295af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.content.res.TypedArray;
2395af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.graphics.Shader.TileMode;
2495af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.graphics.drawable.BitmapDrawable;
2595af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.graphics.drawable.Drawable;
2695af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.graphics.drawable.LayerDrawable;
2795af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.os.Build;
2895af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.util.AttributeSet;
2995af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.view.Gravity;
3095af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.view.LayoutInflater;
3195af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.view.View;
3295af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.view.ViewGroup;
3395af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.widget.FrameLayout;
3495af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport android.widget.TextView;
3595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
3695af12dac219e6945bab6142ea3a4099077314aaMaurice Lamimport com.android.setupwizardlib.view.Illustration;
3795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
3895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam@TargetApi(Build.VERSION_CODES.LOLLIPOP)
3995af12dac219e6945bab6142ea3a4099077314aaMaurice Lampublic class SetupWizardLayout extends FrameLayout {
4095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
4195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    private static final String TAG = "SetupWizardLayout";
4295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
4395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    /**
4495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * The container of the actual content. This will be a view in the template, which child views
4595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * will be added to when {@link #addView(android.view.View)} is called. This will be the layout
4695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * in the template that has the ID of {@link #getContainerId()}. For the default implementation
4795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * of SetupWizardLayout, that would be @id/suw_layout_content.
4895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     */
4995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    private ViewGroup mContainer;
5095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
5195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public SetupWizardLayout(Context context) {
5295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        this(context, 0);
5395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
5495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
5595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public SetupWizardLayout(Context context, int template) {
5695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        this(context, template, null, 0);
5795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
5895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
5995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public SetupWizardLayout(Context context, AttributeSet attrs) {
6095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        this(context, attrs, R.attr.suwLayoutTheme);
6195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
6295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
6395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public SetupWizardLayout(Context context, AttributeSet attrs, int defStyleAttr) {
6495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        this(context, 0, attrs, defStyleAttr);
6595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
6695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
6795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public SetupWizardLayout(Context context, int template, AttributeSet attrs, int defStyleAttr) {
6895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        super(context, attrs, defStyleAttr);
6995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final TypedArray a = context.obtainStyledAttributes(attrs,
7095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                R.styleable.SuwSetupWizardLayout, defStyleAttr, 0);
7195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (template == 0) {
7295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            template = a.getResourceId(R.styleable.SuwSetupWizardLayout_android_layout, 0);
7395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
7495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        inflateTemplate(template);
7595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
7695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        // Set the background from XML, either directly or built from a bitmap tile
7795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final Drawable background =
7895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                a.getDrawable(R.styleable.SuwSetupWizardLayout_suwBackground);
7995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (background != null) {
80b73a15f71e63dd46bf4dab94ca13caa861ac948fMaurice Lam            setLayoutBackground(background);
8195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        } else {
8295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            final Drawable backgroundTile =
8395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                    a.getDrawable(R.styleable.SuwSetupWizardLayout_suwBackgroundTile);
8495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            if (backgroundTile != null) {
8595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                setBackgroundTile(backgroundTile);
8695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            }
8795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
8895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
8995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        // Set the illustration from XML, either directly or built from image + horizontal tile
9095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final Drawable illustration =
9195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                a.getDrawable(R.styleable.SuwSetupWizardLayout_suwIllustration);
9295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (illustration != null) {
9395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            setIllustration(illustration);
9495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        } else {
9595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            final Drawable illustrationImage =
9695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                    a.getDrawable(R.styleable.SuwSetupWizardLayout_suwIllustrationImage);
9795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            final Drawable horizontalTile = a.getDrawable(
9895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                    R.styleable.SuwSetupWizardLayout_suwIllustrationHorizontalTile);
9995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            if (illustrationImage != null && horizontalTile != null) {
10095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                setIllustration(illustrationImage, horizontalTile);
10195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            }
10295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
10395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
10495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        // Set the header text
10595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final CharSequence headerText =
10695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                a.getText(R.styleable.SuwSetupWizardLayout_suwHeaderText);
10795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (headerText != null) {
10895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            setHeaderText(headerText);
10995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
11095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
11195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        a.recycle();
11295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
11395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
11495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    @Override
11595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public void addView(View child, int index, ViewGroup.LayoutParams params) {
11695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        mContainer.addView(child, index, params);
11795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
11895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
11995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    private void addViewInternal(View child) {
12095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        super.addView(child, -1, generateDefaultLayoutParams());
12195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
12295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
12395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    private void inflateTemplate(int templateResource) {
12495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final LayoutInflater inflater = LayoutInflater.from(getContext());
12595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final View templateRoot = onInflateTemplate(inflater, templateResource);
12695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        addViewInternal(templateRoot);
12795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
12895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        mContainer = (ViewGroup) findViewById(getContainerId());
12995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        onTemplateInflated();
13095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
13195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
13295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    /**
13395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * This method inflates the template. Subclasses can override this method to customize the
13495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * template inflation, or change to a different default template. The root of the inflated
13595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * layout should be returned, and not added to the view hierarchy.
13695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     *
13795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * @param inflater A LayoutInflater to inflate the template.
13895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * @param template The resource ID of the template to be inflated, or 0 if no template is
13995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     *                 specified.
14095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * @return Root of the inflated layout.
14195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     */
14295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    protected View onInflateTemplate(LayoutInflater inflater, int template) {
14395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (template == 0) {
14495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            template = R.layout.suw_template;
14595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
14695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        return inflater.inflate(template, this, false);
14795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
14895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
14995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    /**
15095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * This is called after the template has been inflated and added to the view hierarchy.
15195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * Subclasses can implement this method to modify the template as necessary, such as caching
15295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * views retrieved from findViewById, or other view operations that need to be done in code.
15395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * You can think of this as {@link android.view.View#onFinishInflate()} but for inflation of the
15495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * template instead of for child views.
15595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     */
15695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    protected void onTemplateInflated() {
15795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
15895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
15995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    protected int getContainerId() {
16095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        return R.id.suw_layout_content;
16195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
16295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
16395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public void setHeaderText(int title) {
16495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final TextView titleView = (TextView) findViewById(R.id.suw_layout_title);
16595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (titleView != null) {
16695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            titleView.setText(title);
16795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
16895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
16995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
17095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public void setHeaderText(CharSequence title) {
17195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final TextView titleView = (TextView) findViewById(R.id.suw_layout_title);
17295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (titleView != null) {
17395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            titleView.setText(title);
17495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
17595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
17695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
17795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    /**
17895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * Set the illustration of the layout. The drawable will be applied as is, and the bounds will
17995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * be set as implemented in {@link com.android.setupwizardlib.view.Illustration}. To create
18095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * a suitable drawable from an asset and a horizontal repeating tile, use
18195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * {@link #setIllustration(int, int)} instead.
18295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     *
18395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * @param drawable The drawable specifying the illustration.
18495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     */
18595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public void setIllustration(Drawable drawable) {
18695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final View view = findViewById(R.id.suw_layout_decor);
18795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (view instanceof Illustration) {
18895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            final Illustration illustration = (Illustration) view;
18995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            illustration.setIllustration(drawable);
19095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
19195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
19295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
19395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    /**
19495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * Set the illustration of the layout, which will be created asset and the horizontal tile as
19595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * suitable. On phone layouts (not sw600dp), the asset will be scaled, maintaining aspect ratio.
19695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * On tablets (sw600dp), the assets will always have 256dp height and the rest of the
19795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * illustration area that the asset doesn't fill will be covered by the horizontalTile.
19895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     *
19995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * @param asset Resource ID of the illustration asset.
20095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * @param horizontalTile Resource ID of the horizontally repeating tile for tablet layout.
20195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     */
20295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public void setIllustration(int asset, int horizontalTile) {
20395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final View view = findViewById(R.id.suw_layout_decor);
20495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (view instanceof Illustration) {
20595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            final Illustration illustration = (Illustration) view;
20695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            final Drawable illustrationDrawable = getIllustration(asset, horizontalTile);
20795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            illustration.setIllustration(illustrationDrawable);
20895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
20995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
21095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
21195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    private void setIllustration(Drawable asset, Drawable horizontalTile) {
21295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final View view = findViewById(R.id.suw_layout_decor);
21395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (view instanceof Illustration) {
21495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            final Illustration illustration = (Illustration) view;
21595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            final Drawable illustrationDrawable = getIllustration(asset, horizontalTile);
21695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            illustration.setIllustration(illustrationDrawable);
21795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
21895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
21995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
22095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    /**
22195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * Set the background of the layout, which is expected to be able to extend infinitely. If it is
22295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * a bitmap tile and you want it to repeat, use {@link #setBackgroundTile(int)} instead.
22395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     */
224b73a15f71e63dd46bf4dab94ca13caa861ac948fMaurice Lam    public void setLayoutBackground(Drawable background) {
22595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final View view = findViewById(R.id.suw_layout_decor);
22695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (view != null) {
22795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            view.setBackground(background);
22895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
22995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
23095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
23195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    /**
23295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     * Set the background of the layout to a repeating bitmap tile. To use a different kind of
233b73a15f71e63dd46bf4dab94ca13caa861ac948fMaurice Lam     * drawable, use {@link #setLayoutBackground(android.graphics.drawable.Drawable)} instead.
23495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam     */
23595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    public void setBackgroundTile(int backgroundTile) {
23695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final Drawable backgroundTileDrawable = getContext().getDrawable(backgroundTile);
23795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        setBackgroundTile(backgroundTileDrawable);
23895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
23995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
24095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    private void setBackgroundTile(Drawable backgroundTile) {
24195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (backgroundTile instanceof BitmapDrawable) {
24295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            ((BitmapDrawable) backgroundTile).setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
24395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
244b73a15f71e63dd46bf4dab94ca13caa861ac948fMaurice Lam        setLayoutBackground(backgroundTile);
24595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
24695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
24795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    private Drawable getIllustration(int asset, int horizontalTile) {
24895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final Context context = getContext();
24995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final Drawable assetDrawable = context.getResources().getDrawable(asset);
25095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final Drawable tile = context.getResources().getDrawable(horizontalTile);
25195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        return getIllustration(assetDrawable, tile);
25295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
25395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam
25495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    @SuppressLint("RtlHardcoded")
25595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    private Drawable getIllustration(Drawable asset, Drawable horizontalTile) {
25695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        final Context context = getContext();
25795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        if (context.getResources().getBoolean(R.bool.suwUseTabletLayout)) {
25895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            // If it is a "tablet" (sw600dp), create a LayerDrawable with the horizontal tile.
25995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            if (horizontalTile instanceof BitmapDrawable) {
26095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                ((BitmapDrawable) horizontalTile).setTileModeX(TileMode.REPEAT);
26195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                ((BitmapDrawable) horizontalTile).setGravity(Gravity.TOP);
26295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            }
26395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            if (asset instanceof BitmapDrawable) {
26495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                // Always specify TOP | LEFT, Illustration will flip the entire LayerDrawable.
26595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                ((BitmapDrawable) asset).setGravity(Gravity.TOP | Gravity.LEFT);
26695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            }
26795af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            final LayerDrawable layers =
26895af12dac219e6945bab6142ea3a4099077314aaMaurice Lam                    new LayerDrawable(new Drawable[] { horizontalTile, asset });
26995af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            layers.setAutoMirrored(true);
27095af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            return layers;
27195af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        } else {
27295af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            // If it is a "phone" (not sw600dp), simply return the illustration
27395af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            asset.setAutoMirrored(true);
27495af12dac219e6945bab6142ea3a4099077314aaMaurice Lam            return asset;
27595af12dac219e6945bab6142ea3a4099077314aaMaurice Lam        }
27695af12dac219e6945bab6142ea3a4099077314aaMaurice Lam    }
277d4c6ef70612f48c4612ec827bd89964d0955cd6fMaurice Lam}
278