OtherModelImpl.java
7.45 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright (c) 2016 wugian
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xgimi.gimicinema.model;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;
import com.gimi.common.cinema.model.SambaMsg;
import com.gimi.common.cinema.utils.FileReadUtils;
import com.gimi.common.cinema.utils.SambaFileCharge;
import com.gimi.common.cinema.utils.ScanUtils;
import com.gimi.common.cinema.utils.Sort;
import com.gimi.common.cinema.utils.SortUtils;
import com.gimi.common.cinema.utils.Utils;
import com.google.gson.Gson;
import com.xgimi.gimicinema.BuildConfig;
import com.xgimi.gimicinema.activity.CinemaConfig;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
/**
* Created by wugian on 2016/9/28
*/
public class OtherModelImpl implements IOtherModel {
private Handler handler = new Handler();
@Override
public void getAllApk(String path, OnGetApkList l) {
ArrayList<HashMap<String, Object>> data = new ArrayList<>();
File apkFile = new File(path);
if (SambaFileCharge.fileExist(apkFile.toString())) {
String[] list = apkFile.list();
for (String s : list) {
HashMap<String, Object> tempHashMap = new HashMap<>();
tempHashMap.put("userName", s);
data.add(tempHashMap);
}
}
l.onGetApkListSuccess(data);
}
@Override
public void updateClazz(final SharedPreferences sp, final OnUpdateClazzListener l) {
ThreadUtils.subThread(new ThreadUtils.DoSomeThing() {
@Override
public void doSomeThing() {
SambaMsg msg = Utils.getSambaMsg(sp);
String localPath = msg.getLocalPath();
com.gimi.common.cinema.model.CinemaConfig cfgLocal = Utils.readConfigBySP(sp);
if (BuildConfig.MACHINE_TYPE.equals("himedia")) {
localPath = CinemaConfig.BASIC_ROOT + "/";
}
com.gimi.common.cinema.model.CinemaConfig cfgSmb =
FileReadUtils.readConfigBySamba(localPath + ".time");
if (cfgLocal == null /*|| (cfgLocal.getClassArray() != null &&
cfgLocal.getClassArray().length < 1)*/) {
return;
}
if (cfgLocal.getClassVersion() < cfgSmb.getClassVersion()) {
StringBuilder stringBuilder = new StringBuilder();
if (cfgSmb.getClassArray() != null && cfgSmb.getClassArray().length > 0) {
for (String trim : cfgSmb.getClassArray()) {
if (!trim.isEmpty()) {
if (stringBuilder.length() != 0) {
stringBuilder.append("@");
}
stringBuilder.append(trim);
}
}
}
SharedPreferences.Editor edit = sp.edit();
edit.putString("classifications", stringBuilder.toString());
edit.putInt("classifications-version",
cfgSmb.getClassVersion());
edit.apply();
handler.post(new Runnable() {
@Override
public void run() {
l.onUpdateClazzSuccess();
}
});
} else if (cfgLocal.getClassVersion() > cfgSmb.getClassVersion()) {
String content = new Gson().toJson(cfgLocal);
FileReadUtils.writeDates(msg.getConfigPath(), content, false);
} else {
Log.d("lovely", "╟version is same ");
}
}
});
}
@Override
public void scanOtherMovies(final Context context, final String path,
final OnScanOtherMovieListener l) {
ThreadUtils.subThread(new ThreadUtils.DoSomeThing() {
@Override
public void doSomeThing() {
handler.post(new Runnable() {
@Override
public void run() {
l.onScanOtherMovieSuccess(getAllFolderPath(context, path));
}
});
}
});
}
@Override
public void scanMultiMovies(final Context context, final String[] paths, final int position, final OnScanMultiMovieListener l) {
if (paths == null || paths.length == 0) {
l.onScanMultiMovieFailure("path array is null or size is 0");
return;
}
if (paths.length < position) {
l.onScanMultiMovieFailure("wrong position " + position + " with paths size is " + paths.length);
return;
}
final int[] curPosition = {0};
ThreadUtils.subThread(new ThreadUtils.DoSomeThing() {
@Override
public void doSomeThing() {
final ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < paths.length; i++) {
if (i <= position)
curPosition[0] = result.size();
String path = paths[i];
result.addAll(getAllFolderPath(context, path));
}
handler.post(new Runnable() {
@Override
public void run() {
l.onScanMultiMovieSuccess(result, curPosition[0]);
}
});
}
});
}
private ArrayList<String> getAllFolderPath(Context context, String path) {
ScanUtils scanUtils = new ScanUtils(context);
ArrayList<String> video =
scanUtils.getVideo(new File(path).getParent());
ArrayList<String> prefix = SortUtils.getAllPrefix(video, path);
ArrayList<String> suffix = SortUtils.getAllSubfix(video, path);
String sub = suffix.size() > 0 ? suffix.get(suffix.size() - 1) : "";
String pre = prefix.size() > 0 ? prefix.get(0) : "";
if (video.size() > 1) {
Collections.sort(video, new Sort(Sort.UP, pre, sub));
ArrayList<String> files = SortUtils.getOtherFile(video, path);
if (files != null && files.size() > 0) {
video.addAll(files);
}
}
return video;
}
public interface OnGetApkList {
void onGetApkListSuccess(ArrayList<HashMap<String, Object>> result);
}
public interface OnUpdateClazzListener {
void onUpdateClazzSuccess();
}
public interface OnScanOtherMovieListener {
void onScanOtherMovieSuccess(ArrayList<String> result);
}
public interface OnScanMultiMovieListener {
void onScanMultiMovieSuccess(ArrayList<String> result, int position);
void onScanMultiMovieFailure(String msg);
}
}