Home | History | Annotate | Download | only in wifi
      1 /*
      2  * Copyright (C) 2010 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.settings.wifi;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.Context;
     21 import android.content.DialogInterface;
     22 import android.os.Bundle;
     23 import android.view.View;
     24 import android.widget.Button;
     25 
     26 import com.android.settings.R;
     27 import com.android.settingslib.RestrictedLockUtils;
     28 import com.android.settingslib.wifi.AccessPoint;
     29 
     30 class WifiDialog extends AlertDialog implements WifiConfigUiBase, DialogInterface.OnClickListener {
     31 
     32     public interface WifiDialogListener {
     33         void onForget(WifiDialog dialog);
     34         void onSubmit(WifiDialog dialog);
     35     }
     36 
     37     private static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
     38     private static final int BUTTON_FORGET = DialogInterface.BUTTON_NEUTRAL;
     39 
     40     private final int mMode;
     41     private final WifiDialogListener mListener;
     42     private final AccessPoint mAccessPoint;
     43 
     44     private View mView;
     45     private WifiConfigController mController;
     46     private boolean mHideSubmitButton;
     47 
     48     public WifiDialog(Context context, WifiDialogListener listener, AccessPoint accessPoint,
     49             int mode, boolean hideSubmitButton) {
     50         this(context, listener, accessPoint, mode);
     51         mHideSubmitButton = hideSubmitButton;
     52     }
     53 
     54     public WifiDialog(Context context, WifiDialogListener listener, AccessPoint accessPoint,
     55             int mode) {
     56         super(context);
     57         mMode = mode;
     58         mListener = listener;
     59         mAccessPoint = accessPoint;
     60         mHideSubmitButton = false;
     61     }
     62 
     63     @Override
     64     public WifiConfigController getController() {
     65         return mController;
     66     }
     67 
     68     @Override
     69     protected void onCreate(Bundle savedInstanceState) {
     70         mView = getLayoutInflater().inflate(R.layout.wifi_dialog, null);
     71         setView(mView);
     72         setInverseBackgroundForced(true);
     73         mController = new WifiConfigController(this, mView, mAccessPoint, mMode);
     74         super.onCreate(savedInstanceState);
     75 
     76         if (mHideSubmitButton) {
     77             mController.hideSubmitButton();
     78         } else {
     79             /* During creation, the submit button can be unavailable to determine
     80              * visibility. Right after creation, update button visibility */
     81             mController.enableSubmitIfAppropriate();
     82         }
     83 
     84         if (mAccessPoint == null) {
     85             mController.hideForgetButton();
     86         }
     87     }
     88 
     89     public void onRestoreInstanceState(Bundle savedInstanceState) {
     90             super.onRestoreInstanceState(savedInstanceState);
     91             mController.updatePassword();
     92     }
     93 
     94     @Override
     95     public void dispatchSubmit() {
     96         if (mListener != null) {
     97             mListener.onSubmit(this);
     98         }
     99         dismiss();
    100     }
    101 
    102     @Override
    103     public void onClick(DialogInterface dialogInterface, int id) {
    104         if (mListener != null) {
    105             switch (id) {
    106                 case BUTTON_SUBMIT:
    107                     mListener.onSubmit(this);
    108                     break;
    109                 case BUTTON_FORGET:
    110                     if (WifiSettings.isEditabilityLockedDown(
    111                             getContext(), mAccessPoint.getConfig())) {
    112                         RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
    113                                 RestrictedLockUtils.getDeviceOwner(getContext()));
    114                         return;
    115                     }
    116                     mListener.onForget(this);
    117                     break;
    118             }
    119         }
    120     }
    121 
    122     @Override
    123     public int getMode() {
    124         return mMode;
    125     }
    126 
    127     @Override
    128     public Button getSubmitButton() {
    129         return getButton(BUTTON_SUBMIT);
    130     }
    131 
    132     @Override
    133     public Button getForgetButton() {
    134         return getButton(BUTTON_FORGET);
    135     }
    136 
    137     @Override
    138     public Button getCancelButton() {
    139         return getButton(BUTTON_NEGATIVE);
    140     }
    141 
    142     @Override
    143     public void setSubmitButton(CharSequence text) {
    144         setButton(BUTTON_SUBMIT, text, this);
    145     }
    146 
    147     @Override
    148     public void setForgetButton(CharSequence text) {
    149         setButton(BUTTON_FORGET, text, this);
    150     }
    151 
    152     @Override
    153     public void setCancelButton(CharSequence text) {
    154         setButton(BUTTON_NEGATIVE, text, this);
    155     }
    156 }
    157