1 /* 2 * Copyright (C) 2019 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.dpp; 18 19 import android.app.Application; 20 import android.net.wifi.EasyConnectStatusCallback; 21 import android.net.wifi.WifiManager; 22 import android.util.SparseArray; 23 24 import androidx.lifecycle.AndroidViewModel; 25 import androidx.lifecycle.MutableLiveData; 26 27 public class WifiDppInitiatorViewModel extends AndroidViewModel { 28 private MutableLiveData<Integer> mEnrolleeSuccessNetworkId; 29 private MutableLiveData<Integer> mStatusCode; 30 private boolean mIsWifiDppHandshaking; 31 private String mTriedSsid; 32 private SparseArray<int[]> mTriedChannels; 33 private int[] mBandArray; 34 WifiDppInitiatorViewModel(Application application)35 public WifiDppInitiatorViewModel(Application application) { 36 super(application); 37 } 38 getEnrolleeSuccessNetworkId()39 MutableLiveData<Integer> getEnrolleeSuccessNetworkId() { 40 if (mEnrolleeSuccessNetworkId == null) { 41 mEnrolleeSuccessNetworkId = new MutableLiveData<>(); 42 } 43 44 return mEnrolleeSuccessNetworkId; 45 } 46 getStatusCode()47 MutableLiveData<Integer> getStatusCode() { 48 if (mStatusCode == null) { 49 mStatusCode = new MutableLiveData<>(); 50 } 51 52 return mStatusCode; 53 } 54 getTriedSsid()55 String getTriedSsid() { 56 return mTriedSsid; 57 } 58 getTriedChannels()59 SparseArray<int[]> getTriedChannels() { 60 return mTriedChannels; 61 } 62 getBandArray()63 int[] getBandArray() { 64 return mBandArray; 65 } 66 isWifiDppHandshaking()67 boolean isWifiDppHandshaking() { 68 return mIsWifiDppHandshaking; 69 } 70 startEasyConnectAsConfiguratorInitiator(String qrCode, int networkId)71 void startEasyConnectAsConfiguratorInitiator(String qrCode, int networkId) { 72 mIsWifiDppHandshaking = true; 73 final WifiManager wifiManager = getApplication().getSystemService(WifiManager.class); 74 75 wifiManager.startEasyConnectAsConfiguratorInitiator(qrCode, networkId, 76 WifiManager.EASY_CONNECT_NETWORK_ROLE_STA, getApplication().getMainExecutor(), 77 new EasyConnectDelegateCallback()); 78 } 79 startEasyConnectAsEnrolleeInitiator(String qrCode)80 void startEasyConnectAsEnrolleeInitiator(String qrCode) { 81 mIsWifiDppHandshaking = true; 82 final WifiManager wifiManager = getApplication().getSystemService(WifiManager.class); 83 84 wifiManager.startEasyConnectAsEnrolleeInitiator(qrCode, getApplication().getMainExecutor(), 85 new EasyConnectDelegateCallback()); 86 } 87 88 private class EasyConnectDelegateCallback extends EasyConnectStatusCallback { 89 @Override onEnrolleeSuccess(int newNetworkId)90 public void onEnrolleeSuccess(int newNetworkId) { 91 mIsWifiDppHandshaking = false; 92 mEnrolleeSuccessNetworkId.setValue(newNetworkId); 93 } 94 95 @Override onConfiguratorSuccess(int code)96 public void onConfiguratorSuccess(int code) { 97 mIsWifiDppHandshaking = false; 98 mStatusCode.setValue(WifiDppUtils.EASY_CONNECT_EVENT_SUCCESS); 99 } 100 101 @Override onFailure(int code, String ssid, SparseArray<int[]> channelListArray, int[] operatingClassArray)102 public void onFailure(int code, String ssid, SparseArray<int[]> channelListArray, 103 int[] operatingClassArray) { 104 mIsWifiDppHandshaking = false; 105 mTriedSsid = ssid; 106 mTriedChannels = channelListArray; 107 mBandArray = operatingClassArray; 108 mStatusCode.setValue(code); 109 } 110 111 @Override onProgress(int code)112 public void onProgress(int code) { 113 // Do nothing 114 } 115 } 116 } 117