Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BatteryState.java « util « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 218a56706ed1b39b57134a7353584e1e05214906 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.mapswithme.util;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import androidx.annotation.IntDef;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;

import com.mapswithme.maps.MwmApplication;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public final class BatteryState
{
  public static final byte CHARGING_STATUS_UNKNOWN = 0;
  public static final byte CHARGING_STATUS_PLUGGED = 1;
  public static final byte CHARGING_STATUS_UNPLUGGED = 2;

  @Retention(RetentionPolicy.SOURCE)
  @IntDef({ CHARGING_STATUS_UNKNOWN, CHARGING_STATUS_PLUGGED, CHARGING_STATUS_UNPLUGGED })
  public @interface ChargingStatus {}

  private BatteryState() {}

  @NonNull
  public static State getState()
  {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    // Because it's a sticky intent, you don't need to register a BroadcastReceiver
    // by simply calling registerReceiver passing in null
    Intent batteryStatus = MwmApplication.get().registerReceiver(null, filter);
    if (batteryStatus == null)
      return new State(0, CHARGING_STATUS_UNKNOWN);

    return new State(getLevel(batteryStatus), getChargingStatus(batteryStatus));
  }

  @IntRange(from=0, to=100)
  public static int getLevel()
  {
    return getState().getLevel();
  }

  @ChargingStatus
  public static int getChargingStatus()
  {
    return getState().getChargingStatus();
  }

  @IntRange(from=0, to=100)
  private static int getLevel(@NonNull Intent batteryStatus)
  {
    return batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
  }

  @ChargingStatus
  private static int getChargingStatus(@NonNull Intent batteryStatus)
  {
    // Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
    // integer indicating whether the device is plugged in to a power
    // source; 0 means it is on battery, other constants are different
    // types of power sources.
    int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    if (chargePlug > 0)
      return CHARGING_STATUS_PLUGGED;
    else if (chargePlug < 0)
      return CHARGING_STATUS_UNKNOWN;

    return CHARGING_STATUS_UNPLUGGED;
  }

  public static final class State
  {
    @IntRange(from=0, to=100)
    private final int mLevel;

    @ChargingStatus
    private final int mChargingStatus;

    public State(@IntRange(from=0, to=100) int level, @ChargingStatus int chargingStatus)
    {
      mLevel = level;
      mChargingStatus = chargingStatus;
    }

    @IntRange(from=0, to=100)
    public int getLevel()
    {
      return mLevel;
    }

    @ChargingStatus
    public int getChargingStatus()
    {
      return mChargingStatus;
    }
  }
}