use of com.hippo.ehviewer.dao.DownloadLabel in project EhViewer by seven332.
the class CommonOperations method startDownload.
// TODO Add context if activity and context are different style
public static void startDownload(final MainActivity activity, final List<GalleryInfo> galleryInfos, boolean forceDefault) {
final DownloadManager dm = EhApplication.getDownloadManager(activity);
LongList toStart = new LongList();
List<GalleryInfo> toAdd = new ArrayList<>();
for (GalleryInfo gi : galleryInfos) {
if (dm.containDownloadInfo(gi.gid)) {
toStart.add(gi.gid);
} else {
toAdd.add(gi);
}
}
if (!toStart.isEmpty()) {
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START_RANGE);
intent.putExtra(DownloadService.KEY_GID_LIST, toStart);
activity.startService(intent);
}
if (toAdd.isEmpty()) {
activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT);
return;
}
boolean justStart = forceDefault;
String label = null;
// Get default download label
if (!justStart && Settings.getHasDefaultDownloadLabel()) {
label = Settings.getDefaultDownloadLabel();
justStart = label == null || dm.containLabel(label);
}
// If there is no other label, just use null label
if (!justStart && 0 == dm.getLabelList().size()) {
justStart = true;
label = null;
}
if (justStart) {
// Got default label
for (GalleryInfo gi : toAdd) {
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_LABEL, label);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi);
activity.startService(intent);
}
// Notify
activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT);
} else {
// Let use chose label
List<DownloadLabel> list = dm.getLabelList();
final String[] items = new String[list.size() + 1];
items[0] = activity.getString(R.string.default_download_label_name);
for (int i = 0, n = list.size(); i < n; i++) {
items[i + 1] = list.get(i).getLabel();
}
new ListCheckBoxDialogBuilder(activity, items, new ListCheckBoxDialogBuilder.OnItemClickListener() {
@Override
public void onItemClick(ListCheckBoxDialogBuilder builder, AlertDialog dialog, int position) {
String label;
if (position == 0) {
label = null;
} else {
label = items[position];
if (!dm.containLabel(label)) {
label = null;
}
}
// Start download
for (GalleryInfo gi : toAdd) {
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_LABEL, label);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi);
activity.startService(intent);
}
// Save settings
if (builder.isChecked()) {
Settings.putHasDefaultDownloadLabel(true);
Settings.putDefaultDownloadLabel(label);
} else {
Settings.putHasDefaultDownloadLabel(false);
}
// Notify
activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT);
}
}, activity.getString(R.string.remember_download_label), false).setTitle(R.string.download).show();
}
}
use of com.hippo.ehviewer.dao.DownloadLabel in project EhViewer by seven332.
the class DownloadManager method renameLabel.
public void renameLabel(@NonNull String from, @NonNull String to) {
// Find in label list
boolean found = false;
for (DownloadLabel raw : mLabelList) {
if (from.equals(raw.getLabel())) {
found = true;
raw.setLabel(to);
// Update in DB
EhDB.updateDownloadLabel(raw);
break;
}
}
if (!found) {
return;
}
LinkedList<DownloadInfo> list = mMap.remove(from);
if (list == null) {
return;
}
// Update info label
for (DownloadInfo info : list) {
info.label = to;
// Update in DB
EhDB.putDownloadInfo(info);
}
// Put list back with new label
mMap.put(to, list);
// Notify listener
for (DownloadInfoListener l : mDownloadInfoListeners) {
l.onRenameLabel(from, to);
}
}
use of com.hippo.ehviewer.dao.DownloadLabel in project EhViewer by seven332.
the class DownloadManager method moveLabel.
public void moveLabel(int fromPosition, int toPosition) {
final DownloadLabel item = mLabelList.remove(fromPosition);
mLabelList.add(toPosition, item);
EhDB.moveDownloadLabel(fromPosition, toPosition);
for (DownloadInfoListener l : mDownloadInfoListeners) {
l.onUpdateLabels();
}
}
use of com.hippo.ehviewer.dao.DownloadLabel in project EhViewer by seven332.
the class EhDB method importDB.
/**
* @param file The db file
* @return error string, null for no error
*/
public static synchronized String importDB(Context context, File file) {
try {
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
int newVersion = DaoMaster.SCHEMA_VERSION;
int oldVersion = db.getVersion();
if (oldVersion < newVersion) {
upgradeDB(db, oldVersion);
db.setVersion(newVersion);
} else if (oldVersion > newVersion) {
return context.getString(R.string.cant_read_the_file);
}
DaoMaster daoMaster = new DaoMaster(db);
DaoSession session = daoMaster.newSession();
// Downloads
DownloadManager manager = EhApplication.getDownloadManager(context);
List<DownloadInfo> downloadInfoList = session.getDownloadsDao().queryBuilder().list();
manager.addDownload(downloadInfoList);
// Download label
List<DownloadLabel> downloadLabelList = session.getDownloadLabelDao().queryBuilder().list();
manager.addDownloadLabel(downloadLabelList);
// Download dirname
List<DownloadDirname> downloadDirnameList = session.getDownloadDirnameDao().queryBuilder().list();
for (DownloadDirname dirname : downloadDirnameList) {
putDownloadDirname(dirname.getGid(), dirname.getDirname());
}
// History
List<HistoryInfo> historyInfoList = session.getHistoryDao().queryBuilder().list();
putHistoryInfo(historyInfoList);
// QuickSearch
List<QuickSearch> quickSearchList = session.getQuickSearchDao().queryBuilder().list();
List<QuickSearch> currentQuickSearchList = sDaoSession.getQuickSearchDao().queryBuilder().list();
for (QuickSearch quickSearch : quickSearchList) {
String name = quickSearch.name;
for (QuickSearch q : currentQuickSearchList) {
if (ObjectUtils.equal(q.name, name)) {
// The same name
name = null;
break;
}
}
if (null == name) {
continue;
}
insertQuickSearch(quickSearch);
}
// LocalFavorites
List<LocalFavoriteInfo> localFavoriteInfoList = session.getLocalFavoritesDao().queryBuilder().list();
for (LocalFavoriteInfo info : localFavoriteInfoList) {
putLocalFavorites(info);
}
// Bookmarks
// TODO
// Filter
List<Filter> filterList = session.getFilterDao().queryBuilder().list();
List<Filter> currentFilterList = sDaoSession.getFilterDao().queryBuilder().list();
for (Filter filter : filterList) {
if (!currentFilterList.contains(filter)) {
addFilter(filter);
}
}
return null;
} catch (Throwable e) {
ExceptionUtils.throwIfFatal(e);
// Ignore
return context.getString(R.string.cant_read_the_file);
}
}
use of com.hippo.ehviewer.dao.DownloadLabel in project EhViewer by seven332.
the class EhDB method addDownloadLabel.
public static synchronized DownloadLabel addDownloadLabel(String label) {
DownloadLabelDao dao = sDaoSession.getDownloadLabelDao();
DownloadLabel raw = new DownloadLabel();
raw.setLabel(label);
raw.setTime(System.currentTimeMillis());
raw.setId(dao.insert(raw));
return raw;
}
Aggregations