1 /*
2  * Copyright (C) 2016 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.internal.app;
18 
19 import android.content.Context;
20 import android.content.DialogInterface;
21 import android.text.TextUtils;
22 import android.view.Gravity;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.view.Window;
26 import android.widget.FrameLayout;
27 import android.widget.ImageView;
28 import android.widget.ScrollView;
29 import android.widget.TextView;
30 
31 import com.android.internal.app.AlertController;
32 import com.android.internal.R;
33 
34 public class MicroAlertController extends AlertController {
MicroAlertController(Context context, DialogInterface di, Window window)35     public MicroAlertController(Context context, DialogInterface di, Window window) {
36         super(context, di, window);
37     }
38 
39     @Override
setupContent(ViewGroup contentPanel)40     protected void setupContent(ViewGroup contentPanel) {
41         // Special case for small screen - the scroll view is higher in hierarchy
42         mScrollView = (ScrollView) mWindow.findViewById(R.id.scrollView);
43 
44         // Special case for users that only want to display a String
45         mMessageView = (TextView) contentPanel.findViewById(R.id.message);
46         if (mMessageView == null) {
47             return;
48         }
49 
50         if (mMessage != null) {
51             mMessageView.setText(mMessage);
52         } else {
53             // no message, remove associated views
54             mMessageView.setVisibility(View.GONE);
55             contentPanel.removeView(mMessageView);
56 
57             if (mListView != null) {
58                 // has ListView, swap scrollView with ListView
59 
60                 // move topPanel into top of scrollParent
61                 View topPanel = mScrollView.findViewById(R.id.topPanel);
62                 ((ViewGroup) topPanel.getParent()).removeView(topPanel);
63                 FrameLayout.LayoutParams topParams =
64                         new FrameLayout.LayoutParams(topPanel.getLayoutParams());
65                 topParams.gravity = Gravity.TOP;
66                 topPanel.setLayoutParams(topParams);
67 
68                 // move buttonPanel into bottom of scrollParent
69                 View buttonPanel = mScrollView.findViewById(R.id.buttonPanel);
70                 ((ViewGroup) buttonPanel.getParent()).removeView(buttonPanel);
71                 FrameLayout.LayoutParams buttonParams =
72                         new FrameLayout.LayoutParams(buttonPanel.getLayoutParams());
73                 buttonParams.gravity = Gravity.BOTTOM;
74                 buttonPanel.setLayoutParams(buttonParams);
75 
76                 // remove scrollview
77                 final ViewGroup scrollParent = (ViewGroup) mScrollView.getParent();
78                 final int childIndex = scrollParent.indexOfChild(mScrollView);
79                 scrollParent.removeViewAt(childIndex);
80 
81                 // add list view
82                 scrollParent.addView(mListView,
83                         new ViewGroup.LayoutParams(
84                                 ViewGroup.LayoutParams.MATCH_PARENT,
85                                 ViewGroup.LayoutParams.MATCH_PARENT));
86 
87                 // add top and button panel
88                 scrollParent.addView(topPanel);
89                 scrollParent.addView(buttonPanel);
90             } else {
91                 // no content, just hide everything
92                 contentPanel.setVisibility(View.GONE);
93             }
94         }
95     }
96 
97     @Override
setupTitle(ViewGroup topPanel)98     protected void setupTitle(ViewGroup topPanel) {
99         super.setupTitle(topPanel);
100         if (topPanel.getVisibility() == View.GONE) {
101             topPanel.setVisibility(View.INVISIBLE);
102         }
103     }
104 
105     @Override
setupButtons(ViewGroup buttonPanel)106     protected void setupButtons(ViewGroup buttonPanel) {
107         super.setupButtons(buttonPanel);
108         if (buttonPanel.getVisibility() == View.GONE) {
109             buttonPanel.setVisibility(View.INVISIBLE);
110         }
111     }
112 }
113