Android Open Source - android_device Android Device






From Project

Back to project page android_device.

License

The source code is released under:

[Apache License](http://www.apache.org/licenses/): Version 2.0, January 2004 =============== ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION ## ### 1. Definitions. ### "License" sha...

If you think the Android project android_device 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

/*
 * =================================================================================================
 *                Copyright (C) 2013 - 2014 Martin Albedinsky [Wolf-ITechnologies]
 * =================================================================================================
 *         Licensed under the Apache License, Version 2.0 or later (further "License" only).
 * -------------------------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy of this License
 * you may obtain at//ww w.j  a v  a 2 s.co  m
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * You can redistribute, modify or publish any part of the code written within this file but as it
 * is described in the License, the software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 * =================================================================================================
 */
package com.wit.android.device;

import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.Log;

/**
 * <h3>Class Overview</h3>
 * todo: description
 *
 * @author Martin Albedinsky
 */
public class AndroidDevice {

  /**
   * Interface ===================================================================================
   */

  /**
   * Constants ===================================================================================
   */

  /**
   * Log TAG.
   */
  private static final String TAG = "AndroidDevice";

  /**
   * Flag indicating whether the debug output trough log-cat is enabled or not.
   */
  private static final boolean DEBUG_ENABLED = DeviceConfig.LIBRARY_DEBUG_LOG_ENABLED;

  /**
   * Flag indicating whether the output trough log-cat is enabled or not.
   */
  // private static final boolean LOG_ENABLED = DeviceConfig.LIBRARY_LOG_ENABLED;

  /**
   * The version of Android by which is this device powered.
   * <p>
   * See {@link Build.VERSION#RELEASE} for additional info.
   */
  public static final String ANDROID_VERSION = Build.VERSION.RELEASE;

  /**
   * The name of version of Android by which is this device powered.
   * <p>
   * See {@link android.os.Build.VERSION_CODES} for additional info.
   */
  public static final String ANDROID_VERSION_NAME;

  // Set up version name depends on the current SDK version.
  static {
    switch (Build.VERSION.SDK_INT) {
      case Build.VERSION_CODES.BASE:
        ANDROID_VERSION_NAME = "Base";
        break;
      case Build.VERSION_CODES.BASE_1_1:
        ANDROID_VERSION_NAME = "Base 1.1";
        break;
      case Build.VERSION_CODES.CUPCAKE:
        ANDROID_VERSION_NAME = "Cupcake";
        break;
      case Build.VERSION_CODES.DONUT:
        ANDROID_VERSION_NAME = "Donut";
        break;
      case Build.VERSION_CODES.ECLAIR:
        ANDROID_VERSION_NAME = "Eclair";
        break;
      case Build.VERSION_CODES.ECLAIR_0_1:
        ANDROID_VERSION_NAME = "Eclair 2.0.1";
        break;
      case Build.VERSION_CODES.ECLAIR_MR1:
        ANDROID_VERSION_NAME = "Eclair MR1";
        break;
      case Build.VERSION_CODES.FROYO:
        ANDROID_VERSION_NAME = "Froyo";
        break;
      case Build.VERSION_CODES.GINGERBREAD:
        ANDROID_VERSION_NAME = "Gingerbread";
        break;
      case Build.VERSION_CODES.GINGERBREAD_MR1:
        ANDROID_VERSION_NAME = "Gingerbread MR1";
        break;
      case Build.VERSION_CODES.HONEYCOMB:
        ANDROID_VERSION_NAME = "Honeycomb";
        break;
      case Build.VERSION_CODES.HONEYCOMB_MR1:
        ANDROID_VERSION_NAME = "Honeycomb MR1";
        break;
      case Build.VERSION_CODES.HONEYCOMB_MR2:
        ANDROID_VERSION_NAME = "Honeycomb MR2";
        break;
      case Build.VERSION_CODES.ICE_CREAM_SANDWICH:
        ANDROID_VERSION_NAME = "Ice Cream Sandwich";
        break;
      // case Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1:
      case 15:
        ANDROID_VERSION_NAME = "Ice Cream Sandwich MR1";
        break;
      // case Build.VERSION_CODES.JELLY_BEAN:
      case 16:
        ANDROID_VERSION_NAME = "Jelly Bean";
        break;
      // case Build.VERSION_CODES.JELLY_BEAN_MR1:
      case 17:
        ANDROID_VERSION_NAME = "Jelly Bean MR1";
        break;
      // case Build.VERSION_CODES.JELLY_BEAN_MR2:
      case 18:
        ANDROID_VERSION_NAME = "Jelly Bean MR2";
        break;
      // case Build.VERSION_CODES.KITKAT:
      case 19:
        ANDROID_VERSION_NAME = "KitKat";
        break;
      // case Build.VERSION_CODES.KITKAT_WATCH:
      case 20:
        ANDROID_VERSION_NAME = "KitKat Watch";
        break;
      default:
        ANDROID_VERSION_NAME = "UNKNOWN";
    }
  }

  /**
   * The SDK version of Android by which is this device powered.
   * <p>
   * See {@link Build.VERSION#SDK_INT} for additional info.
   */
  public static final int ANDROID_SDK_VERSION = Build.VERSION.SDK_INT;

  /**
   * The identifier for <b>UNKNOWN</b> type of Android powered device.
   * <p>
   * Constant value: <b>0</b>
   */
  public static final int UNKNOWN = 0x00;

  /**
   * The identifier for <b>PHONE</b> type of Android powered device.
   * <p>
   * Constant value: <b>1</b>
   */
  public static final int PHONE = 0x01;

  /**
   * The identifier for <b>PHABLET</b> type of Android powered device.
   * <p>
   * Constant value: <b>2</b>
   */
  public static final int PHABLET = 0x02;

  /**
   * The identifier for <b>TABLET</b> type of Android powered device.
   * <p>
   * Constant value: <b>3</b>
   */
  public static final int TABLET = 0x03;

  /**
   * The identifier for <b>TELEVISION</b> type of Android powered device.
   * <p>
   * Constant value: <b>4</b>
   */
  public static final int TELEVISION = 0x04;

  /**
   * The identifier for <b>WEARABLE</b> type of Android powered device.
   * <p>
   * Constant value: <b>5</b>
   */
  public static final int WEARABLE = 0x05;

  /**
   * Amount of points for TABLET device when performing check {@link #isTablet()} for default screen
   * orientation.
   */
  private static final int TABLET_MATCH_SCREEN_DEFAULT_ORIENTATION_POINTS = 60;

  /**
   * Amount of points for TABLET device when performing check {@link #isTablet()} for screen type.
   */
  private static final int TABLET_MATCH_SCREEN_TYPE_POINTS = 10;

  /**
   * Amount of points for TABLET device when performing check {@link #isTablet()} for screen density.
   */
  private static final int TABLET_MATCH_SCREEN_DENSITY_POINTS = 10;

  /**
   * Amount of points for TABLET device when performing check {@link #isTablet()} for screen diagonal
   * distance.
   */
  private static final int TABLET_MATCH_SCREEN_DIAGONAL_POINTS = 10;

  /**
   * Minimum percentage value to match tablet device.
   */
  private static final int MINIMUM_TABLET_PERCENTAGE_MATCH = 85;

  /**
   * Minimum screen diagonal distance to match tablet device.
   */
  private static final float MINIMUM_TABLET_DIAGONAL_DISTANCE = 6.5f;

  /**
   * Static members ==============================================================================
   */

  /**
   * Lock object for synchronized operations.
   */
  private static final Object LOCK = new Object();

  /**
   * Singleton instance of this implementation of AndroidDevice.
   */
  private static volatile AndroidDevice sInstance;

  /**
   * Members =====================================================================================
   */

  /**
   * Application context. Not an activity context.
   */
  private final Context mContext;

  /**
   * Wrapper of an Android device's screen.
   */
  private volatile Screen mScreen;

  /**
   * Wrapper of an Android device's storage.
   */
  private volatile Storage mStorage;

  /**
   * Wrapper of an Android device's network connection.
   */
  private volatile Connection mConnection;

  /**
   * Wrapper of an Android device's battery.
   */
  private volatile Battery mBattery;

  /**
   * todo:
   */
  private String mUUID = "";

  /**
   * Constructors ================================================================================
   */

  /**
   * Creates a new instance of BaseAndroidDevice. The given <var>context</var> will be used to
   * obtain application context.
   *
   * @param context Application context or activity context.
   */
  AndroidDevice(Context context) {
    this.mContext = context.getApplicationContext();
  }

  /**
   * Methods =====================================================================================
   */

  /**
   * Public --------------------------------------------------------------------------------------
   */

  /**
   * Returns flag indicating whether this implementation of BaseAndroidDevice is already initialized
   * or not.
   *
   * @return {@code True} if the AndroidDevice instance is initialized, {@code false} otherwise.
   */
  public static boolean isInitialized() {
    synchronized (LOCK) {
      return sInstance != null;
    }
  }

  /**
   * Returns the instance of AndroidDevice.
   * <p>
   * <b>Note</b>, that the given <var>context</var> can be {@code null} in case when the
   * instance of AndroidDevice is already initialized ({@link #isInitialized()}).
   *
   * @param context Current activity context or application context.
   * @return AndroidDevice instance.
   * @throws IllegalArgumentException In case when the given context is {@code null} and
   *                                  AndroidDevice instance isn't initialized yet.
   */
  @NonNull
  public static AndroidDevice getInstance(Context context) {
    // Initialize instance if it isn't initialized yet.
    if (!isInitialized()) {
      if (context == null) {
        throw new NullPointerException("Can not to create AndroidDevice with invalid context.");
      }
      synchronized (LOCK) {
        if (sInstance == null) {
          sInstance = new AndroidDevice(context);
        }
      }
    }
    return sInstance;
  }

  /**
   * Destroy this instance of AndroidDevice. All currently initialized wrappers will be also
   * destroyed.
   * <p>
   * A new instance of AndroidDevice can be again initialized by calling {@link #getInstance(android.content.Context)}.
   */
  public void destroy() {
    destroyScreen();
    destroyConnection();
    destroyBattery();
    destroyStorage();
    sInstance = null;
  }

  /**
   * Destroy current <b>screen</b> wrapper if it is initialized.
   */
  public void destroyScreen() {
    this.mScreen = null;
  }

  /**
   * Destroy current <b>connection</b> wrapper if it is initialized.
   * <p>
   * <b>Note</b>, that this will just free the current reference, if there is the connection receiver
   * registered it should be unregistered when there is no need to listen for changes in connection
   * status.
   */
  public void destroyConnection() {
    this.mConnection = null;
  }

  /**
   * Destroy current <b>battery</b> wrapper if it is initialized.
   * <p>
   * <b>Note</b>, that this will just free the current reference, if there are some battery receivers
   * registered they should be unregistered when there is no need to listen for changes in battery
   * status.
   */
  public void destroyBattery() {
    this.mBattery = null;
  }

  /**
   * Destroy current <b>storage</b> wrapper if it is initialized.
   */
  public void destroyStorage() {
    this.mStorage = null;
  }

  /**
   * Checks whether this Android device match tablet specifications or not. <b>Note</b>, that this
   * information is only accurate and shouldn't be used for core logic of your application.
   * <h3>Specifications to check:</h3>
   * <ul>
   * <li>{@link com.wit.android.device.Screen.ScreenOrientation}</li>
   * <li>{@link com.wit.android.device.Screen.ScreenRotation}</li>
   * <li>{@link com.wit.android.device.Screen.ScreenType}</li>
   * <li>{@link com.wit.android.device.Screen.ScreenDensity}</li>
   * <li><b>Screen diagonal distance</b></li>
   * </ul>
   *
   * @return {@code True} if this Android device matches tablet specifications, {@code false}
   * otherwise.
   */
  public boolean isTablet() {
    final Screen screen = getScreen();
    int percentageMatch = 0;

    // Check the default screen orientation.
    // LANDSCAPE => tablet, PORTRAIT => phone
    switch (screen.getDefaultOrientation()) {
      case LANDSCAPE:
        percentageMatch += TABLET_MATCH_SCREEN_DEFAULT_ORIENTATION_POINTS;
    }

    // Check screen type. Most of the Android tablet devices have display type LARGE or X-LARGE.
    switch (screen.getType()) {
      case LARGE:
      case XLARGE:
        percentageMatch += TABLET_MATCH_SCREEN_TYPE_POINTS;
    }

    // Check screen diagonal distance.
    if (screen.getDiagonalDistance(true) >= MINIMUM_TABLET_DIAGONAL_DISTANCE) {
      percentageMatch += TABLET_MATCH_SCREEN_DIAGONAL_POINTS;
    }

    // Check screen density.
    // Most of Android tablet devices have MDPI or HDPI screen density (in some cases XHDPI too).
    switch (screen.getDensity()) {
      case MDPI:
      case HDPI:
      case XHDPI:
        percentageMatch += TABLET_MATCH_SCREEN_DENSITY_POINTS;
    }

    if (DEBUG_ENABLED) {
      Log.v(TAG, "This device match tablet specifications at " + percentageMatch + "%" + ".");
    }
    return percentageMatch >= MINIMUM_TABLET_PERCENTAGE_MATCH;
  }

  /**
   *
   * @param flags
   * @return
   */
  /*public String generateUUID(int flags) {
    String uuid = "";
    return mUUID = uuid;
  }*/

  /**
   * Getters + Setters ---------------------------------------------------------------------------
   */

  /**
   * Returns a screen wrapper implementation for this Android device.
   *
   * @return {@link com.wit.android.device.Screen} wrapper with this Android device's screen
   * data like <i>current orientation, diagonal distance, density, ...</i>.
   */
  @NonNull
  public Screen getScreen() {
    synchronized (LOCK) {
      if (mScreen == null) {
        this.mScreen = new ScreenImpl(mContext);
      }
    }
    return mScreen;
  }

  /**
   * Returns a storage wrapper implementation for this Android device.
   *
   * @return {@link com.wit.android.device.Storage} wrapper with data about this Android device's
   * storage.
   */
  @NonNull
  public Storage getStorage() {
    synchronized (LOCK) {
      if (mStorage == null) {
        this.mStorage = new StorageImpl(mContext);
      }
    }
    return mStorage;
  }

  /**
   * Returns a connection wrapper implementation for this Android device.
   *
   * @return {@link com.wit.android.device.Connection} wrapper with this Android device's connection
   * data like <i>status, current network, ...</i>.
   */
  @NonNull
  public Connection getConnection() {
    synchronized (LOCK) {
      if (mConnection == null) {
        this.mConnection = new ConnectionImpl(mContext);
      }
    }
    return mConnection;
  }

  /**
   * Returns a battery wrapper implementation for this Android device.
   *
   * @return {@link com.wit.android.device.Battery} wrapper with this Android device's battery
   * data like <i>strength, status, plugged state, ...</i>.
   */
  @NonNull
  public Battery getBattery() {
    synchronized (LOCK) {
      if (mBattery == null) {
        this.mBattery = new BatteryImpl(mContext);
      }
    }
    return mBattery;
  }

  /**
   * <b>For now this will return {@link #PHONE} or {@link #TABLET} as this logic is not implemented
   * yet.</b>
   * <p>
   * Returns a type of this Android device. <b>Note</b>, that this information is only accurate
   * and shouldn't be used to core logic of your application.
   *
   * @return One of {@link #PHONE}, {@link #PHABLET}, {@link #TABLET}, {@link #TELEVISION}, {@link #WEARABLE}
   * or {@link #UNKNOWN} if type of this Android device can not be resolved.
   */
  public int getType() {
    int type = isTablet() ? TABLET : PHONE;
    // todo:
    return type;
  }

  /**
   *
   * @return
   */
  /*public String getUUID() {
    if (TextUtils.isEmpty(mUUID)) {
      // todo: look up for uuid stored at file storage
    }
    return mUUID;
  }*/

  /**
   * Returns the current application {@link Context}. Note, that this isn't the context with which
   * was this instance of Android device initialized.
   *
   * @return Application context.
   */
  @NonNull
  public Context getApplicationContext() {
    return mContext;
  }

  /**
   * Protected -----------------------------------------------------------------------------------
   */

  /**
   * Private -------------------------------------------------------------------------------------
   */

  /**
   * Inner classes ===============================================================================
   */
}




Java Source Code List

com.wit.android.device.AndroidDevice.java
com.wit.android.device.BatteryImpl.java
com.wit.android.device.Battery.java
com.wit.android.device.ConnectionImpl.java
com.wit.android.device.Connection.java
com.wit.android.device.DeviceConfig.java
com.wit.android.device.ScreenImpl.java
com.wit.android.device.Screen.java
com.wit.android.device.StorageAction.java
com.wit.android.device.StorageImpl.java
com.wit.android.device.Storage.java
com.wit.android.device.examples.HomeActivity.java
com.wit.android.device.examples.adapter.BatteryInfoAdapter.java
com.wit.android.device.examples.adapter.ConnectionInfoAdapter.java
com.wit.android.device.examples.adapter.FilesAdapter.java
com.wit.android.device.examples.adapter.OrientationsAdapter.java
com.wit.android.device.examples.adapter.SimpleInfoAdapter.java
com.wit.android.device.examples.adapter.StorageAdapter.java
com.wit.android.device.examples.dialog.NewFileDialog.java
com.wit.android.device.examples.fragment.BaseDeviceFragment.java
com.wit.android.device.examples.fragment.BatteryInfoFragment.java
com.wit.android.device.examples.fragment.ConnectionInfoFragment.java
com.wit.android.device.examples.fragment.DeviceInfoFragment.java
com.wit.android.device.examples.fragment.FragmentsFactory.java
com.wit.android.device.examples.fragment.ScreenInfoFragment.java
com.wit.android.device.examples.fragment.ScreenInterfaceFragment.java
com.wit.android.device.examples.fragment.StorageFilesFragment.java
com.wit.android.device.examples.fragment.StorageInfoFragment.java
com.wit.android.device.examples.fragment.StorageInterfaceFragment.java
com.wit.android.device.examples.model.BatteryInfo.java
com.wit.android.device.examples.model.ConnectionInfo.java
com.wit.android.device.examples.model.SimpleInfo.java
com.wit.android.device.examples.model.StorageItem.java
com.wit.android.device.examples.module.StorageAssistant.java
com.wit.android.device.receiver.BatteryHealthReceiver.java
com.wit.android.device.receiver.BatteryPluggedStateReceiver.java
com.wit.android.device.receiver.BatteryStatusReceiver.java
com.wit.android.device.receiver.BroadcastProcessor.java
com.wit.android.device.receiver.ConnectionStateReceiver.java
com.wit.android.device.util.ConnectionUtils.java
com.wit.android.device.util.ScreenUtils.java
com.wit.android.device.util.StorageEditor.java
com.wit.android.device.util.StorageUtils.java