-
Notifications
You must be signed in to change notification settings - Fork 0
/
GifManagerService.java
99 lines (81 loc) · 2.68 KB
/
GifManagerService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.kimboo.giffy.services;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.kimboo.giffy.db.DatabaseHelper;
import com.kimboo.giffy.model.Gif;
import com.kimboo.giffy.server.rest.RestClient;
import com.kimboo.giffy.server.rest.RestFactory;
import com.kimboo.giffy.utils.Constants;
/**
* This service is designed to do the following things: -Download the gif from
* Internet and put the in the assets folder. -Take the gif images and resize
* then to two kind of sizes. One to show as thumbnail. Another to show full
* screen.
*
* @author astinx
*/
public class GifManagerService extends Service {
private final IBinder mBinder = new LocalBinder();
private RestClient restClient;
private DatabaseHelper databaseHelper = null;
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
public GifManagerService getService() {
return GifManagerService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
restClient = new RestClient();
}
@Override
public void onDestroy() {
super.onDestroy();
/*
* You'll need this in your class to release the helper when done.
*/
if (databaseHelper != null) {
OpenHelperManager.releaseHelper();
databaseHelper = null;
}
}
/**
* You'll need this in your class to get the helper from the manager once
* per class.
*/
private DatabaseHelper getHelper() {
if (databaseHelper == null) {
databaseHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
}
return databaseHelper;
}
/**
* Download latests gifs from the server
*/
public void updateGifs() {
try {
Gif[] gifs = RestFactory.getPopularGifs(restClient);
for (Gif gif : gifs) {
getHelper().getGifDao().createOrUpdate(gif);
}
Intent intent = new Intent(Constants.Service.ServerUpdateReceiver.NEW_GIFS_INCOMING);
sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
//TODO: Verify behaviour
Intent intent = new Intent(Constants.Service.ServerUpdateReceiver.GIF_UPDATE_FAILED);
sendBroadcast(intent);
}
}
}