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.content.Context;
20import android.os.Bundle;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24import android.widget.Button;
25import android.widget.TextView;
26
27import com.android.email.R;
28import com.android.email.activity.UiUtilities;
29import com.android.email.service.EmailServiceUtils;
30
31public class AccountSetupABFragment extends AccountSetupFragment {
32
33    private static final String ACCOUNT_EMAIL_ARG = "accountEmail";
34    private static final String USER_PROTOCOL_ARG = "userProtocol";
35    private static final String PROVIDER_PROTOCOL_ARG = "providerProtocol";
36
37    private String mAccountEmail;
38    private String mUserProtocol;
39    private String mProviderProtocol;
40
41    public interface Callback extends AccountSetupFragment.Callback {
42        void onABProtocolDisambiguated(String chosenProtocol);
43    }
44
45    public AccountSetupABFragment() {}
46
47    /**
48     * Setup flow fragment for disambiguating the user's choice of protocol (when launched from the
49     * system account manager) and what is indicated in providers.xml
50     *
51     * @param accountEmail Email address of account being set up
52     * @param userProtocol Protocol that the user initiated account creation for
53     * @param providerProtocol Protocol indicated in providers.xml
54     * @return Fresh ready-to-use disambiguation fragment
55     */
56    public static AccountSetupABFragment newInstance(final String accountEmail,
57            final String userProtocol, final String providerProtocol) {
58        final Bundle b = new Bundle(3);
59        b.putString(ACCOUNT_EMAIL_ARG, accountEmail);
60        b.putString(USER_PROTOCOL_ARG, userProtocol);
61        b.putString(PROVIDER_PROTOCOL_ARG, providerProtocol);
62        final AccountSetupABFragment f = new AccountSetupABFragment();
63        f.setArguments(b);
64        return f;
65    }
66
67    @Override
68    public void onCreate(Bundle savedInstanceState) {
69        super.onCreate(savedInstanceState);
70
71        final Bundle b = getArguments();
72        mAccountEmail = b.getString(ACCOUNT_EMAIL_ARG);
73        mUserProtocol = b.getString(USER_PROTOCOL_ARG);
74        mProviderProtocol = b.getString(PROVIDER_PROTOCOL_ARG);
75    }
76
77    @Override
78    public View onCreateView(LayoutInflater inflater, ViewGroup container,
79            Bundle savedInstanceState) {
80        final Context context = inflater.getContext();
81
82        final String userProtocolName =
83                EmailServiceUtils.getServiceInfo(context, mUserProtocol).name;
84        final String providerProtocolName =
85                EmailServiceUtils.getServiceInfo(context, mProviderProtocol).name;
86
87        final View view = inflateTemplatedView(inflater, container,
88                R.layout.account_setup_ab_fragment, R.string.account_setup_ab_headline);
89
90        final TextView abInstructions = UiUtilities.getView(view, R.id.ab_instructions);
91        abInstructions.setText(context.getString(R.string.account_setup_ab_instructions_format,
92                mAccountEmail, userProtocolName, providerProtocolName));
93
94        final View nextButton = UiUtilities.getView(view, R.id.next);
95        nextButton.setVisibility(View.INVISIBLE);
96
97        final Button abButtonA = UiUtilities.getView(view, R.id.ab_button_a);
98        abButtonA.setOnClickListener(this);
99        abButtonA.setText(userProtocolName);
100
101        final Button abButtonB = UiUtilities.getView(view, R.id.ab_button_b);
102        abButtonB.setOnClickListener(this);
103        abButtonB.setText(providerProtocolName);
104
105        return view;
106    }
107
108    @Override
109    public void onClick(View v) {
110        final int viewId = v.getId();
111        final Callback callback = (Callback) getActivity();
112        if (viewId == R.id.ab_button_a) {
113            callback.onABProtocolDisambiguated(mUserProtocol);
114        } else if (viewId == R.id.ab_button_b) {
115            callback.onABProtocolDisambiguated(mProviderProtocol);
116        } else {
117            super.onClick(v);
118        }
119    }
120}
121