Back to project page Calma.
The source code is released under:
Apache License
If you think the Android project Calma listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.scto.android.calma.utils; //from w w w . ja va 2 s .c om import android.app.Activity; import android.content.Context; import android.content.res.Configuration; import android.widget.Toast; import com.stericson.RootTools.RootTools; import com.stericson.RootTools.exceptions.RootDeniedException; import com.stericson.RootTools.execution.Command; import com.stericson.RootTools.execution.CommandCapture; import com.stericson.RootTools.execution.Shell; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.ArrayList; import java.util.Comparator; import java.util.concurrent.TimeoutException; import java.util.Stack; public class Utils { private static final int SORT_ALPHA = 0; private static final int SORT_TYPE = 1; private static final int SORT_SIZE = 2; private Stack<String> mPathStack; private ArrayList<String> mDirContent; private static int fileCount = 0; private boolean mShowHiddenFiles; private int mSortType = SORT_TYPE; public Utils(){ mPathStack = new Stack<String>(); mPathStack.push("/"); mPathStack.push(mPathStack.peek()); } public static boolean isTablet(Context context){ return ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE); } public static boolean isPhone(Context context){ return ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) < Configuration.SCREENLAYOUT_SIZE_LARGE); } public static boolean isSmall(Context context){ return ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) <= Configuration.SCREENLAYOUT_SIZE_SMALL); } public static void makeToast(Context context, String msg) { CharSequence text = msg; int duration = Toast.LENGTH_LONG; Toast toast = Toast.makeText(context, text, duration); toast.show(); } // This will return a string of the current directory path public String getCurrentDir() { return mPathStack.peek(); } // This will return a string of the current home path public ArrayList<String> setHomeDir(String path) { // This will eventually be placed as a settings item mPathStack.clear(); mPathStack.push("/"); mPathStack.push(path); return populate_list(); } // This will return to the previous Directory public ArrayList<String> getPreviousDir(String path) { File file = new File(path); String parent = file.getParent(); mPathStack.clear(); mPathStack.push("/"); mPathStack.push(parent); return populate_list(); } public ArrayList<String> getNextDir(String path, boolean isFullPath) { int size = mPathStack.size(); if (!path.equals(mPathStack.peek()) && !isFullPath) { if (size == 1) mPathStack.push("/" + path); else mPathStack.push(mPathStack.peek() + "/" + path); } else if (!path.equals(mPathStack.peek()) && isFullPath) { mPathStack.push(path); } return populate_list(); } public static int getFileCount(File file) { fileCount = 0; calculateFileCount(file); return fileCount; } private static void calculateFileCount(File file) { if (!file.isDirectory()) { fileCount++; return; } if (file.list() == null) { return; } for (String fileName : file.list()) { File f = new File(file.getAbsolutePath() + File.separator + fileName); calculateFileCount(f); } } /* * (non-Javadoc) this function will take the string from the top of the * directory stack and list all files/folders that are in it and return that * list so it can be displayed. Since this function is called every time we * need to update the the list of files to be shown to the user, this is * where we do our sorting (by type, alphabetical, etc). */ @SuppressWarnings("unchecked") private ArrayList<String> populate_list() { mDirContent = new ArrayList<String>(); if (!mDirContent.isEmpty()) mDirContent.clear(); final File file = new File(mPathStack.peek()); if (file.exists() && file.canRead()) { String[] list = file.list(); int len = list.length; // add files/folder to ArrayList depending on hidden status for (int i = 0; i < len; i++) { if (!mShowHiddenFiles) { if (list[i].toString().charAt(0) != '.') mDirContent.add(list[i]); } else { mDirContent.add(list[i]); } } // Set SortType switch (mSortType) { case SORT_ALPHA: Object[] tt = mDirContent.toArray(); mDirContent.clear(); Arrays.sort(tt, alph); for (Object a : tt) { mDirContent.add((String) a); } break; case SORT_SIZE: int index = 0; Object[] size_ar = mDirContent.toArray(); String dir = mPathStack.peek(); Arrays.sort(size_ar, size); mDirContent.clear(); for (Object a : size_ar) { if (new File(dir + "/" + (String) a).isDirectory()) mDirContent.add(index++, (String) a); else mDirContent.add((String) a); } break; case SORT_TYPE: int dirindex = 0; Object[] type_ar = mDirContent.toArray(); String current = mPathStack.peek(); Arrays.sort(type_ar, type); mDirContent.clear(); for (Object a : type_ar) { if (new File(current + "/" + (String) a).isDirectory()) mDirContent.add(dirindex++, (String) a); else mDirContent.add((String) a); } break; } } else { mDirContent = new ArrayList<String>(); try { Process p = Runtime.getRuntime().exec( new String[] { "su", "-c", "ls -a \"" + file.getAbsolutePath() + "\"" }); BufferedReader in = new BufferedReader(new InputStreamReader( p.getInputStream())); String line; while ((line = in.readLine()) != null) { if (!mShowHiddenFiles) { if (line.toString().charAt(0) != '.') mDirContent.add(line); } else { mDirContent.add(line); } } } catch (IOException e) { e.printStackTrace(); } // Set SortType switch (mSortType) { case SORT_ALPHA: Object[] tt = mDirContent.toArray(); mDirContent.clear(); Arrays.sort(tt, alph); for (Object a : tt) { mDirContent.add((String) a); } break; case SORT_SIZE: int index = 0; Object[] size_ar = mDirContent.toArray(); String dir = mPathStack.peek(); Arrays.sort(size_ar, size); mDirContent.clear(); for (Object a : size_ar) { if (new File(dir + "/" + (String) a).isDirectory()) mDirContent.add(index++, (String) a); else mDirContent.add((String) a); } break; case SORT_TYPE: int dirindex = 0; Object[] type_ar = mDirContent.toArray(); String current = mPathStack.peek(); Arrays.sort(type_ar, type); mDirContent.clear(); for (Object a : type_ar) { if (new File(current + "/" + (String) a).isDirectory()) mDirContent.add(dirindex++, (String) a); else mDirContent.add((String) a); } break; } } return mDirContent; } // Sort Comparator @SuppressWarnings("rawtypes") private static final Comparator alph = new Comparator<String>() { @Override public int compare(String arg0, String arg1) { return arg0.toLowerCase().compareTo(arg1.toLowerCase()); } }; @SuppressWarnings("rawtypes") private final Comparator size = new Comparator<String>() { @Override public int compare(String arg0, String arg1) { String dir = mPathStack.peek(); Long first = new File(dir + "/" + arg0).length(); Long second = new File(dir + "/" + arg1).length(); return first.compareTo(second); } }; @SuppressWarnings("rawtypes") private final Comparator type = new Comparator<String>() { @Override public int compare(String arg0, String arg1) { String ext = null; String ext2 = null; int ret; try { ext = arg0.substring(arg0.lastIndexOf(".") + 1, arg0.length()) .toLowerCase(); ext2 = arg1.substring(arg1.lastIndexOf(".") + 1, arg1.length()) .toLowerCase(); } catch (IndexOutOfBoundsException e) { return 0; } ret = ext.compareTo(ext2); if (ret == 0) return arg0.toLowerCase().compareTo(arg1.toLowerCase()); return ret; } }; public static boolean checkBusyBox(){ if (RootTools.isBusyboxAvailable()) { // busybox exists, do something return true; } else { // do something else return false; } } public static void offerBusyBox(Activity activity){ RootTools.offerBusyBox(activity); } public static boolean checkSU(){ if (RootTools.isRootAvailable()) { // su exists, do something return true; } else { // do something else return false; } } public static boolean checkSUAccess(){ if (RootTools.isAccessGiven()) { // your app has been granted root access return true; } else { // no access return false; } } public static void offerSU(Activity activity){ RootTools.offerSuperUser(activity); } private void runCommand(int id, String com) throws IOException, InterruptedException, RootDeniedException, TimeoutException { CommandCapture command = new CommandCapture(id, com); RootTools.getShell(true).add(command).wait(); //RootTools.getShell(true).add(command).waitForFinish(); } }