summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/ModePicker.java
blob: 594c813e8de4edcf86080f5e937baf4b028db190 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera;

import com.android.camera.ui.RotateImageView;

import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * A widget that includes three mode selections {@code RotateImageView}'s and
 * a current mode indicator.
 */
public class ModePicker extends RelativeLayout implements View.OnClickListener {
    public static final int MODE_PANORAMA = 0;
    public static final int MODE_VIDEO = 1;
    public static final int MODE_CAMERA = 2;

    /** A callback to be called when the user wants to switch activity. */
    public interface OnModeChangeListener {
        // Returns true if the listener agrees that the mode can be changed.
        public boolean onModeChanged(int newMode);
    }

    private final int DISABLED_COLOR;

    private OnModeChangeListener mListener;
    private View mModeIcons[] = new View[3];
    private View mCurrentModeIcon;
    private View mModeSelection;
    private int mCurrentMode = MODE_CAMERA;
    private Context mContext;

    public ModePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        DISABLED_COLOR = context.getResources().getColor(R.color.icon_disabled_color);
    }

    protected void onFinishInflate() {
        super.onFinishInflate();

        mModeSelection = findViewById(R.id.mode_selection);

        mCurrentModeIcon = findViewById(R.id.current_mode);
        mCurrentModeIcon.setOnClickListener(this);

        mModeIcons[MODE_PANORAMA] = findViewById(R.id.mode_panorama);
        mModeIcons[MODE_VIDEO] = findViewById(R.id.mode_video);
        mModeIcons[MODE_CAMERA] = findViewById(R.id.mode_camera);
    }

    private void enableModeSelection(boolean enabled) {
        mCurrentModeIcon.setVisibility(enabled ? View.INVISIBLE : View.VISIBLE);
        mCurrentModeIcon.setOnClickListener(enabled ? null : this);
        mModeSelection.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
        for (int i = 0; i < 3; ++i) {
            mModeIcons[i].setOnClickListener(enabled ? this : null);
            highlightCurrentMode(mModeIcons[i], (i == mCurrentMode));
        }
    }

    public void onClick(View view) {
        for (int i = 0; i < 3; ++i) {
            if (view == mModeIcons[i]) {
                setCurrentMode(i);
                enableModeSelection(false);
                return;
            }
        }
        if (view == mCurrentModeIcon) enableModeSelection(true);
    }

    private void setMode(int mode) {
        for (int i = 0; i < 3; ++i) mModeIcons[i].setSelected(mode == i);
    }

    public void setOnModeChangeListener(OnModeChangeListener listener) {
        mListener = listener;
    }

    public void setCurrentMode(int mode) {
        setMode(mode);
        tryToSetMode(mode);
    }

    private void tryToSetMode(int mode) {
        if (mListener != null) {
            if (!mListener.onModeChanged(mode)) {
                setMode(mCurrentMode);
                return;
            }
        }
        ((RotateImageView) mCurrentModeIcon).setImageDrawable(
                ((RotateImageView) mModeIcons[mode]).getDrawable());
        mCurrentMode = mode;
    }

    public boolean onModeChanged(int mode) {
        setCurrentMode(mode);
        return true;
    }

    public void setDegree(int degree) {
        for (int i = 0 ; i < 3 ; ++i) {
            ((RotateImageView) mModeIcons[i]).setDegree(degree);
        }
        ((RotateImageView) mCurrentModeIcon).setDegree(degree);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
    }

    private void highlightCurrentMode(View view, boolean enabled) {
        Drawable drawable = ((ImageView) view).getDrawable();
        if (enabled) {
            drawable.clearColorFilter();
        } else {
            drawable.setColorFilter(DISABLED_COLOR, PorterDuff.Mode.SRC_ATOP);
        }
    }

    public void setEnabled(View view, boolean enabled) {
        view.setEnabled(enabled);

        // render disabled effect for tablet only.
        if (mContext.getResources().getConfiguration().screenWidthDp < 1024) return;

        Drawable drawable = ((ImageView) view).getDrawable();
        if (enabled) {
            drawable.clearColorFilter();
        } else {
            drawable.setColorFilter(DISABLED_COLOR, PorterDuff.Mode.SRC_ATOP);
        }
    }
}