Skip to content
This repository has been archived by the owner on Feb 10, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpak committed Apr 30, 2016
2 parents dfc24d4 + 0a1019c commit 81c6c57
Show file tree
Hide file tree
Showing 66 changed files with 2,742 additions and 148 deletions.
41 changes: 29 additions & 12 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "net.kacpak.batterychargingmonitor"
Expand All @@ -13,26 +14,42 @@ android {
}
buildTypes {
release {
minifyEnabled false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
minifyEnabled true
applicationIdSuffix ".debug"
versionNameSuffix '.debug'
}
}
}

def supportLibrary = "23.1.1"
repositories {
maven { url "https://jitpack.io" } // To compile from GitHub when MavenCentral is outdated (circleprogress)
}

ext {
supportLibrary_version = "23.3.0"
circleProgress_version = "a947edf" // TODO change to new release when startingPoint is merged
butterKnife_version = "8.0.1"
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'

// Support Libraries
compile "com.android.support:appcompat-v7:$supportLibrary"
compile "com.android.support:design:$supportLibrary"
compile "com.android.support:support-annotations:$supportLibrary"

compile "com.android.support:appcompat-v7:$supportLibrary_version"
compile "com.android.support:design:$supportLibrary_version"
compile "com.android.support:support-annotations:$supportLibrary_version"
compile "com.android.support:cardview-v7:$supportLibrary_version"
compile "com.android.support:gridlayout-v7:$supportLibrary_version"
compile "com.android.support:recyclerview-v7:$supportLibrary_version"
// Circle Progress
compile 'com.github.lzyzsd:circleprogress:1.1.0@aar'

compile "com.github.lzyzsd:circleprogress:$circleProgress_version"
// Butter Knife
compile 'com.jakewharton:butterknife:7.0.1'
compile "com.jakewharton:butterknife:$butterKnife_version"
apt "com.jakewharton:butterknife-compiler:$butterKnife_version"
}
26 changes: 25 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.kacpak.batterychargingmonitor">

<uses-permission android:name="android.permission.BATTERY_STATS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/AppTheme">
<activity
android:name=".ui.MainActivity"
android:label="@string/app_name">
Expand All @@ -17,6 +21,26 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ui.settings.SettingsActivity"
android:label="@string/action_settings"
android:parentActivityName=".ui.MainActivity"
android:theme="@style/AppTheme.ColoredActionBar">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>

<provider
android:authorities="net.kacpak.batterychargingmonitor"
android:name=".data.database.DataProvider"
android:exported="false" />

<receiver android:name=".data.receiver.PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
</application>

</manifest>
19 changes: 19 additions & 0 deletions app/src/main/java/net/kacpak/batterychargingmonitor/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.kacpak.batterychargingmonitor;

import android.app.Application;
import android.content.Context;

