Skip to content

Commit

Permalink
refactor: fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hmatalonga committed Oct 4, 2019
1 parent d715e89 commit 4a98d70
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 37 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
zipAlignEnabled true
shrinkResources true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Locale;

import static com.hmatalonga.greenhub.util.LogUtils.makeLogTag;

Expand Down Expand Up @@ -368,7 +369,7 @@ private void resetBatteryCurrent() {
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
double now = Battery.getBatteryCurrentNow(mContext);
double now = Battery.getBatteryCurrentNowInAmperes(mContext);
double level = Inspector.getCurrentBatteryLevel();
String value;

Expand All @@ -378,25 +379,26 @@ public void run() {
mBatteryCurrentMin.setText(value);
value = "max: --";
mBatteryCurrentMax.setText(value);
value = mContext.getString(R.string.battery_full);
// value = mContext.getString(R.string.battery_full);
value = String.format(Locale.getDefault(), "%.3f", now) + " A";
mBatteryCurrentNow.setText(value);
mHandler.postDelayed(this, Config.REFRESH_CURRENT_INTERVAL);
return;
}

if (Math.abs(now) < Math.abs(mMin)) {
mMin = now;
value = "min: " + mMin + " mA";
value = "min: " + String.format(Locale.getDefault(), "%.3f", mMin) + " A";
mBatteryCurrentMin.setText(value);
}

if (Math.abs(now) > Math.abs(mMax)) {
mMax = now;
value = "max: " + mMax + " mA";
value = "max: " + String.format(Locale.getDefault(), "%.3f", mMax) + " A";
mBatteryCurrentMax.setText(value);
}

value = now + " mA";
value = String.format(Locale.getDefault(), "%.3f", now) + " A";
mBatteryCurrentNow.setText(value);
mHandler.postDelayed(this, Config.REFRESH_CURRENT_INTERVAL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ public void onReceive(Context context, Intent intent) {
return;
}

LogUtils.logI(TAG, "Now: " + Battery.getBatteryCurrentNow(context));

mIntent = intent;
mAction = intent.getAction();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ static Sample getSample(final Context context, Intent intent) {
batteryDetails.capacity = Battery.getBatteryDesignCapacity(context);
batteryDetails.chargeCounter = Battery.getBatteryChargeCounter(context);
batteryDetails.currentAverage = Battery.getBatteryCurrentAverage(context);
batteryDetails.currentNow = (int) Battery.getBatteryCurrentNow(context);
batteryDetails.currentNow = Battery.getBatteryCurrentNow(context);
batteryDetails.energyCounter = Battery.getBatteryEnergyCounter(context);

boolean isCharging = "Charging".equals(batteryStatus);
Expand All @@ -417,11 +417,11 @@ static Sample getSample(final Context context, Intent intent) {
EventBus.getDefault().post(
new BatteryTimeEvent(batteryRemainingHours, batteryRemainingMinutes, isCharging)
);
Notifier.remainingBatteryTimeAlert(
context,
batteryRemainingHours + "h " + batteryRemainingMinutes + "m",
isCharging
);
// Notifier.remainingBatteryTimeAlert(
// context,
// batteryRemainingHours + "h " + batteryRemainingMinutes + "m",
// isCharging
// );

newSample.batteryDetails = batteryDetails;
newSample.batteryLevel = sCurrentBatteryLevel;
Expand Down
21 changes: 20 additions & 1 deletion app/src/main/java/com/hmatalonga/greenhub/models/Battery.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public static int getBatteryCurrentAverage(final Context context) {
* @param context Application context
* @return battery current now (in mA)
*/
public static double getBatteryCurrentNow(final Context context) {
public static int getBatteryCurrentNow(final Context context) {
int value = 0;

BatteryManager manager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
Expand All @@ -178,6 +178,25 @@ public static double getBatteryCurrentNow(final Context context) {
return (value != 0 && value != Integer.MIN_VALUE) ? value : 0;
}

/**
* Get the Battery current at the moment (in mA)
*
* @param context Application context
* @return battery current now (in mA)
*/
public static double getBatteryCurrentNowInAmperes(final Context context) {
int value = 0;

BatteryManager manager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
if (manager != null) {
value = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
}

value = (value != 0 && value != Integer.MIN_VALUE) ? value : 0;

return (double) value / 1000000;
}

/**
* Get the battery energy counter capacity (in mWh)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void uploadSample(final int id) {
final Upload upload = sample == null ? null : new Upload(bundleSample(sample));
realm.close();

LogUtils.logI(TAG, "Sample found => " + String.valueOf(sample != null));
LogUtils.logI(TAG, "Sample found => " + (sample != null));

if (sample == null && mCollection.hasNext()) {
uploadSample(mCollection.next());
Expand Down Expand Up @@ -251,7 +251,7 @@ private JsonObject bundleSample(final Sample sample) {
root.addProperty("uuId", sample.uuId);
root.addProperty("timestamp", sample.timestamp);
root.addProperty("version", sample.version);
root.addProperty("mDatabase", sample.database);
root.addProperty("database", sample.database);
root.addProperty("batteryState", sample.batteryState);
root.addProperty("batteryLevel", sample.batteryLevel);
root.addProperty("memoryWired", sample.memoryWired);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public RegisterDeviceHandler(final Context context) {
String url = SettingsUtils.fetchServerUrl(context);

if (BuildConfig.DEBUG) {
//url = Config.SERVER_URL_DEVELOPMENT;
url = Config.SERVER_URL_DEVELOPMENT;
}

Retrofit retrofit = new Retrofit.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,23 @@ private LineData loadData(ChartCard card) {
}

private void setup(DashboardViewHolder holder, final ChartCard card) {
ValueFormatter formatterX = new ValueFormatter() {

holder.chart.getXAxis().setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
public String getFormattedValue(float value) {
return DateUtils.convertMilliSecondsToFormattedDate((long) value);
}
};
});

if (card.type == BATTERY_LEVEL) {
holder.chart.getAxisLeft().setAxisMaximum(1f);
}

ValueFormatter formatterY = new ValueFormatter() {
holder.chart.setExtraBottomOffset(5f);
holder.chart.getAxisLeft().setDrawGridLines(false);
holder.chart.getAxisLeft().setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
public String getFormattedValue(float value) {
switch (card.type) {
case BATTERY_LEVEL:
return StringHelper.formatPercentageNumber(value);
Expand All @@ -198,17 +205,7 @@ public String getFormattedValue(float value, AxisBase axis) {
return String.valueOf(value);
}
}
};

holder.chart.getXAxis().setValueFormatter(formatterX);

if (card.type == BATTERY_LEVEL) {
holder.chart.getAxisLeft().setAxisMaximum(1f);
}

holder.chart.setExtraBottomOffset(5f);
holder.chart.getAxisLeft().setDrawGridLines(false);
holder.chart.getAxisLeft().setValueFormatter(formatterY);
});
holder.chart.getAxisRight().setDrawGridLines(false);
holder.chart.getAxisRight().setDrawLabels(false);
holder.chart.getXAxis().setDrawGridLines(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void refreshContent(Entry e, Highlight highlight) {
public MPPointF getOffset() {
if (mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
mOffset = new MPPointF(-(getWidth() >> 1), -getHeight());
}

return mOffset;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/hmatalonga/greenhub/util/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public class DateUtils {
private static final String DATE_FORMAT = "dd-MM HH:mm";

private static SimpleDateFormat sSimpleDateFormat =
new SimpleDateFormat(DATE_FORMAT, Locale.UK);
new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());

public static String convertMilliSecondsToFormattedDate(Long milliSeconds) {
public static String convertMilliSecondsToFormattedDate(long milliSeconds) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'io.realm:realm-gradle-plugin:5.1.0'

// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit 4a98d70

Please sign in to comment.