Android Open Source - Chopping Utils






From Project

Back to project page Chopping.

License

The source code is released under:

Apache License

If you think the Android project Chopping 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.chopping.utils;
/*w w w.  ja  va  2s.  co m*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.support.annotation.StringRes;
import android.widget.Toast;

import com.chopping.application.IApp;
import com.chopping.application.LL;

/**
 * Utils for tools used in common Apps.
 *
 * @author Xinyue Zhao
 */
public final class Utils {
  /**
   * Show long time toast.
   *
   * @param context
   *     {@link android.content.Context}.
   * @param messageId
   *     {@link android.support.annotation.StringRes}. Message to show.
   */
  public static void showLongToast(Context context, @StringRes int messageId) {
    Toast.makeText(context, context.getString(messageId), Toast.LENGTH_LONG).show();
  }

  /**
   * Show short time toast.
   *
   * @param context
   *     {@link android.content.Context}.
   * @param messageId
   *     {@link android.support.annotation.StringRes}. Message to show.
   */
  public static void showShortToast(Context context, @StringRes int messageId) {
    Toast.makeText(context, context.getString(messageId), Toast.LENGTH_SHORT).show();
  }

  /**
   * Show short time toast.
   *
   * @param context
   *     {@link android.content.Context}.
   * @param message
   *     {@link java.lang.String}. Message to show.
   */
  public static void showLongToast(Context context, String message) {
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
  }

  /**
   * Show short time toast.
   *
   * @param context
   *     {@link android.content.Context}.
   * @param message
   *     {@link java.lang.String}. Message to show.
   */
  public static void showShortToast(Context context, String message) {
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
  }

  /**
   * Encode string for http query with UTF-8.
   * @param keywords
   * @return The encoded string.
   */
  public static String encode(String keywords) {
    try {
      return URLEncoder.encode(keywords, "UTF-8");
    } catch (UnsupportedEncodingException _e1) {
      return new String(keywords.trim().replace(" ", "%20").replace("&", "%26").replace(",", "%2c")
          .replace("(", "%28").replace(")", "%29").replace("!", "%21").replace("=", "%3D")
          .replace("<", "%3C").replace(">", "%3E").replace("#", "%23").replace("$", "%24")
          .replace("'", "%27").replace("*", "%2A").replace("-", "%2D").replace(".", "%2E")
          .replace("/", "%2F").replace(":", "%3A").replace(";", "%3B").replace("?", "%3F")
          .replace("@", "%40").replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D")
          .replace("_", "%5F").replace("`", "%60").replace("{", "%7B").replace("|", "%7C")
          .replace("}", "%7D"));
    }
  }

  /**
   * Encode string for http query with simple replacement.
   * @param keywords
   * @return The encoded string.
   */
  public static String replaceToEncode(String keywords) {
    return new String(keywords.trim().replace(" ", "%20").replace("&", "%26").replace(",", "%2c")
        .replace("(", "%28").replace(")", "%29").replace("!", "%21").replace("=", "%3D").replace("<", "%3C")
        .replace(">", "%3E").replace("#", "%23").replace("$", "%24").replace("'", "%27").replace("*", "%2A")
        .replace("-", "%2D").replace(".", "%2E").replace("/", "%2F").replace(":", "%3A").replace(";", "%3B")
        .replace("?", "%3F").replace("@", "%40").replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D")
        .replace("_", "%5F").replace("`", "%60").replace("{", "%7B").replace("|", "%7C").replace("}", "%7D"));
  }


  /**
   * Link to an external app that has _packageName. If the App has not been installed, then links to store.
   * <p/>
   * It will be tracked by Tracker.
   *
   * @param context
   *     A context object
   * @param app
   *     The app to open or direct to store if not be installed before.
   */
  public static void linkToExternalApp(Context context, IApp app) {
    /* Try to find the app with _packageName. */
    boolean found;
    PackageManager pm = context.getPackageManager();
    found = isAppInstalled(app.getPackageName(), pm);
    /* Launch the App or go to store. */
    if (found) {
      /* Found. Start app. */
      Intent LaunchIntent = pm.getLaunchIntentForPackage(app.getPackageName());
      context.startActivity(LaunchIntent);
    } else {
      /*To Store.*/
      openUrl(context, app.getStoreUrl());
    }
  }

  /**
   * Check whether the App with _packageName has been installed or not.
   *
   * @param packageName
   * @param pm
   *
   * @return
   */
  public static boolean isAppInstalled(String packageName, PackageManager pm) {
    boolean found;
    try {
      pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
      found = true;
    } catch (PackageManager.NameNotFoundException e) {
      found = false;
    }
    return found;
  }

  /**
   * Link to an external view.
   *
   * @param context
   * @param to
   */
  public static void openUrl(Context context, String to) {
    if (context != null) {
      Intent i = new Intent(Intent.ACTION_VIEW);
      i.setData(Uri.parse(to));
      context.startActivity(i);
    }
  }


  /**
   * Helper method to dump all data from DB from private location to public readable one.
   * <p/>
   * It is used for debug.
   * @param cxt {@link android.content.Context}.
   * @param dbName Database name.
   */
  public static void dumpSqlite(Context cxt, String dbName) {
    String path = "/data/data/"+cxt.getPackageName()+"/databases";
    LL.d("Under Path: " + path);
    File f = new File(path);
    File files[] = f.listFiles();
    LL.d("Under Path find files: " + files.length);
    for (int i=0; i < files.length; i++)   {
      LL.d("File:" + files[i].getName());
    }
    String sourceLocation = path +"/" +dbName;// Your database path
    String destLocation = dbName + ".db";
    try {
      File sd = Environment.getExternalStorageDirectory();
      if(sd.canWrite()){
        File source=new File(sourceLocation);
        File dest=new File(sd+"/"+destLocation);
        if(!dest.exists()){
          dest.createNewFile();
        }
        LL.d("dumpSqlite from: " + source.getAbsolutePath());
        if(source.exists()){
          LL.d("dumpSqlite to: " + dest.getAbsolutePath());
          InputStream src=new FileInputStream(source);
          OutputStream dst=new FileOutputStream(dest);
          // Copy the bits from instream to outstream
          byte[] buf = new byte[1024];
          int len;
          while ((len = src.read(buf)) > 0) {
            dst.write(buf, 0, len);
          }
          src.close();
          dst.close();
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}




Java Source Code List

com.android.internal.telephony.ITelephony.java
com.chopping.ApplicationTest.java
com.chopping.activities.BaseActivity.java
com.chopping.activities.BrightnessRefreshActivity.java
com.chopping.activities.ErrorHandlerActivity.java
com.chopping.application.BasicPrefs.java
com.chopping.application.ErrorHandler.java
com.chopping.application.IApp.java
com.chopping.application.InstalledAppReceiver.java
com.chopping.application.LL.java
com.chopping.bus.AirplaneModeOnEvent.java
com.chopping.bus.ApplicationConfigurationDownloadedEvent.java
com.chopping.bus.ApplicationConfigurationLoadingIgnoredEvent.java
com.chopping.bus.CloseDrawerEvent.java
com.chopping.bus.ExternalAppChangedEvent.java
com.chopping.bus.LinkToExternalAppEvent.java
com.chopping.bus.ReloadEvent.java
com.chopping.data.AppListItem.java
com.chopping.data.AppList.java
com.chopping.exceptions.CanNotOpenOrFindAppPropertiesException.java
com.chopping.exceptions.InvalidAppPropertiesException.java
com.chopping.exceptions.OperationFailException.java
com.chopping.fragments.AppListFragment.java
com.chopping.fragments.BaseFragment.java
com.chopping.fragments.ErrorHandlerFragment.java
com.chopping.net.GsonRequestTask.java
com.chopping.net.TaskHelper.java
com.chopping.utils.Consts.java
com.chopping.utils.DeviceUtils.java
com.chopping.utils.IncomingCallReceiver.java
com.chopping.utils.MediaUtils.java
com.chopping.utils.NetworkUtils.java
com.chopping.utils.Utils.java
com.chopping.utils.views.OneDirectionScrollView.java