1/*
2 * Copyright (C) 2011 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.launcher3;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.util.AttributeSet;
22import android.widget.FrameLayout;
23import android.widget.ImageView;
24
25public class PageIndicatorMarker extends FrameLayout {
26    @SuppressWarnings("unused")
27    private static final String TAG = "PageIndicator";
28
29    private static final int MARKER_FADE_DURATION = 175;
30
31    private ImageView mActiveMarker;
32    private ImageView mInactiveMarker;
33    private boolean mIsActive = false;
34
35    public PageIndicatorMarker(Context context) {
36        this(context, null);
37    }
38
39    public PageIndicatorMarker(Context context, AttributeSet attrs) {
40        this(context, attrs, 0);
41    }
42
43    public PageIndicatorMarker(Context context, AttributeSet attrs, int defStyle) {
44        super(context, attrs, defStyle);
45    }
46
47    protected void onFinishInflate() {
48        mActiveMarker = (ImageView) findViewById(R.id.active);
49        mInactiveMarker = (ImageView) findViewById(R.id.inactive);
50    }
51
52    void setMarkerDrawables(int activeResId, int inactiveResId) {
53        Resources r = getResources();
54        mActiveMarker.setImageDrawable(r.getDrawable(activeResId));
55        mInactiveMarker.setImageDrawable(r.getDrawable(inactiveResId));
56    }
57
58    void activate(boolean immediate) {
59        if (immediate) {
60            mActiveMarker.animate().cancel();
61            mActiveMarker.setAlpha(1f);
62            mActiveMarker.setScaleX(1f);
63            mActiveMarker.setScaleY(1f);
64            mInactiveMarker.animate().cancel();
65            mInactiveMarker.setAlpha(0f);
66        } else {
67            mActiveMarker.animate()
68                    .alpha(1f)
69                    .scaleX(1f)
70                    .scaleY(1f)
71                    .setDuration(MARKER_FADE_DURATION).start();
72            mInactiveMarker.animate()
73                    .alpha(0f)
74                    .setDuration(MARKER_FADE_DURATION).start();
75        }
76        mIsActive = true;
77    }
78
79    void inactivate(boolean immediate) {
80        if (immediate) {
81            mInactiveMarker.animate().cancel();
82            mInactiveMarker.setAlpha(1f);
83            mActiveMarker.animate().cancel();
84            mActiveMarker.setAlpha(0f);
85            mActiveMarker.setScaleX(0.5f);
86            mActiveMarker.setScaleY(0.5f);
87        } else {
88            mInactiveMarker.animate().alpha(1f)
89                    .setDuration(MARKER_FADE_DURATION).start();
90            mActiveMarker.animate()
91                    .alpha(0f)
92                    .scaleX(0.5f)
93                    .scaleY(0.5f)
94                    .setDuration(MARKER_FADE_DURATION).start();
95        }
96        mIsActive = false;
97    }
98
99    boolean isActive() {
100        return mIsActive;
101    }
102}
103