Android Open Source - codemap Utils






From Project

Back to project page codemap.

License

The source code is released under:

GNU General Public License

If you think the Android project codemap 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.hdweiss.codemap.util;
//ww w.ja v  a  2  s . c  om
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.hdweiss.codemap.view.project.ProjectBrowser.SynchServiceReceiver;

public class Utils {

  private static final int DEFAULT_FONTSIZE = 15;

  public static String runCommand(String command) {
    return runCommand(command, null);
  }
  
  public static String runCommand(String command, String[] environment) {
    try {
      final String[] shellCommand = { "/system/bin/sh", "-c", command };

      Process process;
      if(environment != null)
        process = Runtime.getRuntime().exec(shellCommand, environment);
      else
        process = Runtime.getRuntime().exec(shellCommand);
      
      String output = getProcessOutput(process);
      return output;

    } catch (IOException e) {
      Log.e("CodeMap", e.getStackTrace().toString());
      return "";
    }
  }

  @SuppressWarnings("deprecation")
  private static String getProcessOutput(Process process) throws IOException {
    DataInputStream dis = new DataInputStream(process.getInputStream());

    StringBuilder builder = new StringBuilder();
    String output;
    while ((output = dis.readLine()) != null) {
      builder.append(output).append("\n");
    }

    return builder.toString();
  }
  
  public static String inputStreamToString(InputStream inputStream) {
    return new java.util.Scanner(inputStream).useDelimiter("\\A").next();
  }
  
  public static String getFileFragment(String filePath, int startLine, int endLine) {
    StringBuilder result = new StringBuilder();
    
    int realStartLine = startLine - 2;
    int linesToRead = endLine - startLine + 2;
    
    try {
      BufferedReader reader = new BufferedReader(new FileReader(filePath));
      
      int numOfLines = 0;
      while((reader.readLine()) != null && numOfLines < realStartLine) {
        numOfLines++;
      }
      String line;
      int functionLines = 0;
      while((line = reader.readLine()) != null && functionLines < linesToRead) {
        result.append(line).append("\n");
        functionLines++;
      }
      
      reader.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
    
    return result.toString();
  }

  public static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
      ObjectOutput out = new ObjectOutputStream(bos);
      out.writeObject(o);
      out.close();

      byte[] buf = bos.toByteArray();

      return buf;
    } catch (IOException ioe) {
      Log.e("serializeObject", "error", ioe);

      return null;
    }
  }

  public static Object deserializeObject(byte[] b) {
    try {
      ObjectInputStream in = new ObjectInputStream(
          new ByteArrayInputStream(b));
      Object object = in.readObject();
      in.close();

      return object;
    } catch (ClassNotFoundException cnfe) {
      Log.e("deserializeObject", "class not found error", cnfe);

      return null;
    } catch (IOException ioe) {
      Log.e("deserializeObject", "io error", ioe);

      return null;
    }
  }
  
  public static void deleteRecursive(File fileOrDirectory) {
      if (fileOrDirectory.isDirectory())
          for (File child : fileOrDirectory.listFiles())
              deleteRecursive(child);

      fileOrDirectory.delete();
  }
  
  public static void setupSpinner(Spinner spinner, ArrayList<String> data,
      String selection) {    
    if(!TextUtils.isEmpty(selection) && !data.contains(selection))
      data.add(selection);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(spinner.getContext(),
        android.R.layout.simple_spinner_item, data);
    spinner.setAdapter(adapter);
    int pos = data.indexOf(selection);
    if (pos < 0) {
      pos = 0;
    }
    spinner.setSelection(pos, true);
  }
  
  
  public static void announceSyncDone(Context context, String name) {
    Intent intent = new Intent(SynchServiceReceiver.SYNC_UPDATE);
    intent.putExtra(SynchServiceReceiver.SYNC_DONE, true);
    intent.putExtra(SynchServiceReceiver.SYNC_NAME, name);
    context.sendBroadcast(intent);
  }

  public static void announceSyncStart(Context context, String name) {
    Intent intent = new Intent(SynchServiceReceiver.SYNC_UPDATE);
    intent.putExtra(SynchServiceReceiver.SYNC_START, true);
    intent.putExtra(SynchServiceReceiver.SYNC_NAME, name);
    context.sendBroadcast(intent);
  }

  public static void announceSyncUpdateProgress(Context context, String name, int progress, String status) {
    Intent intent = new Intent(SynchServiceReceiver.SYNC_UPDATE);
    intent.putExtra(SynchServiceReceiver.SYNC_PROGRESS_UPDATE, progress);
    intent.putExtra(SynchServiceReceiver.SYNC_NAME, name);
    intent.putExtra(SynchServiceReceiver.SYNC_STATUS, status);
    context.sendBroadcast(intent);
  }
  
  
  public static Runnable getTouchDelegateAction(final View parent,
      final View delegate, final int topPadding, final int bottomPadding,
      final int leftPadding, final int rightPadding) {
        return new Runnable() {
            public void run() {
                
                //Construct a new Rectangle and let the Delegate set its values
                Rect touchRect = new Rect();
                delegate.getHitRect(touchRect);
                
                //Modify the dimensions of the Rectangle
                //Padding values below zero are replaced by zeros
                touchRect.top-=Math.max(0, topPadding);
                touchRect.bottom+=Math.max(0, bottomPadding);
                touchRect.left-=Math.max(0, leftPadding);
                touchRect.right+=Math.max(0, rightPadding);
                
                //Now we are going to construct the TouchDelegate
                TouchDelegate touchDelegate = new TouchDelegate(touchRect, delegate);
                
                //And set it on the parent
                parent.setTouchDelegate(touchDelegate);
                
            }
        };
    }
  
  public static int getSourceFontsize(Context context) {
    try {
      int fontSize = Integer.parseInt(PreferenceManager
          .getDefaultSharedPreferences(context).getString(
              "sourceFontSize", "15"));

      if (fontSize > 6)
        return fontSize;
    } catch (NumberFormatException e) {
    }

    return DEFAULT_FONTSIZE;
  }
  
  public static boolean isNetworkOnline(Context context) {
    ConnectivityManager conMan = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);

    State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    
    if (mobile == NetworkInfo.State.CONNECTED) {
        return true;
    } 
    if (wifi == NetworkInfo.State.CONNECTED) {
       return true;
    }
    
    return false;

  }
}




