AccountSetupFragment.java revision ae980cd685a867b3fecfd38c876fd18d98b7c86b
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        headlineView.setText(headline);
85
86        final ViewGroup contentContainer =
87                (ViewGroup) template.findViewById(R.id.setup_fragment_content);
88        inflater.inflate(contentLayout, contentContainer, true);
89
90        mNextButton = UiUtilities.getView(template, R.id.next);
91        mNextButton.setOnClickListener(this);
92        mPreviousButton = UiUtilities.getView(template, R.id.previous);
93        mPreviousButton.setOnClickListener(this);
94        return template;
95    }
96
97    @Override
98    public void onClick(View v) {
99        final int viewId = v.getId();
100        final Callback callback = (Callback) getActivity();
101
102        if (viewId == R.id.next) {
103            callback.onNextButton();
104        } else if (viewId == R.id.previous) {
105            callback.onBackPressed();
106        }
107    }
108
109    public void setNextButtonEnabled(boolean enabled) {
110        if (mNextButton != null) {
111            mNextButton.setEnabled(enabled);
112        }
113    }
114
115    /**
116     * Set visibility of the "previous" button
117     * @param visibility {@link View#INVISIBLE}, {@link View#VISIBLE}, {@link View#GONE}
118     */
119    public void setPreviousButtonVisibility(int visibility) {
120        mPreviousButton.setVisibility(visibility);
121    }
122
123    /**
124     * Set the visibility of the "next" button
125     * @param visibility {@link View#INVISIBLE}, {@link View#VISIBLE}, {@link View#GONE}
126     */
127    public void setNextButtonVisibility(int visibility) {
128        mNextButton.setVisibility(visibility);
129    }
130}
131