Android Open Source - MaterialManager Root File






From Project

Back to project page MaterialManager.

License

The source code is released under:

There is no license, someone decided to pretty much republish Cabinet with no credit so I?m taking away the license altogether.

If you think the Android project MaterialManager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.afollestad.cabinet.file.root;
//from   w w w . j a  va2 s. co m
import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.afollestad.cabinet.R;
import com.afollestad.cabinet.file.LocalFile;
import com.afollestad.cabinet.file.base.File;
import com.afollestad.cabinet.file.base.FileFilter;
import com.afollestad.cabinet.sftp.SftpClient;
import com.afollestad.cabinet.utils.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import eu.chainfire.libsuperuser.Shell;

public class RootFile extends File {

    public RootFile(Activity context) {
        super(context, null);
    }

    public RootFile(Activity context, java.io.File from) {
        super(context, from.getAbsolutePath());
    }

    public String permissions;
    public String owner;
    public String creator;
    public long size = -1;
    public String date;
    public String time;
    public String originalName;

    @Override
    public boolean isHidden() {
        return getName().startsWith(".");
    }

    @Override
    public File getParent() {
        java.io.File mFile = new java.io.File(getPath());
        if (mFile.getParent() == null) return null;
        return new RootFile(getContext(), mFile.getParentFile());
    }

