DirectoryView.java revision 6963148e226dfadc48c36da7db14ee4587a62f6a
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
19import android.content.Context;
20import android.graphics.Rect;
21import android.graphics.drawable.Drawable;
22import android.graphics.drawable.InsetDrawable;
23import android.util.AttributeSet;
24import android.widget.FrameLayout;
25
26public class DirectoryView extends FrameLayout {
27    private float mPosition = 0f;
28
29    private int mWidth;
30
31    public DirectoryView(Context context) {
32        super(context);
33    }
34
35    public DirectoryView(Context context, AttributeSet attrs) {
36        super(context, attrs);
37    }
38
39    @Override
40    public void setBackground(Drawable background) {
41        final Rect rect = new Rect();
42        background.getPadding(rect);
43
44        final boolean insetLeft = getResources().getBoolean(R.bool.list_divider_inset_left);
45        if (insetLeft) {
46            super.setBackground(new InsetDrawable(background, -rect.left, 0, -rect.right, 0));
47        } else {
48            super.setBackground(new InsetDrawable(background, -rect.right, 0, -rect.left, 0));
49        }
50    }
51
52    @Override
53    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
54        super.onSizeChanged(w, h, oldw, oldh);
55        mWidth = w;
56        setPosition(mPosition);
57    }
58
59    public float getPosition() {
60        return mPosition;
61    }
62
63    public void setPosition(float position) {
64        mPosition = position;
65        setX((mWidth > 0) ? (mPosition * mWidth) : 0);
66    }
67}
68