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