package de.c3nav.droid;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private WifiManager wifiManager;
    private WifiReceiver wifiReceiver;
    private WebView webView;
    private MobileClient mobileClient;
    private Map<String, Integer> lastLevelValues = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final View pView = LayoutInflater.from(this).inflate(R.layout.progressbar, null);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setCustomView(pView);
        getSupportActionBar().getCustomView().setVisibility(View.GONE);

        mobileClient = new MobileClient();

        webView = (WebView) findViewById(R.id.webView);

        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUserAgentString("c3navClient/Android/" + BuildConfig.VERSION_CODE);
        webView.addJavascriptInterface(mobileClient, "mobileclient");

        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView v, int progress) {
                if (progress < 100 && pView.getVisibility() == View.GONE) {
                    pView.setVisibility(View.VISIBLE);
                }
                if (progress == 100) {
                    pView.setVisibility(View.GONE);
                }
            }

        });

        String url_to_call = BuildConfig.WEB_URL;
        Intent intent = getIntent();
        Uri data = intent.getData();
        if (data != null) {
            Uri.Builder tmp_uri = data.buildUpon();
            tmp_uri.scheme("https");
            url_to_call = tmp_uri.build().toString();
        }

        webView.loadUrl(url_to_call);

        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiReceiver = new WifiReceiver();
        registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        wifiManager.startScan();
    }

    class MobileClient {
        private JSONArray nearbyStations;

        @JavascriptInterface
        public String getNearbyStations() {
            return this.nearbyStations.toString();
        }

        public void setNearbyStations(JSONArray nearbyStations) {
            this.nearbyStations = nearbyStations;
        }

        @JavascriptInterface
        public void scanNow() {
            wifiManager.startScan();
        }
    }

    class WifiReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {
            webView.post(new Runnable() {
                @Override
                public void run() {

                    List<ScanResult> wifiList = wifiManager.getScanResults();
                    JSONArray ja = new JSONArray();
                    Map<String, Integer> newLevelValues = new HashMap<String, Integer>();
                    for (ScanResult result : wifiList) {
                        JSONObject jo = new JSONObject();
                        try {
                            jo.put("bssid", result.BSSID);
                            jo.put("ssid", result.SSID);
                            jo.put("level", result.level);

                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                                if (SystemClock.elapsedRealtime() - result.timestamp / 1000 > 1000) {
                                    continue;
                                }
                                jo.put("last", SystemClock.elapsedRealtime() - result.timestamp / 1000);
                            } else {
                                // Workaround for older devices: If the signal level did not change
                                // at all since the last scan, we will assume that it is a cached
                                // value and should not be used.
                                newLevelValues.put(result.BSSID, result.level);
                                if (lastLevelValues.containsKey(result.BSSID) && lastLevelValues.get(result.BSSID) == result.level) {
                                    continue;
                                }
                            }
                            ja.put(jo);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    mobileClient.setNearbyStations(ja);
                    lastLevelValues = newLevelValues;
                    webView.loadUrl("javascript:nearby_stations_available();");
                }
            });
        }
    }
}

@@ -18,16 +18,41 @@
 droid;%0A%0A
+import android.Manifest;%0A
 import a
@@ -204,15 +204,33 @@
 oid.
-net.Uri
+content.pm.PackageManager
 ;%0Aim
@@ -382,16 +382,62 @@
 mClock;%0A
+import android.support.v4.app.ActivityCompat;%0A
 import a
@@ -526,16 +526,41 @@
 tivity;%0A
+import android.util.Log;%0A
 import a
@@ -950,38 +950,47 @@
    p
-rivate WifiManager wifiManager
+ublic static final int PERM_REQUEST = 1
 ;%0A  
@@ -1007,27 +1007,25 @@
 Wifi
-Receiv
+Manag
 er wifi
-Receiv
+Manag
 er;%0A
@@ -1159,16 +1159,55 @@
 Map%3C%3E();
+%0A    private boolean permAsked = false;
 %0A%0A    @O
@@ -2890,24 +2890,37 @@
 E);%0A        
+WifiReceiver 
 wifiReceiver
@@ -3824,24 +3824,661 @@
 oid run() %7B%0A
+                    int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this,%0A                            Manifest.permission.ACCESS_FINE_LOCATION);%0A                    if (permissionCheck != PackageManager.PERMISSION_GRANTED) %7B%0A                        if (!permAsked) %7B%0A                            ActivityCompat.requestPermissions(MainActivity.this,%0A                                    new String%5B%5D%7BManifest.permission.ACCESS_FINE_LOCATION%7D,%0A                                    PERM_REQUEST);%0A                            permAsked = true;%0A                        %7D%0A                        return;%0A                    %7D
 %0A           
@@ -6194,32 +6194,89 @@
               %7D%0A
+                    Log.i(%22scan result%22, ja.toString());%0A
                 

Add permission checks for API 23