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

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArsentiy Milchakov <milcars@mapswithme.com>2017-06-05 15:16:23 +0300
committerGitHub <noreply@github.com>2017-06-05 15:16:23 +0300
commit816bcb9c346524ba68f6ee448b2c4ede98c2ab72 (patch)
treee21ce124c7f81fe75c8666f454b7c7478ffbb0bd
parentc6d502e234788d1b479fa4ca66408459e3887c59 (diff)
parent1f02c129b565c0f885e499a951ef1e672a1cb47c (diff)
Merge pull request #6195 from goblinr/MAPSME-4643-android-platform-charging-statusbeta-849
[android] implemented Platform::GetChargingStatus() method.
-rw-r--r--android/jni/com/mapswithme/platform/Platform.cpp13
-rw-r--r--android/src/com/mapswithme/maps/MwmApplication.java5
-rw-r--r--android/src/com/mapswithme/util/BatteryState.java94
-rw-r--r--android/src/com/mapswithme/util/statistics/Statistics.java34
4 files changed, 129 insertions, 17 deletions
diff --git a/android/jni/com/mapswithme/platform/Platform.cpp b/android/jni/com/mapswithme/platform/Platform.cpp
index 829464fba2..e86961f868 100644
--- a/android/jni/com/mapswithme/platform/Platform.cpp
+++ b/android/jni/com/mapswithme/platform/Platform.cpp
@@ -56,7 +56,18 @@ Platform::EConnectionType Platform::ConnectionStatus()
Platform::ChargingStatus Platform::GetChargingStatus()
{
- return Platform::ChargingStatus::Unknown;
+ JNIEnv * env = jni::GetEnv();
+ if (env == nullptr)
+ return Platform::ChargingStatus::Unknown;
+
+ static jclass const clazzBatteryState =
+ jni::GetGlobalClassRef(env, "com/mapswithme/util/BatteryState");
+ ASSERT(clazzBatteryState, ());
+
+ static jmethodID const getChargingMethodId =
+ jni::GetStaticMethodID(env, clazzBatteryState, "getChargingStatus", "()I");
+ return static_cast<Platform::ChargingStatus>(
+ env->CallStaticIntMethod(clazzBatteryState, getChargingMethodId));
}
namespace android
diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java
index 0ee1824fad..3479417571 100644
--- a/android/src/com/mapswithme/maps/MwmApplication.java
+++ b/android/src/com/mapswithme/maps/MwmApplication.java
@@ -110,10 +110,7 @@ public class MwmApplication extends Application
@Override
public void onVisibleAppLaunch()
{
- IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
- Intent batteryStatus = registerReceiver(null, filter);
- if (batteryStatus != null)
- Statistics.INSTANCE.trackColdStartupInfo(batteryStatus);
+ Statistics.INSTANCE.trackColdStartupInfo();
}
};
diff --git a/android/src/com/mapswithme/util/BatteryState.java b/android/src/com/mapswithme/util/BatteryState.java
new file mode 100644
index 0000000000..bf7b73e7cc
--- /dev/null
+++ b/android/src/com/mapswithme/util/BatteryState.java
@@ -0,0 +1,94 @@
+package com.mapswithme.util;
+
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.BatteryManager;
+import android.support.annotation.IntDef;
+import android.support.annotation.IntRange;
+import android.support.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));
+ }
+
+ @ChargingStatus
+ public static int getChargingStatus()
+ {
+ return getState().mChargingStatus;
+ }
+
+ @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;
+ }
+ }
+}
diff --git a/android/src/com/mapswithme/util/statistics/Statistics.java b/android/src/com/mapswithme/util/statistics/Statistics.java
index 858915f6a4..0010734171 100644
--- a/android/src/com/mapswithme/util/statistics/Statistics.java
+++ b/android/src/com/mapswithme/util/statistics/Statistics.java
@@ -2,11 +2,9 @@ package com.mapswithme.util.statistics;
import android.app.Activity;
import android.content.Context;
-import android.content.Intent;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
-import android.os.BatteryManager;
import android.os.Build;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
@@ -29,6 +27,7 @@ import com.mapswithme.maps.editor.Editor;
import com.mapswithme.maps.editor.OsmOAuth;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.widget.placepage.Sponsored;
+import com.mapswithme.util.BatteryState;
import com.mapswithme.util.Config;
import com.mapswithme.util.ConnectionState;
import com.my.tracker.MyTracker;
@@ -40,6 +39,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import static com.mapswithme.util.BatteryState.CHARGING_STATUS_PLUGGED;
+import static com.mapswithme.util.BatteryState.CHARGING_STATUS_UNKNOWN;
+import static com.mapswithme.util.BatteryState.CHARGING_STATUS_UNPLUGGED;
import static com.mapswithme.util.statistics.Statistics.EventName.APPLICATION_COLD_STARTUP_INFO;
import static com.mapswithme.util.statistics.Statistics.EventName.DOWNLOADER_DIALOG_ERROR;
import static com.mapswithme.util.statistics.Statistics.EventName.PP_BANNER_BLANK;
@@ -620,17 +622,25 @@ public enum Statistics
.get());
}
- public void trackColdStartupInfo(@NonNull Intent batteryStatus)
+ public void trackColdStartupInfo()
{
- int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
- int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
+ BatteryState.State state = BatteryState.getState();
final String charging;
- if (chargePlug > 0)
- charging = "on";
- else if (chargePlug < 0)
- charging = "unknown";
- else
- charging = "off";
+ switch (state.getChargingStatus())
+ {
+ case CHARGING_STATUS_UNKNOWN:
+ charging = "unknown";
+ break;
+ case CHARGING_STATUS_PLUGGED:
+ charging = "on";
+ break;
+ case CHARGING_STATUS_UNPLUGGED:
+ charging = "off";
+ break;
+ default:
+ charging = "unknown";
+ break;
+ }
final String network;
if (ConnectionState.isWifiConnected())
@@ -651,7 +661,7 @@ public enum Statistics
trackEvent(APPLICATION_COLD_STARTUP_INFO,
params()
- .add(BATTERY, level)
+ .add(BATTERY, state.getLevel())
.add(CHARGING, charging)
.add(NETWORK, network)
.get());