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.content.Context;
20 import android.os.Bundle;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.Button;
25 import android.widget.RelativeLayout;
26 
27 import com.android.email.R;
28 import com.android.email.activity.UiUtilities;
29 import com.android.email.service.EmailServiceUtils;
30 
31 public class AccountSetupTypeFragment extends AccountSetupFragment
32         implements View.OnClickListener {
33     private int mLastId;
34 
35     public interface Callback extends AccountSetupFragment.Callback {
36         /**
37          * called when the user has selected a protocol type for the account
38          * @param protocol {@link EmailServiceUtils.EmailServiceInfo#protocol}
39          */
onChooseProtocol(String protocol)40         void onChooseProtocol(String protocol);
41     }
42 
newInstance()43     public static AccountSetupTypeFragment newInstance() {
44         return new AccountSetupTypeFragment();
45     }
46 
47     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)48     public View onCreateView(LayoutInflater inflater, ViewGroup container,
49             Bundle savedInstanceState) {
50         final View view = inflateTemplatedView(inflater, container,
51                 R.layout.account_setup_type_fragment, R.string.account_setup_account_type_headline);
52 
53         final Context appContext = inflater.getContext().getApplicationContext();
54 
55         final ViewGroup parent = UiUtilities.getView(view, R.id.accountTypes);
56         View lastView = parent.getChildAt(0);
57         int i = 1;
58         for (final EmailServiceUtils.EmailServiceInfo info
59                 : EmailServiceUtils.getServiceInfoList(appContext)) {
60             if (EmailServiceUtils.isServiceAvailable(appContext, info.protocol)) {
61                 // Don't show types with "hide" set
62                 if (info.hide) {
63                     continue;
64                 }
65                 inflater.inflate(R.layout.account_type, parent);
66                 final Button button = (Button)parent.getChildAt(i);
67                 if (parent instanceof RelativeLayout) {
68                     final RelativeLayout.LayoutParams params =
69                             (RelativeLayout.LayoutParams)button.getLayoutParams();
70                     params.addRule(RelativeLayout.BELOW, lastView.getId());
71                 }
72                 button.setId(i);
73                 button.setTag(info.protocol);
74                 button.setText(info.name);
75                 button.setOnClickListener(this);
76                 lastView = button;
77                 i++;
78             }
79         }
80         mLastId = i - 1;
81 
82         setNextButtonVisibility(View.INVISIBLE);
83 
84         return view;
85     }
86 
87     @Override
onClick(View v)88     public void onClick(View v) {
89         final int viewId = v.getId();
90         if (viewId <= mLastId) {
91             final String protocol = (String) v.getTag();
92             final Callback callback = (Callback) getActivity();
93             callback.onChooseProtocol(protocol);
94         } else {
95             super.onClick(v);
96         }
97     }
98 }
99