Home | History | Annotate | Download | only in widget
      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.view.View;
     22 import android.widget.FrameLayout;
     23 
     24 import com.android.camera.exif.Rational;
     25 import com.android.camera.settings.ResolutionUtil;
     26 import com.android.camera2.R;
     27 
     28 /**
     29  * Displays a dialog that allows people to choose aspect ratio. Please
     30  * instantiate this class programmatically.
     31  */
     32 public class AspectRatioDialogLayout extends FrameLayout {
     33 
     34     private AspectRatioDialogListener mListener;
     35 
     36     private View mAspectRatio4x3Button;
     37     private View mAspectRatio16x9Button;
     38     private View mConfirmButton;
     39 
     40     private Rational mAspectRatio;
     41     private int mLastOrientation;
     42 
     43     /**
     44      * Constructs a new AspectRatioDialogLayout object.
     45      *
     46      * @param context The application context.
     47      * @param defaultAspectRatio The default aspect ratio to choose.
     48      */
     49     public AspectRatioDialogLayout(Context context, Rational defaultAspectRatio) {
     50         super(context);
     51         mAspectRatio = defaultAspectRatio;
     52         mLastOrientation = context.getResources().getConfiguration().orientation;
     53         setBackgroundResource(R.color.fullscreen_dialog_background_color);
     54         inflate(context, R.layout.aspect_ratio_dialog_content, this);
     55         updateSubviewReferences();
     56     }
     57 
     58     public void setListener(AspectRatioDialogListener listener) {
     59         mListener = listener;
     60     }
     61 
     62     @Override
     63     public void onConfigurationChanged(Configuration config) {
     64         super.onConfigurationChanged(config);
     65 
     66         if (config.orientation == mLastOrientation) {
     67             return;
     68         }
     69         mLastOrientation = config.orientation;
     70 
     71         removeAllViews();
     72         inflate(getContext(), R.layout.aspect_ratio_dialog_content, this);
     73         updateSubviewReferences();
     74     }
     75 
     76     private void updateSubviewReferences() {
     77         mAspectRatio4x3Button = findViewById(R.id.aspect_ratio_4x3_button);
     78         mAspectRatio16x9Button = findViewById(R.id.aspect_ratio_16x9_button);
     79         mConfirmButton = findViewById(R.id.confirm_button);
     80 
     81         // Set aspect ratio after references to views are established.
     82         setAspectRatio(mAspectRatio);
     83 
     84         // Hook onclick events.
     85         mAspectRatio4x3Button.setOnClickListener(new OnClickListener() {
     86             @Override
     87             public void onClick(View v) {
     88                 setAspectRatio(ResolutionUtil.ASPECT_RATIO_4x3);
     89             }
     90         });
     91         mAspectRatio16x9Button.setOnClickListener(new OnClickListener() {
     92             @Override
     93             public void onClick(View v) {
     94                 setAspectRatio(ResolutionUtil.ASPECT_RATIO_16x9);
     95             }
     96         });
     97         mConfirmButton.setOnClickListener(new OnClickListener() {
     98             @Override
     99             public void onClick(View v) {
    100                 if (mListener != null) {
    101                     mListener.onConfirm(mAspectRatio);
    102                 }
    103             }
    104         });
    105     }
    106 
    107     private void setAspectRatio(Rational aspectRatio) {
    108         mAspectRatio = aspectRatio;
    109 
    110         if (mAspectRatio.equals(ResolutionUtil.ASPECT_RATIO_4x3)) {
    111             // Select 4x3 view and unselect 16x9 view.
    112             mAspectRatio4x3Button.setSelected(true);
    113             mAspectRatio16x9Button.setSelected(false);
    114         } else if (mAspectRatio.equals(ResolutionUtil.ASPECT_RATIO_16x9)) {
    115             // Select 16x9 view and unselect 4x3 view.
    116             mAspectRatio16x9Button.setSelected(true);
    117             mAspectRatio4x3Button.setSelected(false);
    118         }
    119     }
    120 
    121     public interface AspectRatioDialogListener {
    122         public void onConfirm(Rational chosenAspectRatio);
    123     }
    124 }
    125