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