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 
17 package com.android.email.activity.setup;
18 
19 import android.app.Fragment;
20 import android.os.Bundle;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.TextView;
25 
26 import com.android.email.R;
27 import com.android.email.activity.UiUtilities;
28 import 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  */
35 public 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 {
onNextButton()43         void onNextButton();
onBackPressed()44         void onBackPressed();
45     }
46 
47     @Override
onCreate(Bundle savedInstanceState)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
onSaveInstanceState(Bundle outState)56     public void onSaveInstanceState(Bundle outState) {
57         super.onSaveInstanceState(outState);
58         outState.putInt(SAVESTATE_STATE, mState);
59     }
60 
setState(int state)61     public void setState(int state) {
62         mState = state;
63     }
64 
getState()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      */
inflateTemplatedView(final LayoutInflater inflater, final ViewGroup container, final int contentLayout, final int headline)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
onClick(View v)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 
setNextButtonEnabled(boolean enabled)114     public void setNextButtonEnabled(boolean enabled) {
115         if (mNextButton != null) {
116             mNextButton.setEnabled(enabled);
117         }
118     }
119 
isNextButtonEnabled()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      */
setPreviousButtonVisibility(int visibility)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      */
setNextButtonVisibility(int visibility)137     public void setNextButtonVisibility(int visibility) {
138         mNextButton.setVisibility(visibility);
139     }
140 }
141