Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
yotta
/
pictogram
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
60
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d70364dd
authored
Mar 09, 2016
by
Fernando Martínez Santiago
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
working on local vocabulary management when offline
parent
f386fccf
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
72 additions
and
66 deletions
android/Pictogram/app/src/main/java/com/yottacode/net/RestapiWrapper.java
android/Pictogram/app/src/main/java/com/yottacode/pictogram/dao/Device.java
android/Pictogram/app/src/main/java/com/yottacode/pictogram/dao/PCBDBHelper.java
android/Pictogram/app/src/main/java/com/yottacode/pictogram/grammar/Vocabulary.java
android/Pictogram/app/src/main/java/com/yottacode/pictogram/net/ImgDownloader.java
android/Pictogram/app/src/main/java/com/yottacode/pictogram/net/NetService.java
android/Pictogram/app/src/main/java/com/yottacode/pictogram/net/PictoUploader.java
android/Pictogram/app/src/main/java/com/yottacode/pictogram/tools/PCBcontext.java
android/Pictogram/app/src/main/java/com/yottacode/net/RestapiWrapper.java
View file @
d70364dd
...
...
@@ -122,7 +122,6 @@ public class RestapiWrapper {
public
static
String
GET
(
String
surl
,
Hashtable
<
String
,
String
>
params
,
iRestapiListener
listener
)
{
String
result
=
null
;
InputStream
inputStream
=
null
;
URL
url
=
null
;
try
{
...
...
@@ -179,7 +178,6 @@ public class RestapiWrapper {
sparams
=
""
;
for
(
String
param
:
params
.
keySet
())
{
String
value
=
params
.
get
(
param
);
Log
.
e
(
this
.
getClass
().
getCanonicalName
(),
param
+
"="
+
value
+
" total:"
+
sparams
+
" json?"
+
json_params
);
if
(
param
.
equals
(
"token"
))
urlConnection
.
setRequestProperty
(
"Authorization"
,
"Bearer "
+
value
);
else
{
...
...
@@ -253,7 +251,7 @@ public class RestapiWrapper {
try
{
//if (params.e)
if
(
params
.
result
!=
null
)
{
Log
.
d
(
LOG_TAG
,
"JSON
: "
+
params
.
result
);
Log
.
i
(
LOG_TAG
,
"Picto JSON Result
: "
+
params
.
result
);
Object
jsonResult
=
new
JSONTokener
(
params
.
result
).
nextValue
();
if
(
jsonResult
instanceof
JSONObject
)
{
...
...
android/Pictogram/app/src/main/java/com/yottacode/pictogram/dao/Device.java
View file @
d70364dd
...
...
@@ -306,7 +306,7 @@ public class Device extends SQLiteOpenHelper {
for
(
User
user
:
prev_users
)
db
.
delete
(
"users_detail"
,
"id_stu=? AND id_sup=?"
,
new
String
[]{
Integer
.
toString
(
user
.
get_id_stu
()),
Integer
.
toString
(
user
.
get_id_sup
())});
ImgDownloader
downloader
=
new
ImgDownloader
(
this
.
context
,
imgListener
,
ImgDownloader
.
source
.
remote
);
ImgDownloader
downloader
=
new
ImgDownloader
(
this
.
context
,
imgListener
,
ImgDownloader
.
t
source
.
remote
);
downloader
.
executeOnExecutor
(
AsyncTask
.
THREAD_POOL_EXECUTOR
,
imgs
);
...
...
android/Pictogram/app/src/main/java/com/yottacode/pictogram/dao/PCBDBHelper.java
View file @
d70364dd
...
...
@@ -266,7 +266,7 @@ public class PCBDBHelper extends SQLiteOpenHelper {
ContentValues
values
=
new
ContentValues
(
1
);
values
.
put
(
"attributes"
,
attrs
);
int
updates
=
db
.
update
(
"collection"
,
values
,
"id_stu=? AND id_picto=?"
,
new
String
[]
{
Integer
.
toString
(
id_stu
),
Integer
.
toString
(
picto_id
)});
Log
.
i
(
this
.
getClass
().
getCanonicalName
(),
"Modify "
+
updates
+
"
p
icto, id. "
+
picto_id
+
" attributes="
+
attrs
);
Log
.
i
(
this
.
getClass
().
getCanonicalName
(),
"Modify "
+
updates
+
"
P
icto, id. "
+
picto_id
+
" attributes="
+
attrs
);
db
.
close
();
}
...
...
android/Pictogram/app/src/main/java/com/yottacode/pictogram/grammar/Vocabulary.java
View file @
d70364dd
...
...
@@ -47,11 +47,11 @@ public class Vocabulary implements Iterable<Picto> {
this
.
pictos
=
new
Hashtable
<>(
Vocabulary
.
DEFAULT_VOCABULARY_SIZE
);
this
.
imgListener
=
listener
;
if
(
PCBcontext
.
getNetService
().
online
())
{
Log
.
i
(
this
.
getClass
().
getName
(),
"
d
ownloading vocabulary"
);
Log
.
i
(
this
.
getClass
().
getName
(),
"
FERNANDO D
ownloading vocabulary"
);
synchronize
();
}
else
try
{
Log
.
i
(
this
.
getClass
().
getName
(),
"
l
ocal vocabulary"
);
Log
.
i
(
this
.
getClass
().
getName
(),
"
FERNANDO L
ocal vocabulary"
);
PCBcontext
.
getPcbdb
().
getStudentVocabulary
(
this
);
listener
.
loadComplete
();
}
catch
(
JSONException
e
)
{
...
...
@@ -69,6 +69,7 @@ public class Vocabulary implements Iterable<Picto> {
break
;
}
case
update:
{
Log
.
i
(
this
.
getClass
().
getCanonicalName
(),
"Picto update "
+
args
.
toString
());
modifyAttsPicto
(
picto_cat
,
picto_id
,
args
);
break
;
}
...
...
@@ -79,7 +80,7 @@ public class Vocabulary implements Iterable<Picto> {
String
uri
=
args
.
getJSONObject
(
"picto"
).
getString
(
"uri"
);
JSONObject
attrs_picto
=
args
.
getJSONObject
(
"attributes"
);
addPicto
(
new
Picto
(
picto_id
,
uri
,
text
,
attrs_picto
),
ImgDownloader
.
source
.
remote
);
addPicto
(
new
Picto
(
picto_id
,
uri
,
text
,
attrs_picto
),
ImgDownloader
.
t
source
.
remote
);
}
catch
(
JSONException
e
)
{
Log
.
e
(
this
.
getClass
().
getCanonicalName
(),
e
.
getClass
().
getCanonicalName
()
+
"--"
+
e
);
...
...
@@ -94,29 +95,44 @@ public class Vocabulary implements Iterable<Picto> {
},
listener
};
talk
=
new
VocabularyTalk
(
room
,
listeners
);
}
/**
* the vocabulary is (i) updated to and (ii) downloaded from the server.
* UPload local status modifications and new pictos. Note that when
* a picto is uploaded is not required to delete from the local PCB
* because the PCB will get the remote vocabulary at the end of the process
* The only issue is: what happens whether the upload fails? --> The image will be lost.
* TODO: keep record of failed uploaded images to re-include as local pictos
*/
p
ublic
void
synchronize
()
{
p
rivate
void
synchronize_upload
()
{
try
{
PCBcontext
.
getPcbdb
().
getStudentVocabulary
(
this
);
}
catch
(
JSONException
e
)
{
Log
.
e
(
this
.
getClass
().
getName
(),
" Picto json error from local storage: "
+
e
.
getMessage
());
}
for
(
Picto
picto:
this
)
{
if
(
picto
.
status_modified
())
{
new
PictoUploader
(
picto
).
uploadState
();
Log
.
i
(
this
.
getClass
().
getCanonicalName
(),
"Picto status modified while offline. Picto label:
"
+
picto
.
get_translation
()
+
"
, id:"
+
picto
.
get_id
()
+
" Current status:"
+
picto
.
status_modified
());
}
if
(
picto
.
get_id
()
<
0
)
try
{
new
PictoUploader
(
picto
).
upload
();
//id<0 iif it is a local id
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
Log
.
e
(
this
.
getClass
().
getName
(),
" Picto json error from server: "
+
picto
.
toString
());
}
if
(
picto
.
status_modified
())
{
new
PictoUploader
(
picto
).
uploadState
();
Log
.
i
(
this
.
getClass
().
getCanonicalName
(),
"Picto status modified while offline. Picto translation: '
"
+
picto
.
get_translation
()
+
"'
, id:"
+
picto
.
get_id
()
+
" Current status:"
+
picto
.
status_modified
());
}
if
(
picto
.
get_id
()
<
0
)
//id<0 iif it is a local id
try
{
new
PictoUploader
(
picto
).
upload
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
Log
.
e
(
this
.
getClass
().
getName
(),
" Picto json error from server: "
+
picto
.
toString
());
}
}
}
/**
* the vocabulary is (i) updated to and (ii) downloaded from the server.
*/
public
void
synchronize
()
{
synchronize_upload
();
//download
final
String
picto_str
=
"/pictos"
;
String
operation
=
PCBcontext
.
getPcbdb
().
getCurrentUser
().
get_restapi_operation_stu
()+
picto_str
;
PCBcontext
.
getRestapiWrapper
().
ask
(
operation
,
new
iRestapiListener
()
{
...
...
@@ -199,7 +215,7 @@ public class Vocabulary implements Iterable<Picto> {
imgs
.
add
(
new
Img
(
picto
.
get_id
(),
picto
.
get_url
(),
Img
.
VOCABULARY
));
}
Log
.
d
(
this
.
getClass
().
getName
(),
"Vocabulary size: "
+
updated_collection
.
length
);
ImgDownloader
downloader
=
new
ImgDownloader
(
PCBcontext
.
getContext
(),
imgListener
,
ImgDownloader
.
source
.
remote
);
ImgDownloader
downloader
=
new
ImgDownloader
(
PCBcontext
.
getContext
(),
imgListener
,
ImgDownloader
.
t
source
.
remote
);
downloader
.
executeOnExecutor
(
AsyncTask
.
THREAD_POOL_EXECUTOR
,
imgs
);
PCBcontext
.
getPcbdb
().
setStudentVocabulary
(
this
);
...
...
@@ -211,7 +227,7 @@ public class Vocabulary implements Iterable<Picto> {
* It includes/updates a new picto to the user collection: download the image from remote or local storage, add the picto to the current vocabulary and update the database
* @param pic
*/
public
void
addPicto
(
Picto
pic
,
ImgDownloader
.
source
source
){
public
void
addPicto
(
Picto
pic
,
ImgDownloader
.
t
source
source
){
Vector
<
Img
>
imgs
=
new
Vector
<
Img
>(
1
);
imgs
.
add
(
new
Img
(
pic
.
get_id
(),
pic
.
get_url
(),
Img
.
VOCABULARY
));
...
...
@@ -330,7 +346,7 @@ public class Vocabulary implements Iterable<Picto> {
try
{
picto
=
new
Picto
(
id
,
url
,
exp
,
cat
,
coord_x
,
coord_y
);
addPicto
(
picto
,
ImgDownloader
.
source
.
local
);
addPicto
(
picto
,
ImgDownloader
.
t
source
.
local
);
new
PictoUploader
(
picto
).
upload
();
//id<0 iif it is a local id
}
catch
(
Exception
e
)
{
picto
=
null
;
...
...
android/Pictogram/app/src/main/java/com/yottacode/pictogram/net/ImgDownloader.java
View file @
d70364dd
...
...
@@ -29,16 +29,16 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
iImgDownloaderListener
imgListener
;
public
static
enum
status
{
downloading
,
downloaded_ok
,
downloaded_failed
}
public
static
enum
source
{
remote
,
local
}
public
static
enum
t
source
{
remote
,
local
}
public
status
current
;
private
boolean
force_download
;
Context
context
;
ActivityManager
.
MemoryInfo
mi
;
ActivityManager
activityManager
;
source
source
;
t
source
source
;
public
ImgDownloader
(
Context
context
,
iImgDownloaderListener
listener
,
source
source
)
{
public
ImgDownloader
(
Context
context
,
iImgDownloaderListener
listener
,
t
source
source
)
{
this
.
imgListener
=
listener
;
this
.
context
=
context
;
this
.
mi
=
new
ActivityManager
.
MemoryInfo
();
...
...
android/Pictogram/app/src/main/java/com/yottacode/pictogram/net/NetService.java
View file @
d70364dd
...
...
@@ -60,9 +60,6 @@ public class NetService implements Runnable {
exec
.
scheduleWithFixedDelay
(
this
,
0
,
delay
,
TimeUnit
.
SECONDS
);
}
public
NetService
(){
}
public
boolean
online
()
{
return
updated
;}
...
...
@@ -93,7 +90,7 @@ public class NetService implements Runnable {
PCBcontext
.
getActionLog
().
batch
();
updated
=
true
;
}
Log
.
i
(
this
.
getClass
().
getName
(),
"PCB status checked. Updated? "
+
updated
+
" in Room? "
+
PCBcontext
.
getRoom
().
inRoom
()
);
Log
.
i
(
this
.
getClass
().
getName
(),
"PCB status checked. Updated? "
+
updated
);
}
@Override
...
...
android/Pictogram/app/src/main/java/com/yottacode/pictogram/net/PictoUploader.java
View file @
d70364dd
...
...
@@ -11,6 +11,7 @@ import com.yottacode.pictogram.R;
import
com.yottacode.pictogram.action.PictoAction
;
import
com.yottacode.pictogram.action.VocabularyAction
;
import
com.yottacode.pictogram.dao.Picto
;
import
com.yottacode.pictogram.grammar.Vocabulary
;
import
com.yottacode.pictogram.tools.Img
;
import
com.yottacode.pictogram.tools.PCBcontext
;
...
...
@@ -85,8 +86,7 @@ public class PictoUploader {
/**
* if the a picto was included from the PCB, the translation is uploaded to the server
*/
private
int
uploadAttributes
(
int
id_picto
)
{
final
int
id_stupicto
[]=
new
int
[
1
];
private
void
uploadAttributes
(
int
id_picto
)
{
Hashtable
<
String
,
String
>
params
=
new
Hashtable
<
String
,
String
>(
4
);
try
{
params
.
put
(
"json"
,
new
JSONObject
().
put
(
"attributes"
,
...
...
@@ -111,22 +111,14 @@ public class PictoUploader {
@Override
public
void
result
(
JSONObject
result
)
{
Log
.
i
(
this
.
getClass
().
getCanonicalName
(),
" Attributes uploaded: "
+
result
.
toString
());
/* try {
id_stupicto[0]=result.getJSONObject("picto").getInt("id");
} catch (JSONException e) {
e.printStackTrace();
Log.e(this.getClass().getCanonicalName(), "Error: " + e.getLocalizedMessage());
}*/
}
@Override
public
void
error
(
Exception
e
)
{
Log
.
e
(
this
.
getClass
().
getCanonicalName
(),
" Error uploading attributes: "
+
e
.
getLocalizedMessage
());
id_stupicto
[
0
]=-
1
;
PCBcontext
.
getVocabulary
().
addPicto
(
picto
,
ImgDownloader
.
tsource
.
local
)
;
}
});
return
id_stupicto
[
0
];
}
/**
...
...
@@ -155,22 +147,22 @@ public class PictoUploader {
@Override
public
void
error
(
Exception
e
)
{
Log
.
e
(
this
.
getClass
().
getCanonicalName
(),
"Error: "
+
e
.
getLocalizedMessage
());
PCBcontext
.
getVocabulary
().
addPicto
(
picto
,
ImgDownloader
.
tsource
.
local
);
}
});
}
/**
*Upload local picto. It requires: i) to upload the image, ii) to upload the attributes and iii) to upload the expression
*
Try to
Upload local picto. It requires: i) to upload the image, ii) to upload the attributes and iii) to upload the expression
**/
public
void
upload
()
throws
IOException
{
if
(
PCBcontext
.
getNetService
().
online
())
{
int
img_id
=
uploadImg
(
this
.
picto
);
if
(
img_id
>
0
)
{
uploadAttributes
(
img_id
);
uploadTranslation
(
img_id
);
}
}
int
img_id
=
uploadImg
(
this
.
picto
);
if
(
img_id
>
0
)
{
uploadAttributes
(
img_id
);
uploadTranslation
(
img_id
);
}
else
PCBcontext
.
getVocabulary
().
addPicto
(
picto
,
ImgDownloader
.
tsource
.
local
);
}
/**
...
...
@@ -184,12 +176,10 @@ public class PictoUploader {
params
.
put
(
"id_stu"
,
Integer
.
toString
(
PCBcontext
.
getPcbdb
().
getCurrentUser
().
get_id_stu
()));
params
.
put
(
"id_pic"
,
Integer
.
toString
(
this
.
picto
.
get_id
()));
if
(
PCBcontext
.
getNetService
().
online
())
{
Log
.
i
(
this
.
getClass
().
getCanonicalName
(),
"Uploading "
+
params
.
toString
());
PCBcontext
.
getRestapiWrapper
().
ask
(
PCBcontext
.
getPcbdb
().
getCurrentUser
().
get_restapi_operation_stu
()
+
"/picto"
,
params
,
"put"
,
new
iRestapiListener
()
{
Log
.
i
(
this
.
getClass
().
getCanonicalName
(),
"Picto Uploading "
+
params
.
toString
());
PCBcontext
.
getRestapiWrapper
().
ask
(
PCBcontext
.
getPcbdb
().
getCurrentUser
().
get_restapi_operation_stu
()
+
"/picto"
,
params
,
"put"
,
new
iRestapiListener
()
{
@Override
public
void
preExecute
()
{
}
...
...
@@ -214,7 +204,6 @@ public class PictoUploader {
}
}
);
}
}
...
...
android/Pictogram/app/src/main/java/com/yottacode/pictogram/tools/PCBcontext.java
View file @
d70364dd
...
...
@@ -28,7 +28,7 @@ public final class PCBcontext {
private
static
RestapiWrapper
wrapper
;
private
static
Vocabulary
vocabulary
;
private
static
ActionLog
actionLog
;
private
static
boolean
init
=
false
;
protected
PCBcontext
()
{
// Initialize internal objects. This initialization is run only the first time
...
...
@@ -50,10 +50,15 @@ public final class PCBcontext {
// Init method for passing params to the singleton
public
static
void
init
(
Context
c
){
context
=
c
;
device
=
new
Device
(
c
,
null
,
1
);
wrapper
=
new
RestapiWrapper
(
context
.
getResources
().
getString
(
R
.
string
.
server
),
null
);
Log
.
i
(
PCBcontext
.
class
.
getCanonicalName
(),
"PCB context started, waiting. It's required set user "
);
if
(!
init
)
{
init
=
true
;
context
=
c
;
device
=
new
Device
(
c
,
null
,
1
);
wrapper
=
new
RestapiWrapper
(
context
.
getResources
().
getString
(
R
.
string
.
server
),
null
);
service
=
new
NetService
(
context
.
getResources
().
getInteger
(
R
.
integer
.
netservice_timing
));
Log
.
i
(
PCBcontext
.
class
.
getCanonicalName
(),
"PCB context started. It's required to be invoked set_user method"
);
}
else
Log
.
e
(
PCBcontext
.
class
.
getClass
().
getCanonicalName
(),
"Init method was previously invoked! Please, revise your code"
);
}
/**
...
...
@@ -64,13 +69,14 @@ public final class PCBcontext {
* @param listener
*/
public
static
void
set_user
(
User
student
,
String
token
,
iImgDownloaderListener
listener
)
{
Log
.
i
(
PCBcontext
.
class
.
getCanonicalName
(),
"User set at student "
+
student
.
get_name_stu
());
if
(!
init
)
throw
new
java
.
lang
.
AssertionError
(
"init must be called once previously "
);
Log
.
i
(
PCBcontext
.
class
.
getCanonicalName
(),
"User set at student "
+
student
.
get_name_stu
());
wrapper
.
setToken
(
token
);
pcbdb
=
new
PCBDBHelper
(
null
,
1
,
student
);
service
=
new
NetService
(
context
.
getResources
().
getInteger
(
R
.
integer
.
netservice_timing
));
vocabulary
=
new
Vocabulary
(
listener
);
room
=
new
Room
();
actionLog
=
new
ActionLog
();
vocabulary
=
new
Vocabulary
(
listener
);
}
// Return the context
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment