Home | History | Annotate | Download | only in browser
      1 /*
      2  * Copyright (C) 2011 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 package com.android.browser;
     17 
     18 import android.content.Context;
     19 import android.util.AttributeSet;
     20 import android.view.ContextThemeWrapper;
     21 import android.view.View;
     22 import android.view.View.OnClickListener;
     23 import android.widget.ArrayAdapter;
     24 import android.widget.Button;
     25 import android.widget.LinearLayout;
     26 import android.widget.ProgressBar;
     27 import android.widget.Spinner;
     28 import android.widget.TextView;
     29 
     30 import com.android.browser.DeviceAccountLogin.AutoLoginCallback;
     31 
     32 public class AutologinBar extends LinearLayout implements OnClickListener,
     33         AutoLoginCallback {
     34 
     35     protected Spinner mAutoLoginAccount;
     36     protected Button mAutoLoginLogin;
     37     protected ProgressBar mAutoLoginProgress;
     38     protected TextView mAutoLoginError;
     39     protected View mAutoLoginCancel;
     40     protected DeviceAccountLogin mAutoLoginHandler;
     41     protected ArrayAdapter<String> mAccountsAdapter;
     42     protected TitleBar mTitleBar;
     43 
     44     public AutologinBar(Context context) {
     45         super(context);
     46     }
     47 
     48     public AutologinBar(Context context, AttributeSet attrs) {
     49         super(context, attrs);
     50     }
     51 
     52     public AutologinBar(Context context, AttributeSet attrs, int defStyle) {
     53         super(context, attrs, defStyle);
     54     }
     55 
     56     @Override
     57     protected void onFinishInflate() {
     58         super.onFinishInflate();
     59         mAutoLoginAccount = (Spinner) findViewById(R.id.autologin_account);
     60         mAutoLoginLogin = (Button) findViewById(R.id.autologin_login);
     61         mAutoLoginLogin.setOnClickListener(this);
     62         mAutoLoginProgress = (ProgressBar) findViewById(R.id.autologin_progress);
     63         mAutoLoginError = (TextView) findViewById(R.id.autologin_error);
     64         mAutoLoginCancel = findViewById(R.id.autologin_close);
     65         mAutoLoginCancel.setOnClickListener(this);
     66     }
     67 
     68     public void setTitleBar(TitleBar titleBar) {
     69         mTitleBar = titleBar;
     70     }
     71 
     72     @Override
     73     public void onClick(View v) {
     74         if (mAutoLoginCancel == v) {
     75             if (mAutoLoginHandler != null) {
     76                 mAutoLoginHandler.cancel();
     77                 mAutoLoginHandler = null;
     78             }
     79             hideAutoLogin(true);
     80         } else if (mAutoLoginLogin == v) {
     81             if (mAutoLoginHandler != null) {
     82                 mAutoLoginAccount.setEnabled(false);
     83                 mAutoLoginLogin.setEnabled(false);
     84                 mAutoLoginProgress.setVisibility(View.VISIBLE);
     85                 mAutoLoginError.setVisibility(View.GONE);
     86                 mAutoLoginHandler.login(
     87                         mAutoLoginAccount.getSelectedItemPosition(), this);
     88             }
     89         }
     90     }
     91 
     92     public void updateAutoLogin(Tab tab, boolean animate) {
     93         DeviceAccountLogin login = tab.getDeviceAccountLogin();
     94         if (login != null) {
     95             mAutoLoginHandler = login;
     96             ContextThemeWrapper wrapper = new ContextThemeWrapper(mContext,
     97                     android.R.style.Theme_Holo_Light);
     98             mAccountsAdapter = new ArrayAdapter<String>(wrapper,
     99                     android.R.layout.simple_spinner_item, login.getAccountNames());
    100             mAccountsAdapter.setDropDownViewResource(
    101                     android.R.layout.simple_spinner_dropdown_item);
    102             mAutoLoginAccount.setAdapter(mAccountsAdapter);
    103             mAutoLoginAccount.setSelection(0);
    104             mAutoLoginAccount.setEnabled(true);
    105             mAutoLoginLogin.setEnabled(true);
    106             mAutoLoginProgress.setVisibility(View.INVISIBLE);
    107             mAutoLoginError.setVisibility(View.GONE);
    108             switch (login.getState()) {
    109                 case DeviceAccountLogin.PROCESSING:
    110                     mAutoLoginAccount.setEnabled(false);
    111                     mAutoLoginLogin.setEnabled(false);
    112                     mAutoLoginProgress.setVisibility(View.VISIBLE);
    113                     break;
    114                 case DeviceAccountLogin.FAILED:
    115                     mAutoLoginProgress.setVisibility(View.INVISIBLE);
    116                     mAutoLoginError.setVisibility(View.VISIBLE);
    117                     break;
    118                 case DeviceAccountLogin.INITIAL:
    119                     break;
    120                 default:
    121                     throw new IllegalStateException();
    122             }
    123             showAutoLogin(animate);
    124         } else {
    125             hideAutoLogin(animate);
    126         }
    127     }
    128 
    129     void showAutoLogin(boolean animate) {
    130         mTitleBar.showAutoLogin(animate);
    131     }
    132 
    133     void hideAutoLogin(boolean animate) {
    134         mTitleBar.hideAutoLogin(animate);
    135     }
    136 
    137     @Override
    138     public void loginFailed() {
    139         mAutoLoginAccount.setEnabled(true);
    140         mAutoLoginLogin.setEnabled(true);
    141         mAutoLoginProgress.setVisibility(View.INVISIBLE);
    142         mAutoLoginError.setVisibility(View.VISIBLE);
    143     }
    144 
    145 }
    146