public class App extends Application {

private static Context mContext;

@Override
public void onCreate() {
super.onCreate();
mContext = this;
}

public static Context getContext(){
return mContext;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,232 @@
package net.kacpak.batterychargingmonitor.data;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;

import net.kacpak.batterychargingmonitor.R;
import net.kacpak.batterychargingmonitor.data.database.ChargeInformation;
import net.kacpak.batterychargingmonitor.data.database.DatabaseContract.DataEntry;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* Zarządzanie danymi o baterii i historii ładowania
*/
public class BatteryDataRepository {

static int p = 0;
private final Context mContext;

/**
* Tworzy nowy obiekt do zarządzania danymi o baterii i historii ładowania
* @param context {@see Context} aplikacji
*/
public BatteryDataRepository(Context context) {
mContext = context;
}

/**
* Obecny stan baterii
*/
public BatteryStatus getStatus() {
return new BatteryStatus(++p);
return new BatteryStatus();
}

/**
* Zwraca stan ładowania baterii
* @param chargeId id z historii ładowania baterii
* @return stan ładowania baterii
*/
public ChargeInformation getChargeInformation(long chargeId) {
Uri dataUri = DataEntry.buildUri(chargeId);
Cursor entryInfo = mContext.getContentResolver().query(
dataUri,
null,
null,
null,
null
);

if (null == entryInfo)
return null;

entryInfo.moveToFirst();
return new ChargeInformation(entryInfo);
}

/**
* Dodaje nowy wpis do historii ładowania
* @param start Data i godzina rozpoczęcia ładowania
* @param type Typ ładowarki
* @param percentage Początkowy procent naładowania baterii
* @param temperatureCelsius Temperatura w Celsjuszach
* @param voltage Początkowe napięcie na baterii
*/
public Uri add(Date start, int type, int percentage, float temperatureCelsius, int voltage) {
ContentValues values = new ContentValues();
values.put(DataEntry.COLUMN_TYPE, type);
values.put(DataEntry.COLUMN_START, start.getTime());
values.put(DataEntry.COLUMN_START_PERCENTAGE, percentage);
values.put(DataEntry.COLUMN_START_TEMPERATURE_C, temperatureCelsius);
values.put(DataEntry.COLUMN_START_VOLTAGE, voltage);

return mContext.getContentResolver().insert(
DataEntry.CONTENT_URI,
values
);
}

/**
* Zakańcza ładowanie baterii
* @param stop Data i godzina zakończenia ładowania
* @param percentage Końcowy procent naładowania baterii
* @param temperatureCelsius Temperatura w Celsjuszach
* @param voltage Ostateczne napięcie na baterii
* @param note Notatka do ładowania
*/
public int finishCharging(Date stop, int percentage, float temperatureCelsius, int voltage, String note) {
ContentValues values = new ContentValues();
values.put(DataEntry.COLUMN_CHARGE_FINISHED, 1);
values.put(DataEntry.COLUMN_STOP, stop.getTime());
values.put(DataEntry.COLUMN_STOP_PERCENTAGE, percentage);
values.put(DataEntry.COLUMN_STOP_TEMPERATURE_C, temperatureCelsius);
values.put(DataEntry.COLUMN_STOP_VOLTAGE, voltage);
values.put(DataEntry.COLUMN_NOTE, note);

return mContext.getContentResolver().update(
DataEntry.CONTENT_URI_UNFINISHED,
values,
null,
null
);
}

/**
* Usuwa z historii podane wpisy
* @param ids ID wpisów w bazie danych
*/
public int delete(List<Long> ids) {
String inClause = ids.toString().replace("[", "(").replace("]", ")");

return mContext.getContentResolver().delete(
DataEntry.CONTENT_URI,
DataEntry._ID + " IN " + inClause,
null
);
}

/**
* Usuwa nic nie wnoszące wpisy z historii (krótsze niż podane w preferencjach)
*/
public int deleteIrrelevant() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
int minDuration = 0;
try {
String irrelevantDurationPreference = prefs.getString(mContext.getString(R.string.pref_key_irrelevant_duration), "0");
minDuration = Integer.parseInt(irrelevantDurationPreference) * 1000;
} catch (Exception e) {
// Do nothing
}

return mContext.getContentResolver().delete(
DataEntry.CONTENT_URI,
"CAST(ABS(" + DataEntry.COLUMN_STOP + " - " + DataEntry.COLUMN_START + ") AS INTEGER) <= " + minDuration,
null
);
}

/**
* Łączy podane wpisy w jeden
* @param ids ID wpisów w bazie danych
*/
public int merge(List<Long> ids) {
// Będziemy szukać tylko wpisów z id "(a, b, ...)"
String inClause = ids.toString().replace("[", "(").replace("]", ")");

Cursor entries = mContext.getContentResolver().query(
DataEntry.CONTENT_URI,
null,
DataEntry._ID + " IN " + inClause,
null,
DataEntry.COLUMN_START + " ASC"
);

// Ustalam typ wpisu dla łączenia
entries.moveToFirst();
final int typeColumnIndex = entries.getColumnIndex(DataEntry.COLUMN_TYPE);
int type = entries.getInt(typeColumnIndex);
while (entries.moveToNext()) {
if (entries.getInt(typeColumnIndex) != type) {
type = 0;
break;
}
}

// Wartości do wpisania
ContentValues values = new ContentValues();

// Sczytujemy część początkową
entries.moveToFirst();
values.put(DataEntry.COLUMN_START, entries.getLong(entries.getColumnIndex(DataEntry.COLUMN_START)));
values.put(DataEntry.COLUMN_START_PERCENTAGE, entries.getInt(entries.getColumnIndex(DataEntry.COLUMN_START_PERCENTAGE)));
values.put(DataEntry.COLUMN_START_TEMPERATURE_C, entries.getFloat(entries.getColumnIndex(DataEntry.COLUMN_START_TEMPERATURE_C)));
values.put(DataEntry.COLUMN_START_VOLTAGE, entries.getInt(entries.getColumnIndex(DataEntry.COLUMN_START_VOLTAGE)));
values.put(DataEntry.COLUMN_TYPE, type);

// Sczytujemy wartość końcową
entries.moveToLast();
values.put(DataEntry.COLUMN_STOP, entries.getLong(entries.getColumnIndex(DataEntry.COLUMN_STOP)));
values.put(DataEntry.COLUMN_STOP_PERCENTAGE, entries.getInt(entries.getColumnIndex(DataEntry.COLUMN_STOP_PERCENTAGE)));
values.put(DataEntry.COLUMN_STOP_TEMPERATURE_C, entries.getFloat(entries.getColumnIndex(DataEntry.COLUMN_STOP_TEMPERATURE_C)));
values.put(DataEntry.COLUMN_STOP_VOLTAGE, entries.getInt(entries.getColumnIndex(DataEntry.COLUMN_STOP_VOLTAGE)));

if (entries.getInt(entries.getColumnIndex(DataEntry.COLUMN_CHARGE_FINISHED)) == 1)
values.put(DataEntry.COLUMN_CHARGE_FINISHED, 1);

entries.close();


mContext.getContentResolver().insert(
DataEntry.CONTENT_URI,
values
);

return delete(ids);
}

/**
* Ilość ładowań z uwzględnieniem preferencji
*/
public int getChargedCount() {
return getChargedCount(true);
}

/**
* Ilość ładowań
* @param withPreferences true dla uwzględnienia danych od użytkownika
*/
public int getChargedCount(boolean withPreferences) {
Cursor countCursor = mContext.getContentResolver().query(
DataEntry.CONTENT_URI,
new String[] {"count(*) AS count"},
null,
null,
null
);

int count = countCursor.moveToFirst() ? countCursor.getInt(0) : 0;
countCursor.close();

// Jeśli nie uwzględniamy preferencji, zwróć wartość
if (!withPreferences)
return count;

// Jeśli je uwzględniamy dodaj je do wyniku
return count + new UserPreferences(mContext).getPreviousChargesCount();
}
}
Loading

0 comments on commit 81c6c57

Please sign in to comment.