xie
2024-11-23 3fdfdea0721fe7a36f6aaa509075f01a194f6748
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (C) 2014 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.widget;
 
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
 
import com.android.camera.CaptureLayoutHelper;
import com.android.camera.ShutterButton;
import com.android.camera.debug.Log;
import com.android.camera.ui.PreviewOverlay;
import com.android.camera.ui.TouchCoordinate;
import com.android.camera2.R;
 
/**
 * ModeOptionsOverlay is a FrameLayout which positions mode options in
 * in the bottom of the preview that is visible above the bottom bar.
 */
public class ModeOptionsOverlay extends FrameLayout
    implements PreviewOverlay.OnPreviewTouchedListener,
               ShutterButton.OnShutterButtonListener {
 
    private final static Log.Tag TAG = new Log.Tag("ModeOptionsOverlay");
 
    private static final int BOTTOMBAR_OPTIONS_TIMEOUT_MS = 2000;
    private static final int BOTTOM_RIGHT = Gravity.BOTTOM | Gravity.RIGHT;
    private static final int TOP_RIGHT = Gravity.TOP | Gravity.RIGHT;
 
    private ModeOptions mModeOptions;
    // need a reference to set the onClickListener and fix the layout gravity on orientation change
    private LinearLayout mModeOptionsToggle;
    // need a reference to fix the rotation on orientation change
    private ImageView mThreeDots;
    private CaptureLayoutHelper mCaptureLayoutHelper = null;
 
    public ModeOptionsOverlay(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    /**
     * Whether the mode options are hidden.
     */
    public boolean isModeOptionsHidden() {
        return mModeOptions.isHiddenOrHiding();
    }
 
    /**
     * Gets the current width of the mode options toggle including the three dots and various mode
     * option indicators.
     */
    public float getModeOptionsToggleWidth() {
        return mModeOptionsToggle.getWidth();
    }
 
    /**
     * Sets a capture layout helper to query layout rect from.
     */
    public void setCaptureLayoutHelper(CaptureLayoutHelper helper) {
        mCaptureLayoutHelper = helper;
    }
 
    public void setToggleClickable(boolean clickable) {
        mModeOptionsToggle.setClickable(clickable);
    }
 
    public void showExposureOptions() {
        mModeOptions.showExposureOptions();
    }
 
    /**
     * Sets the mode options listener.
     *
     * @param listener The listener to be set.
     */
    public void setModeOptionsListener(ModeOptions.Listener listener) {
        mModeOptions.setListener(listener);
    }
 
    @Override
    public void onFinishInflate() {
        mModeOptions = (ModeOptions) findViewById(R.id.mode_options);
        mModeOptions.setClickable(true);
        mModeOptions.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeModeOptions();
            }
        });
 
        mModeOptionsToggle = (LinearLayout) findViewById(R.id.mode_options_toggle);
        mModeOptionsToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mModeOptions.animateVisible();
            }
        });
        mModeOptions.setViewToShowHide(mModeOptionsToggle);
 
        mThreeDots = (ImageView) findViewById(R.id.three_dots);
    }
 
    @Override
    public void onPreviewTouched(MotionEvent ev) {
        closeModeOptions();
    }
 
    @Override
    public void onShutterButtonClick() {
        closeModeOptions();
    }
 
    @Override
    public void onShutterCoordinate(TouchCoordinate coord) {
        // Do nothing.
    }
 
    @Override
    public void onShutterButtonFocus(boolean pressed) {
        // noop
    }
 
    @Override
    public void onShutterButtonLongPressed() {
        // noop
    }
 
    /**
     * Schedule (or re-schedule) the options menu to be closed after a number
     * of milliseconds.  If the options menu is already closed, nothing is
     * scheduled.
     */
    public void closeModeOptions() {
        mModeOptions.animateHidden();
    }
 
    @Override
    public void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        checkOrientation(configuration.orientation);
    }
 
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mCaptureLayoutHelper == null) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            Log.e(TAG, "Capture layout helper needs to be set first.");
        } else {
            RectF uncoveredPreviewRect = mCaptureLayoutHelper.getUncoveredPreviewRect();
            super.onMeasure(MeasureSpec.makeMeasureSpec(
                            (int) uncoveredPreviewRect.width(), MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec((int) uncoveredPreviewRect.height(),
                            MeasureSpec.EXACTLY)
            );
        }
    }
 
    /**
     * Set the layout gravity of the child layout to be bottom or top right
     * depending on orientation.
     */
    private void checkOrientation(int orientation) {
        final boolean isPortrait = (Configuration.ORIENTATION_PORTRAIT == orientation);
 
        final int modeOptionsDimension = (int) getResources()
            .getDimension(R.dimen.mode_options_height);
 
        FrameLayout.LayoutParams modeOptionsParams
            = (FrameLayout.LayoutParams) mModeOptions.getLayoutParams();
        FrameLayout.LayoutParams modeOptionsToggleParams
            = (FrameLayout.LayoutParams) mModeOptionsToggle.getLayoutParams();
 
        if (isPortrait) {
            modeOptionsParams.height = modeOptionsDimension;
            modeOptionsParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            modeOptionsParams.gravity = Gravity.BOTTOM;
 
            modeOptionsToggleParams.gravity = BOTTOM_RIGHT;
 
            mThreeDots.setImageResource(R.drawable.ic_options_port);
        } else {
            modeOptionsParams.width = modeOptionsDimension;
            modeOptionsParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            modeOptionsParams.gravity = Gravity.RIGHT;
 
            modeOptionsToggleParams.gravity = TOP_RIGHT;
 
            mThreeDots.setImageResource(R.drawable.ic_options_land);
        }
 
        requestLayout();
    }
}