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 public 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 
     49     /** Creates a WifiDialog with fullscreen style. It displays in fullscreen mode. */
     50     public static WifiDialog createFullscreen(Context context, WifiDialogListener listener,
     51             AccessPoint accessPoint, int mode) {
     52         return new WifiDialog(context, listener, accessPoint, mode,
     53                 R.style.Theme_Settings_NoActionBar, false /* hideSubmitButton */);
     54     }
     55 
     56     /**
     57      * Creates a WifiDialog with no additional style. It displays as a dialog above the current
     58      * view.
     59      */
     60     public static WifiDialog createModal(Context context, WifiDialogListener listener,
     61             AccessPoint accessPoint, int mode) {
     62         return new WifiDialog(context, listener, accessPoint, mode, 0 /* style */,
     63                 mode == WifiConfigUiBase.MODE_VIEW /* hideSubmitButton*/);
     64     }
     65 
     66     /* package */ WifiDialog(Context context, WifiDialogListener listener, AccessPoint accessPoint,
     67         int mode, int style, boolean hideSubmitButton) {
     68         super(context, style);
     69         mMode = mode;
     70         mListener = listener;
     71         mAccessPoint = accessPoint;
     72         mHideSubmitButton = hideSubmitButton;
     73     }
     74 
     75     @Override
     76     public WifiConfigController getController() {
     77         return mController;
     78     }
     79 
     80     @Override
     81     protected void onCreate(Bundle savedInstanceState) {
     82         mView = getLayoutInflater().inflate(R.layout.wifi_dialog, null);
     83         setView(mView);
     84         setInverseBackgroundForced(true);
     85         mController = new WifiConfigController(this, mView, mAccessPoint, mMode);
     86         super.onCreate(savedInstanceState);
     87 
     88         if (mHideSubmitButton) {
     89             mController.hideSubmitButton();
     90         } else {
     91             /* During creation, the submit button can be unavailable to determine
     92              * visibility. Right after creation, update button visibility */
     93             mController.enableSubmitIfAppropriate();
     94         }
     95 
     96         if (mAccessPoint == null) {
     97             mController.hideForgetButton();
     98         }
     99     }
    100 
    101     public void onRestoreInstanceState(Bundle savedInstanceState) {
    102             super.onRestoreInstanceState(savedInstanceState);
    103             mController.updatePassword();
    104     }
    105 
    106     @Override
    107     public void dispatchSubmit() {
    108         if (mListener != null) {
    109             mListener.onSubmit(this);
    110         }
    111         dismiss();
    112     }
    113 
    114     @Override
    115     public void onClick(DialogInterface dialogInterface, int id) {
    116         if (mListener != null) {
    117             switch (id) {
    118                 case BUTTON_SUBMIT:
    119                     mListener.onSubmit(this);
    120                     break;
    121                 case BUTTON_FORGET:
    122                     if (WifiUtils.isNetworkLockedDown(getContext(), mAccessPoint.getConfig())) {
    123                         RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
    124                                 RestrictedLockUtils.getDeviceOwner(getContext()));
    125                         return;
    126                     }
    127                     mListener.onForget(this);
    128                     break;
    129             }
    130         }
    131     }
    132 
    133     @Override
    134     public int getMode() {
    135         return mMode;
    136     }
    137 
    138     @Override
    139     public Button getSubmitButton() {
    140         return getButton(BUTTON_SUBMIT);
    141     }
    142 
    143     @Override
    144     public Button getForgetButton() {
    145         return getButton(BUTTON_FORGET);
    146     }
    147 
    148     @Override
    149     public Button getCancelButton() {
    150         return getButton(BUTTON_NEGATIVE);
    151     }
    152 
    153     @Override
    154     public void setSubmitButton(CharSequence text) {
    155         setButton(BUTTON_SUBMIT, text, this);
    156     }
    157 
    158     @Override
    159     public void setForgetButton(CharSequence text) {
    160         setButton(BUTTON_FORGET, text, this);
    161     }
    162 
    163     @Override
    164     public void setCancelButton(CharSequence text) {
    165         setButton(BUTTON_NEGATIVE, text, this);
    166     }
    167 }
    168