Android Open Source - sdl_tester_android Android Utils






From Project

Back to project page sdl_tester_android.

License

The source code is released under:

Copyright (c) 2014, Ford Motor Company All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

If you think the Android project sdl_tester_android 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.livio.sdl.utils;
//from   www  . jav a 2  s  . co  m
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.widget.ArrayAdapter;

/**
 * Contains static methods that will help with typical Android tasks.  For example,
 * there are methods to determine internet connectivity, creating adapters for spinners
 * and lists, etc.
 *
 * @author Mike Burke
 *
 */
public final class AndroidUtils {

  private AndroidUtils(){} // don't allow instantiation of static classes
  
  /**
   * Determines if the network is currently available or not.
   * 
   * @param context The context with which to access the system connectivity service
   * @return True if the network is available, false if not
   */
  public static boolean isNetworkAvailable(ContextWrapper context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network = cm.getActiveNetworkInfo();
    return ( (network != null) && (network.isConnected()) );
  }
  
  /**
   * Creates a standard Android spinner adapter.  Input items can be of any type.
   * 
   * @param context Context with which to create the adapter
   * @param items List of items to populate the adapter with
   * @return The created adapter
   */
  public static <E> ArrayAdapter<E> createSpinnerAdapter(Context context, List<E> items){
    ArrayAdapter<E> adapter = createAdapter(context, android.R.layout.select_dialog_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    return adapter;
  }
  
  /**
   * Creates a standard Android spinner adapter.  Input items can be of any type.
   * 
   * @param context Context with which to create the adapter
   * @param items Array of items to populate the adapter with
   * @return The created adapter
   */
  public static <E> ArrayAdapter<E> createSpinnerAdapter(Context context, E[] items){
    return createSpinnerAdapter(context, Arrays.asList(items));
  }

  /**
   * Creates a standard Android ListView adapter.  Input items can be of any type.
   * 
   * @param context Context with which to create the adapter
   * @param items List of items to populate the adapter with
   * @return The created adapter
   */
  public static <E> ArrayAdapter<E> createListViewAdapter(Context context, List<E> items){
    return createAdapter(context, android.R.layout.simple_list_item_1, items);
  }

  /**
   * Creates a standard Android ListView adapter.  Input items can be of any type.
   * 
   * @param context Context with which to create the adapter
   * @param items Array of items to populate the adapter with
   * @return The created adapter
   */
  public static <E> ArrayAdapter<E> createListViewAdapter(Context context, E[] items){
    return createListViewAdapter(context, Arrays.asList(items));
  }

  /**
   * Creates a standard Android ListView multiple-choice adapter.  Input items can be of any type.
   * 
   * @param context Context with which to create the adapter
   * @param items List of items to populate the adapter with
   * @return The created adapter
   */
  public static <E> ArrayAdapter<E> createMultipleListViewAdapter(Context context, List<E> items){
    return createAdapter(context, android.R.layout.simple_list_item_multiple_choice, items);
  }

  /**
   * Creates a standard Android ListView multiple-choice adapter.  Input items can be of any type.
   * 
   * @param context Context with which to create the adapter
   * @param items Array of items to populate the adapter with
   * @return The created adapter
   */
  public static <E> ArrayAdapter<E> createMultipleListViewAdapter(Context context, E[] items){
    return createMultipleListViewAdapter(context, Arrays.asList(items));
  }

  /**
   * Creates a standard Android adapter.  Input items can be of any type.
   * 
   * @param context Context with which to create the adapter
   * @param layoutId Android resource id to be used for a list row
   * @param items List of items to populate the adapter with
   * @return The created adapter
   */
  public static <E> ArrayAdapter<E> createAdapter(Context context, int layoutId, List<E> items){
    return new ArrayAdapter<E>(context, layoutId, items);
  }
  
  /**
   * Converts and Android bitmap file to an array of raw bytes that are ready to be sent over bluetooth,
   * wifi, usb, etc.
   * 
   * @param bitmap The bitmap to translate
   * @param format The format of the bitmap
   * @return The raw byte representation of the bitmap
   */
  public static byte[] bitmapToRawBytes(Bitmap bitmap, CompressFormat format){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(format, 100, baos);
    byte[] result = baos.toByteArray();
    return result;
  }
  
  /**
   * Enables or disables wifi.  Requires CHANGE_WIFI_STATE permission.
   * 
   * @param context A context with which to access wifi system service
   * @param enable True if wifi should be enabled, false if it should be disabled
   */
  public static void enableWifi(ContextWrapper context, boolean enable){
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    wifiManager.setWifiEnabled(enable);
  }
  
  /**
   * Determines if the device's wifi is currently enabled or not.
   * 
   * @param context A context with which to access wifi system service
   * @return True if wifi is enabled or enabling, false otherwise
   */
  public static boolean wifiIsEnabled(ContextWrapper context){
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int wifiState = wifiManager.getWifiState();
    return (wifiState == WifiManager.WIFI_STATE_ENABLED || wifiState == WifiManager.WIFI_STATE_ENABLING);
  }
  
}




Java Source Code List

com.livio.sdl.IdGenerator.java
com.livio.sdl.IpAddress.java
com.livio.sdl.SdlConstants.java
com.livio.sdl.SdlImageItem.java
com.livio.sdl.SdlLogMessage.java
com.livio.sdl.SdlRequestFactory.java
com.livio.sdl.SdlResponseFactory.java
com.livio.sdl.SdlResponseTracker.java
com.livio.sdl.SdlService.java
com.livio.sdl.adapters.SdlImageAdapter.java
com.livio.sdl.adapters.SdlMessageAdapter.java
com.livio.sdl.dialogs.BaseAlertDialog.java
com.livio.sdl.dialogs.BaseImageListDialog.java
com.livio.sdl.dialogs.BaseMultipleListViewDialog.java
com.livio.sdl.dialogs.BaseOkCancelDialog.java
com.livio.sdl.dialogs.BaseSingleListViewDialog.java
com.livio.sdl.dialogs.ImageListDialog.java
com.livio.sdl.dialogs.IndeterminateProgressDialog.java
com.livio.sdl.dialogs.JsonFlipperDialog.java
com.livio.sdl.dialogs.ListViewDialog.java
com.livio.sdl.dialogs.MultipleListViewDialog.java
com.livio.sdl.dialogs.SingleJsonDialog.java
com.livio.sdl.dialogs.TextViewAlertDialog.java
com.livio.sdl.dialogs.TextViewOkCancelDialog.java
com.livio.sdl.enums.EnumClickListener.java
com.livio.sdl.enums.EnumComparator.java
com.livio.sdl.enums.SdlButton.java
com.livio.sdl.enums.SdlCommand.java
com.livio.sdl.enums.SdlImageType.java
com.livio.sdl.enums.SdlInteractionMode.java
com.livio.sdl.enums.SdlLanguage.java
com.livio.sdl.enums.SdlSpeechCapability.java
com.livio.sdl.enums.SdlSystemAction.java
com.livio.sdl.enums.SdlTextAlignment.java
com.livio.sdl.enums.SdlTransportType.java
com.livio.sdl.enums.SdlUpdateMode.java
com.livio.sdl.enums.SdlVehicleData.java
com.livio.sdl.menu.CommandButton.java
com.livio.sdl.menu.MenuItem.java
com.livio.sdl.menu.MenuManager.java
com.livio.sdl.menu.SubmenuButton.java
com.livio.sdl.utils.AndroidUtils.java
com.livio.sdl.utils.ApplicationPreferences.java
com.livio.sdl.utils.Counter.java
com.livio.sdl.utils.DownCounter.java
com.livio.sdl.utils.MathUtils.java
com.livio.sdl.utils.SdlUtils.java
com.livio.sdl.utils.StringUtils.java
com.livio.sdl.utils.Timeout.java
com.livio.sdl.utils.UpCounter.java
com.livio.sdl.utils.WifiUtils.java
com.livio.sdl.viewhelpers.MinMaxInputFilter.java
com.livio.sdl.viewhelpers.SeekBarCalculator.java
com.livio.sdltester.HelpActivity.java
com.livio.sdltester.LivioSdlTesterPreferences.java
com.livio.sdltester.MainActivity.java
com.livio.sdltester.SdlTesterImageResource.java
com.livio.sdltester.dialogs.AddCommandDialog.java
com.livio.sdltester.dialogs.AddSubMenuDialog.java
com.livio.sdltester.dialogs.ButtonSubscriptionDialog.java
com.livio.sdltester.dialogs.ButtonUnsubscriptionDialog.java
com.livio.sdltester.dialogs.ChangeRegistrationDialog.java
com.livio.sdltester.dialogs.ChoiceItemDialog.java
com.livio.sdltester.dialogs.CreateInteractionChoiceSetDialog.java
com.livio.sdltester.dialogs.DeleteCommandDialog.java
com.livio.sdltester.dialogs.DeleteFileDialog.java
com.livio.sdltester.dialogs.DeleteInteractionDialog.java
com.livio.sdltester.dialogs.DeleteSubmenuDialog.java
com.livio.sdltester.dialogs.GetDtcsDialog.java
com.livio.sdltester.dialogs.PerformInteractionDialog.java
com.livio.sdltester.dialogs.PutFileDialog.java
com.livio.sdltester.dialogs.ReadDidsDialog.java
com.livio.sdltester.dialogs.ScrollableMessageDialog.java
com.livio.sdltester.dialogs.SdlAlertDialog.java
com.livio.sdltester.dialogs.SdlConnectionDialog.java
com.livio.sdltester.dialogs.SetAppIconDialog.java
com.livio.sdltester.dialogs.SetMediaClockTimerDialog.java
com.livio.sdltester.dialogs.ShowDialog.java
com.livio.sdltester.dialogs.SliderDialog.java
com.livio.sdltester.dialogs.SoftButtonItemDialog.java
com.livio.sdltester.dialogs.SoftButtonListDialog.java
com.livio.sdltester.dialogs.SpeakDialog.java