1246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan/*
2246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan * Copyright (C) 2016 The Android Open Source Project
3246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan *
4246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan * Licensed under the Apache License, Version 2.0 (the "License");
5246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan * you may not use this file except in compliance with the License.
6246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan * You may obtain a copy of the License at
7246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan *
8246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan *      http://www.apache.org/licenses/LICENSE-2.0
9246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan *
10246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan * Unless required by applicable law or agreed to in writing, software
11246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan * distributed under the License is distributed on an "AS IS" BASIS,
12246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan * See the License for the specific language governing permissions and
14246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan * limitations under the License.
15246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan */
16246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
17246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanpackage com.android.internal.app;
18246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
19246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanimport android.content.Context;
20246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanimport android.content.DialogInterface;
2155e4030f77d5e66fc9616806f4c82434602369f1Michael Kwanimport android.text.TextUtils;
2255e4030f77d5e66fc9616806f4c82434602369f1Michael Kwanimport android.view.Gravity;
23246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanimport android.view.View;
24246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanimport android.view.ViewGroup;
25246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanimport android.view.Window;
2655e4030f77d5e66fc9616806f4c82434602369f1Michael Kwanimport android.widget.FrameLayout;
2755e4030f77d5e66fc9616806f4c82434602369f1Michael Kwanimport android.widget.ImageView;
28246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanimport android.widget.ScrollView;
29246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanimport android.widget.TextView;
30246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
31246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanimport com.android.internal.app.AlertController;
32246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanimport com.android.internal.R;
33246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
34246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwanpublic class MicroAlertController extends AlertController {
35246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan    public MicroAlertController(Context context, DialogInterface di, Window window) {
36246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan        super(context, di, window);
37246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan    }
38246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
39246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan    @Override
40246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan    protected void setupContent(ViewGroup contentPanel) {
41246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan        // Special case for small screen - the scroll view is higher in hierarchy
42246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan        mScrollView = (ScrollView) mWindow.findViewById(R.id.scrollView);
43246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
44246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan        // Special case for users that only want to display a String
45246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan        mMessageView = (TextView) contentPanel.findViewById(R.id.message);
46246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan        if (mMessageView == null) {
47246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan            return;
48246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan        }
49246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
50246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan        if (mMessage != null) {
51246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan            mMessageView.setText(mMessage);
52246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan        } else {
53246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan            // no message, remove associated views
54246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan            mMessageView.setVisibility(View.GONE);
55246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan            contentPanel.removeView(mMessageView);
56246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
57246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan            if (mListView != null) {
5855e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                // has ListView, swap scrollView with ListView
59246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
6055e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                // move topPanel into top of scrollParent
61246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                View topPanel = mScrollView.findViewById(R.id.topPanel);
62246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                ((ViewGroup) topPanel.getParent()).removeView(topPanel);
6355e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                FrameLayout.LayoutParams topParams =
6455e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                        new FrameLayout.LayoutParams(topPanel.getLayoutParams());
6555e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                topParams.gravity = Gravity.TOP;
6655e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                topPanel.setLayoutParams(topParams);
67246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
6855e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                // move buttonPanel into bottom of scrollParent
69246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                View buttonPanel = mScrollView.findViewById(R.id.buttonPanel);
70246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                ((ViewGroup) buttonPanel.getParent()).removeView(buttonPanel);
7155e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                FrameLayout.LayoutParams buttonParams =
7255e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                        new FrameLayout.LayoutParams(buttonPanel.getLayoutParams());
7355e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                buttonParams.gravity = Gravity.BOTTOM;
7455e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                buttonPanel.setLayoutParams(buttonParams);
75246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan
7655e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                // remove scrollview
77246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                final ViewGroup scrollParent = (ViewGroup) mScrollView.getParent();
78246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                final int childIndex = scrollParent.indexOfChild(mScrollView);
79246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                scrollParent.removeViewAt(childIndex);
8055e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan
8155e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                // add list view
8255e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                scrollParent.addView(mListView,
83246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                        new ViewGroup.LayoutParams(
84246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                                ViewGroup.LayoutParams.MATCH_PARENT,
85246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                                ViewGroup.LayoutParams.MATCH_PARENT));
8655e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan
8755e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                // add top and button panel
8855e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                scrollParent.addView(topPanel);
8955e4030f77d5e66fc9616806f4c82434602369f1Michael Kwan                scrollParent.addView(buttonPanel);
90246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan            } else {
91246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                // no content, just hide everything
92246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan                contentPanel.setVisibility(View.GONE);
93246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan            }
94246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan        }
95246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan    }
96814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan
97814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan    @Override
98814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan    protected void setupTitle(ViewGroup topPanel) {
99814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan        super.setupTitle(topPanel);
100814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan        if (topPanel.getVisibility() == View.GONE) {
101814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan            topPanel.setVisibility(View.INVISIBLE);
102814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan        }
103814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan    }
104814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan
105814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan    @Override
106814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan    protected void setupButtons(ViewGroup buttonPanel) {
107814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan        super.setupButtons(buttonPanel);
108814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan        if (buttonPanel.getVisibility() == View.GONE) {
109814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan            buttonPanel.setVisibility(View.INVISIBLE);
110814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan        }
111814508166c2b31c84f166f9a40b89612dfe763aaMichael Kwan    }
112246caaced5f621077b5f23daf36701f68d03bf3cMichael Kwan}
113