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