10f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck/*
20f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck * Copyright (C) 2011 The Android Open Source Project
30f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck *
40f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck * Licensed under the Apache License, Version 2.0 (the "License");
50f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck * you may not use this file except in compliance with the License.
60f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck * You may obtain a copy of the License at
70f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck *
80f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck *      http://www.apache.org/licenses/LICENSE-2.0
90f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck *
100f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck * Unless required by applicable law or agreed to in writing, software
110f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck * distributed under the License is distributed on an "AS IS" BASIS,
120f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck * See the License for the specific language governing permissions and
140f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck * limitations under the License.
150f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck */
160f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckpackage com.android.browser;
170f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
180f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.content.Context;
190f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.util.AttributeSet;
200f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.view.ContextThemeWrapper;
210f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.view.View;
220f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.view.View.OnClickListener;
230f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.widget.ArrayAdapter;
240f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.widget.Button;
250f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.widget.LinearLayout;
260f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.widget.ProgressBar;
270f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.widget.Spinner;
280f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport android.widget.TextView;
290f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
300f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckimport com.android.browser.DeviceAccountLogin.AutoLoginCallback;
310f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
320f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reckpublic class AutologinBar extends LinearLayout implements OnClickListener,
330f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        AutoLoginCallback {
340f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
350f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    protected Spinner mAutoLoginAccount;
360f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    protected Button mAutoLoginLogin;
370f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    protected ProgressBar mAutoLoginProgress;
380f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    protected TextView mAutoLoginError;
390f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    protected View mAutoLoginCancel;
400f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    protected DeviceAccountLogin mAutoLoginHandler;
410f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    protected ArrayAdapter<String> mAccountsAdapter;
420f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    protected TitleBar mTitleBar;
430f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
440f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    public AutologinBar(Context context) {
450f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        super(context);
460f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    }
470f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
480f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    public AutologinBar(Context context, AttributeSet attrs) {
490f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        super(context, attrs);
500f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    }
510f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
520f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    public AutologinBar(Context context, AttributeSet attrs, int defStyle) {
530f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        super(context, attrs, defStyle);
540f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    }
550f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
560f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    @Override
570f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    protected void onFinishInflate() {
580f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        super.onFinishInflate();
590f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginAccount = (Spinner) findViewById(R.id.autologin_account);
600f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginLogin = (Button) findViewById(R.id.autologin_login);
610f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginLogin.setOnClickListener(this);
620f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginProgress = (ProgressBar) findViewById(R.id.autologin_progress);
630f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginError = (TextView) findViewById(R.id.autologin_error);
640f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginCancel = findViewById(R.id.autologin_close);
650f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginCancel.setOnClickListener(this);
660f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    }
670f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
680f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    public void setTitleBar(TitleBar titleBar) {
690f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mTitleBar = titleBar;
700f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    }
710f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
720f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    @Override
730f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    public void onClick(View v) {
740f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        if (mAutoLoginCancel == v) {
750f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            if (mAutoLoginHandler != null) {
760f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                mAutoLoginHandler.cancel();
770f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                mAutoLoginHandler = null;
780f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            }
790f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            hideAutoLogin(true);
800f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        } else if (mAutoLoginLogin == v) {
810f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            if (mAutoLoginHandler != null) {
820f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                mAutoLoginAccount.setEnabled(false);
830f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                mAutoLoginLogin.setEnabled(false);
840f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                mAutoLoginProgress.setVisibility(View.VISIBLE);
850f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                mAutoLoginError.setVisibility(View.GONE);
860f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                mAutoLoginHandler.login(
870f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                        mAutoLoginAccount.getSelectedItemPosition(), this);
880f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            }
890f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        }
900f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    }
910f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
920f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    public void updateAutoLogin(Tab tab, boolean animate) {
930f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        DeviceAccountLogin login = tab.getDeviceAccountLogin();
940f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        if (login != null) {
950f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            mAutoLoginHandler = login;
960f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            ContextThemeWrapper wrapper = new ContextThemeWrapper(mContext,
970f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    android.R.style.Theme_Holo_Light);
980f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            mAccountsAdapter = new ArrayAdapter<String>(wrapper,
990f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    android.R.layout.simple_spinner_item, login.getAccountNames());
1000f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            mAccountsAdapter.setDropDownViewResource(
1010f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    android.R.layout.simple_spinner_dropdown_item);
1020f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            mAutoLoginAccount.setAdapter(mAccountsAdapter);
1030f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            mAutoLoginAccount.setSelection(0);
1040f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            mAutoLoginAccount.setEnabled(true);
1050f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            mAutoLoginLogin.setEnabled(true);
1060f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            mAutoLoginProgress.setVisibility(View.INVISIBLE);
1070f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            mAutoLoginError.setVisibility(View.GONE);
1080f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            switch (login.getState()) {
1090f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                case DeviceAccountLogin.PROCESSING:
1100f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    mAutoLoginAccount.setEnabled(false);
1110f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    mAutoLoginLogin.setEnabled(false);
1120f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    mAutoLoginProgress.setVisibility(View.VISIBLE);
1130f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    break;
1140f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                case DeviceAccountLogin.FAILED:
1150f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    mAutoLoginProgress.setVisibility(View.INVISIBLE);
1160f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    mAutoLoginError.setVisibility(View.VISIBLE);
1170f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    break;
1180f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                case DeviceAccountLogin.INITIAL:
1190f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    break;
1200f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                default:
1210f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck                    throw new IllegalStateException();
1220f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            }
1230f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            showAutoLogin(animate);
1240f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        } else {
1250f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck            hideAutoLogin(animate);
1260f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        }
1270f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    }
1280f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
1290f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    void showAutoLogin(boolean animate) {
1300f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mTitleBar.showAutoLogin(animate);
1310f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    }
1320f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
1330f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    void hideAutoLogin(boolean animate) {
1340f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mTitleBar.hideAutoLogin(animate);
1350f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    }
1360f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
1370f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    @Override
1380f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    public void loginFailed() {
1390f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginAccount.setEnabled(true);
1400f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginLogin.setEnabled(true);
1410f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginProgress.setVisibility(View.INVISIBLE);
1420f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck        mAutoLoginError.setVisibility(View.VISIBLE);
1430f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck    }
1440f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck
1450f602f3933dcd88702fdb514b6611e3066ca7a2bJohn Reck}
146