1 /*
2  * Copyright (C) 2014 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 
17 package com.android.camera.widget;
18 
19 import android.content.Context;
20 import android.content.res.Configuration;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.FrameLayout;
24 
25 import com.android.camera2.R;
26 
27 public class AspectRatioDialogLayout extends FrameLayout {
28     private View mConfirmButton;
29     private AspectRatioSelector mAspectRatioSelector;
30     private int mLastOrientation;
31     private AspectRatioChangedListener mListener;
32     private boolean mInitialized;
33     private AspectRatioSelector.AspectRatio mAspectRatio;
34 
35     public interface AspectRatioChangedListener {
onAspectRatioChanged(AspectRatioSelector.AspectRatio aspectRatio)36         public void onAspectRatioChanged(AspectRatioSelector.AspectRatio aspectRatio);
37     }
38 
AspectRatioDialogLayout(Context context, AttributeSet attrs)39     public AspectRatioDialogLayout(Context context, AttributeSet attrs) {
40         super(context, attrs);
41         mLastOrientation = getResources().getConfiguration().orientation;
42     }
43 
44     @Override
onFinishInflate()45     public void onFinishInflate() {
46         updateViewReference();
47     }
48 
updateViewReference()49     private void updateViewReference() {
50         mAspectRatioSelector = (AspectRatioSelector) findViewById(R.id.aspect_ratio_selector);
51         mConfirmButton = findViewById(R.id.confirm_button);
52         mConfirmButton.setOnClickListener(new OnClickListener() {
53             @Override
54             public void onClick(View v) {
55                 if (mListener != null) {
56                     mListener.onAspectRatioChanged(mAspectRatioSelector.getAspectRatio());
57                 }
58             }
59         });
60         if (mInitialized) {
61             mAspectRatioSelector.setAspectRatio(mAspectRatio);
62         }
63     }
64 
65     @Override
onConfigurationChanged(Configuration config)66     public void onConfigurationChanged(Configuration config) {
67         super.onConfigurationChanged(config);
68         if (config.orientation == mLastOrientation) {
69             return;
70         }
71         mLastOrientation = config.orientation;
72         mAspectRatio = mAspectRatioSelector.getAspectRatio();
73         removeAllViews();
74         inflate(getContext(), R.layout.aspect_ratio_dialog_content, this);
75         updateViewReference();
76     }
77 
setAspectRatio(AspectRatioSelector.AspectRatio aspectRatio)78     public void setAspectRatio(AspectRatioSelector.AspectRatio aspectRatio) {
79         mAspectRatioSelector.setAspectRatio(aspectRatio);
80     }
81 
initialize(AspectRatioChangedListener listener, AspectRatioSelector.AspectRatio aspectRatio)82     public void initialize(AspectRatioChangedListener listener,
83             AspectRatioSelector.AspectRatio aspectRatio) {
84         mInitialized = true;
85         mListener = listener;
86         mAspectRatio = aspectRatio;
87         if (mAspectRatioSelector != null) {
88             mAspectRatioSelector.setAspectRatio(mAspectRatio);
89         }
90     }
91 }
92