Wifi Connectivity and loading View
							parent
							
								
									69d1e4bb0b
								
							
						
					
					
						commit
						8e554970f3
					
				@ -0,0 +1,51 @@
 | 
			
		||||
package com.cloud.diplomaticquarterapp
 | 
			
		||||
 | 
			
		||||
import android.content.Intent
 | 
			
		||||
import androidx.appcompat.app.AppCompatActivity
 | 
			
		||||
import android.os.Bundle
 | 
			
		||||
import android.widget.RelativeLayout
 | 
			
		||||
import android.widget.TextView
 | 
			
		||||
import com.cloud.diplomaticquarterapp.utils.PlatformBridge
 | 
			
		||||
import com.wang.avi.AVLoadingIndicatorView
 | 
			
		||||
import io.flutter.embedding.android.FlutterView
 | 
			
		||||
import io.flutter.embedding.engine.FlutterEngine
 | 
			
		||||
import io.flutter.embedding.engine.dart.DartExecutor
 | 
			
		||||
import io.flutter.plugin.common.MethodChannel
 | 
			
		||||
import io.flutter.view.FlutterMain
 | 
			
		||||
import org.jetbrains.anko.find
 | 
			
		||||
import java.util.ArrayList
 | 
			
		||||
 | 
			
		||||
open class BaseActivity : AppCompatActivity() {
 | 
			
		||||
 | 
			
		||||
    lateinit var loadingView:RelativeLayout
 | 
			
		||||
    lateinit var lblLoadingView:TextView
 | 
			
		||||
    lateinit var avLoadingView:AVLoadingIndicatorView
 | 
			
		||||
 | 
			
