Merge branch 'feature/timeManager' into develop

Fin de la fonctionnalité du TimeManager
This commit is contained in:
Jacques Foucry 2016-08-30 23:03:25 +02:00
commit 793d0c292d
8 changed files with 131 additions and 81 deletions

View file

@ -3,8 +3,9 @@
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.14.1" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />

View file

@ -43,6 +43,7 @@
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<service android:name=".TimeService"/>
</application>
</manifest>

View file

@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -19,16 +20,16 @@ public class DBMedoc extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static String DATABASE_PATH = "/data/data/net.foucry.pilldroid/databases/";
private static String DATABASE_NAME = "medicaments.db";
File dbFile;
private final static String DB_PATH = "/data/data/net.foucry.pilldroid/databases/";
private static String dbName = "medicaments.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
Context myContext;
private static final String TABLE_NAME = "medicaments";
// private static final String MEDOC_ID = "id";
private static final String MEDOC_CIS = "cis";
private static final String MEDOC_CIP13 = "cip13";
// private static final String MEDOC_CIP7 = "cip7";
private static final String MEDOC_ADMIN = "mode_administration";
private static final String MEDOC_NOM = "nom";
private static final String MEDOC_PRES = "presentation";
@ -36,78 +37,57 @@ public class DBMedoc extends SQLiteOpenHelper{
private static final String[] COLUMNS_NAMES = {MEDOC_CIS, MEDOC_CIP13, MEDOC_ADMIN, MEDOC_NOM, MEDOC_PRES};
public DBMedoc(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
super(context, dbName, null, DATABASE_VERSION);
this.myContext = context;
}
@Override
public synchronized SQLiteDatabase getWritableDatabase() {
if (!dbFile.exists()) {
SQLiteDatabase db = super.getWritableDatabase();
copyDatabase(db.getPath());
}
return super.getWritableDatabase();
}
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
if (!dbFile.exists()) {
SQLiteDatabase db = super.getReadableDatabase();
copyDatabase(db.getPath());
}
return super.getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
private void copyDatabase(String dbPath) {
try {
createDatabase();
InputStream assestDB = myContext.getAssets().open(dbName);
OutputStream appDB = new FileOutputStream(dbPath, false);
byte[] buffer = new byte[1024];
int length;
while ((length = assestDB.read(buffer)) > 0) {
appDB.write(buffer,0, length);
}
appDB.flush();
appDB.close();
assestDB.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void createDatabase() throws IOException {
Log.e(MedicamentListActivity.Constants.TAG, "createDatabase called");
boolean dbExist = checkDatabase();
if (dbExist) {
// Nothing to do, DB already exist
} else {
this.getDatabaseName();
try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error coping Database");
}
}
}
private boolean checkDatabase() {
if (BuildConfig.DEBUG) {
Log.e(MedicamentListActivity.Constants.TAG, "checkDatabase called");
}
SQLiteDatabase checkDB = null;
try {
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does not exists
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDatabase() throws IOException {
Log.e(MedicamentListActivity.Constants.TAG, "copyDatabase called");
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer,0,length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDatabase() throws SQLiteException {
Log.e(MedicamentListActivity.Constants.TAG, "openDatabase called");
String myPath = DATABASE_PATH + DATABASE_NAME;
String myPath = DB_PATH + dbName;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@ -118,17 +98,8 @@ public class DBMedoc extends SQLiteOpenHelper{
myDataBase.close();
}
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private DBMedoc dbMedoc;
// private DBMedoc dbMedoc;
public Medicament getMedocByCIP13(String cip13) {
Log.e(MedicamentListActivity.Constants.TAG, "getNedocByCIP13 - " + cip13);
@ -151,6 +122,7 @@ public class DBMedoc extends SQLiteOpenHelper{
// Build medicament object
Medicament medicament = new Medicament();
// medicament.setId(Integer.parseInt(cursor.getString(0)));
assert cursor != null;
medicament.setCis(cursor.getString(0));
medicament.setCip13(cursor.getString(1));
medicament.setMode_administration(cursor.getString(2));
@ -172,6 +144,7 @@ public class DBMedoc extends SQLiteOpenHelper{
// Return medicament
cursor.close();
return medicament;
}
}

View file

@ -71,6 +71,8 @@ public class MedicamentListActivity extends AppCompatActivity {
toolbar.setTitle(getTitle());
}
startService(new Intent(this, TimeService.class));
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override

View file

@ -0,0 +1,56 @@
package net.foucry.pilldroid;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by jacques on 22/08/16.
*/
public class TimeService extends Service {
//public static final long NOTIFY_INTERVAL = 10 *1000;
private Handler mHandler = new Handler();
private Timer mTimer = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
if(mTimer != null) {
mTimer.cancel();
} else {
mTimer = new Timer();
}
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(),0, UtilDate.tomorrowAtNoon());
}
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),getDateTime(), Toast.LENGTH_SHORT).show();
}
});
}
private String getDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
return sdf.format(new Date());
}
}
}

View file

@ -91,4 +91,21 @@ public class UtilDate {
return (int) (todayDate.getTime() - oldDate.getTime());
}
/**
* @param: none
* return int
*/
public static long tomorrowAtNoon() {
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.DAY_OF_YEAR,1);
Date tomorrowAtNoon = dateAtNoon(calendar.getTime());
long millis = tomorrowAtNoon.getTime() - now.getTime();
return millis;
}
}

View file

@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.android.tools.build:gradle:2.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

View file

@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Sun Aug 28 18:16:44 CEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip