1 /*
2  * Copyright (C) 2015 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.documentsui;
18 
19 import android.annotation.Nullable;
20 import android.app.Fragment;
21 import android.app.FragmentManager;
22 import android.app.FragmentTransaction;
23 import android.os.Bundle;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.Button;
28 import android.widget.ImageView;
29 import android.widget.TextView;
30 
31 /**
32  * A message bar displaying some info/error messages and a Dismiss button.
33  */
34 public class MessageBar extends Fragment {
35     private View mView;
36     private ViewGroup mContainer;
37 
38     /**
39      * Creates an instance of a MessageBar. Note that the new MessagBar is not visible by default,
40      * and has to be shown by calling MessageBar.show.
41      */
create(FragmentManager fm)42     public static MessageBar create(FragmentManager fm) {
43         final MessageBar fragment = new MessageBar();
44 
45         final FragmentTransaction ft = fm.beginTransaction();
46         ft.replace(R.id.container_message_bar, fragment);
47         ft.commitAllowingStateLoss();
48 
49         return fragment;
50     }
51 
52     /**
53      * Sets the info message. Can be null, in which case no info message will be displayed. The
54      * message bar layout will be adjusted accordingly.
55      */
setInfo(@ullable String info)56     public void setInfo(@Nullable String info) {
57         View infoContainer = mView.findViewById(R.id.container_info);
58         if (info != null) {
59             TextView infoText = (TextView) mView.findViewById(R.id.textview_info);
60             infoText.setText(info);
61             infoContainer.setVisibility(View.VISIBLE);
62         } else {
63             infoContainer.setVisibility(View.GONE);
64         }
65     }
66 
67     /**
68      * Sets the error message. Can be null, in which case no error message will be displayed. The
69      * message bar layout will be adjusted accordingly.
70      */
setError(@ullable String error)71     public void setError(@Nullable String error) {
72         View errorView = mView.findViewById(R.id.container_error);
73         if (error != null) {
74             TextView errorText = (TextView) mView.findViewById(R.id.textview_error);
75             errorText.setText(error);
76             errorView.setVisibility(View.VISIBLE);
77         } else {
78             errorView.setVisibility(View.GONE);
79         }
80     }
81 
82     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)83     public View onCreateView(
84             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
85 
86         mView = inflater.inflate(R.layout.fragment_message_bar, container, false);
87 
88         ImageView infoIcon = (ImageView) mView.findViewById(R.id.icon_info);
89         infoIcon.setImageResource(R.drawable.ic_dialog_info);
90 
91         ImageView errorIcon = (ImageView) mView.findViewById(R.id.icon_error);
92         errorIcon.setImageResource(R.drawable.ic_dialog_alert);
93 
94         Button dismiss = (Button) mView.findViewById(R.id.button_dismiss);
95         dismiss.setOnClickListener(
96                 new View.OnClickListener() {
97                     @Override
98                     public void onClick(View v) {
99                         hide();
100                     }
101                 });
102 
103         mContainer = container;
104 
105         return mView;
106     }
107 
hide()108     public void hide() {
109         // The container view is used to show/hide the error bar. If a container is not provided,
110         // fall back to showing/hiding the error bar View, which also works, but does not provide
111         // the same animated transition.
112         if (mContainer != null) {
113             mContainer.setVisibility(View.GONE);
114         } else {
115             mView.setVisibility(View.GONE);
116         }
117     }
118 
show()119     public void show() {
120         // The container view is used to show/hide the error bar. If a container is not provided,
121         // fall back to showing/hiding the error bar View, which also works, but does not provide
122         // the same animated transition.
123         if (mContainer != null) {
124             mContainer.setVisibility(View.VISIBLE);
125         } else {
126             mView.setVisibility(View.VISIBLE);
127         }
128     }
129 }
130