Java Source Code List

com.hdweiss.codemap.data.CodeMapApp.java
com.hdweiss.codemap.data.CscopeEntry.java
com.hdweiss.codemap.data.CscopeWrapper.java
com.hdweiss.codemap.data.Cscope.java
com.hdweiss.codemap.data.ICodeMapItem.java
com.hdweiss.codemap.data.JGitWrapper.java
com.hdweiss.codemap.data.ProjectController.java
com.hdweiss.codemap.data.Project.java
com.hdweiss.codemap.data.SerializableItem.java
com.hdweiss.codemap.data.SerializableLink.java
com.hdweiss.codemap.util.AbsoluteLayout.java
com.hdweiss.codemap.util.CodeMapCursorPoint.java
com.hdweiss.codemap.util.CodeMapPoint.java
com.hdweiss.codemap.util.FlowLayout.java
com.hdweiss.codemap.util.ObjectSerializer.java
com.hdweiss.codemap.util.SpanUtils.java
com.hdweiss.codemap.util.SyntaxHighlighter.java
com.hdweiss.codemap.util.Utils.java
com.hdweiss.codemap.util.ZoomableAbsoluteLayout.java
com.hdweiss.codemap.util.ZoomableLinearLayout.java
com.hdweiss.codemap.view.CodeMapActivity.java
com.hdweiss.codemap.view.CodeMapTabListener.java
com.hdweiss.codemap.view.Preferences.java
com.hdweiss.codemap.view.project.ProjectAdapter.java
com.hdweiss.codemap.view.project.ProjectBrowser.java
com.hdweiss.codemap.view.project.ProjectItemView.java
com.hdweiss.codemap.view.project.ProjectWizard.java
com.hdweiss.codemap.view.workspace.CollisionManager.java
com.hdweiss.codemap.view.workspace.FindDeclarationTask.java
com.hdweiss.codemap.view.workspace.WorkspaceController.java
com.hdweiss.codemap.view.workspace.WorkspaceFragment.java
com.hdweiss.codemap.view.workspace.WorkspaceStateLoader.java
com.hdweiss.codemap.view.workspace.WorkspaceState.java
com.hdweiss.codemap.view.workspace.WorkspaceViewListeners.java
com.hdweiss.codemap.view.workspace.WorkspaceView.java
com.hdweiss.codemap.view.workspace.browser.WorkspaceBrowserAdapter.java
com.hdweiss.codemap.view.workspace.browser.WorkspaceBrowser.java
com.hdweiss.codemap.view.workspace.fragments.CodeMapAnnotation.java
com.hdweiss.codemap.view.workspace.fragments.CodeMapFunction.java
com.hdweiss.codemap.view.workspace.fragments.CodeMapImage.java
com.hdweiss.codemap.view.workspace.fragments.CodeMapItem.java
com.hdweiss.codemap.view.workspace.fragments.CodeMapLink.java
com.hdweiss.codemap.view.workspace.fragments.FunctionLinkSpan.java
com.hdweiss.codemap.view.workspace.outline.CscopeEntryAdapter.java
com.hdweiss.codemap.view.workspace.outline.OutlineAdapter.java
com.hdweiss.codemap.view.workspace.outline.OutlineBrowser.java
com.hdweiss.codemap.view.workspace.outline.OutlineItemComparator.java
com.hdweiss.codemap.view.workspace.outline.OutlineItemView.java
com.hdweiss.codemap.view.workspace.outline.OutlineItem.java