AccountSetupFragment.java revision 0dae4d3ffb65eb23d5a2ea19acd4603cfba914cd
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.email.activity.setup;
18
19import android.app.Fragment;
20import android.os.Bundle;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24import android.widget.TextView;
25
26import com.android.email.R;
27import com.android.email.activity.UiUtilities;
28import com.google.common.annotations.VisibleForTesting;
29
30/**
31 * Superclass for setup UI fragments.
32 * Currently holds a super-interface for the callbacks, as well as the state it was launched for so
33 * we can unwind things correctly when the user navigates the back stack.
34 */
35public class AccountSetupFragment extends Fragment implements View.OnClickListener {
36    private static final String SAVESTATE_STATE = "AccountSetupFragment.state";
37    private int mState;
38
39    protected View mNextButton;
40    protected View mPreviousButton;
41
42    public interface Callback {
43        void onNextButton();
44        void onBackPressed();
45    }
46
47    @Override
48    public void onCreate(Bundle savedInstanceState) {
49        super.onCreate(savedInstanceState);
50        if (savedInstanceState != null) {
51            mState = savedInstanceState.getInt(SAVESTATE_STATE);
52        }
53    }
54
55    @Override
56    public void onSaveInstanceState(Bundle outState) {
57        super.onSaveInstanceState(outState);
58        outState.putInt(SAVESTATE_STATE, mState);
59    }
60
61    public void setState(int state) {
62        mState = state;
63    }
64
65    public int getState() {
66        return mState;
67    }
68
69    /**
70     * This method wraps the given content layout with the chrome appropriate for the account setup
71     * flow. It also attaches itself as a click handler to the previous and next buttons.
72     *
73     * @param inflater LayoutInflater scoped to the appropriate context
74     * @param container ViewGroup to inflate the view into
75     * @param contentLayout Resource ID of the main content layout to insert into the template
76     * @param headline Resource ID of the headline string
77     * @return Fully inflated view hierarchy.
78     */
79    protected View inflateTemplatedView(final LayoutInflater inflater, final ViewGroup container,
80            final int contentLayout, final int headline) {
81        final View template = inflater.inflate(R.layout.account_setup_template, container, false);
82
83        TextView headlineView = UiUtilities.getView(template, R.id.headline);
84        if (headline > 0) {
85            headlineView.setText(headline);
86            headlineView.setVisibility(View.VISIBLE);
87        } else {
88            headlineView.setVisibility(View.GONE);
89        }
90
91        final ViewGroup contentContainer =
92                (ViewGroup) template.findViewById(R.id.setup_fragment_content);
93        inflater.inflate(contentLayout, contentContainer, true);
94
95        mNextButton = UiUtilities.getView(template, R.id.next);
96        mNextButton.setOnClickListener(this);
97        mPreviousButton = UiUtilities.getView(template, R.id.previous);
98        mPreviousButton.setOnClickListener(this);
99        return template;
100    }
101
102    @Override
103    public void onClick(View v) {
104        final int viewId = v.getId();
105        final Callback callback = (Callback) getActivity();
106
107        if (viewId == R.id.next) {
108            callback.onNextButton();
109        } else if (viewId == R.id.previous) {
110            callback.onBackPressed();
111        }
112    }
113
114    public void setNextButtonEnabled(boolean enabled) {
115        if (mNextButton != null) {
116            mNextButton.setEnabled(enabled);
117        }
118    }
119
120    public boolean isNextButtonEnabled() {
121        return mNextButton.isEnabled();
122    }
123
124
125    /**
126     * Set visibility of the "previous" button
127     * @param visibility {@link View#INVISIBLE}, {@link View#VISIBLE}, {@link View#GONE}
128     */
129    public void setPreviousButtonVisibility(int visibility) {
130        mPreviousButton.setVisibility(visibility);
131    }
132
133    /**
134     * Set the visibility of the "next" button
135     * @param visibility {@link View#INVISIBLE}, {@link View#VISIBLE}, {@link View#GONE}
136     */
137    public void setNextButtonVisibility(int visibility) {
138        mNextButton.setVisibility(visibility);
139    }
140}
141