Home | History | Annotate | Download | only in camera
      1 /*
      2  * Copyright (C) 2011 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;
     18 
     19 import com.android.camera.ui.Rotatable;
     20 import com.android.camera.ui.RotateLayout;
     21 
     22 import android.app.Activity;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.view.animation.Animation;
     27 import android.view.animation.AnimationUtils;
     28 import android.widget.Button;
     29 import android.widget.TextView;
     30 import android.widget.ProgressBar;
     31 
     32 public class RotateDialogController implements Rotatable {
     33 
     34     private static final String TAG = "RotateDialogController";
     35     private static final long ANIM_DURATION = 150;  // millis
     36 
     37     private Activity mActivity;
     38     private int mLayoutResourceID;
     39     private View mDialogRootLayout;
     40     private RotateLayout mRotateDialog;
     41     private View mRotateDialogTitleLayout;
     42     private View mRotateDialogButtonLayout;
     43     private TextView mRotateDialogTitle;
     44     private ProgressBar mRotateDialogSpinner;
     45     private TextView mRotateDialogText;
     46     private TextView mRotateDialogButton1;
     47     private TextView mRotateDialogButton2;
     48 
     49     private Animation mFadeInAnim, mFadeOutAnim;
     50 
     51     public RotateDialogController(Activity a, int layoutResource) {
     52         mActivity = a;
     53         mLayoutResourceID = layoutResource;
     54     }
     55 
     56     private void inflateDialogLayout() {
     57         if (mDialogRootLayout == null) {
     58             ViewGroup layoutRoot = (ViewGroup) mActivity.getWindow().getDecorView();
     59             LayoutInflater inflater = mActivity.getLayoutInflater();
     60             View v = inflater.inflate(mLayoutResourceID, layoutRoot);
     61             mDialogRootLayout = v.findViewById(R.id.rotate_dialog_root_layout);
     62             mRotateDialog = (RotateLayout) v.findViewById(R.id.rotate_dialog_layout);
     63             mRotateDialogTitleLayout = v.findViewById(R.id.rotate_dialog_title_layout);
     64             mRotateDialogButtonLayout = v.findViewById(R.id.rotate_dialog_button_layout);
     65             mRotateDialogTitle = (TextView) v.findViewById(R.id.rotate_dialog_title);
     66             mRotateDialogSpinner = (ProgressBar) v.findViewById(R.id.rotate_dialog_spinner);
     67             mRotateDialogText = (TextView) v.findViewById(R.id.rotate_dialog_text);
     68             mRotateDialogButton1 = (Button) v.findViewById(R.id.rotate_dialog_button1);
     69             mRotateDialogButton2 = (Button) v.findViewById(R.id.rotate_dialog_button2);
     70 
     71             mFadeInAnim = AnimationUtils.loadAnimation(
     72                     mActivity, android.R.anim.fade_in);
     73             mFadeOutAnim = AnimationUtils.loadAnimation(
     74                     mActivity, android.R.anim.fade_out);
     75             mFadeInAnim.setDuration(ANIM_DURATION);
     76             mFadeOutAnim.setDuration(ANIM_DURATION);
     77         }
     78     }
     79 
     80     @Override
     81     public void setOrientation(int orientation) {
     82         inflateDialogLayout();
     83         mRotateDialog.setOrientation(orientation);
     84     }
     85 
     86     public void resetRotateDialog() {
     87         inflateDialogLayout();
     88         mRotateDialogTitleLayout.setVisibility(View.GONE);
     89         mRotateDialogSpinner.setVisibility(View.GONE);
     90         mRotateDialogButton1.setVisibility(View.GONE);
     91         mRotateDialogButton2.setVisibility(View.GONE);
     92         mRotateDialogButtonLayout.setVisibility(View.GONE);
     93     }
     94 
     95     private void fadeOutDialog() {
     96         mDialogRootLayout.startAnimation(mFadeOutAnim);
     97         mDialogRootLayout.setVisibility(View.GONE);
     98     }
     99 
    100     private void fadeInDialog() {
    101         mDialogRootLayout.startAnimation(mFadeInAnim);
    102         mDialogRootLayout.setVisibility(View.VISIBLE);
    103     }
    104 
    105     public void dismissDialog() {
    106         if (mDialogRootLayout != null && mDialogRootLayout.getVisibility() != View.GONE) {
    107             fadeOutDialog();
    108         }
    109     }
    110 
    111     public void showAlertDialog(String title, String msg, String button1Text,
    112                 final Runnable r1, String button2Text, final Runnable r2) {
    113         resetRotateDialog();
    114 
    115         mRotateDialogTitle.setText(title);
    116         mRotateDialogTitleLayout.setVisibility(View.VISIBLE);
    117 
    118         mRotateDialogText.setText(msg);
    119 
    120         if (button1Text != null) {
    121             mRotateDialogButton1.setText(button1Text);
    122             mRotateDialogButton1.setVisibility(View.VISIBLE);
    123             mRotateDialogButton1.setOnClickListener(new View.OnClickListener() {
    124                 @Override
    125                 public void onClick(View v) {
    126                     if (r1 != null) r1.run();
    127                     dismissDialog();
    128                 }
    129             });
    130             mRotateDialogButtonLayout.setVisibility(View.VISIBLE);
    131         }
    132         if (button2Text != null) {
    133             mRotateDialogButton2.setText(button2Text);
    134             mRotateDialogButton2.setVisibility(View.VISIBLE);
    135             mRotateDialogButton2.setOnClickListener(new View.OnClickListener() {
    136                 @Override
    137                 public void onClick(View v) {
    138                     if (r2 != null) r2.run();
    139                     dismissDialog();
    140                 }
    141             });
    142             mRotateDialogButtonLayout.setVisibility(View.VISIBLE);
    143         }
    144 
    145         fadeInDialog();
    146     }
    147 
    148     public void showWaitingDialog(String msg) {
    149         resetRotateDialog();
    150 
    151         mRotateDialogText.setText(msg);
    152         mRotateDialogSpinner.setVisibility(View.VISIBLE);
    153 
    154         fadeInDialog();
    155     }
    156 
    157 }
    158