Android Open Source - Calma Utils






From Project

Back to project page Calma.

License

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.

Java Source Code

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();
  }
}




Java Source Code List

com.cyanogenmod.filemanager.activities.preferences.SettingsPreferences.java
com.espian.showcaseview.OnShowcaseEventListener.java
com.espian.showcaseview.ShowcaseViewBuilder.java
com.espian.showcaseview.ShowcaseView.java
com.espian.showcaseview.ShowcaseViews.java
com.espian.showcaseview.actionbar.ActionBarViewWrapper.java
com.espian.showcaseview.actionbar.reflection.ActionBarReflector.java
com.espian.showcaseview.actionbar.reflection.AppCompatReflector.java
com.espian.showcaseview.actionbar.reflection.BaseReflector.java
com.espian.showcaseview.actionbar.reflection.SherlockReflector.java
com.espian.showcaseview.anim.AnimationUtils.java
com.espian.showcaseview.drawing.ClingDrawerImpl.java
com.espian.showcaseview.drawing.ClingDrawer.java
com.espian.showcaseview.drawing.TextDrawerImpl.java
com.espian.showcaseview.drawing.TextDrawer.java
com.espian.showcaseview.targets.ActionItemTarget.java
com.espian.showcaseview.targets.ActionViewTarget.java
com.espian.showcaseview.targets.PointTarget.java
com.espian.showcaseview.targets.Target.java
com.espian.showcaseview.targets.ViewTarget.java
com.espian.showcaseview.utils.Calculator.java
com.espian.showcaseview.utils.PointAnimator.java
com.espian.showcaseview.utils.ShowcaseAreaCalculator.java
com.scto.android.calma.CalmaApp.java
com.scto.android.calma.activities.BaseActivity.java
com.scto.android.calma.activities.CalmaActivity.java
com.scto.android.calma.activities.SettingsActivity.java
com.scto.android.calma.activities.preferences.AboutPreferenceFragment.java
com.scto.android.calma.activities.preferences.DisplayPreferenceFragment.java
com.scto.android.calma.activities.preferences.GeneralPreferenceFragment.java
com.scto.android.calma.activities.preferences.NavigationPreferenceFragment.java
com.scto.android.calma.activities.preferences.SettingsActivity.java
com.scto.android.calma.activities.preferences.TitlePreferenceFragment.java
com.scto.android.calma.adapter.NavDrawerListAdapter.java
com.scto.android.calma.fragments.CommunityFragment.java
com.scto.android.calma.fragments.FindPeopleFragment.java
com.scto.android.calma.fragments.HomeFragment.java
com.scto.android.calma.fragments.PagesFragment.java
com.scto.android.calma.fragments.PhotosFragment.java
com.scto.android.calma.fragments.WhatsHotFragment.java
com.scto.android.calma.model.NavDrawerItem.java
com.scto.android.calma.preferences.AccessMode.java
com.scto.android.calma.preferences.CalmaSettings.java
com.scto.android.calma.preferences.ConfigurationListener.java
com.scto.android.calma.preferences.ObjectIdentifier.java
com.scto.android.calma.preferences.ObjectStringIdentifier.java
com.scto.android.calma.preferences.Preferences.java
com.scto.android.calma.ui.MyPrerference.java
com.scto.android.calma.utils.CalmaUtils.java
com.scto.android.calma.utils.Compress.java
com.scto.android.calma.utils.Decompress.java
com.scto.android.calma.utils.FileUtils.java
com.scto.android.calma.utils.LinuxShell.java
com.scto.android.calma.utils.PreferenceUtils.java
com.scto.android.calma.utils.SharedPreferencesCompat.java
com.scto.android.calma.utils.SortOrder.java
com.scto.android.calma.utils.Utils.java
com.stericson.RootTools.Constants.java
com.stericson.RootTools.RootTools.java
com.stericson.RootToolsTests.NativeJavaClass.java
com.stericson.RootToolsTests.SanityCheckRootTools.java
com.stericson.RootTools.containers.Mount.java
com.stericson.RootTools.containers.Permissions.java
com.stericson.RootTools.containers.RootClass.java
com.stericson.RootTools.containers.Symlink.java
com.stericson.RootTools.exceptions.RootDeniedException.java
com.stericson.RootTools.execution.CommandCapture.java
com.stericson.RootTools.execution.Command.java
com.stericson.RootTools.execution.JavaCommandCapture.java
com.stericson.RootTools.execution.Shell.java
com.stericson.RootTools.internal.Installer.java
com.stericson.RootTools.internal.InternalVariables.java
com.stericson.RootTools.internal.Remounter.java
com.stericson.RootTools.internal.RootToolsInternalMethods.java
com.stericson.RootTools.internal.Runner.java