BarTransitions.java revision 89835ddf327ef256b44d91cf1fd1898c0599eb48
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.systemui.statusbar.phone;
18
19import android.app.ActivityManager;
20import android.content.Context;
21import android.content.res.Resources;
22import android.graphics.drawable.ColorDrawable;
23import android.graphics.drawable.Drawable;
24import android.util.Log;
25import android.view.View;
26
27import com.android.systemui.R;
28
29public class BarTransitions {
30    private static final boolean DEBUG = false;
31
32    public static final int MODE_NORMAL = 0;
33    public static final int MODE_SEMI_TRANSPARENT = 1;
34    public static final int MODE_TRANSPARENT = 2;
35
36    private final String mTag;
37    private final View mTarget;
38    private final Drawable mOpaque;
39    private final Drawable mSemiTransparent;
40
41    protected Drawable mTransparent;
42    private int mMode;
43
44    public BarTransitions(Context context, View target) {
45        mTag = "BarTransitions." + target.getClass().getSimpleName();
46        mTarget = target;
47        final Resources res = context.getResources();
48        mOpaque = new ColorDrawable(res.getColor(R.drawable.status_bar_background));
49        mSemiTransparent =
50                new ColorDrawable(res.getColor(R.color.status_bar_background_semi_transparent));
51    }
52
53    public void setTransparent(Drawable transparent) {
54        mTransparent = transparent;
55        if (mMode == MODE_TRANSPARENT) {
56            transitionTo(MODE_TRANSPARENT);
57        }
58    }
59
60    public void transitionTo(int mode) {
61        if (mMode == mode) return;
62        int oldMode = mMode;
63        mMode = mode;
64        if (!ActivityManager.isHighEndGfx()) return;
65        if (DEBUG) Log.d(mTag, String.format("transition from %s to %s",
66                modeToString(oldMode), modeToString(mode)));
67        onTransition(oldMode, mMode);
68    }
69
70    protected void onTransition(int oldMode, int newMode) {
71        Drawable background = newMode == MODE_SEMI_TRANSPARENT ? mSemiTransparent
72                : newMode == MODE_TRANSPARENT ? mTransparent
73                : mOpaque;
74        mTarget.setBackground(background);
75    }
76
77    public static String modeToString(int mode) {
78        if (mode == MODE_NORMAL) return "MODE_NORMAL";
79        if (mode == MODE_SEMI_TRANSPARENT) return "MODE_SEMI_TRANSPARENT";
80        if (mode == MODE_TRANSPARENT) return "MODE_TRANSPARENT";
81        throw new IllegalArgumentException("Unknown mode " + mode);
82    }
83}
84