1/*
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
39    private static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
40    private static final String EXTRA_ACTION = "EXTRA_ACTION";
41
42    public interface OnActionListener {
43        public void onActionPerformed();
44    }
45
46    public static PrintErrorFragment newInstance(CharSequence message, int action) {
47        Bundle arguments = new Bundle();
48        arguments.putCharSequence(EXTRA_MESSAGE, message);
49        arguments.putInt(EXTRA_ACTION, action);
50        PrintErrorFragment fragment = new PrintErrorFragment();
51        fragment.setArguments(arguments);
52        return fragment;
53    }
54
55    @Override
56    public View onCreateView(LayoutInflater inflater, ViewGroup root,
57            Bundle savedInstanceState) {
58        return inflater.inflate(R.layout.print_error_fragment, root, false);
59    }
60
61    @Override
62    public void onViewCreated(View view, Bundle savedInstanceState) {
63        super.onViewCreated(view, savedInstanceState);
64
65        CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
66
67        if (!TextUtils.isEmpty(message)) {
68            TextView messageView = (TextView) view.findViewById(R.id.message);
69            messageView.setText(message);
70        }
71
72        Button actionButton = (Button) view.findViewById(R.id.action_button);
73
74        final int action = getArguments().getInt(EXTRA_ACTION);
75        switch (action) {
76            case ACTION_RETRY: {
77                actionButton.setVisibility(View.VISIBLE);
78                actionButton.setText(R.string.print_error_retry);
79            } break;
80
81            case ACTION_NONE: {
82                actionButton.setVisibility(View.GONE);
83            } break;
84        }
85
86        actionButton.setOnClickListener(new OnClickListener() {
87            @Override
88            public void onClick(View view) {
89                Activity activity = getActivity();
90                if (activity instanceof OnActionListener) {
91                    ((OnActionListener) getActivity()).onActionPerformed();
92                }
93            }
94        });
95    }
96}
97