1 /* 2 * Copyright (C) 2017 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.settings.wifi; 17 18 import android.app.Dialog; 19 import android.app.FragmentManager; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.net.wifi.WifiManager; 25 import android.net.wifi.WpsInfo; 26 import android.os.Bundle; 27 import android.support.annotation.VisibleForTesting; 28 import android.support.v7.preference.Preference; 29 import android.support.v7.preference.PreferenceScreen; 30 31 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 32 import com.android.settings.core.PreferenceController; 33 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 34 import com.android.settings.core.lifecycle.Lifecycle; 35 import com.android.settings.core.lifecycle.LifecycleObserver; 36 import com.android.settings.core.lifecycle.events.OnPause; 37 import com.android.settings.core.lifecycle.events.OnResume; 38 39 /** 40 * {@link PreferenceController} that shows Dialog for WPS progress. Disabled when Wi-Fi is off. 41 */ 42 public class WpsPreferenceController extends PreferenceController implements 43 LifecycleObserver, OnPause, OnResume { 44 45 private static final String KEY_WPS_PUSH = "wps_push_button"; 46 private static final String KEY_WPS_PIN = "wps_pin_entry"; 47 48 private final WifiManager mWifiManager; 49 private final FragmentManager mFragmentManager; 50 @VisibleForTesting 51 final BroadcastReceiver mReceiver = new BroadcastReceiver() { 52 @Override 53 public void onReceive(Context context, Intent intent) { 54 togglePreferences(); 55 } 56 }; 57 private final IntentFilter mFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION); 58 59 private Preference mWpsPushPref; 60 private Preference mWpsPinPref; 61 62 public WpsPreferenceController( 63 Context context, 64 Lifecycle lifecycle, 65 WifiManager wifiManager, 66 FragmentManager fragmentManager) { 67 super(context); 68 mWifiManager = wifiManager; 69 mFragmentManager = fragmentManager; 70 lifecycle.addObserver(this); 71 } 72 73 @Override 74 public boolean isAvailable() { 75 // Always show preference. 76 return true; 77 } 78 79 @Override 80 public String getPreferenceKey() { 81 // Returns null because this controller contains more than 1 preference. 82 return null; 83 } 84 85 @Override 86 public void displayPreference(PreferenceScreen screen) { 87 super.displayPreference(screen); 88 mWpsPushPref = screen.findPreference(KEY_WPS_PUSH); 89 mWpsPinPref = screen.findPreference(KEY_WPS_PIN); 90 if (mWpsPushPref == null || mWpsPinPref == null) { 91 return; 92 } 93 // WpsDialog: Create the dialog like WifiSettings does. 94 mWpsPushPref.setOnPreferenceClickListener((arg) -> { 95 WpsFragment wpsFragment = new WpsFragment(WpsInfo.PBC); 96 wpsFragment.show(mFragmentManager, KEY_WPS_PUSH); 97 return true; 98 } 99 ); 100 101 // WpsDialog: Create the dialog like WifiSettings does. 102 mWpsPinPref.setOnPreferenceClickListener((arg) -> { 103 WpsFragment wpsFragment = new WpsFragment(WpsInfo.DISPLAY); 104 wpsFragment.show(mFragmentManager, KEY_WPS_PIN); 105 return true; 106 }); 107 togglePreferences(); 108 } 109 110 @Override 111 public void onResume() { 112 mContext.registerReceiver(mReceiver, mFilter); 113 } 114 115 @Override 116 public void onPause() { 117 mContext.unregisterReceiver(mReceiver); 118 } 119 120 private void togglePreferences() { 121 if (mWpsPushPref != null && mWpsPinPref != null) { 122 boolean enabled = mWifiManager.isWifiEnabled(); 123 mWpsPushPref.setEnabled(enabled); 124 mWpsPinPref.setEnabled(enabled); 125 } 126 } 127 128 /** 129 * Fragment for Dialog to show WPS progress. 130 */ 131 public static class WpsFragment extends InstrumentedDialogFragment { 132 private static int mWpsSetup; 133 134 // Public default constructor is required for rotation. 135 public WpsFragment() { 136 super(); 137 } 138 139 public WpsFragment(int wpsSetup) { 140 super(); 141 mWpsSetup = wpsSetup; 142 } 143 144 @Override 145 public int getMetricsCategory() { 146 return MetricsEvent.DIALOG_WPS_SETUP; 147 } 148 149 @Override 150 public Dialog onCreateDialog(Bundle savedInstanceState) { 151 return new WpsDialog(getActivity(), mWpsSetup); 152 } 153 } 154 }