		||||
    override fun onCreate(savedInstanceState: Bundle?) {
 | 
			
		||||
        super.onCreate(savedInstanceState)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun setContentView(layoutResID: Int) {
 | 
			
		||||
        super.setContentView(layoutResID)
 | 
			
		||||
 | 
			
		||||
        loadingView = find(R.id.loadingView)
 | 
			
		||||
        lblLoadingView = find(R.id.lblLoadingView)
 | 
			
		||||
        avLoadingView = find(R.id.avLoadingView)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun onResume() {
 | 
			
		||||
        super.onResume()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun onPause() {
 | 
			
		||||
        super.onPause()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun onStop() {
 | 
			
		||||
        super.onStop()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun onDestroy() {
 | 
			
		||||
        super.onDestroy()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,99 @@
 | 
			
		||||
package com.cloud.diplomaticquarterapp.hmgwifi
 | 
			
		||||
 | 
			
		||||
import android.annotation.SuppressLint
 | 
			
		||||
import android.util.Log
 | 
			
		||||
import com.cloud.diplomaticquarterapp.API
 | 
			
		||||
import com.cloud.diplomaticquarterapp.FlutterMainActivity
 | 
			
		||||
import com.github.kittinunf.fuel.Fuel
 | 
			
		||||
import com.github.kittinunf.fuel.core.extensions.jsonBody
 | 
			
		||||
import com.github.kittinunf.fuel.httpGet
 | 
			
		||||
import com.github.kittinunf.fuel.httpPost
 | 
			
		||||
import org.jetbrains.anko.doAsync
 | 
			
		||||
import org.json.JSONObject
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@SuppressLint("MissingPermission")
 | 
			
		||||
class HMG_Wifi(flutterMainActivity: FlutterMainActivity) {
 | 
			
		||||
    private val TAG = "HMG_Wifi"
 | 
			
		||||
    private val TEST = true
 | 
			
		||||
 | 
			
		||||
    private var context = flutterMainActivity;
 | 
			
		||||
 | 
			
		||||
    private lateinit var completionListener: ((status: Boolean, message: String) -> Unit)
 | 
			
		||||
 | 
			
		||||
    private var SSID = "GUEST-POC"
 | 
			
		||||
    private var USER_NAME = ""
 | 
			
		||||
    private var PASSWORD = ""
 | 
			
		||||
 | 
			
		||||
    fun completionOnUiThread(status: Boolean, message: String){
 | 
			
		||||
        context.runOnUiThread {
 | 
			
		||||
            completionListener(status, message)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
	 * Helpful:
 | 
			
		||||
	 * http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-programmatically
 | 
			
		||||
	 */
 | 
			
		||||
    fun connectToHMGGuestNetwork(patientId: String, completion: (status: Boolean, message: String) -> Unit): HMG_Wifi {
 | 
			
		||||
        completionListener = completion
 | 
			
		||||
        getWifiCredentials(patientId) {
 | 
			
		||||
            WPA(context,SSID).connect(USER_NAME,PASSWORD) { status, message ->
 | 
			
		||||
                completionOnUiThread(status,message)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return this
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun haveInternet(completion: ((status: Boolean) -> Unit)){
 | 
			
		||||
        if (TEST)
 | 
			
		||||
            completion(true)
 | 
			
		||||
 | 
			
		||||
        "https://captive.apple.com".httpGet().response { request, response, result ->
 | 
			
		||||
            val have = response.statusCode == 200  && String(response.data).contains("<TITLE>Success</TITLE>", true)
 | 
			
		||||
            completion(have)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun getWifiCredentials(patientId:String, completion: (() -> Unit)){
 | 
			
		||||
//        if (TEST){
 | 
			
		||||
//            SSID = "GUEST-POC"
 | 
			
		||||
//            USER_NAME = "0696"
 | 
			
		||||
//            PASSWORD = "0000"
 | 
			
		||||
//            completion()
 | 
			
		||||
//            return
 | 
			
		||||
//        }
 | 
			
		||||
 | 
			
		||||
        val jsonBody = """{"PatientID":$patientId}"""
 | 
			
		||||
        API.WIFI_CREDENTIALS.
 | 
			
		||||
                httpPost()
 | 
			
		||||
                .jsonBody(jsonBody, Charsets.UTF_8)
 | 
			
		||||
                .response { request, response, result ->
 | 
			
		||||
 | 
			
		||||
                    result.fold(success = { data ->
 | 
			
		||||
                        val jsonString = String(data)
 | 
			
		||||
                        val jsonObject = JSONObject(jsonString)
 | 
			
		||||
                        if(!jsonObject.getString("ErrorMessage").equals("null")){
 | 
			
		||||
                            val errorMsg = jsonObject.getString("ErrorMessage")
 | 
			
		||||
                            completionOnUiThread(false, errorMsg)
 | 
			
		||||
 | 
			
		||||
                        }else{
 | 
			
		||||
                            jsonObject.getJSONArray("Hmg_SMS_Get_By_ProjectID_And_PatientIDList").let { array ->
 | 
			
		||||
                                array.getJSONObject(0).let { object_ ->
 | 
			
		||||
                                    if (object_.has("UserName") && object_.has("UserName")){
 | 
			
		||||
                                        USER_NAME = object_.getString("UserName")
 | 
			
		||||
                                        PASSWORD = object_.getString("Password")
 | 
			
		||||
                                        completion()
 | 
			
		||||
                                    }else{
 | 
			
		||||
                                        completionOnUiThread(false, "Failed to get your internet credentials")
 | 
			
		||||
                                    }
 | 
			
		||||
                                }
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                    },failure = { error ->
 | 
			
		||||
                        completionOnUiThread(false, error.localizedMessage )
 | 
			
		||||
                    })
 | 
			
		||||
                }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,98 @@
 | 
			
		||||
package com.cloud.diplomaticquarterapp.hmgwifi
 | 
			
		||||
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import android.net.ConnectivityManager
 | 
			
		||||
import android.net.wifi.*
 | 
			
		||||
import android.util.Log
 | 
			
		||||
import com.cloud.diplomaticquarterapp.FlutterMainActivity
 | 
			
		||||
import com.cloud.diplomaticquarterapp.utils.HMGUtils
 | 
			
		||||
 | 
			
		||||
class WPA(mainActivity: FlutterMainActivity, SSID:String) {
 | 
			
		||||
    private var TAG = "WPA"
 | 
			
		||||
    private var SSID = "GUEST-POC"
 | 
			
		||||
    private var wifiManager_: WifiManager? = null
 | 
			
		||||
    private var connectivityManager_: ConnectivityManager? = null
 | 
			
		||||
 | 
			
		||||
    init {
 | 
			
		||||
        wifiManager_ = mainActivity.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager?
 | 
			
		||||
        connectivityManager_ = mainActivity.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun connect(identity:String, password:String, completion: (status: Boolean, message: String) -> Unit) {
 | 
			
		||||
        if(wifiManager_ == null || connectivityManager_ == null){
 | 
			
		||||
            completion(false, "Failed to access system connectivity services")
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        val wifiManager  = wifiManager_!!
 | 
			
		||||
        val connectivityManager = connectivityManager_!!
 | 
			
		||||
 | 
			
		||||
        // Initialize the WifiConfiguration object
 | 
			
		||||
        val enterpriseConfig = WifiEnterpriseConfig()
 | 
			
		||||
        val wifi = WifiConfiguration()
 | 
			
		||||
        wifi.SSID = """"$SSID""""
 | 
			
		||||
        wifi.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP)
 | 
			
		||||
        wifi.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X)
 | 
			
		||||
        enterpriseConfig.eapMethod = WifiEnterpriseConfig.Eap.PEAP
 | 
			
		||||
        enterpriseConfig.identity = identity
 | 
			
		||||
        enterpriseConfig.password = password
 | 
			
		||||
        wifi.enterpriseConfig = enterpriseConfig
 | 
			
		||||
        wifi.networkId = ssidToNetworkId(wifi.SSID)
 | 
			
		||||
        if (wifi.networkId == -1) {
 | 
			
		||||
            wifiManager.addNetwork(wifi)
 | 
			
		||||
        } else {
 | 
			
		||||
            Log.v(TAG, "WiFi found - updating it.\n")
 | 
			
		||||
            wifiManager.updateNetwork(wifi)
 | 
			
		||||
        }
 | 
			
		||||
        Log.v(TAG, "saving config.\n")
 | 
			
		||||
        wifiManager.saveConfiguration()
 | 
			
		||||
        wifi.networkId = ssidToNetworkId(wifi.SSID)
 | 
			
		||||
 | 
			
		||||
        Log.v(TAG, "wifi ID in device = " + wifi.networkId)
 | 
			
		||||
 | 
			
		||||
        var supState: SupplicantState
 | 
			
		||||
        val networkIdToConnect = wifi.networkId
 | 
			
		||||
        if (networkIdToConnect >= 0) {
 | 
			
		||||
            Log.v(TAG, "Start connecting...\n")
 | 
			
		||||
 | 
			
		||||
            // We disable the network before connecting, because if this was the last connection before
 | 
			
		||||
            // a disconnect(), this will not reconnect.
 | 
			
		||||
            wifiManager.disableNetwork(networkIdToConnect)
 | 
			
		||||
            wifiManager.enableNetwork(networkIdToConnect, true)
 | 
			
		||||
 | 
			
		||||
            val wifiInfo: WifiInfo = wifiManager.connectionInfo
 | 
			
		||||
 | 
			
		||||
            HMGUtils.timer(5000,false){
 | 
			
		||||
                supState = wifiInfo.supplicantState
 | 
			
		||||
                Log.i(TAG, "WifiWizard: Done connect to network : status =  $supState")
 | 
			
		||||
                if (supState == SupplicantState.COMPLETED)
 | 
			
		||||
                    completion(true,"Connected to Wifi")
 | 
			
		||||
                else
 | 
			
		||||
                    completion(false,"Failed to connect with HMG network")
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } else {
 | 
			
		||||
            Log.v(TAG, "WifiWizard: cannot connect to network")
 | 
			
		||||
            completion(false,"Failed to connect to Wifi")
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This method takes a given String, searches the current list of configured WiFi
 | 
			
		||||
     * networks, and returns the networkId for the network if the SSID matches. If not,
 | 
			
		||||
     * it returns -1.
 | 
			
		||||
     */
 | 
			
		||||
    private fun ssidToNetworkId(ssid: String): Int {
 | 
			
		||||
        val currentNetworks = wifiManager_!!.configuredNetworks
 | 
			
		||||
        var networkId = -1
 | 
			
		||||
 | 
			
		||||
        // For each network in the list, compare the SSID with the given one
 | 
			
		||||
        for (test in currentNetworks) {
 | 
			
		||||
            if (test.SSID == ssid) {
 | 
			
		||||
                networkId = test.networkId
 | 
			
		||||
                break
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return networkId
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,51 @@
 | 
			
		||||
package com.cloud.diplomaticquarterapp.utils
 | 
			
		||||
 | 
			
		||||
import android.opengl.Visibility
 | 
			
		||||
import android.view.View
 | 
			
		||||
import android.widget.Toast
 | 
			
		||||
import com.cloud.diplomaticquarterapp.BaseActivity
 | 
			
		||||
import java.util.*
 | 
			
		||||
import kotlin.concurrent.timerTask
 | 
			
		||||
 | 
			
		||||
class HMGUtils {
 | 
			
		||||
 | 
			
		||||
    companion object{
 | 
			
		||||
        fun timer(delay:Long, repeat:Boolean, tick:(Timer)->Unit) : Timer{
 | 
			
		||||
            val timer = Timer()
 | 
			
		||||
            if(repeat)
 | 
			
		||||
                timer.schedule(timerTask {
 | 
			
		||||
                    tick(timer)
 | 
			
		||||
                },delay,delay)
 | 
			
		||||
            else
 | 
			
		||||
                timer.schedule(timerTask {
 | 
			
		||||
                    tick(timer)
 | 
			
		||||
                },delay)
 | 
			
		||||
 | 
			
		||||
            return timer
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        fun showLoading(context: BaseActivity, show:Boolean = true, message:String = "Please wait"){
 | 
			
		||||
            if(show){
 | 
			
		||||
                context.loadingView.visibility = View.VISIBLE
 | 
			
		||||
                context.avLoadingView.smoothToShow()
 | 
			
		||||
                context.lblLoadingView.text = message
 | 
			
		||||
            }else{
 | 
			
		||||
                context.loadingView.visibility = View.GONE
 | 
			
		||||
                context.avLoadingView.smoothToHide()
 | 
			
		||||
                context.lblLoadingView.text = ""
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        fun showMessage(context:BaseActivity, title:String = "", message:String){
 | 
			
		||||
            Toast.makeText(context,message,Toast.LENGTH_LONG).show()
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
private fun Timer.schedule(timerTask: TimerTask) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,42 @@
 | 
			
		||||
<?xml version="1.0" encoding="utf-8"?>
 | 
			
		||||
<RelativeLayout
 | 
			
		||||
    xmlns:android="http://schemas.android.com/apk/res/android"
 | 
			
		||||
    xmlns:tools="http://schemas.android.com/tools"
 | 
			
		||||
    xmlns:app="http://schemas.android.com/apk/res-auto"
 | 
			
		||||
    android:id="@+id/loadingView"
 | 
			
		||||
    android:layout_width="match_parent"
 | 
			
		||||
    android:layout_height="match_parent"
 | 
			
		||||
    android:background="#B3000000">
 | 
			
		||||
 | 
			
		||||
    <androidx.constraintlayout.widget.ConstraintLayout
 | 
			
		||||
        android:layout_margin="60dp"
 | 
			
		||||
        android:layout_centerInParent="true"
 | 
			
		||||
        android:layout_width="match_parent"
 | 
			
		||||
        android:layout_height="wrap_content">
 | 
			
		||||
 | 
			
		||||
        <com.wang.avi.AVLoadingIndicatorView
 | 
			
		||||
            android:id="@+id/avLoadingView"
 | 
			
		||||
            style="@style/AVLoadingIndicatorView"
 | 
			
		||||
            android:layout_width="0dp"
 | 
			
		||||
            android:layout_height="0dp"
 | 
			
		||||
            android:layout_centerInParent="true"
 | 
			
		||||
            android:visibility="visible"
 | 
			
		||||
            app:indicatorColor="#4CAF50"
 | 
			
		||||
            app:indicatorName="BallScaleMultipleIndicator"
 | 
			
		||||
            app:layout_constraintBottom_toBottomOf="parent"
 | 
			
		||||
            app:layout_constraintDimensionRatio="w,1:1"
 | 
			
		||||
            app:layout_constraintEnd_toEndOf="parent"
 | 
			
		||||
            app:layout_constraintStart_toStartOf="parent"
 | 
			
		||||
            app:layout_constraintTop_toTopOf="parent" />
 | 
			
		||||
 | 
			
		||||
    </androidx.constraintlayout.widget.ConstraintLayout>
 | 
			
		||||
 | 
			
		||||
    <TextView
 | 
			
		||||
        android:id="@+id/lblLoadingView"
 | 
			
		||||
        android:textSize="12sp"
 | 
			
		||||
        android:textColor="#FFFFFF"
 | 
			
		||||
        android:layout_centerInParent="true"
 | 
			
		||||
        android:layout_width="wrap_content"
 | 
			
		||||
        android:layout_height="wrap_content"/>
 | 
			
		||||
 | 
			
		||||
</RelativeLayout>
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue