Android Open Source - campus Utils






From Project

Back to project page campus.

License

The source code is released under:

GNU General Public License

If you think the Android project campus 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 lecho.app.campus.utils;
//from w  ww  .ja v  a 2  s.  co  m
import java.io.File;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.util.Pair;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;

public final class Utils {
  private static final String TAG = "Utils";

  public static boolean launchGMaps(Context context, double latitude, double longitude) {
    final String GMAPS = "geo:";
    final String ZOOM = "?z=17";
    StringBuilder sb = new StringBuilder().append(GMAPS).append(Double.toString(latitude)).append(",")
        .append(Double.toString(longitude)).append(ZOOM);
    try {
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sb.toString()));
      ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent,
          PackageManager.MATCH_DEFAULT_ONLY);
      if (null == resolveInfo) {
        Log.e(TAG, "No activity to handle geo intent");
        return false;
      }
      context.startActivity(intent);
      return true;
    } catch (Exception e) {
      Log.e(TAG, "Could not start google navigation", e);
      return false;
    }
  }

  @SuppressLint("DefaultLocale")
  public static boolean launchWebBrowser(Context context, String url) {
    try {
      url = url.toLowerCase();
      if (!url.startsWith("http://") || !url.startsWith("https://")) {
        url = "http://" + url;
      }

      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setData(Uri.parse(url));
      ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent,
          PackageManager.MATCH_DEFAULT_ONLY);
      if (null == resolveInfo) {
        Log.e(TAG, "No activity to handle web intent");
        return false;
      }
      context.startActivity(intent);
      Log.i(TAG, "Launching browser with url: " + url);
      return true;
    } catch (Exception e) {
      Log.e(TAG, "Could not start web browser", e);
      return false;
    }
  }

  @SuppressWarnings("deprecation")
  @SuppressLint("NewApi")
  public static Pair<Integer, Integer> getScreendDimension(Context ctx) {
    WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
      Pair<Integer, Integer> dimens = new Pair<Integer, Integer>(display.getWidth(), display.getHeight());
      return dimens;
    } else {
      Point size = new Point();
      display.getSize(size);
      Pair<Integer, Integer> dimens = new Pair<Integer, Integer>(size.x, size.y);
      return dimens;
    }

  }

  public static int dp2px(Context context, int dp) {
    // Get the screen's density scale
    final float scale = context.getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale
    return (int) (dp * scale + 0.5f);

  }

  /**
   * Returns current ActionBar height for given activity.
   * 
   * @param activity
   * @return
   */
  public static int getActionBarHeight(Activity activity) {
    TypedValue tv = new TypedValue();
    int actionBarHeight = 0;
    if (activity.getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true)) {
      actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources()
          .getDisplayMetrics());
    }
    return actionBarHeight;
  }

  public static String getPlaceImagesDir(String symbol) {
    return new StringBuilder(Config.APP_ASSETS_DIR).append(File.separator).append(symbol).toString();
  }

  public static boolean isOnline(Context context) {
    // TODO check timeout
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
      return true;
    }
    return false;
  }

  public static Pair<String, Integer> getAppVersionAndBuild(Context context) {
    try {
      PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
      return new Pair<String, Integer>(pInfo.versionName, pInfo.versionCode);
    } catch (Exception e) {
      Log.e(TAG, "Could not get version number");
      return new Pair<String, Integer>("", 0);
    }
  }
}




Java Source Code List

lecho.app.campus.activity.AboutAppActivity.java
lecho.app.campus.activity.CampusMapActivity.java
lecho.app.campus.activity.GalleryActivity.java
lecho.app.campus.activity.LegalInfoActivity.java
lecho.app.campus.activity.PlaceDetailsActivity.java
lecho.app.campus.adapter.GalleryPagesFragmentAdapter.java
lecho.app.campus.adapter.MarkerInfoWindowAdapter.java
lecho.app.campus.adapter.NavigationDrawerAdapter.java
lecho.app.campus.adapter.PlaceDetailsFragmentAdapter.java
lecho.app.campus.adapter.SearchResultViewAdapter.java
lecho.app.campus.adapter.SearchSuggestionAdapter.java
lecho.app.campus.dao.CategoryDao.java
lecho.app.campus.dao.CategoryDao.java
lecho.app.campus.dao.Category.java
lecho.app.campus.dao.Category.java
lecho.app.campus.dao.DaoMaster.java
lecho.app.campus.dao.DaoMaster.java
lecho.app.campus.dao.DaoSession.java
lecho.app.campus.dao.DaoSession.java
lecho.app.campus.dao.FacultyDao.java
lecho.app.campus.dao.FacultyDao.java
lecho.app.campus.dao.Faculty.java
lecho.app.campus.dao.Faculty.java
lecho.app.campus.dao.Main.java
lecho.app.campus.dao.PlaceCategoryDao.java
lecho.app.campus.dao.PlaceCategoryDao.java
lecho.app.campus.dao.PlaceCategory.java
lecho.app.campus.dao.PlaceCategory.java
lecho.app.campus.dao.PlaceDao.java
lecho.app.campus.dao.PlaceDao.java
lecho.app.campus.dao.PlaceFacultyDao.java
lecho.app.campus.dao.PlaceFacultyDao.java
lecho.app.campus.dao.PlaceFaculty.java
lecho.app.campus.dao.PlaceFaculty.java
lecho.app.campus.dao.PlaceUnitDao.java
lecho.app.campus.dao.PlaceUnitDao.java
lecho.app.campus.dao.PlaceUnit.java
lecho.app.campus.dao.PlaceUnit.java
lecho.app.campus.dao.Place.java
lecho.app.campus.dao.Place.java
lecho.app.campus.dao.UnitDao.java
lecho.app.campus.dao.UnitDao.java
lecho.app.campus.dao.Unit.java
lecho.app.campus.dao.Unit.java
lecho.app.campus.fragment.GalleryPageFragment.java
lecho.app.campus.fragment.PlaceDetailsFragment.java
lecho.app.campus.fragment.dialog.NoInternetConnectionDialogFragment.java
lecho.app.campus.fragment.dialog.PlayServicesErrorDialogFragment.java
lecho.app.campus.loader.PlaceDetailsLoader.java
lecho.app.campus.loader.PlacesLoader.java
lecho.app.campus.provider.SearchSuggestionProvider.java
lecho.app.campus.service.PopulateDBIntentService.java
lecho.app.campus.utils.ABSMenuItemConverter.java
lecho.app.campus.utils.BitmapAsyncTask.java
lecho.app.campus.utils.Config.java
lecho.app.campus.utils.DataParser.java
lecho.app.campus.utils.DatabaseHelper.java
lecho.app.campus.utils.ImagesDirAsyncTask.java
lecho.app.campus.utils.NavigationDrawerItem.java
lecho.app.campus.utils.PlaceDetails.java
lecho.app.campus.utils.PlacesList.java
lecho.app.campus.utils.UnitsGroup.java
lecho.app.campus.utils.Utils.java
lecho.app.campus.view.CheckableDrawerItem.java
lecho.app.campus.view.UnitsGroupLayout.java
lecho.app.campus.view.ZoomImageView.java
lecho.app.campus.view.ZoomViewPager.java