Example usage for android.support.v4.content AsyncTaskLoader AsyncTaskLoader

List of usage examples for android.support.v4.content AsyncTaskLoader AsyncTaskLoader

Introduction

In this page you can find the example usage for android.support.v4.content AsyncTaskLoader AsyncTaskLoader.

Prototype

public AsyncTaskLoader(Context context) 

Source Link

Usage

From source file:com.nononsenseapps.filepicker.local.LocalFilePickerFragment.java

/**
 * Get a loader that lists the Files in the current path,
 * and monitors changes.//from   www  .j a  va 2  s  .c  om
 */
@Override
protected Loader<List<FileSystemObjectInterface>> getLoader() {
    return new AsyncTaskLoader<List<FileSystemObjectInterface>>(getActivity()) {
        FileObserver fileObserver;

        @Override
        public List<FileSystemObjectInterface> loadInBackground() {
            ArrayList<FileSystemObjectInterface> files = new ArrayList<FileSystemObjectInterface>();
            File[] listFiles = ((LocalFileSystemObject) currentPath).getFile().listFiles();

            if (listFiles != null) {
                for (java.io.File f : listFiles) {
                    if ((mode == SelectionMode.MODE_FILE || mode == SelectionMode.MODE_FILE_AND_DIR)
                            || f.isDirectory()) {
                        LocalFileSystemObject obj = new LocalFileSystemObject(f);
                        files.add(obj);
                    }
                }
            }
            return files;
        }

        /**
         * Handles a request to start the Loader.
         */
        @Override
        protected void onStartLoading() {
            super.onStartLoading();

            // handle if directory does not exist. Fall back to root.
            if (currentPath == null || !currentPath.isDir()) {
                currentPath = getRoot();
            }

            // Start watching for changes
            fileObserver = new FileObserver(currentPath.getFullPath(), FileObserver.CREATE | FileObserver.DELETE
                    | FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {
                @Override
                public void onEvent(int event, String path) {
                    // Reload
                    onContentChanged();
                }
            };
            fileObserver.startWatching();

            forceLoad();
        }

        /**
         * Handles a request to completely reset the Loader.
         */
        @Override
        protected void onReset() {
            super.onReset();

            // Stop watching
            if (fileObserver != null) {
                fileObserver.stopWatching();
                fileObserver = null;
            }
        }
    };
}

From source file:org.rm3l.ddwrt.tiles.services.vpn.OpenVPNLogsTile.java

@Override
protected Loader<NVRAMInfo> getLoader(int id, Bundle args) {
    return new AsyncTaskLoader<NVRAMInfo>(this.mParentFragmentActivity) {

        @Nullable// w w  w . ja v a  2  s  . c  o m
        @Override
        public NVRAMInfo loadInBackground() {
            try {
                Log.d(LOG_TAG,
                        "Init background loader for " + OpenVPNLogsTile.class + ": routerInfo=" + mRouter
                                + " / this.mAutoRefreshToggle= " + mAutoRefreshToggle + " / nbRunsLoader="
                                + nbRunsLoader);

                if (nbRunsLoader > 0 && !mAutoRefreshToggle) {
                    //Skip run
                    Log.d(LOG_TAG, "Skip loader run");
                    return new NVRAMInfo().setException(new DDWRTTileAutoRefreshNotAllowedException());
                }
                nbRunsLoader++;

                @NotNull
                final NVRAMInfo nvramInfo = new NVRAMInfo();
                NVRAMInfo nvramInfoTmp = null;
                String[] logs = null;
                String logsStr = "";
                int openvpnclMgmtPort = -1;
                try {
                    //Find OpenVPN Management Port
                    final String[] openvpnclConf = SSHUtils.getManualProperty(mRouter, mGlobalPreferences,
                            "cat /tmp/openvpncl/openvpn.conf | grep \"management \" 2>/dev/null");
                    if (openvpnclConf != null && openvpnclConf.length > 0) {
                        final String managementLine = openvpnclConf[0];
                        if (managementLine != null) {
                            final List<String> list = Splitter.on(" ").splitToList(managementLine);
                            if (list != null && list.size() >= 3) {
                                try {
                                    openvpnclMgmtPort = Integer.parseInt(list.get(2));
                                } catch (final NumberFormatException nfe) {
                                    nfe.printStackTrace();
                                    //No Worries
                                }
                            }
                        }
                    }
                } finally {
                    try {
                        //Telnet on Management Port to retrieve latest logs
                        if (openvpnclMgmtPort > 0) {
                            logs = SSHUtils.execCommandOverTelnet(mRouter, mGlobalPreferences,
                                    openvpnclMgmtPort, String.format("log %s", MAX_LOG_LINES));
                            if (logs != null) {
                                logsStr = LOGS_JOINER.join(logs);
                            }
                        }
                    } finally {
                        try {
                            nvramInfoTmp = SSHUtils.getNVRamInfoFromRouter(mRouter, mGlobalPreferences,
                                    SYSLOGD_ENABLE);
                        } finally {

                            if (nvramInfoTmp != null) {
                                nvramInfo.putAll(nvramInfoTmp);
                            }

                            try {
                                //Get last log lines
                                logs = SSHUtils.getManualProperty(mRouter, mGlobalPreferences,
                                        String.format("tail -n %d /tmp/var/log/messages %s", MAX_LOG_LINES,
                                                " | grep -i -E \"" + OPENVPN + "\""));
                            } finally {
                                if (logs != null) {
                                    final String logsToSet = logsStr + "\n" + LOGS_JOINER.join(logs);
                                    if (!"\n".equals(logsToSet)) {
                                        nvramInfo.setProperty(SYSLOG, logsToSet);
                                    }
                                }
                            }
                        }
                    }
                }

                return nvramInfo;

            } catch (@NotNull final Exception e) {
                e.printStackTrace();
                return new NVRAMInfo().setException(e);
            }
        }
    };
}

From source file:org.rm3l.ddwrt.tiles.status.lan.LANStateTile.java

@Nullable
@Override//from ww w . j  a v  a 2 s.com
protected Loader<NVRAMInfo> getLoader(int id, Bundle args) {
    return new AsyncTaskLoader<NVRAMInfo>(this.mParentFragmentActivity) {

        @Nullable
        @Override
        public NVRAMInfo loadInBackground() {

            try {
                Log.d(LOG_TAG,
                        "Init background loader for " + LANStateTile.class + ": routerInfo=" + mRouter
                                + " / this.mAutoRefreshToggle= " + mAutoRefreshToggle + " / nbRunsLoader="
                                + nbRunsLoader);

                if (nbRunsLoader > 0 && !mAutoRefreshToggle) {
                    //Skip run
                    Log.d(LOG_TAG, "Skip loader run");
                    return new NVRAMInfo().setException(new DDWRTTileAutoRefreshNotAllowedException());
                }
                nbRunsLoader++;

                return SSHUtils.getNVRamInfoFromRouter(mRouter, mGlobalPreferences, NVRAMInfo.LAN_IPADDR,
                        NVRAMInfo.LAN_DOMAIN, NVRAMInfo.LAN_GATEWAY, NVRAMInfo.LAN_HWADDR,
                        NVRAMInfo.LAN_NETMASK, NVRAMInfo.LOCAL_DNS);

            } catch (@NotNull final Exception e) {
                e.printStackTrace();
                return new NVRAMInfo().setException(e);
            }
        }
    };
}

From source file:de.dreier.mytargets.base.fragments.FragmentBase.java

@Override
public Loader<LoaderUICallback> onCreateLoader(int id, Bundle args) {
    return new AsyncTaskLoader<LoaderUICallback>(getContext()) {
        @Override//from www.java 2s.c o  m
        public LoaderUICallback loadInBackground() {
            return onLoad(args);
        }
    };
}

From source file:org.rm3l.ddwrt.tiles.status.bandwidth.IfacesTile.java

@Override
protected Loader<NVRAMInfo> getLoader(int id, Bundle args) {
    return new AsyncTaskLoader<NVRAMInfo>(this.mParentFragmentActivity) {

        @Nullable/*w ww .  ja  va  2  s. c o  m*/
        @Override
        public NVRAMInfo loadInBackground() {

            try {
                Log.d(LOG_TAG,
                        "Init background loader for " + WANConfigTile.class + ": routerInfo=" + mRouter
                                + " / this.mAutoRefreshToggle= " + mAutoRefreshToggle + " / nbRunsLoader="
                                + nbRunsLoader);

                if (nbRunsLoader > 0 && !mAutoRefreshToggle) {
                    //Skip run
                    Log.d(LOG_TAG, "Skip loader run");
                    return new NVRAMInfo().setException(new DDWRTTileAutoRefreshNotAllowedException());
                }
                nbRunsLoader++;

                final NVRAMInfo nvramInfo = new NVRAMInfo();

                NVRAMInfo nvramInfoTmp = null;
                try {
                    nvramInfoTmp = SSHUtils.getNVRamInfoFromRouter(mRouter, mGlobalPreferences,
                            NVRAMInfo.LAN_IFNAME, NVRAMInfo.WAN_IFNAME, NVRAMInfo.LANDEVS);
                } finally {
                    if (nvramInfoTmp != null) {
                        nvramInfo.putAll(nvramInfoTmp);
                    }

                    String landevs = nvramInfo.getProperty(NVRAMInfo.LANDEVS, null);
                    if (landevs != null) {
                        final List<String> splitToList = SPLITTER.splitToList(landevs);
                        if (splitToList != null && !splitToList.isEmpty()) {

                            for (@Nullable
                            final String landev : splitToList) {
                                if (landev == null || !(landev.startsWith("wl") || landev.startsWith("ath"))) {
                                    continue;
                                }
                                //Also get Virtual Interfaces
                                try {
                                    final String landevVifsKeyword = landev + "_vifs";
                                    final NVRAMInfo landevVifsNVRAMInfo = SSHUtils.getNVRamInfoFromRouter(
                                            mRouter, mGlobalPreferences, landevVifsKeyword);
                                    if (landevVifsNVRAMInfo == null) {
                                        continue;
                                    }
                                    final String landevVifsNVRAMInfoProp = landevVifsNVRAMInfo.getProperty(
                                            landevVifsKeyword, DDWRTCompanionConstants.EMPTY_STRING);
                                    if (landevVifsNVRAMInfoProp == null) {
                                        continue;
                                    }
                                    final List<String> list = SPLITTER.splitToList(landevVifsNVRAMInfoProp);
                                    if (list == null) {
                                        continue;
                                    }
                                    for (final String landevVif : list) {
                                        if (landevVif == null || landevVif.isEmpty()) {
                                            continue;
                                        }
                                        landevs += (" " + landevVif);
                                    }
                                } catch (final Exception e) {
                                    e.printStackTrace();
                                    //No worries
                                }
                            }
                        }

                        nvramInfo.setProperty(NVRAMInfo.LANDEVS, landevs);
                    }

                }

                if (nvramInfo.isEmpty()) {
                    throw new DDWRTNoDataException("No Data");
                }

                return nvramInfo;

            } catch (@NotNull final Exception e) {
                e.printStackTrace();
                return new NVRAMInfo().setException(e);
            }
        }
    };
}

From source file:org.rm3l.ddwrt.tiles.status.wan.WANTrafficTile.java

@Nullable
@Override/*from   w w w .  j a  va  2  s.  c o  m*/
protected Loader<NVRAMInfo> getLoader(int id, Bundle args) {
    return new AsyncTaskLoader<NVRAMInfo>(this.mParentFragmentActivity) {

        @Nullable
        @Override
        public NVRAMInfo loadInBackground() {

            try {
                Log.d(LOG_TAG,
                        "Init background loader for " + WANConfigTile.class + ": routerInfo=" + mRouter
                                + " / this.mAutoRefreshToggle= " + mAutoRefreshToggle + " / nbRunsLoader="
                                + nbRunsLoader);

                if (nbRunsLoader > 0 && !mAutoRefreshToggle) {
                    //Skip run
                    Log.d(LOG_TAG, "Skip loader run");
                    return new NVRAMInfo().setException(new DDWRTTileAutoRefreshNotAllowedException());
                }
                nbRunsLoader++;

                //TODO
                //Start by getting information about the WAN iface name
                @Nullable
                final NVRAMInfo nvRamInfoFromRouter = SSHUtils.getNVRamInfoFromRouter(mRouter,
                        mGlobalPreferences, NVRAMInfo.WAN_IFACE);
                if (nvRamInfoFromRouter == null) {
                    throw new IllegalStateException("Whoops. WAN Iface could not be determined.");
                }

                final String wanIface = nvRamInfoFromRouter.getProperty(NVRAMInfo.WAN_IFACE);

                if (Strings.isNullOrEmpty(wanIface)) {
                    throw new IllegalStateException("Whoops. WAN Iface could not be determined.");
                }

                @Nullable
                final String[] netDevWanIfaces = SSHUtils.getManualProperty(mRouter, mGlobalPreferences,
                        "cat /proc/net/dev | grep \"" + wanIface + "\"");
                if (netDevWanIfaces == null || netDevWanIfaces.length == 0) {
                    return null;
                }

                String netDevWanIface = netDevWanIfaces[0];
                if (netDevWanIface == null) {
                    return null;
                }

                netDevWanIface = netDevWanIface.replaceAll(wanIface + ":", "");

                final List<String> netDevWanIfaceList = Splitter.on(" ").omitEmptyStrings().trimResults()
                        .splitToList(netDevWanIface);
                if (netDevWanIfaceList == null || netDevWanIfaceList.size() <= 15) {
                    return null;
                }

                nvRamInfoFromRouter.setProperty(wanIface + "_rcv_bytes", netDevWanIfaceList.get(0));
                nvRamInfoFromRouter.setProperty(wanIface + "_rcv_packets", netDevWanIfaceList.get(1));
                nvRamInfoFromRouter.setProperty(wanIface + "_rcv_errs", netDevWanIfaceList.get(2));
                nvRamInfoFromRouter.setProperty(wanIface + "_rcv_drop", netDevWanIfaceList.get(3));
                nvRamInfoFromRouter.setProperty(wanIface + "_rcv_fifo", netDevWanIfaceList.get(4));
                nvRamInfoFromRouter.setProperty(wanIface + "_rcv_frame", netDevWanIfaceList.get(5));
                nvRamInfoFromRouter.setProperty(wanIface + "_rcv_compressed", netDevWanIfaceList.get(6));
                nvRamInfoFromRouter.setProperty(wanIface + "_rcv_multicast", netDevWanIfaceList.get(7));

                nvRamInfoFromRouter.setProperty(wanIface + "_xmit_bytes", netDevWanIfaceList.get(8));
                nvRamInfoFromRouter.setProperty(wanIface + "_xmit_packets", netDevWanIfaceList.get(9));
                nvRamInfoFromRouter.setProperty(wanIface + "_xmit_errs", netDevWanIfaceList.get(10));
                nvRamInfoFromRouter.setProperty(wanIface + "_xmit_drop", netDevWanIfaceList.get(11));
                nvRamInfoFromRouter.setProperty(wanIface + "_xmit_fifo", netDevWanIfaceList.get(12));
                nvRamInfoFromRouter.setProperty(wanIface + "_xmit_colls", netDevWanIfaceList.get(13));
                nvRamInfoFromRouter.setProperty(wanIface + "_xmit_carrier", netDevWanIfaceList.get(14));
                nvRamInfoFromRouter.setProperty(wanIface + "_xmit_compressed", netDevWanIfaceList.get(15));

                return nvRamInfoFromRouter;

            } catch (@NotNull final Exception e) {
                e.printStackTrace();
                return new NVRAMInfo().setException(e);
            }
        }
    };
}

From source file:org.rm3l.ddwrt.tiles.status.lan.DHCPStatusTile.java

@Nullable
@Override/*from   w  ww. j a  v a 2 s  . co m*/
protected Loader<NVRAMInfo> getLoader(int id, Bundle args) {
    return new AsyncTaskLoader<NVRAMInfo>(this.mParentFragmentActivity) {

        @Nullable
        @Override
        public NVRAMInfo loadInBackground() {

            try {
                Log.d(LOG_TAG,
                        "Init background loader for " + DHCPStatusTile.class + ": routerInfo=" + mRouter
                                + " / this.mAutoRefreshToggle= " + mAutoRefreshToggle + " / nbRunsLoader="
                                + nbRunsLoader);

                if (nbRunsLoader > 0 && !mAutoRefreshToggle) {
                    //Skip run
                    Log.d(LOG_TAG, "Skip loader run");
                    return new NVRAMInfo().setException(new DDWRTTileAutoRefreshNotAllowedException());
                }
                nbRunsLoader++;

                @Nullable
                final NVRAMInfo nvramInfo = new NVRAMInfo();

                NVRAMInfo nvramInfoTmp = null;
                try {
                    nvramInfoTmp = SSHUtils.getNVRamInfoFromRouter(mRouter, mGlobalPreferences,
                            NVRAMInfo.LAN_PROTO, NVRAMInfo.DHCP_DNSMASQ, NVRAMInfo.DHCP_START,
                            NVRAMInfo.DHCP_NUM, NVRAMInfo.DHCP_LEASE, NVRAMInfo.LAN_IPADDR,
                            NVRAMInfo.LAN_NETMASK);
                } finally {
                    if (nvramInfoTmp != null) {
                        nvramInfo.putAll(nvramInfoTmp);
                    }

                    //Manually compute Start and End IP Addresses
                    final String lanAddr = nvramInfo.getProperty(NVRAMInfo.LAN_IPADDR);
                    final String dhcpStart = nvramInfo.getProperty(NVRAMInfo.DHCP_START);

                    String dhcpStartIp = null;
                    if (dhcpStart != null && lanAddr != null) {
                        final List<String> lowAddressSplit = IP_ADDR_SPLITTER.splitToList(lanAddr);
                        if (lowAddressSplit != null && lowAddressSplit.size() >= 3) {
                            dhcpStartIp = String.format("%s.%s.%s.%s", lowAddressSplit.get(0),
                                    lowAddressSplit.get(1), lowAddressSplit.get(2), dhcpStart);
                            nvramInfo.setProperty(DHCP_START_IP, dhcpStartIp);
                        }
                    }

                    final String dhcpHostCountStr = nvramInfo.getProperty(NVRAMInfo.DHCP_NUM);
                    final String netmask = nvramInfo.getProperty(NVRAMInfo.LAN_NETMASK);
                    if (netmask != null) {
                        if (dhcpStartIp != null) {
                            final SubnetUtils subnetUtils = new SubnetUtils(dhcpStartIp, netmask);
                            final SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();

                            if (dhcpHostCountStr != null) {
                                try {
                                    final int dhcpHostCount = Integer.parseInt(dhcpHostCountStr);
                                    final String[] allAddresses = subnetInfo.getAllAddresses();
                                    //Compute number of hosts between low ip and dhcp_start ip
                                    if (allAddresses != null) {
                                        int distFromLowIpToStartIp = 0;
                                        for (String address : allAddresses) {
                                            if (address.equals(dhcpStartIp)) {
                                                break;
                                            }
                                            distFromLowIpToStartIp++;
                                        }
                                        //Then get end address, starting from DHCP Start IP
                                        if (allAddresses.length >= distFromLowIpToStartIp + dhcpHostCount) {
                                            nvramInfo.setProperty(DHCP_END_IP,
                                                    allAddresses[distFromLowIpToStartIp + dhcpHostCount - 1]);
                                        }
                                    }

                                } catch (final NumberFormatException nfe) {
                                    //No worries
                                    nfe.printStackTrace();
                                }
                            }
                        }
                    }
                }

                if (nvramInfo.isEmpty()) {
                    throw new DDWRTNoDataException("No Data!");
                }

                return nvramInfo;

            } catch (@NotNull final Exception e) {
                e.printStackTrace();
                return new NVRAMInfo().setException(e);
            }
        }
    };
}

From source file:org.rm3l.ddwrt.tiles.status.wan.WANConfigTile.java

/**
 * Instantiate and return a new Loader for the given ID.
 *
 * @param id   The ID whose loader is to be created.
 * @param args Any arguments supplied by the caller.
 * @return Return a new Loader instance that is ready to start loading.
 */// w w  w  . j  ava  2s .co  m
@Override
protected Loader<NVRAMInfo> getLoader(final int id, final Bundle args) {
    return new AsyncTaskLoader<NVRAMInfo>(this.mParentFragmentActivity) {

        @Nullable
        @Override
        public NVRAMInfo loadInBackground() {

            try {
                Log.d(LOG_TAG,
                        "Init background loader for " + WANConfigTile.class + ": routerInfo=" + mRouter
                                + " / this.mAutoRefreshToggle= " + mAutoRefreshToggle + " / nbRunsLoader="
                                + nbRunsLoader);

                if (nbRunsLoader > 0 && !mAutoRefreshToggle) {
                    //Skip run
                    Log.d(LOG_TAG, "Skip loader run");
                    return new NVRAMInfo().setException(new DDWRTTileAutoRefreshNotAllowedException());
                }
                nbRunsLoader++;

                @NotNull
                final NVRAMInfo nvramInfo = new NVRAMInfo();

                NVRAMInfo nvramInfoTmp = null;
                try {
                    nvramInfoTmp = SSHUtils.getNVRamInfoFromRouter(mRouter, mGlobalPreferences,
                            NVRAMInfo.WAN_PROTO, NVRAMInfo.WAN_3_G_SIGNAL, NVRAMInfo.WAN_HWADDR,
                            NVRAMInfo.WAN_LEASE, NVRAMInfo.WAN_IPADDR, NVRAMInfo.WAN_NETMASK,
                            NVRAMInfo.WAN_GATEWAY, NVRAMInfo.WAN_DNS);
                } finally {
                    if (nvramInfoTmp != null) {
                        nvramInfo.putAll(nvramInfoTmp);
                    }

                    //Connection Uptime is stored in /tmp/.wanuptime and sys uptime from /proc/uptime
                    final String[] uptimes = SSHUtils.getManualProperty(mRouter, mGlobalPreferences,
                            "cat /tmp/.wanuptime; echo; cat /proc/uptime");
                    if (uptimes != null && uptimes.length > 1) {
                        final String wanUptimeStr = uptimes[0];

                        /*
                         * /proc/uptime Shows how long the system has been on since it was last restarted.
                         * The first number is the total number of seconds the system has been up.
                         * The second number is how much of that time the machine has spent idle, in seconds.
                         * On multi core systems (and some linux versions), the second number is the sum of
                         * the idle time accumulated by each CPU.
                         */
                        final List<String> sysUptimeSplitList = Splitter.on(" ").trimResults()
                                .omitEmptyStrings().splitToList(uptimes[1]);
                        if (sysUptimeSplitList != null && !sysUptimeSplitList.isEmpty()) {
                            try {
                                final float uptime = Float.valueOf(sysUptimeSplitList.get(0))
                                        - Float.valueOf(wanUptimeStr);
                                final int days = (int) uptime / (60 * 60 * 24);

                                String wanConnectionUptimeStr = "";
                                if (days > 0) {
                                    wanConnectionUptimeStr += String.format("%d day%s, ", days,
                                            (days == 1 ? "" : "s"));
                                }
                                final int minutes = (int) uptime / 60;
                                wanConnectionUptimeStr += String.format("%d:%02d:%02d", (minutes / 60) % 24,
                                        minutes % 60, (int) uptime % 60);

                                nvramInfo.setProperty(NVRAMInfo.WAN_CONNECTION_UPTIME, wanConnectionUptimeStr);

                            } catch (final NumberFormatException nfe) {
                                nfe.printStackTrace();
                                //No Worries - WAN Uptime will be marked as "-"
                            }
                        }
                    }
                }

                if (nvramInfo.isEmpty()) {
                    throw new DDWRTNoDataException("No Data!");
                }

                return nvramInfo;

            } catch (@NotNull final Exception e) {
                e.printStackTrace();
                return new NVRAMInfo().setException(e);
            }
        }
    };
}

From source file:org.rm3l.ddwrt.tiles.status.router.StatusRouterSpaceUsageTile.java

@Override
protected Loader<NVRAMInfo> getLoader(int id, Bundle args) {
    return new AsyncTaskLoader<NVRAMInfo>(this.mParentFragmentActivity) {

        @NotNull/*from ww  w.  j  a va 2 s .  co  m*/
        @Override
        public NVRAMInfo loadInBackground() {

            try {
                Log.d(LOG_TAG,
                        "Init background loader for " + StatusRouterSpaceUsageTile.class + ": routerInfo="
                                + mRouter + " / this.mAutoRefreshToggle= " + mAutoRefreshToggle
                                + " / nbRunsLoader=" + nbRunsLoader);

                if (nbRunsLoader > 0 && !mAutoRefreshToggle) {
                    //Skip run
                    Log.d(LOG_TAG, "Skip loader run");
                    return new NVRAMInfo().setException(new DDWRTTileAutoRefreshNotAllowedException());
                }
                nbRunsLoader++;

                @NotNull
                final NVRAMInfo nvramInfo = new NVRAMInfo();

                @NotNull
                final Map<String, ProcMountPoint> mountPointMap = new HashMap<String, ProcMountPoint>();
                @NotNull
                final Map<String, List<ProcMountPoint>> mountTypes = new HashMap<String, List<ProcMountPoint>>();

                @Nullable
                final String[] catProcMounts = SSHUtils.getManualProperty(mRouter, mGlobalPreferences,
                        "nvram show 2>&1 1>/dev/null", "cat /proc/mounts");
                Log.d(LOG_TAG, "catProcMounts: " + Arrays.toString(catProcMounts));
                @Nullable
                String cifsMountPoint = null;
                if (catProcMounts != null && catProcMounts.length >= 1) {
                    final List<String> nvramUsageList = NVRAM_SIZE_SPLITTER.splitToList(catProcMounts[0]);
                    if (nvramUsageList != null && !nvramUsageList.isEmpty()) {
                        nvramInfo.setProperty("nvram_space", nvramUsageList.get(0));
                    }

                    int i = 0;
                    for (@Nullable
                    final String procMountLine : catProcMounts) {
                        if (i == 0 || procMountLine == null) {
                            i++;
                            continue;
                        }
                        final List<String> procMountLineItem = Splitter.on(" ").omitEmptyStrings().trimResults()
                                .splitToList(procMountLine);

                        if (procMountLineItem != null) {
                            if (procMountLineItem.size() >= 6) {
                                @NotNull
                                final ProcMountPoint procMountPoint = new ProcMountPoint();
                                procMountPoint.setDeviceType(procMountLineItem.get(0));
                                procMountPoint.setMountPoint(procMountLineItem.get(1));
                                procMountPoint.setFsType(procMountLineItem.get(2));

                                if ("cifs".equalsIgnoreCase(procMountPoint.getFsType())) {
                                    cifsMountPoint = procMountPoint.getMountPoint();
                                }

                                final List<String> procMountLineItemPermissions = Splitter.on(",")
                                        .omitEmptyStrings().trimResults().splitToList(procMountLineItem.get(3));
                                if (procMountLineItemPermissions != null) {
                                    for (String procMountLineItemPermission : procMountLineItemPermissions) {
                                        procMountPoint.addPermission(procMountLineItemPermission);
                                    }
                                }
                                procMountPoint.addOtherAttr(procMountLineItem.get(4));

                                mountPointMap.put(procMountPoint.getMountPoint(), procMountPoint);

                                if (mountTypes.get(procMountPoint.getFsType()) == null) {
                                    mountTypes.put(procMountPoint.getFsType(), new ArrayList<ProcMountPoint>());
                                }
                            }
                        }
                    }
                }

                @NotNull
                final List<String> itemsToDf = new ArrayList<String>();

                //JFFS Space: "jffs_space"
                final ProcMountPoint jffsProcMountPoint = mountPointMap.get("/jffs");
                if (jffsProcMountPoint != null) {
                    itemsToDf.add(jffsProcMountPoint.getMountPoint());
                }

                //CIFS: "cifs_space"
                if (cifsMountPoint != null) {
                    final ProcMountPoint cifsProcMountPoint = mountPointMap.get(cifsMountPoint);
                    if (cifsProcMountPoint != null) {
                        itemsToDf.add(cifsProcMountPoint.getMountPoint());
                    }
                }

                for (final String itemToDf : itemsToDf) {
                    @Nullable
                    final String[] itemToDfResult = SSHUtils.getManualProperty(mRouter, mGlobalPreferences,
                            "df -h " + itemToDf + " | grep -v Filessytem | grep \"" + itemToDf + "\"");
                    Log.d(LOG_TAG, "catProcMounts: " + Arrays.toString(catProcMounts));
                    if (itemToDfResult != null && itemToDfResult.length > 0) {
                        final List<String> procMountLineItem = Splitter.on(" ").omitEmptyStrings().trimResults()
                                .splitToList(itemToDfResult[0]);
                        if (procMountLineItem == null) {
                            continue;
                        }

                        if ("/jffs".equalsIgnoreCase(itemToDf)) {
                            if (procMountLineItem.size() >= 4) {
                                nvramInfo.setProperty("jffs_space_max", procMountLineItem.get(1));
                                nvramInfo.setProperty("jffs_space_used", procMountLineItem.get(2));
                                nvramInfo.setProperty("jffs_space_available", procMountLineItem.get(3));
                                nvramInfo.setProperty("jffs_space",
                                        procMountLineItem.get(1) + " (" + procMountLineItem.get(3) + " left)");
                            }
                        } else if (cifsMountPoint != null && cifsMountPoint.equalsIgnoreCase(itemToDf)) {
                            if (procMountLineItem.size() >= 3) {
                                nvramInfo.setProperty("cifs_space_max", procMountLineItem.get(0));
                                nvramInfo.setProperty("cifs_space_used", procMountLineItem.get(1));
                                nvramInfo.setProperty("cifs_space_available", procMountLineItem.get(2));
                                nvramInfo.setProperty("cifs_space",
                                        procMountLineItem.get(0) + " (" + procMountLineItem.get(2) + " left)");
                            }
                        }
                    }
                }

                return nvramInfo;
            } catch (@NotNull final Exception e) {
                e.printStackTrace();
                return new NVRAMInfo().setException(e);
            }
        }
    };
}

From source file:org.rm3l.ddwrt.tiles.status.time.StatusTimeTile.java

@Nullable
@Override//from   w  w w  . ja va  2 s  . c  o  m
protected Loader<NVRAMInfo> getLoader(int id, Bundle args) {
    return new AsyncTaskLoader<NVRAMInfo>(this.mParentFragmentActivity) {

        @Nullable
        @Override
        public NVRAMInfo loadInBackground() {

            try {
                Log.d(LOG_TAG,
                        "Init background loader for " + StatusTimeTile.class + ": routerInfo=" + mRouter
                                + " / this.mAutoRefreshToggle= " + mAutoRefreshToggle + " / nbRunsLoader="
                                + nbRunsLoader);

                if (nbRunsLoader > 0 && !mAutoRefreshToggle) {
                    //Skip run
                    Log.d(LOG_TAG, "Skip loader run");
                    return new NVRAMInfo().setException(new DDWRTTileAutoRefreshNotAllowedException());
                }
                nbRunsLoader++;

                @NotNull
                final NVRAMInfo nvramInfo = new NVRAMInfo();

                NVRAMInfo nvramInfoTmp = null;
                try {
                    nvramInfoTmp = SSHUtils.getNVRamInfoFromRouter(mRouter, mGlobalPreferences,
                            NVRAMInfo.NTP_ENABLE, NVRAMInfo.NTP_MODE, NVRAMInfo.NTP_SERVER, NVRAMInfo.TIME_ZONE,
                            NVRAMInfo.DAYLIGHT_TIME);
                } finally {
                    if (nvramInfoTmp != null) {
                        nvramInfo.putAll(nvramInfoTmp);
                    }

                    final String[] currentDate = SSHUtils.getManualProperty(mRouter, mGlobalPreferences,
                            "date");
                    if (currentDate != null && currentDate.length > 0) {
                        nvramInfo.setProperty(NVRAMInfo.CURRENT_DATE, currentDate[0]);
                    }
                }

                if (nvramInfo.isEmpty()) {
                    throw new DDWRTNoDataException("No Data!");
                }

                return nvramInfo;
            } catch (@NotNull final Exception e) {
                e.printStackTrace();
                return new NVRAMInfo().setException(e);
            }

        }
    };
}