Android Open Source - android-utils Device Status Utils






From Project

Back to project page android-utils.

License

The source code is released under:

Apache License

If you think the Android project android-utils 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 2014 Zhenguo Jin (jinzhenguo1990@gmail.com)
 *//www  .j av a  2s  . c  o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.worthed.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;

/**
 * ???????? ????????????????????????????????
 * 
 * @author zhenguo
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public class DeviceStatusUtils {

  /**
   * ??????????????????????WRITE_SETTINGS?????
   * 
   * @param context
   *            ???
   * @return System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC????System.
   *         SCREEN_BRIGHTNESS_MODE_AUTOMATIC
   *         ???????System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
   */
  public static int getScreenBrightnessModeState(Context context) {
    return Settings.System.getInt(context.getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS_MODE,
        Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
  }

  /**
   * ???????????????????????WRITE_SETTINGS?????
   * 
   * @param context
   *            ???
   * @return true????false???????true
   */
  public static boolean isScreenBrightnessModeAuto(Context context) {
    return getScreenBrightnessModeState(context) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC ? true
        : false;
  }

  /**
   * ????????????????WRITE_SETTINGS?????
   * 
   * @param context
   *            ???
   * @param auto
   *            ??
   * @return ?????????
   */
  public static boolean setScreenBrightnessMode(Context context, boolean auto) {
    boolean result = true;
    if (isScreenBrightnessModeAuto(context) != auto) {
      result = Settings.System.putInt(context.getContentResolver(),
          Settings.System.SCREEN_BRIGHTNESS_MODE,
          auto ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
              : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }
    return result;
  }

  /**
   * ????????????WRITE_SETTINGS?????
   * 
   * @param context
   *            ???
   * @return ??????0-255???255
   */
  public static int getScreenBrightness(Context context) {
    return Settings.System.getInt(context.getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS, 255);
  }

  /**
   * ???????????????????????????????????????????????setWindowBrightness()?????????????
   * ???WRITE_SETTINGS?????
   * 
   * @param context
   *            ???
   * @param screenBrightness
   *            ??????0-255
   * @return ?????????
   */
  public static boolean setScreenBrightness(Context context,
      int screenBrightness) {
    int brightness = screenBrightness;
    if (screenBrightness < 1) {
      brightness = 1;
    } else if (screenBrightness > 255) {
      brightness = screenBrightness % 255;
      if (brightness == 0) {
        brightness = 255;
      }
    }
    boolean result = Settings.System.putInt(context.getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS, brightness);
    return result;
  }

  /**
   * ????Activity??????????????????????????????????
   * 
   * @param activity
   *            ?????Activity????????????
   * @param screenBrightness
   *            ??????0-255
   */
  public static void setWindowBrightness(Activity activity,
      float screenBrightness) {
    float brightness = screenBrightness;
    if (screenBrightness < 1) {
      brightness = 1;
    } else if (screenBrightness > 255) {
      brightness = screenBrightness % 255;
      if (brightness == 0) {
        brightness = 255;
      }
    }
    Window window = activity.getWindow();
    WindowManager.LayoutParams localLayoutParams = window.getAttributes();
    localLayoutParams.screenBrightness = (float) brightness / 255;
    window.setAttributes(localLayoutParams);
  }

  /**
   * ?????????????????????WRITE_SETTINGS?????
   * 
   * @param activity
   *            ?????Activity????????????
   * @param screenBrightness
   *            ??????0-255
   * @return ?????????
   */
  public static boolean setScreenBrightnessAndApply(Activity activity,
      int screenBrightness) {
    boolean result = true;
    result = setScreenBrightness(activity, screenBrightness);
    if (result) {
      setWindowBrightness(activity, screenBrightness);
    }
    return result;
  }

  /**
   * ???????????????WRITE_SETTINGS?????
   * 
   * @param context
   *            ???
   * @return ??????????????????30000
   */
  public static int getScreenDormantTime(Context context) {
    return Settings.System.getInt(context.getContentResolver(),
        Settings.System.SCREEN_OFF_TIMEOUT, 30000);
  }

  /**
   * ?????????????WRITE_SETTINGS?????
   * 
   * @param context
   *            ???
   * @return ?????????
   */
  public static boolean setScreenDormantTime(Context context, int millis) {
    return Settings.System.putInt(context.getContentResolver(),
        Settings.System.SCREEN_OFF_TIMEOUT, millis);
  }

  /**
   * ?????????????????WRITE_APN_SETTINGS?????
   * 
   * @param context
   *            ???
   * @return 1????0?????????
   */
  @SuppressWarnings("deprecation")
  public static int getAirplaneModeState(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
      return Settings.System.getInt(context.getContentResolver(),
          Settings.System.AIRPLANE_MODE_ON, 0);
    } else {
      return Settings.Global.getInt(context.getContentResolver(),
          Settings.Global.AIRPLANE_MODE_ON, 0);
    }
  }

  /**
   * ?????????????????WRITE_APN_SETTINGS?????
   * 
   * @param context
   *            ???
   * @return true????false????????
   */
  public static boolean isAirplaneModeOpen(Context context) {
    return getAirplaneModeState(context) == 1 ? true : false;
  }

  /**
   * ???????????????WRITE_APN_SETTINGS?????
   * 
   * @param context
   *            ???
   * @param enable
   *            ?????????
   * @return ?????????
   */
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) @SuppressWarnings("deprecation")
  public static boolean setAirplaneMode(Context context, boolean enable) {
    boolean result = true;
    // ???????????????????????????
    if (isAirplaneModeOpen(context) != enable) {
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        result = Settings.System.putInt(context.getContentResolver(),
            Settings.System.AIRPLANE_MODE_ON, enable ? 1 : 0);
      } else {
        result = Settings.Global.putInt(context.getContentResolver(),
            Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0);
      }
      // ???????????????????
      context.sendBroadcast(new Intent(
          Intent.ACTION_AIRPLANE_MODE_CHANGED));
    }
    return result;
  }

  /**
   * ???????????
   * 
   * @return ?????BluetoothAdapter???????????STATE_OFF, STATE_TURNING_OFF,
   *         STATE_ON, STATE_TURNING_ON
   * @throws Exception
   *             ?????????
   */
  public static int getBluetoothState() throws Exception {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter
        .getDefaultAdapter();
    if (bluetoothAdapter == null) {
      throw new Exception("bluetooth device not found!");
    } else {
      return bluetoothAdapter.getState();
    }
  }

  /**
   * ???????????
   * 
   * @return true?????????????false????????????
   * @throws DeviceNotFoundException
   *             ?????????
   */
  public static boolean isBluetoothOpen() throws Exception {
    int bluetoothStateCode = getBluetoothState();
    return bluetoothStateCode == BluetoothAdapter.STATE_ON
        || bluetoothStateCode == BluetoothAdapter.STATE_TURNING_ON ? true
        : false;
  }

  /**
   * ????????
   * 
   * @param enable
   *            ??
   * @throws DeviceNotFoundException
   *             ?????????
   */
  public static void setBluetooth(boolean enable) throws Exception {
    // ?????????????????????????
    if (isBluetoothOpen() != enable) {
      // ?????????????????
      if (enable) {
        BluetoothAdapter.getDefaultAdapter().enable();
      } else {
        BluetoothAdapter.getDefaultAdapter().disable();
      }
    }
  }

  /**
   * ?????????????WRITE_APN_SETTINGS?????
   * 
   * @param context
   *            ???
   * @return ?????????????0-15???0
   */
  public static int getMediaVolume(Context context) {
    return Settings.System.getInt(context.getContentResolver(),
        Settings.System.VOLUME_MUSIC, 0);
  }

  /**
   * ?????????????WRITE_APN_SETTINGS?????
   * 
   * @param context
   *            ???
   * @return ?????????????0-15
   */
  public static boolean setMediaVolume(Context context, int mediaVloume) {
    if (mediaVloume < 0) {
      mediaVloume = 0;
    } else if (mediaVloume > 15) {
      mediaVloume = mediaVloume % 15;
      if (mediaVloume == 0) {
        mediaVloume = 15;
      }
    }
    return Settings.System.putInt(context.getContentResolver(),
        Settings.System.VOLUME_MUSIC, mediaVloume);
  }

  /**
   * ?????????????WRITE_APN_SETTINGS?????
   * 
   * @param context
   *            ???
   * @return ?????????????0-7????0
   */
  public static int getRingVolume(Context context) {
    return Settings.System.getInt(context.getContentResolver(),
        Settings.System.VOLUME_RING, 0);
  }

  /**
   * ?????????
   * 
   * @param context
   *            ???
   * @return ?????????????0-7
   */
  public static boolean setRingVolume(Context context, int ringVloume) {
    if (ringVloume < 0) {
      ringVloume = 0;
    } else if (ringVloume > 7) {
      ringVloume = ringVloume % 7;
      if (ringVloume == 0) {
        ringVloume = 7;
      }
    }
    return Settings.System.putInt(context.getContentResolver(),
        Settings.System.VOLUME_MUSIC, ringVloume);
  }

  // /**
  // * ????????
  // */
  // public static class DeviceNotFoundException extends Exception{
  // private static final long serialVersionUID = 1L;
  //
  // public DeviceNotFoundException(){}
  //
  // public DeviceNotFoundException(String message){
  // super(message);
  // }
  // }

}




