blob: b7083566edf1b29b7529e4b72d5ef74f5a45e159 [file] [log] [blame]
Svetoslava798c0a2014-05-15 10:47:19 -07001/*
2 * Copyright (C) 2014 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.printspooler.ui;
18
19import android.app.Activity;
20import android.app.Fragment;
21import android.os.Bundle;
22import android.text.TextUtils;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26import android.view.View.OnClickListener;
27import android.widget.Button;
28import android.widget.TextView;
29
30import com.android.printspooler.R;
31
32/**
33 * Fragment for showing an error UI.
34 */
35public final class PrintErrorFragment extends Fragment {
36 public static final int ACTION_NONE = 0;
37 public static final int ACTION_RETRY = 1;
38 public static final int ACTION_CONFIRM = 2;
39
40 public interface OnActionListener {
41 public void onActionPerformed();
42 }
43
44 private static final String EXTRA_ERROR_MESSAGE = "EXTRA_ERROR_MESSAGE";
45 private static final String EXTRA_ACTION = "EXTRA_ACTION";
46
47 public static PrintErrorFragment newInstance(CharSequence errorMessage, int action) {
48 PrintErrorFragment instance = new PrintErrorFragment();
49 Bundle arguments = new Bundle();
50 arguments.putCharSequence(EXTRA_ERROR_MESSAGE, errorMessage);
51 arguments.putInt(EXTRA_ACTION, action);
52 instance.setArguments(arguments);
53 return instance;
54 }
55
56 @Override
57 public View onCreateView(LayoutInflater inflater, ViewGroup root,
58 Bundle savedInstanceState) {
59 return inflater.inflate(R.layout.print_error_fragment, root, false);
60 }
61
62 @Override
63 public void onViewCreated(View view, Bundle savedInstanceState) {
64 super.onViewCreated(view, savedInstanceState);
65
66 Bundle arguments = getArguments();
67
68 CharSequence error = arguments.getString(EXTRA_ERROR_MESSAGE);
69 if (!TextUtils.isEmpty(error)) {
70 TextView message = (TextView) view.findViewById(R.id.message);
71 message.setText(error);
72 }
73
74 Button actionButton = (Button) view.findViewById(R.id.action_button);
75
76 final int action = getArguments().getInt(EXTRA_ACTION);
77 switch (action) {
78 case ACTION_RETRY: {
79 actionButton.setVisibility(View.VISIBLE);
80 actionButton.setText(R.string.print_error_retry);
81 } break;
82
83 case ACTION_CONFIRM: {
84 actionButton.setVisibility(View.VISIBLE);
85 actionButton.setText(android.R.string.ok);
86 } break;
87
88 case ACTION_NONE: {
89 actionButton.setVisibility(View.GONE);
90 } break;
91 }
92
93 actionButton.setOnClickListener(new OnClickListener() {
94 @Override
95 public void onClick(View view) {
96 Activity activity = getActivity();
97 if (activity instanceof OnActionListener) {
98 ((OnActionListener) getActivity()).onActionPerformed();
99 }
100 }
101 });
102 }
103}