Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 659eb6a6 authored by sssemil's avatar sssemil Committed by Abhisek Devkota
Browse files

Show devices connected to the WiFi hotspot(1/2)

Change-Id: I8378e0ae07332f62b92a1681951f1236f6ef5ff9
parent cf73aec4
Loading
Loading
Loading
Loading
+146 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.util.wifi;

import android.content.Context;
import android.util.Log;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class ClientsList {

    private static final String TAG = "ClientsList";

    /**
     * Gets a list of the clients connected to the Hotspot, reachable timeout is 3000
     *
     * @param onlyReachables {@code false} if the list should contain unreachable
     *                       (probably disconnected) clients, {@code true} otherwise
     * @return ArrayList of {@link ClientScanResult}
     */
    public static ArrayList<ClientScanResult> get(boolean onlyReachables, Context context) {
        BufferedReader br = null;
        ArrayList<ClientScanResult> result = new ArrayList<ClientScanResult>();

        try {
            br = new BufferedReader(new FileReader("/proc/net/arp"));
            String line;

            while ((line = br.readLine()) != null) {
                String[] splitted = line.split(" +");

                if (splitted.length >= 6) {
                    // Basic sanity check
                    String mac = splitted[3];

                    if (mac.matches("..:..:..:..:..:..")) {
                        InetAddress address = InetAddress.getByName(splitted[0]);
                        boolean isReachable = address.isReachable(3000);

                        if (!onlyReachables || isReachable) {
                            ClientScanResult client = new ClientScanResult();
                            client.ipAddr = splitted[0];
                            if (mac.equals("00:00:00:00:00:00")) {
                                client.hwAddr = "---:---:---:---:---:---";
                            } else {
                                client.hwAddr = mac;
                            }
                            client.device = splitted[5];
                            client.isReachable = isReachable;
                            client.vendor = getVendor(mac, context);
                            result.add(client);
                        }
                    }
                }
            }
        } catch (UnknownHostException e) {
            Log.d(TAG, "catch UnknownHostException hit in run", e);
        } catch (FileNotFoundException e) {
            Log.d(TAG, "catch FileNotFoundException hit in run", e);
        } catch (IOException e) {
            Log.d(TAG, "catch IOException hit in run", e);
        } catch (XmlPullParserException e) {
            Log.d(TAG, "catch XmlPullParserException hit in run", e);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            }
        }

        return result;
    }

    public static String getVendor(String mac, Context context)
            throws XmlPullParserException, IOException {
        class Item {
            public String mac;
            public String vendor;
        }
        String[] macS = mac.split(":");
        mac = macS[0] + ":" + macS[1] + ":" + macS[2];
        XmlPullParser parser = context.getResources().getXml(com.android.internal.R.xml.vendors);

        int eventType = parser.getEventType();
        Item currentProduct = null;

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String name;
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    if (name.equals("item")) {
                        currentProduct = new Item();
                    } else if (currentProduct != null) {
                        if (name.equals("item-mac")) {
                            currentProduct.mac = parser.nextText();
                        } else if (name.equals("item-vendor")) {
                            currentProduct.vendor = parser.nextText();
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("item") && currentProduct != null) {
                        if (currentProduct.mac.equalsIgnoreCase(mac)) return currentProduct.vendor;
                    }
            }
            eventType = parser.next();
        }
        return "";
    }

    public static class ClientScanResult {
        public String ipAddr;
        public String hwAddr;
        public String device;
        public String vendor;
        public boolean isReachable;
    }
}
+22 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2014 The CyanogenMod Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <plurals name="tethered_clients_connected">
        <item quantity="one">%d client connected</item>
        <item quantity="other">%d clients connected</item>
    </plurals>
</resources>
+4 −0
Original line number Diff line number Diff line
@@ -2070,4 +2070,8 @@
  <java-symbol type="string" name="usb_ext_media_safe_unmount_notification_title" />
  <java-symbol type="string" name="usb_ext_media_unmountable_notification_message" />
  <java-symbol type="string" name="usb_ext_media_unmountable_notification_title" />

  <!-- Wifi tethering-->
  <java-symbol type="plurals" name="tethered_clients_connected" />
  <java-symbol type="xml" name="vendors" />
</resources>
+79114 −0

File added.

Preview size limit exceeded, changes collapsed.

+2 −1
Original line number Diff line number Diff line
@@ -54,7 +54,8 @@ public class WifiAPTile extends QuickSettingsTile {
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
                intent.setClassName("com.android.settings",
                        "com.android.settings.Settings$WifiApSettingsActivity");
                startSettingsActivity(intent);
                return true;
            }
Loading