Java Source Code List

com.worthed.BuildConfig.java
com.worthed.app.BaseApplication.java
com.worthed.app.BaseCrashHandler.java
com.worthed.app.RebootThreadExceptionHandler.java
com.worthed.app.StartAppReceiver.java
com.worthed.demo.BitmapActivity.java
com.worthed.demo.MainActivity.java
com.worthed.demo.ViewFinderActivity.java
com.worthed.util.AnimationUtils.java
com.worthed.util.AppUtils.java
com.worthed.util.AssetDatabaseOpenHelper.java
com.worthed.util.BitmapUtil.java
com.worthed.util.CipherUtils.java
com.worthed.util.Colors.java
com.worthed.util.CommonUtil.java
com.worthed.util.DataCleanManager.java
com.worthed.util.DatabaseExportUtils.java
com.worthed.util.DateUtils.java
com.worthed.util.DeviceStatusUtils.java
com.worthed.util.DisplayUtils.java
com.worthed.util.DoubleKeyValueMap.java
com.worthed.util.DownloadManagerPro.java
com.worthed.util.FileUtils.java
com.worthed.util.HanziToPinyin.java
com.worthed.util.ImsiUtil.java
com.worthed.util.LocationUtils.java
com.worthed.util.LogUtils.java
com.worthed.util.NetUtil.java
com.worthed.util.PackageUtils.java
com.worthed.util.PhoneUtil.java
com.worthed.util.PollingUtils.java
com.worthed.util.PreferencesCookieStore.java
com.worthed.util.RUtils.java
com.worthed.util.RandomUtils.java
com.worthed.util.RegUtils.java
com.worthed.util.ResourceUtils.java
com.worthed.util.SDCardUtils.java
com.worthed.util.SettingUtils.java
com.worthed.util.ShellUtils.java
com.worthed.util.ShortCutUtils.java
com.worthed.util.Singleton.java
com.worthed.util.StringUtils.java
com.worthed.util.ViewAnimationUtils.java
com.worthed.util.ViewFinder.java
com.worthed.util.ViewUtils.java
com.worthed.util.WindowUtils.java