    @Override
    public void createFile(final SftpClient.CompletionCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    runAsRoot("touch \"" + getPath() + "\"", true);
                    callback.onComplete();
                } catch (Exception e) {
                    e.printStackTrace();
                    callback.onError(e);
                }
            }
        }).start();
    }

    @Override
    public void mkdir(final SftpClient.CompletionCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    runAsRoot("mkdir -P \"" + getPath() + "\"", true);
                    callback.onComplete();
                } catch (Exception e) {
                    e.printStackTrace();
                    callback.onError(e);
                }
            }
        }).start();
    }

    @Override
    public void rename(File newFile, final SftpClient.FileCallback callback) {
        Utils.checkDuplicates(getContext(), newFile, new Utils.DuplicateCheckResult() {
            @Override
            public void onResult(final File newFile) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            runAsRoot("mv -f \"" + getPath() + "\" \"" + newFile.getPath() + "\"", true);
                            getContext().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    setPath(newFile.getPath());
                                    callback.onComplete(newFile);
                                    updateMediaDatabase(newFile, MediaUpdateType.ADD);
                                }
                            });
                        } catch (final Exception e) {
                            e.printStackTrace();
                            getContext().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Utils.showErrorDialog(getContext(), R.string.failed_rename_file, e);
                                    callback.onError(null);
                                }
                            });
                        }
                    }
                }).start();
            }
        });
    }

    @Override
    public void copy(final File dest, final SftpClient.FileCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Utils.checkDuplicates(getContext(), dest, new Utils.DuplicateCheckResult() {
                    @Override
                    public void onResult(final File dest) {
                        try {
                            runAsRoot("cp -R \"" + getPath() + "\" \"" + dest.getPath() + "\"", true);
                            getContext().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onComplete(dest);
                                }
                            });
                        } catch (final Exception e) {
                            e.printStackTrace();
                            getContext().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Utils.showErrorDialog(getContext(), R.string.failed_copy_file, e);
                                    callback.onError(null);
                                }
                            });
                        }
                    }
                });
            }
        }).start();
    }

    @Override
    public void delete(final SftpClient.CompletionCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    deleteSync();
                    getContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (callback != null) callback.onComplete();
                        }
                    });
                } catch (final Exception e) {
                    e.printStackTrace();
                    getContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Utils.showErrorDialog(getContext(), R.string.failed_delete_file, e);
                            if (callback != null) callback.onError(null);
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    public boolean deleteSync() throws Exception {
        String deleteCmd = "rm -f \"" + getPath() + "\"";
        if (isDirectory()) {
            deleteCmd = "rm -rf \"" + getPath() + "\"";
        }
        runAsRoot(deleteCmd, true);
        return true;
    }

    @Override
    public boolean isRemote() {
        return false;
    }

    @Override
    public boolean isDirectory() {
        return size == -1;
    }

    @Override
    public void exists(final BooleanCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final boolean exists = existsSync();
                    getContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (callback != null) callback.onComplete(exists);
                        }
                    });
                } catch (final Exception e) {
                    e.printStackTrace();
                    getContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Utils.showErrorDialog(getContext(), R.string.error, e);
                            if (callback != null) callback.onError(null);
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    public boolean existsSync() throws Exception {
        String cmd;
        if (isDirectory()) {
            cmd = "[ -d \"" + getPath() + "\" ] && echo \"1\" || echo \"0\"";
        } else {
            cmd = "[ -f \"" + getPath() + "\" ] && echo \"1\" || echo \"0\"";
        }
        return Integer.parseInt(runAsRoot(cmd, true).get(0)) == 1;
    }

    @Override
    public long length() {
        return size;
    }

    @Override
    public void listFiles(boolean includeHidden, ArrayCallback callback) {
        listFiles(includeHidden, null, callback);
    }

    @Override
    public void listFiles(final boolean includeHidden, final FileFilter filter, final ArrayCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final List<File> results = listFilesSync(includeHidden, filter);
                    getContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            callback.onComplete(results != null ? results.toArray(new File[results.size()]) : null);
                        }
                    });
                } catch (final Exception e) {
                    e.printStackTrace();
                    getContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            callback.onError(e);
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    public List<File> listFilesSync(boolean includeHidden, FileFilter filter) throws Exception {
        List<File> results = new ArrayList<File>();
        if (requiresRoot()) {
            if (Shell.SU.available()) {
                List<String> response = runAsRoot("ls -l \"" + getPath() + "\"", false);
                if (response == null) return results;
                return LsParser.parse(getContext(), getPath(), response, filter, includeHidden).getFiles();
            }
        }
        java.io.File[] list;
        if (filter != null) list = new java.io.File(getPath()).listFiles();
        else list = new java.io.File(getPath()).listFiles();
        if (list == null || list.length == 0) return new ArrayList<File>();
        for (java.io.File local : list) {
            if (!includeHidden && (local.isHidden() || local.getName().startsWith(".")))
                continue;
            LocalFile file = new LocalFile(getContext(), local);
            if (filter != null) {
                if (filter.accept(file)) {
                    file.isSearchResult = true;
                    results.add(file);
                }
            } else results.add(file);
        }
        return results;
    }

    @Override
    public long lastModified() {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:ss", Locale.getDefault()).parse(date + " " + time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String getMountablePath(File from) {
        if (from == null) return null;
        if (from.getParent() == null || from.getParent().equals("/")) return from.getPath();
        File lastParent = from.getParent();
        if (lastParent.getParent() == null) {
            return lastParent.getPath();
        } else {
            while (true) {
                if (lastParent.getParent() == null || lastParent.getParent().getPath().equals("/")) {
                    return lastParent.getPath();
                }
                lastParent = lastParent.getParent();
            }
        }
    }

    public List<String> runAsRoot(String command, boolean mount) throws Exception {
        return runAsRoot(getContext(), command, mount ? getParent() : null);
    }

    public void mountParent(boolean chmodThis) {
        if (getParent() == null) return;
        String mountablePath = getMountablePath(getParent());
        Log.v("Cabinet-SU", "Mount: " + mountablePath);
        // CHMOD gives temporary permission to write
        String[] cmds;
        if (chmodThis) {
            cmds = new String[]{
                    "chmod 777 " + getPath(),
                    "mount -o remount,rw " + mountablePath
            };
        } else {
            cmds = new String[]{
                    "mount -o remount,rw " + mountablePath
            };
        }
        List<String> results = Shell.SU.run(cmds);
        if (results != null && results.size() > 0) {
            for (String r : results)
                Log.v("Cabinet-SU", "Mount result: " + r);
        }
    }

    public void unmountParent(boolean chmodThis) {
        if (getParent() == null) return;
        String mountablePath = getMountablePath(getParent());
        Log.v("Cabinet-SU", "Un-mount: " + mountablePath);
        String[] cmds;
        if (chmodThis) {
            cmds = new String[]{
                    "chmod 600 " + getPath(),
                    "mount -o remount,ro " + mountablePath
            };
        } else {
            cmds = new String[]{
                    "mount -o remount,ro " + mountablePath
            };
        }
        List<String> results = Shell.SU.run(cmds);
        if (results != null && results.size() > 0) {
            for (String r : results)
                Log.v("Cabinet-SU", "Mount result: " + r);
        }
    }

    public static List<String> runAsRoot(Context context, String command, File mount) throws Exception {
        String mountPath = getMountablePath(mount);
        if (mountPath != null)
            Log.v("Cabinet-SU", "Mount: " + mountPath);
        Log.v("Cabinet-SU", command);
        boolean suAvailable = Shell.SU.available();
        if (!suAvailable)
            throw new Exception(context.getString(R.string.superuser_not_available));
        String[] cmds;
        if (mountPath != null) {
            cmds = new String[]{
                    "mount -o remount,rw " + mountPath,
                    command
            };
        } else {
            cmds = new String[]{command};
        }
        return Shell.SU.run(cmds);
    }
}




