blob: 5c6213fdc933e1f16e38130c2e32c73966eea69c [file] [log] [blame]
Ben Kwac42fa402015-09-16 08:04:37 -07001/*
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
17package com.android.documentsui;
18
19import android.annotation.Nullable;
20import android.app.Fragment;
21import android.app.FragmentManager;
22import android.app.FragmentTransaction;
23import android.os.Bundle;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.Button;
28import android.widget.ImageView;
29import android.widget.TextView;
30
31/**
32 * A message bar displaying some info/error messages and a Dismiss button.
33 */
34public 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 */
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 */
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 */
71 public void setError(@Nullable String error) {
Ben Kwa379e1762015-09-21 10:49:52 -070072 View errorView = mView.findViewById(R.id.container_error);
Ben Kwac42fa402015-09-16 08:04:37 -070073 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
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
Steve McKayf68210e2015-11-03 15:23:16 -0800108 public void hide() {
Ben Kwac42fa402015-09-16 08:04:37 -0700109 // 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
Steve McKayf68210e2015-11-03 15:23:16 -0800119 public void show() {
Ben Kwac42fa402015-09-16 08:04:37 -0700120 // 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}