Gerrix90
11/7/2017 - 3:48 PM

PacketManager

PacketManager


    private void getAllBackgroundServices() {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();
        if (runningProcesses != null && runningProcesses.size() > 0) {
            for (int i = 0; i < runningProcesses.size(); i++) {

                try {
                    Log.v(TAG, String.valueOf(getPackageManager().getApplicationLabel(getPackageManager().getApplicationInfo(runningProcesses.get(i).processName, PackageManager.GET_META_DATA))));
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
                Log.v(TAG,"REcived: "+ TrafficStats.getUidRxBytes(runningProcesses.get(i).uid)/ (1024 * 1024) );
                Log.v(TAG,"SEnd: "+ TrafficStats.getUidTxBytes(runningProcesses.get(i).uid)/ (1024 * 1024) );
            }
        }
    }

    private void allAppsTrafic() {
        PackageManager pm = getPackageManager();
        List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);

        for (ApplicationInfo ai: installedApps) {

            if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                try {
                    ApplicationInfo applicationInfo = pm.getApplicationInfo(ai.packageName, 0);
                    double received = (double) TrafficStats.getUidRxBytes(applicationInfo.uid)

                            / (1024 * 1024);
                    double send = (double) TrafficStats.getUidTxBytes(applicationInfo.uid)
                            / (1024 * 1024);
                    double total = received + send;
                    if (total >0)
                    Log.v(TAG, String.valueOf(pm.getApplicationLabel(applicationInfo))+" : " + String.format("%.2f", total) + " MB");
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }


            } else {
                try {
                    ApplicationInfo applicationInfo = pm.getApplicationInfo(ai.packageName, 0);
                    double received = (double) TrafficStats.getUidRxBytes(applicationInfo.uid)

                            / (1024 * 1024);
                    double send = (double) TrafficStats.getUidTxBytes(applicationInfo.uid)
                            / (1024 * 1024);
                    double total = received + send;
                    if (total >0)
                        Log.v(TAG, String.valueOf(pm.getApplicationLabel(applicationInfo))+" : " + String.format("%.2f", total) + " MB");
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }

            }
        }
    }


    public void getPakagesInfoUsingHashMap() {
        String appName = null;
        double data = 0;

        final PackageManager pm = getPackageManager();
        // get a list of installed apps.
        List<ApplicationInfo> packages = pm.getInstalledApplications(0);

        // loop through the list of installed packages and see if the selected
        // app is in the list
        for (ApplicationInfo packageInfo : packages) {
            // get the UID for the selected app
            int UID = packageInfo.uid;
            String package_name = packageInfo.packageName;
            ApplicationInfo app = null;
            try {
                app = pm.getApplicationInfo(package_name, 0);
            } catch (PackageManager.NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String name = (String) pm.getApplicationLabel(app);
            Drawable icon = pm.getApplicationIcon(app);
            // internet usage for particular app(sent and received)
            double received = (double) TrafficStats.getUidRxBytes(UID)

                    / (1024 * 1024);
            double send = (double) TrafficStats.getUidTxBytes(UID)
                    / (1024 * 1024);
            double total = received + send;

            if (total > 0) {
                if (total > data) {
                    data = total;
                    appName = name;
                }
                Log.v(TAG, "---------- START ----------");
                Log.v(TAG, "Name: " + name);
                Log.v(TAG, "Package name: " + package_name);
                Log.v(TAG, "This app spent: " + String.format("%.2f", total) + " MB");
                Log.v(TAG, "----------- END ----------");
                totalData += total;

            }

        }
        Log.v(TAG, "Total spent: " + String.format("%.2f", totalData) + " MB");
        Log.v(TAG, String.format("%s spent the most: %.2f MB", appName, data));

    }

    public void getClientList() {
        int macCount = 0;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("/proc/net/arp"));
            String line;
            while ((line = br.readLine()) != null) {
                String[] splitted = line.split(" +");
                if (splitted != null) {
                    // Basic sanity check
                    String mac = splitted[3];
                    System.out.println("Mac : Outside If " + mac);
                    if (mac.matches("..:..:..:..:..:..")) {
                        macCount++;
                        Log.v(TAG, "Mac : " + mac + " IP Address : " + splitted[0]);

//                        System.out.println("Mac : "+ mac + " IP Address : "+splitted[0] );
                        System.out.println("Mac_Count  " + macCount + " MAC_ADDRESS  " + mac);
//                        Toast.makeText(
//                                getApplicationContext(),
//                                "Mac_Count  " + macCount + "   MAC_ADDRESS  "
//                                        + mac, Toast.LENGTH_SHORT).show();

                    }
                }
            }
        } catch (Exception e) {

        }
    }