DbUpdateUtils.java
5.49 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.gimi.common.cinema.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.gimi.common.cinema.db.NewDBManager;
import com.gimi.common.cinema.model.AsyncCallback;
import com.gimi.common.cinema.model.Constant;
import com.gimi.common.cinema.model.LocalMovieMessage;
import com.gimi.common.cinema.model.MovieMessage;
import com.gimi.common.cinema.model.Rating;
import com.gimi.common.cinema.model.SambaMsg;
import com.gimi.common.cinema.model.WrongMsg;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* 数据库辅助
* Created by 李攀 on 2015/5/7.
*/
public class DbUpdateUtils {
private Context context;
private SambaMsg sambaMsg;
private NewDBManager dbManager;
private String localPath;
private String rootPath;
private String folder;
/**
* init
* sometimes need charge localPath exists and remount
*
* @param context
*/
public DbUpdateUtils(Context context) {
this.context = context;
dbManager = NewDBManager.getInstance();
SharedPreferences sharedPreferences = context.getSharedPreferences(Constant.XML_NAME, Context.MODE_PRIVATE);
sambaMsg = Utils.getSambaMsg(sharedPreferences);
folder = sambaMsg.getFolder();
localPath = sambaMsg.getLocalPath();
rootPath = sambaMsg.getRootPath();
}
/**
* @param callback success
*/
public void addAllLocalData(final AsyncCallback<Integer> callback) {
new Thread() {
@Override
public void run() {
super.run();
WrongMsg caught = new WrongMsg();
callback.onStart();
try {
LocalMovieScanUtils scanUtils = new LocalMovieScanUtils(context);
ArrayList<LocalMovieMessage> allLocalMovies;
// if (BuildConfig.MACHINE_TYPE.equals("himedia")) {
allLocalMovies = scanUtils.getAllLocalMovie(rootPath, folder, callback);
// }else{
// allLocalMovies = scanUtils.getAllLocalMovie(sambaMsg,callback);
// }
//区分结束还是卡死
callback.onMessage("已扫描:" + allLocalMovies.size());
if (allLocalMovies.size() != 0) {
dbManager.addByTransaction(allLocalMovies);
callback.onSuccess(allLocalMovies.size());
} else {
caught.setCode(0);
callback.onFailure(caught);
}
} catch (Exception e) {
caught.setCode(1);
callback.onFailure(caught);
e.printStackTrace();
} finally {
callback.onDone();
}
}
}.start();
}
int i = 0;
public void deleteMovieNotExists(final AsyncCallback<Integer> callback) {
new Thread() {
@Override
public void run() {
super.run();
callback.onStart();
List<LocalMovieMessage> query = dbManager.query();
for (LocalMovieMessage localMovieMessage : query) {
if (!new File(localMovieMessage.getPlayPath()).exists()) {
dbManager.delete(localMovieMessage);
i++;
Log.d("lovely", "delete success " + localMovieMessage.getMovieName());
}
}
callback.onSuccess(i);
i = 0;
callback.onDone();
}
}.start();
}
public void initDoubanMsg(final AsyncCallback<Integer> callback) {
new Thread() {
@Override
public void run() {
super.run();
callback.onStart();
//query the class describe or the douban rating is null
List<LocalMovieMessage> query = dbManager.query();
for (LocalMovieMessage localMovieMessage : query) {
String movieFolderPath = FolderUtils.getMovieFolderPath(localMovieMessage.getPlayPath(), localPath, null);
MovieMessage mm;
//Read it out and show on the ui
mm = MovieMessageUtils.getLocalMovieMessage(movieFolderPath);
if (mm == null) {
continue;
}
Rating rating = mm.getRating();
double average = 0;//rating
if (rating != null) {
average = rating.getAverage();
}
String[] genres = mm.getGenres();
//types
StringBuilder types = new StringBuilder();
for (String s : genres) {
types.append(s + "/");
} //clazz
// how to get the movie length
localMovieMessage.setDoubanRating(average);
localMovieMessage.setDoubanId(mm.getId());
localMovieMessage.setClassDescribe(types.toString());
localMovieMessage.setScreenTime(mm.getYear());
dbManager.updateDoubanMsg(localMovieMessage);
}
callback.onSuccess(i);
i = 0;
callback.onDone();
}
}.start();
}
}