Java Source Code List

com.afollestad.cabinet.App.java
com.afollestad.cabinet.ApplicationTest.java
com.afollestad.cabinet.adapters.FileAdapter.java
com.afollestad.cabinet.adapters.NavigationDrawerAdapter.java
com.afollestad.cabinet.cab.CopyCab.java
com.afollestad.cabinet.cab.CutCab.java
com.afollestad.cabinet.cab.MainCab.java
com.afollestad.cabinet.cab.PickerCab.java
com.afollestad.cabinet.cab.base.BaseCab.java
com.afollestad.cabinet.cab.base.BaseFileCab.java
com.afollestad.cabinet.comparators.AlphabeticalComparator.java
com.afollestad.cabinet.comparators.ExtensionComparator.java
com.afollestad.cabinet.comparators.FoldersFirstComparator.java
com.afollestad.cabinet.comparators.HighLowSizeComparator.java
com.afollestad.cabinet.comparators.LastModifiedComparator.java
com.afollestad.cabinet.comparators.LowHighSizeComparator.java
com.afollestad.cabinet.file.CloudFile.java
com.afollestad.cabinet.file.LocalFile.java
com.afollestad.cabinet.file.Remote.java
com.afollestad.cabinet.file.base.FileFilter.java
com.afollestad.cabinet.file.base.File.java
com.afollestad.cabinet.file.root.LsParser.java
com.afollestad.cabinet.file.root.LsTokenizer.java
com.afollestad.cabinet.file.root.RootFile.java
com.afollestad.cabinet.fragments.AboutDialog.java
com.afollestad.cabinet.fragments.DetailsDialog.java
com.afollestad.cabinet.fragments.DirectoryFragment.java
com.afollestad.cabinet.fragments.NavigationDrawerFragment.java
com.afollestad.cabinet.fragments.RemoteConnectionDialog.java
com.afollestad.cabinet.fragments.WelcomeFragment.java
com.afollestad.cabinet.services.NetworkService.java
com.afollestad.cabinet.sftp.FileNotExistsException.java
com.afollestad.cabinet.sftp.SftpClient.java
com.afollestad.cabinet.ui.DrawerActivity.java
com.afollestad.cabinet.ui.SettingsActivity.java
com.afollestad.cabinet.ui.TextEditor.java
com.afollestad.cabinet.ui.base.NetworkedActivity.java
com.afollestad.cabinet.ui.base.ThemableActivity.java
com.afollestad.cabinet.utils.APKIconDownloader.java
com.afollestad.cabinet.utils.PauseOnScrollListener.java
com.afollestad.cabinet.utils.Perm.java
com.afollestad.cabinet.utils.Pins.java
com.afollestad.cabinet.utils.StorageHelper.java
com.afollestad.cabinet.utils.ThemeUtils.java
com.afollestad.cabinet.utils.TimeUtils.java
com.afollestad.cabinet.utils.Utils.java
com.afollestad.cabinet.zip.Unzipper.java
com.afollestad.cabinet.zip.Zipper.java
eu.chainfire.libsuperuser.ApplicationTest.java
eu.chainfire.libsuperuser.Application.java
eu.chainfire.libsuperuser.Debug.java
eu.chainfire.libsuperuser.HideOverlaysReceiver.java
eu.chainfire.libsuperuser.ShellNotClosedException.java
eu.chainfire.libsuperuser.ShellOnMainThreadException.java
eu.chainfire.libsuperuser.Shell.java
eu.chainfire.libsuperuser.StreamGobbler.java